Complete Guide to % Format Specifiers in C for Beginners in 2023

Article by: Manish Methani

Last Updated: November 7, 2021 at 2:04pm IST
5 min 33 sec read

Introduction:

In C programming, the printf() function is used to output values to the console. The function uses format specifiers to determine the type of data that is being output. The % format specifiers are used to format the output and can be used with various flags and modifiers to achieve different effects.

Types of Format Specifiers:

There are several types of format specifiers in C programming, including:

  • %d: prints a signed integer
  • %u: prints an unsigned integer
  • %f: prints a floating-point number
  • %c: prints a character
  • %s: prints a string
  • %p: prints a pointer
  • %x: prints a hexadecimal value
  • %o: prints an octal value

Using the % Format Specifiers with printf():

The % format specifiers are used in conjunction with the printf() function to format the output. Here are some examples of how to use the % format specifiers with the printf() function:

Example 1: Printing a signed integer

int num = -123;
printf("The value of num is %d", num);

Output:

The value of num is -123

Example 2: Printing an unsigned integer

unsigned int num = 123;
printf("The value of num is %u", num);

Output:

The value of num is 123

Example 3: Printing a floating-point number with precision

float num = 3.14159;
printf("The value of num with 2 decimal places is %.2f", num);

Output:

The value of num with 2 decimal places is 3.14

Example 4: Printing a character

char ch = 'A';
printf("The character is %c", ch);

Output:

The character is A

Example 5: Printing a string

char str[] = "Hello, world!";
printf("The string is %s", str);

Output:

The string is Hello, world!

Example 6: Printing floating-point number with precision %.2f

 float a = 3.122222223;
printf(“a = %.2f ”, a);

Output:

a = 3.12

Example 7: Printing floating-point number with precision %.3f

 float a = 3.122222223;
printf(“a = %.3f ”, a);

Output:

a = 3.122

%.3f will restrict the values upto three decimal values.

Example 8: Printing floating-point number with precision %6.2f

 float a = 1.289999;
printf(“ a = %6.2f ”, a);

Output:

_ _1.29

where _ _ are spaces

%6.2f :- means output will be in 6 columns

Example 9: Printing floating-point number with precision %6.2f

a = 1.28999;

While printing float values using %6.2f format specifier, the final output should be of length 6. As 6.2f indicates, the total length of output should be 6 and 2 values after "." The rest part is covered with spaces. As you can see in the output image below, 2 decimal values after dot and before that we need three more values so that total length becomes 6.

Example 10: Printing floating-point number with precision %5.3f

    float a = 1.289999;
    printf(“ a = %5.3f ”, a);
    

Output: 

In this case, the final output length should be of length 5 and 3 decimal values after "." dot and 2 values before "." dot

Example 11: Printing floating-point number with precision %5.2f

float a = 1.289999;
printf(“ a = %5.2f ”, a);

Output:

In this case, the final output length should be of length 5 and 2 decimal values after "." dot and 3 values before "." dot

Example 12: Printing floating-point number with precision %5.3f

    float a = 1111.289999;
    printf(“ a = %5.3f ”, a);

Output:

a = 1111.290

In this case, before dot it contains enough digits than 5 so it will store 1111 in one block and rest in another.

Space will only be added when there are not enough digits.

Quick Recap:

Question

printf(“ %5.2f “, a);

Answer

    If a is 1111.2344444, it prints 1111.23
    If a is 11.2344444, it prints 11.23
    If a is 1.2344444 , it prints _1.234 ,
    where _ is one leading whitespace character.

Integer Output Format

Example 1:

int a = 1234;
printf(“a = %3d”, a);

Output:

a = 1234

Example 2:

int a = 123;
printf(“a = %3d”, a);

Output:

a = 123

Example 3:

int a = 12;
printf(“a = %3d”, a); 

Output:

a = _12
where, _ is space

Example 4:

int a = 1;
printf(“a = %3d”, a);

Output:

 a = _ _1
where, _ are spaces.

In conclusion, the % format specifiers are a powerful tool in C programming that allows you to format output in various ways. By using the examples provided in this tutorial, you can master the art of formatting output and improve your C programming skills.

Frequently Asked Questions

Q: What are format specifiers in C?

A: Format specifiers are placeholders used in C language to print the value of a variable in a specific format. They are used with printf() and scanf() functions to format input and output.

Q: What is the syntax of a format specifier?

A: The syntax of a format specifier starts with the percentage sign (%) followed by optional flags, width, precision, length modifier and conversion specifier.

Q: What is the purpose of width and precision in format specifiers?

A: Width specifies the minimum number of characters to be printed while printing a variable. Precision specifies the number of digits to be printed after the decimal point while printing floating-point numbers.

Q: What are the most commonly used format specifiers in C?

A: The most commonly used format specifiers in C are %d for integers, %f for floating-point numbers, %c for characters, %s for strings, and %p for pointers.

Q: Can I use multiple format specifiers in a printf() or scanf() statement?

A: Yes, you can use multiple format specifiers in a printf() or scanf() statement to format the output or input of multiple variables.

Q: What are some advanced format specifiers in C?

A: Some advanced format specifiers in C include %e and %E for scientific notation, %u for unsigned integers, %x and %X for hexadecimal numbers, and %n for the number of characters printed so far.

Q: What happens if I use the wrong format specifier for a variable?

A: Using the wrong format specifier for a variable can result in undefined behavior and may cause the program to crash or produce unexpected results. It is important to always use the correct format specifier for the type of variable being printed or scanned.

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>