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
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;
}
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, ...);
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
Summary
- Variables hold values that can change; always initialize before use.
- Constants hold values that never change — use
constfor type-checked constants,#definefor simple preprocessor macros. printf()writes formatted output;scanf()reads formatted input.- Always pass the address (
&) of a variable toscanf()— except for arrays/strings, whose name is already an address.
Further Reading
- Introduction to Variables in C (video embedded above)
- Constants in C (Part 1) — Neso Academy
- GeeksforGeeks: Format Specifiers in C
Next in this series: operators, expressions, precedence, associativity, and type conversion.