C Variables, Data Types and I/O
In C, every variable must be declared with a type before use.
| Type | Typical size | Format specifier | Example |
|---|---|---|---|
| char | 1 byte | %c | 'A' |
| int | 4 bytes | %d | 25 |
| float | 4 bytes | %f | 3.14f |
| double | 8 bytes | %lf | 3.14159 |
| long | 8 bytes* | %ld | 100000L |
| char[] (string) | n bytes | %s | "hello" |
*Sizes depend on the compiler and platform. Use sizeof to check.
Input with scanf
#include <stdio.h>
int main() {
int age;
float height;
char name[30];
printf("Enter name, age and height: ");
scanf("%s %d %f", name, &age, &height); // & gives the address
printf("%s is %d years old and %.1f cm tall\n", name, age, height);
printf("sizeof(int) = %zu bytes\n", sizeof(int));
return 0;
}
Enter name, age and height: Asha 20 162.5
Asha is 20 years old and 162.5 cm tall
sizeof(int) = 4 bytes
Why no & for name?An array name already decays to the address of its first element.
Constants
#define PI 3.14159 // macro constant (preprocessor)
const int MAX = 100; // typed constant
Track your progress through the C course