Variables, printf & scanf
C data types, formatted output, and reading input from the terminal
Quick check before you start: Can you name the four basic data types in C? If not, read on. If you can, skip to printf Format Specifiers.
Practice this topic: C Variables skill drill
After this lesson, you will be able to:
- Declare variables using
int,float,double, andchar - Use
printfformat specifiers to display values - Read user input with
scanfand explain why&is required - Perform explicit type casting between numeric types
C Data Types
In Java, you had int, double, boolean, and String. C is simpler — and more dangerous. There is no boolean (until C99) and no String type. Here are the types you will use most:
| Type | Size (typical) | Example | Format Specifier |
|---|---|---|---|
int |
4 bytes | 42 |
%d |
float |
4 bytes | 3.14f |
%f |
double |
8 bytes | 3.14159 |
%lf (scanf) / %f (printf) |
char |
1 byte | 'A' |
%c |
Declaration looks like Java but with no default values. Uninitialized variables contain garbage — whatever was in that memory before.
int count = 0; /* always initialize */
double gpa = 3.85;
char grade = 'A';
int x; /* DANGER: x holds garbage */
printf Format Specifiers
printf writes formatted text to standard output. The first argument is a format string; the rest are values that fill in the % placeholders.
int age = 20;
double gpa = 3.72;
char initial = 'J';
printf("Age: %d\n", age);
printf("GPA: %.2f\n", gpa); /* 2 decimal places */
printf("Initial: %c\n", initial);
printf("Name: %s\n", "Alice");
Output:
Age: 20
GPA: 3.72
Initial: J
Name: Alice
Common format specifiers:
| Specifier | Meaning |
|---|---|
%d |
integer (decimal) |
%f |
float/double |
%c |
single character |
%s |
string (char array) |
%x |
hexadecimal |
%ld |
long int |
%% |
literal percent sign |
scanf and the & Operator
scanf reads formatted input from standard input. It needs the address of the variable so it knows where to store the value. That is what & does — it gives the address.
int age;
double gpa;
printf("Enter age: ");
scanf("%d", &age);
printf("Enter GPA: ");
scanf("%lf", &gpa); /* %lf for double in scanf */
printf("Age: %d, GPA: %.2f\n", age, gpa);
Why &? In Java, you passed objects by reference automatically. C passes everything by value. scanf needs to modify your variable, so it needs the memory address. Without &, scanf gets the value of the variable (garbage) instead of the address, and the program either crashes or corrupts memory.
Key Insight:
&agemeans “the address of age.” You will see&everywhere in C. It is the address-of operator, and it is fundamental to how pointers work in Week 6.
Type Casting
C performs implicit conversions (int to double is safe), but narrowing conversions lose data. Use explicit casts to be clear about intent.
int a = 7, b = 2;
double result;
result = a / b; /* integer division: result = 3.0 */
result = (double)a / b; /* floating-point division: result = 3.5 */
printf("Integer div: %d\n", a / b); /* 3 */
printf("Float div: %.1f\n", (double)a / b); /* 3.5 */
The cast (double)a converts a to a double before the division happens. Without it, C performs integer division and truncates the remainder.
scanf("%d", age) (without &) cause a problem?&, scanf gets the current value stored in age (which is likely garbage) and interprets it as a memory address. It then tries to write the input to that random address, causing a segmentation fault or silent memory corruption. The & operator passes the actual address of age so scanf knows where to store the result.
What Comes Next
You now know how to declare variables, print formatted output, and read input. Next, you will learn the operators that transform those values — arithmetic, comparison, logical, and bitwise.