Home C Programming Language / Programs / Program to Demonstrate Format Specifiers in C Language
🚀 Programming Example

Program to Demonstrate Format Specifiers in C Language

👁 4 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 Information & Algorithm

  1. Start the program.
  2. Declare variables of different data types such as integer, float, character, and string.
  3. Assign values to the variables.
  4. Use appropriate format specifiers with the printf() function to display the values.
  5. Observe how different format specifiers are used for different data types.
  6. End the program.

💻 Program Code

#include <stdio.h>

int main()
{
    int number = 100;
    float percentage = 95.75;
    char grade = 'A';
    char course[] = "C Programming";

    printf("Integer Value = %d\n", number);
    printf("Float Value = %.2f\n", percentage);
    printf("Character Value = %c\n", grade);
    printf("String Value = %s\n", course);

    return 0;
}
                        

🖥 Program Output

Integer Value = 100
Float Value = 95.75
Character Value = A
String Value = C Programming
                            

📘 Explanation

A format specifier is a special code used with input and output functions such as printf() and scanf(). It tells the compiler the type of data that should be displayed or accepted.

The following program demonstrates some commonly used format specifiers in C.




Program Explanation

Different data types require different format specifiers. Using the correct format specifier ensures that data is displayed correctly.

Format Specifier Data Type Example Value
%d int 100
%f float 95.75
%c char A
%s string (character array) C Programming

In the statement printf("Integer Value = %d", number);, the %d format specifier is used to display an integer value. Similarly, %f is used for floating-point numbers, %c is used for characters, and %s is used for strings.

Some other commonly used format specifiers in C are:

  • %ld - Long Integer
  • %lf - Double
  • %u - Unsigned Integer
  • %x - Hexadecimal Number
  • %o - Octal Number

Format specifiers are essential because they help the compiler correctly interpret and display data stored in variables.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.