1. C Programming for Beginners: Writing Your First Hello World Program in C in 2023 2. Mastering Datatypes in C Programming: A Comprehensive Guide for Beginners in 2023 3. Complete Guide to % Format Specifiers in C for Beginners in 2023 4. A Comprehensive Guide to Operators in C Programming: Learn Everything About Operators in C in 2023 5. Master C Operator Precedence and Associativity in 2023: Definition, Examples, and Code 6. Learn Increment and Decrement Operators in C: A Beginners Guide with Examples - Codzify 7. Mastering Bitwise Operators in C: A Comprehensive Guide with Examples - Codzify 8. Ternary Operators in C Programming: A Beginners Guide (2023) 9. Switch Statements in C 2023: Complete Guide for Beginners 10. Learn Functions in C Programming - A Complete Tutorial for Beginners in 2023 11. Call by Value and Call by Reference in C Programming - A Complete Tutorial for Beginners in 2023 12. Storage Classes in C Programming: A Comprehensive Guide for 2023 13. Arrays in C Programming: A Comprehensive Guide for 2023 14. Basics of 2D Array in C Programming in 2023: Definition, Initialization, and Accessing Elements 15. Beginners Guide to Strings in C Programming with Codes and Examples 16. Strings Functions in C Language - strlen, strcpy,strcmp,strcat functions 17. A Comprehensive Guide to Structures in C Programming in 2023 18. Learn Unions in C Programming with Codes, Examples, and Explanations in 2023 19. Dynamic Memory Allocation in C: Understanding malloc, calloc, realloc, and free Functions 20. Introduction to C Programming 21. Hello world program in C 22. String functions in C - strncat, strcpy, strncpy, strchr, strrchr, strstr functions 23. Program for Fibonacci number in C - Codzify.com 24. What is palindrome number in C? 25. Write a C program to find a factorial of a given number 26. Write a C program to check whether a given number is prime or not 27. Write a C program to check whether a given number is an Armstrong number or not. 28. Write a C Program to transpose a matrix 29. C Programming Tutorial: Understanding Comments and Identifiers with Code Examples 30. Master String Handling Functions in C in 2023 - Codzify Topics

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.

Explore Codzify Courses

PREMIUM

Learn to create the Dating App using No-Code Tool FlutterFlow

Level: Advanced

Course Fees: INR 6999/-

Apply Coupon Code: UNLOCK

4 days 100% Money Back Guarantee

Explore Curriculum
PREMIUM

Flutter Mobile App Development Course

Level: Intermediate

Course Fees: INR 1299/-

4 days 100% Money Back Guarantee

Explore Curriculum
PREMIUM

The Complete Angular Course

Level: Beginners

Course Fees: INR 1299/-

4 days 100% Money Back Guarantee

Explore Curriculum
PREMIUM

Dart Programming for Absolute Beginners

Level: Beginners

Course Fees: INR 1299/-

4 days 100% Money Back Guarantee

Coming Soon
FREE

Next.js course

Level: Beginners

Course Fees: FREE

Start Watching
FREE

Fundamentals of Computer Programming Languages

Level: Beginners

Course Fees: FREE

Start Watching
FREE

Learn HTML, CSS & Bootstrap

Level: Beginners

Course Fees: FREE

Start Watching

Latest Web Stories

1

Learn how to open WhatsApp using FlutterFlow | Step by Step Guide

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

Download the Codzify
Mobile App


Learn Anytime, Anywhere at your own pace. Scan the QR Code with your Mobile Camera to Download the Codzify Mobile App.

Codzify Mobile App Codzify Mobile App
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer