Variables, Constants, and Input/Output Statements in C

Now that we know the basic data types (from Post 2), it’s time to put them to work: declaring variables to hold changing data, constants for values that must never change, and reading/printing data with C’s input/output statements. This is Post 3 of the Unit I series.

Variables

A variable is a named location in memory that holds a value which can change during program execution.

int age;              // declaration only — value is unpredictable (garbage) until assigned
int score = 0;         // declaration with initialization
float price = 99.5;
char grade;
grade = 'B';           // assignment after declaration
Common mistake: using an uninitialized variable: int age; printf(“%d”, age); prints whatever garbage bits happened to already be in that memory location — not 0. Always initialize a variable before reading its value, or explicitly assign it first.

Multiple Declarations

int a, b, c;             // three variables of the same type
int x = 1, y = 2, z = 3;  // each initialized individually

Constants

A constant is a value that cannot change once the program starts running. C offers two main ways to define one:

1. Using #define (a preprocessor macro)

#include <stdio.h>
#define PI 3.14159
#define MAX_STUDENTS 60

int main() {
    float radius = 5.0;
    float area = PI * radius * radius;
    printf("Area: %.2f\n", area);
    printf("Max students allowed: %d\n", MAX_STUDENTS);
    return 0;
}

#define works via simple text substitution before compilation even begins — everywhere PI appears in the code, the preprocessor literally replaces it with 3.14159.

2. Using the const Keyword

#include <stdio.h>

int main() {
    const float PI = 3.14159;
    const int MAX_STUDENTS = 60;

    float radius = 5.0;
    printf("Area: %.2f\n", PI * radius * radius);

    // PI = 3.14;   // COMPILE ERROR: cannot assign to a const variable

    return 0;
}
#define vs const: const creates an actual typed variable that the compiler tracks and can catch errors on (like trying to reassign it) — generally preferred in modern C. #define is a blunt text-replacement tool from the preprocessor with no type checking at all, but is still common for simple numeric/string constants in existing codebases.

Input and Output Statements

printf() — Formatted Output

printf("format string", arg1, arg2, ...);

Common format specifiers: %d (int), %f (float), %lf (double), %c (char), %s (string), %x (hexadecimal), %o (octal).

scanf() — Formatted Input

scanf("format string", &var1, &var2, ...);
The most common scanf() mistake: Forgetting the & (address-of operator) before a variable name, e.g. writing scanf(“%d”, age); instead of scanf(“%d”, &age);. scanf() needs the variable’s memory address to know where to store the input — without &, it either crashes or silently corrupts memory, since it’s writing to whatever number happens to be in age rather than to age’s actual address.

Full Example: Reading and Displaying Student Details

#include <stdio.h>

int main() {
    char name[50];
    int rollNumber;
    float marks;

    printf("Enter student name: ");
    scanf("%s", name);              // no & needed for arrays/strings — see note below

    printf("Enter roll number: ");
    scanf("%d", &rollNumber);

    printf("Enter marks: ");
    scanf("%f", &marks);

    printf("\n--- Student Details ---\n");
    printf("Name: %s\n", name);
    printf("Roll Number: %d\n", rollNumber);
    printf("Marks: %.2f\n", marks);

    return 0;
}

Sample Output

Enter student name: Rahul
Enter roll number: 21
Enter marks: 87.5

--- Student Details ---
Name: Rahul
Roll Number: 21
Marks: 87.50
Why no & before name: An array’s name (like the character array name here) already represents the address of its first element in C — so scanf(“%s”, name) is already passing an address, and adding another & would be incorrect. This will make more sense once we cover arrays in Unit II.

Summary

  • Variables hold values that can change; always initialize before use.
  • Constants hold values that never change — use const for type-checked constants, #define for simple preprocessor macros.
  • printf() writes formatted output; scanf() reads formatted input.
  • Always pass the address (&) of a variable to scanf() — except for arrays/strings, whose name is already an address.

Further Reading

Next in this series: operators, expressions, precedence, associativity, and type conversion.

Leave a Comment