C Variables, Data Types and I/O

In C, every variable must be declared with a type before use.

TypeTypical sizeFormat specifierExample
char1 byte%c'A'
int4 bytes%d25
float4 bytes%f3.14f
double8 bytes%lf3.14159
long8 bytes*%ld100000L
char[] (string)n bytes%s"hello"

*Sizes depend on the compiler and platform. Use sizeof to check.

Input with scanf

C
#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

C
#define PI 3.14159        // macro constant (preprocessor)
const int MAX = 100;      // typed constant
Track your progress through the C course