Last lesson the program talked: printf. Today the user talks back: scanf reads what the user types and stores it in a variable.
Goal for the end of the lesson: a program that asks for a board ID, a voltage and a channel, and prints a ticket.
כל הסצנות במצבן הסופי, עם ההערות של המרצה. נוח לחזרה מהטלפון.
Last lesson the program talked: printf. Today the user talks back: scanf reads what the user types and stores it in a variable.
Goal for the end of the lesson: a program that asks for a board ID, a voltage and a channel, and prints a ticket.
Before we can read a number, we need somewhere to keep it: a variable. int board_id; reserves a box for a whole number. Until something is stored in it, its value is unknown (we write ?), not 0.
Ask: what type would you use for a voltage? For a channel letter?
scanf looks like printf, with two differences. The format string says what to read, not what to print: %d means "read an integer". And after it comes an address, not a value: &board_id.
Read the line as a sentence: read an integer and store it at the address of board_id.
Every variable lives at an address in memory, like a house number. The & operator means "the address of". scanf needs it because scanf writes into the variable: it must know where the variable is.
Act it out: three students are memory cells with house numbers on paper (1000, 1004, 1008). One of them is board_id. "scanf" gets a note "write 417 at 1004" and walks to the right student. Without the address, scanf has only a "?" and nowhere to go. Real addresses are long hexadecimal numbers; 1004 is only for the picture.
The most common scanf bug. Without &, scanf receives the value of board_id (an unknown number) and treats it as an address. Writing there usually crashes the program, or the number silently goes somewhere else.
Visual Studio usually catches it: a warning that scanf expects an int * (a pointer) and got an int, or an error that board_id is used before it has a value. Read those messages; they point to the line.
The core pattern of the unit. Prompt: printf tells the user what to type (no \n, so the cursor waits on the same line). Read: scanf stops the program and waits until the user types and presses Enter. Echo: print the stored value back, to prove it arrived.
In Visual Studio this program needs #define _CRT_SECURE_NO_WARNINGS on line 1. That is the scene after the next one.
The same program as a trace table: after each step, what is on the console and what is in board_id. Students fill it on paper before each row is revealed.
Be the computer: one student is board_id with a sticky note, one is the keyboard, one writes the console on the board. The class reads the code line by line; each student acts only when their line runs.
C4996 is a warning in C terms, not a syntax error: the code is correct. Visual Studio's security checks (SDL, on by default in new projects) turn it into an error, so the build fails. The message tells you the fix: _CRT_SECURE_NO_WARNINGS.
scanf_s is Microsoft only and works differently for text input. The official formula sheet and the exam use scanf, so we keep scanf. Teacher action: set the definition in the starter project before class (Project Properties, C/C++, Preprocessor, Preprocessor Definitions), and teach the #define line for home computers.
One scanf can read several fields: one conversion and one address for each. %d fills board_id, %lf fills voltage, in order, like printf.
Interactive: switch between "417 space 4.98" and "417 Enter 4.98". The result is the same, because %d and %lf skip spaces and Enters before a number. Ask: what would happen if the user typed 4.98 417? (board_id gets 4, and the rest goes wrong.)
The one place where input and output spell a type differently. scanf writes into the variable, so it must know its exact size: %f is for float, %lf (long float) for double. With %f, scanf fills only half of the double and the value is wrong.
printf only receives a copy of the value, and a float is passed as a double anyway, so %f prints both. This matches the formula sheet.
What the user types goes into the input first, character by character, including the Enter. %d reads digits and stops at the first character that is not part of a number. The Enter stays.
%c reads exactly one character, whatever it is. So it takes the Enter, and the program runs on without waiting for the letter. Students see it as "scanf was skipped". Show it live in Visual Studio.
A space in a scanf format string means "skip any whitespace here": spaces, tabs and Enters. So " %c" first skips the leftover Enter, then waits and reads the real letter.
Beginner rule for this course: read a char after a number with " %c". It never hurts, even when nothing is left over.
Live demonstration: type the program in Visual Studio in front of the class, with the #define on line 1 and the main function around it. Three prompt-read pairs, one per value. Point at the three conversions: %d, %lf, " %c".
Run it and type 417, 4.98 and B.
The second half prints the ticket. The \n at the start of the first line leaves an empty line between the questions and the ticket.
Ask before each line runs: what exactly will appear? The %.2f is why 4.98 does not print as 4.980000.
Bug hunt race in pairs: copy the code, find the four bugs, fix them one at a time, and build after each fix. The first pair with a working ticket explains one fix to the class.
Clue for line 8: printf("%d", channel) does not crash, it prints 66, the character code of 'B'. Wrong conversion, wrong output.
A program is tested with more than one input. A typical case, zero values, and a value that checks the rounding: 12.345 with %.2f prints 12.35.
Acceptance tests for the mission: every input has a prompt, all three values are stored, voltage shows exactly two digits, the channel is read after a number, labels and line breaks match, and the program builds without errors. Later: scanf's return value and conditions will let us detect invalid input.
On paper, two minutes, no computer: one scanf for a double named temperature and one printf with one digit after the point. Then the class votes on the three options.
Then trace the data journey aloud: keyboard, scanf, variable, printf, console. 23.46 prints as 23.5 because %.1f rounds.
Students copy the Hebrew words next to the English ones. "Address" and "&" come back in a big way when we reach pointers.
Team mission, a new project. Roles: the driver types, the navigator checks types, conversions and tests. Switch after the first build.
Beginner scaffold on the board: three declarations, then printf("Enter ..."); scanf("...", ...); three times, then printf("--- BOARD RECORD ---\n"). Stretch: align the labels with a field width and add a line with a literal %.
Home practice: a new project named OperatorBadge. Read an integer operator ID and one shift letter, echo both with clear labels, test twice and write down the expected output first.
Exit question: explain the complete data journey from the keyboard to the console. Next unit: conditions. The program will make decisions about the values it reads.