0
0
Cprogramming~5 mins

Format specifiers

Choose your learning style9 modes available
Introduction
Format specifiers tell the computer how to show or read different types of data like numbers or letters.
When you want to print a number or text on the screen in C.
When you need to read input from the user and store it correctly.
When you want to format output neatly, like showing decimals or padding spaces.
Syntax
C
printf("format_specifier", value);
scanf("format_specifier", &variable);
Format specifiers start with % and tell how to display or read data.
Common specifiers include %d for integers, %f for floating-point numbers, and %c for characters.
Examples
Prints the integer 10.
C
printf("%d", 10);
Prints the floating-point number 3.14.
C
printf("%f", 3.14);
Reads a single character from user input into variable ch.
C
scanf("%c", &ch);
Prints the string 'Hello World'.
C
printf("Hello %s", "World");
Sample Program
This program prints an integer, a floating-point number with one decimal, and a character using format specifiers.
C
#include <stdio.h>

int main() {
    int age = 25;
    float height = 5.9f;
    char initial = 'J';

    printf("Age: %d\n", age);
    printf("Height: %.1f feet\n", height);
    printf("Initial: %c\n", initial);

    return 0;
}
OutputSuccess
Important Notes
Use %d for integers, %f for floats, %c for characters, and %s for strings.
You can control decimal places with %.nf where n is the number of decimals.
Always use & before variable names in scanf to store input correctly.
Summary
Format specifiers tell C how to print or read data.
Common specifiers: %d (int), %f (float), %c (char), %s (string).
Use them in printf to show data and scanf to get data.