C Format Specifier

C Format Specifier

The C format specifier is used for input and output and It is a method to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf() these are %c, %d, %f, etc and it pointed that the type of data to be printed on standard output.

 

Format specifier Description
%d or %i %d is pointed to print the signed integer value that the variable can hold both positive and negative values.
%u %u is used to print the unsigned integer value that the variable can hold only a positive value.
%o %o is used to print the octal unsigned integer and it is the value that always starts with a 0 value.
%x %x used to print the hexadecimal unsigned integer a value always starts with a 0x value. %x  printed in small letters such as a, b, c, etc.
%X %X used to print the hexadecimal unsigned integer, prints the alphabetical characters in uppercase such as A, B, C, etc.
%f %f used for printing the decimal floating-point values.
%e/%E %e is used for scientific notation.
%g %g is used to print the decimal floating-point values i.e., the value after the decimal in the input will be the same as the value in the output.
%p %p used to print the address in a hexadecimal form.
%c %c used to print the unsigned character.
%s %s used to print the strings.
%ld %ld is used to print long-signed integer values.

 

For Signed Integer format specifier: %d, %i

Program

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main()
{
int x = 87, y = 66;
printf("%d\n", x);
printf("%i\n", x);
return 0;
}
#include <stdio.h> int main() { int x = 87, y = 66; printf("%d\n", x); printf("%i\n", x); return 0; }
#include <stdio.h>
int main()
{
    int x = 87, y = 66;
    printf("%d\n", x);
    printf("%i\n", x);
    return 0;
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
87
87
87 87
87
87

Character format specifier: %c

Program

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main()
{
char ch = 'B';
printf("%c\n", ch);
return 0;
}
#include <stdio.h> int main() { char ch = 'B'; printf("%c\n", ch); return 0; }
#include <stdio.h>
int main()
{
    char ch = 'B';
    printf("%c\n", ch);
    return 0;
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
B
B
B

Unsigned Integer Format Specifier: %u:

Program

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<stdio.h>
int main()
{
printf("%u\n", -10);
printf("%u\n", 5);
return 0;
}
#include<stdio.h> int main() { printf("%u\n", -10); printf("%u\n", 5); return 0; }
#include<stdio.h>
int main()
{
    printf("%u\n", -10);
    printf("%u\n", 5);
    return 0;
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Output: 4294967286
5
Output: 4294967286 5
Output: 4294967286
        5

Floating-point format specifier: %f, %e or %E

Program

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main()
{
float a = 12.67;
printf("%f\n", a);
printf("%e\n", a);
return 0;
}
#include <stdio.h> int main() { float a = 12.67; printf("%f\n", a); printf("%e\n", a); return 0; }
#include <stdio.h>
int main()
{
    float a = 12.67;
    printf("%f\n", a);
    printf("%e\n", a);
    return 0;
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
12.670000
1.267000e+01
12.670000 1.267000e+01
12.670000
1.267000e+01

Unsigned Octal number for integer: %o

Program

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main()
{
int a = 76;
printf("%o\n", a);
return 0;
}
#include <stdio.h> int main() { int a = 76; printf("%o\n", a); return 0; }
#include <stdio.h>
int main()
{
    int a = 76;
    printf("%o\n", a);
    return 0;
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
114
114
114

Unsigned Hexadecimal for integer: %x, %X

Program

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main()
{
int a = 11;
printf("%x\n", a);
return 0;
}
#include <stdio.h> int main() { int a = 11; printf("%x\n", a); return 0; }
#include <stdio.h>
int main()
{
    int a = 11;
    printf("%x\n", a);
    return 0;
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
b
b
b

String printing: %s

Program

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main()
{
char a[] = "abcd";
printf("%s\n", a);
return 0;
}
#include <stdio.h> int main() { char a[] = "abcd"; printf("%s\n", a); return 0; }
#include <stdio.h>
int main()
{
    char a[] = "abcd";
    printf("%s\n", a);
    return 0;
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
abcd
abcd
abcd

Address Printing: %p

Program

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main()
{
int a = 10;
printf("The Memory Address of a: %p\n",(void*)&a);
return 0;
}
#include <stdio.h> int main() { int a = 10; printf("The Memory Address of a: %p\n",(void*)&a); return 0; }
#include <stdio.h>
int main()
{
    int a = 10;
    printf("The Memory Address of a: %p\n",(void*)&a);
    return 0;
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
The Memory Address of a: 0x7ffc85861624
The Memory Address of a: 0x7ffc85861624
The Memory Address of a: 0x7ffc85861624

Double floating-point number: %lf

Program

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main()
{
double a = 0.0;
scanf("%lf", &a);//input is 45
printf("%lf\n", a);
return 0;
}
#include <stdio.h> int main() { double a = 0.0; scanf("%lf", &a);//input is 45 printf("%lf\n", a); return 0; }
#include <stdio.h>
int main()
{
    double a = 0.0;
    scanf("%lf", &a);//input is 45
    printf("%lf\n", a);
    return 0;
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
45.650000
45.650000
45.650000

String input : %s 

Program

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main()
{
char str[20];
scanf("%s", str);//input is abcd
printf("%s\n", str);
return 0;
}
#include <stdio.h> int main() { char str[20]; scanf("%s", str);//input is abcd printf("%s\n", str); return 0; }
#include <stdio.h>
int main()
{
    char str[20];
    scanf("%s", str);//input is abcd
    printf("%s\n", str);
    return 0;
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
abcd
abcd
abcd

 

Also read, C-Comments

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *