C Format Specifiers
Used with functions like printf and scanf.
They define how data is presented or interpreted during input and output operations.
They basically starts with a %
They define how data is presented or interpreted during input and output operations.
They basically starts with a %
Format Specifier
Description
Example
%c
Character
printf("%c", 'A');
%s
String
printf("%s", "Hello");
%d
Decimal integer
printf("%d", 42);
%ld
Long decimal integer
printf("%ld", 1000000L);
%f
Floating-point number (default precision)
printf("%f", 3.14)
%lf
Double floating-point number
printf("%lf", 3.14);
%e, %E
Scientific notation (exponential format)
printf("%e", 3000.0);
%u
Unsigned decimal integer
printf("%u", 42);
%x, %X
Hexadecimal integer
printf("%x", 255);
%o
Octal integer
printf("%o", 64);
%p
Pointer address
printf("%p", &variable);
%n
Number of characters written so far
printf("Hello%n", &count);
%%%
Literal % character
printf("100%%");
Example
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
printf("You entered: %c\n", ch);
char str[20];
printf("Enter a string: ");
scanf("%s", str);
printf("You entered: %s\n", str);
int num;
printf("Enter an integer: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
float floatNum;
printf("Enter a floating-point number: ");
scanf("%f", &floatNum);
printf("You entered: %f\n", floatNum);
double doubleNum;
printf("Enter a double floating-point number: ");
scanf("%lf", &doubleNum);
printf("You entered: %lf\n", doubleNum);
double scientificNum = 3000.0;
printf("Scientific Notation: %e\n", scientificNum);
unsigned int unsignedNum = 42;
printf("Unsigned Decimal Integer: %u\n", unsignedNum);
int hexNum = 255;
printf("Hexadecimal Integer: %x\n", hexNum);
int octalNum = 64;
printf("Octal Integer: %o\n", octalNum);
int variable = 42;
printf("Pointer Address: %p\n", &variable);
int count;
printf("Hello%n", &count);
printf("Number of Characters Written So Far: %d\n", count);
printf("Literal Percent Sign: %%\n");
return 0;
}
Quick Recap
Topics Covered
Format specifiers in C
Practice With Examples in Compilers
The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compile
Example 1
Example 1
Example 2
Example 3
Example 4
Example 5