General

How to apply format specifiers in C?

specifiers in C

Top of Form

Format specifiers are used in C for input and output purposes. The format specifier defines the type of data that is output to standard output. Whether you use printf () to print formatted output or scanf () to accept input, you must use the format specifier. Format identifiers are also known as format strings. These are the basic format specifiers. You can add other parts using the format specifier. These are:

  • The minus sign () indicates left justification.
  • The number after% indicates the minimum field width. If the string is less than the width, it will be filled with spaces
  • The period (.) Is used to separate the field width and precision.

Various types of format specifiers in C

The precision specifies the minimum number of digits in an integer, the maximum number of characters in a string, and the number of digits after the fractional part of a floating-point value.  Some of the identifiers that  can be used with ANSI C are:

The use of %c is to denote a single character while the %s is for a string. To show the short, there are two kinds of specifiers called signed and unsigned. The signed one is denoted by %hi while the unsigned one is shown by %hi. The specifier called %Lf is used to show long double and the %n is utilized for prints nothing. %d is a specifier which holds the assumption of base 10 and is a decimal integer. You also must be aware of the %i which has the use of detecting the base automatically. One of the specifiers holds the %o considers the base as 8 which is an octal integer. Similarly, %x is a specifier which is a hexadecimal integer and the %p is used for the address or also for a pointer. Moreover, %u is also used for the int unsigned decimal and %f is utilized for a floating-point number for floats. The use of %e specifier is for the floating-point number which is in scientific notation.

 

Examples of format specifiers in C

 % C format specifier for individual characters:

#include

int main () {

char first_ch = `f’;

printf (“% c \ n “, first_ch);

returns 0.

}

 Output:

% S String format specifier:

#include

int main () {

char str [] = “freeCodeCamp”;

printf (“% s \ n”, str);

returns 0.

}

 Output:

Entering characters using the format identifier:

#include

int main () {

char user_ch;

scanf (“% c”, & user_ch); // User input Y

printf (“% c \ n”, user_ch);

returns 0.

}

Output:

Yes

String input using format specifier% s:

#include

int main () {

char user_str [20];

scanf (“% s”, user_str); // User input fCC

printf (“% s \ n”, user_str);

returns 0.

}

Output:

fCC

nd% i decimal integer format specifier:

#include

int main () {

int found = 2015, curr = 2020;

printf (“% d \ n”, found);

printf (“% i \ n”, curr);

returns 0 increase.

}

Output:

2015

2020

nd Loating Point Number format specifier:

#include

int main () {

float num = 19.99;

printf (“% f \ n”, number);

printf (“% e \ n”, num);

returns 0.

}

 output:

19.990000

1.999000e + 01

% O Octal integer format identifier:

#include

int main () {

int num = 31;

printf (“% o \ n”, num);

returns 0.

}

output:

37 37

% X Hexadecimal Integer Format ID:

#include

int main () {

int c = 28;

printf (“% x \ n”, c);

returns 0.

}

 Output:

1c

 Address printing:% p

When you try to print the memory address of a variable/pointer, `% d` tries to format the address to a number, and a value like 0xbfdd812 is not a number, so`% d` doesn’t work. Use% P. scanf (char * format, arg1, arg2, …)

This function accepts input via standard input (keyboard) and stores it in a variable accordingly. Returns the number of elements that were successfully read. Formal parameters arg1, agr2, .. must be pointers

 

 Decimal:% d

There are many other format specifiers

1.% U for unsigned integers.

2.% lld for long long int.

  1. % O Octal integer without leading zero

4.% x Hexadecimal integer without 0x in front of the number.

 

Hence, these are the format specifiers in C. Now, that you know about them, it will be easier for you to implement them.

About the author

Jeremmy Wade

Hey I am Jaremmy Wade a Tech blogger, writer and contributor i am working as blogger from last 11 years and co- founder of Etechnoblogs.com, also i contribute my thoughts about tech related or internet errors, computer or email errors and business , health tips writer also shares gaming platform knowledge some etc. IF you wish to write for us on E Techno Blogs then feel free to contact Me @ etechnoblogs@gmail.com

Add Comment

Click here to post a comment