Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
50 views4 pages

05 Format Specifier

The document provides a comprehensive overview of format specifiers in C programming, detailing their usage for input and output operations with functions like scanf() and printf(). It includes a list of common format specifiers, their corresponding data types, and examples of how to use them in code. Additionally, it explains modifiers for alignment and field width in formatted output.

Uploaded by

Gurpreet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views4 pages

05 Format Specifier

The document provides a comprehensive overview of format specifiers in C programming, detailing their usage for input and output operations with functions like scanf() and printf(). It includes a list of common format specifiers, their corresponding data types, and examples of how to use them in code. Additionally, it explains modifiers for alignment and field width in formatted output.

Uploaded by

Gurpreet Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

The Format Specifiers are used in C for input and output purposes.

Using this
concept the compiler can understand that what type of data is in a variable during taking
input using the scanf() function and printing using printf() function. Here is a list of format
specifiers.

Format Specifier Type

%c Character

%d Signed integer

%e or %E Scientific notation of floats

%f Float values

%g or %G Similar as %e or %E

%hi Signed integer (short)

%hu Unsigned Integer (short)

%i Unsigned integer

%l or %ld or %li Long

%lf Double

%Lf Long double

%lu Unsigned int or unsigned long

%lli or %lld Long long

%llu Unsigned long long

%o Octal representation

%p Pointer

%s String

%u Unsigned int

%x or %X Hexadecimal representation

%n Prints nothing

%% Prints % character
These are the basic format specifiers. We can add some other parts with the format
specifiers. These are like below −
 A minus symbol (-) sign tells left alignment
 A number after % specifies the minimum field width. If string is less than the
width, it will be filled with spaces
 A period (.) is used to separate field width and precision

Example

#include <stdio.h>
main() {
char ch = 'B';
printf("%c\n", ch); //printing character data
//print decimal or integer data with d and i
int x = 45, y = 90;
printf("%d\n", x);
printf("%i\n", y);
float f = 12.67;
printf("%f\n", f); //print float value
printf("%e\n", f); //print in scientific notation
int a = 67;
printf("%o\n", a); //print in octal format
printf("%x\n", a); //print in hex format
char str[] = "Hello World";
printf("%s\n", str);
printf("%20s\n", str); //shift to the right 20 characters including the string
printf("%-20s\n", str); //left align
printf("%20.5s\n", str); //shift to the right 20 characters including the string, and print string up
to 5 character
printf("%-20.5s\n", str); //left align and print string up to 5 character
}

Output
B
45
90
12.670000
1.267000e+001
103
43
Hello World
Hello World
Hello World
Hello
Hello
SPECIFIE
R USED FOR
%c a single character
%s a string
%hi short (signed)
%hu short (unsigned)
%Lf long double
%n prints nothing
%d a decimal integer (assumes base 10)
%i a decimal integer (detects the base automatically)
%o an octal (base 8) integer
%x a hexadecimal (base 16) integer
%p an address (or pointer)
%f a floating point number for floats
%u int unsigned decimal
%e a floating point number in scientific notation
%E a floating point number in scientific notation
%% the % symbol

Example:
#include <stdio.h>

int main() {
char first_ch = 'f';
printf("%c\n", first_ch);
return 0;
}

You might also like