Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Format Specifiers in C

Last Updated on March 13, 2023 by Prepbytes

Format specifiers in C programming are used to specify the kind of data being passed as an argument to the printf() or scanf() functions. The format specifier in c is a special character that starts with a percentage sign (%) and continues with a letter or group of letters that denotes the data type.

What is the Format Specifier in C?

The format specifier in c is very useful to work with input and output in C language. Format specifiers in C are special characters used to define the type and formatting of data that is input or output through standard input and output streams.

Let’s take an example to understand the format specifier in C language.

Example of Format Specifier in C:

// format specifiers in c
#include <stdio.h>

int main() {
    int number = 42;
    float PI = 3.14159;
    char letter = 'A';
    printf("The integer is %d, the float is %.2f, and the character is %c.\n", number, PI, letter);
    return 0;
}

Output:

The integer is 42, the float is 3.14, and the character is A.

In the above example, we have three variables: an integer number, a float PI, and a character letter. We use the printf function to display the values of these variables, along with some descriptive text. The format specifier %d is used to format the integer value, %f is used to format the floating-point value, and %c is used to format the character. The %.2f format specifier is used to format the floating-point value with two decimal places which is also called the format specifier for double.

Types of Format Specifiers in C

There are several types of format specifiers in C language. Let’s see various types of format specifiers in C with details.

Format Specifier for Integer:

To specify the integer data type %d and %i format specifiers in C are used. Let’s see an example to understand how to use Integer format specifiers in C.

Example of Format Specifier for Integer:

#include <stdio.h>
int main()
{
    int number1 = 45;
    int number2 = 90;
    
    printf("The number1 is: %d\n", number1);
    printf("The number2 is: %i\n", number2);
    
    return 0;
}

Output:

The number1 is: 45
The number2 is: 90

Format Specifier for Unsigned Integer:

To specify the unsigned integer data type %u format specifiers in C are used. Let’s see an example to understand how to use unsigned Integer format specifiers in C.

Example of Format Specifier for Unsigned Integer:

#include <stdio.h>
int main()
{
    unsigned int number1 = -45;
    unsigned int number2 = 90;
    
    printf("The number1 is: %u\n", number1);
    printf("The number2 is: %u\n", number2);
    
    return 0;
}

Output:

The number1 is: 4294967251
The number2 is: 90

Format specifiers for Floting-point:

To specify the floating-point data type %f and %e or %E format specifiers in C are used. Let’s see an example to understand how to use floating-point format specifiers in C.

Example of Format Specifiers for Floting-point:

#include <stdio.h>
int main()
{
    float number1 = 15.25;
    float number2 = 18.75;
    float number3 = 20.50;
    
    printf("The number1 is: %f\n", number1);
    printf("The number2 is: %e\n", number2);
    printf("The number3 is: %E\n", number3);
    
    return 0;
}

Output:

The number1 is: 15.250000
The number2 is: 1.875000e+01
The number3 is: 2.050000E+01

Format Specifier for Double:

The format specifier for double in C is %lf. The l in %lf specifies that the argument is a long double, which is the data type that double gets promoted to when passed as a variadic argument in C. Let’s see an example to understand how to use a format specifier for double in C language.

Example of Format Specifier for Double:

#include <stdio.h>

int main() {
    double number = 3.141592653589793;
    printf("The value of pi is approximately %.15lf\n", number);
    return 0;
}

Output:

The value of pi is approximately 3.141592653589793

Format Specifier for Unsigned Octal Number:

To specify the unsigned octal number %o format specifiers in C are used. Let’s see an example to understand how to use unsigned octal number format specifiers in C.

Example of Format Specifier for Unsigned Octal Number:

#include <stdio.h>
int main()
{
    int number1 = 100;
    printf("The number1 is: %o\n", number1);
    return 0;
}

Output:

The number1 is: 144

Format Specifier for Unsigned Hexadecimal:

To specify the unsigned Hexadecimal %x or %X format specifiers in C are used. Let’s see an example to understand how to use Hexadecimal format specifiers in C.

Example of Format Specifier for Unsigned Hexadecimal:

#include <stdio.h>
int main()
{
    int number1 = 100;
    printf("The number1 is: %x\n", number1);
    return 0;
}

Output:

The number1 is: 64

Format Specifier for Character:

To specify the character %c format specifiers in C are used. Let’s see an example to understand how to use character format specifiers in C.

Example of Format Specifier for Character:

#include <stdio.h>
int main()
{
    char ch = 'A';
    printf("The character is: %c\n", ch);
    return 0;
}

Output:

The character is: A

Format Specifier for String:

To specify the String %s format specifiers in C are used. Let’s see an example to understand how to use String format specifiers in C.

Example of Format Specifier for String:

#include <stdio.h>
int main()
{
    char st[] = "PrepBytes";
    printf("The String is: %s\n", st);
    return 0;
}

Output:

The String is: PrepBytes

Format Specifier for Printing Address:

To specify the Printing Address %p format specifiers in C are used. Let’s see an example to understand how to use Printing Addressformat specifiers in C.

Example of Format Specifier for Printing Address:

#include <stdio.h>
int main()
{
    int number = 264;
    printf("The Memory Address of variable number is: %p\n",(void*)&number);
    return 0;
}

Output:

The Memory Address of variable number is: 0x7ffc538bfdf4

How to use Format Specifier with scanf Function to take User Inputs

Format specifiers in C helps to determine the type of user input. Let’s see the syntax of how to take user input with format specifiers in C.

Syntax of Format Specifier in C:

scanf(“ %format specifier ”, & variable_name);

Let’s see an example to understand how to use format specifiers in C with scanf function.

Example:

#include <stdio.h>
int main()
{
    int number;
    printf("Enter the number: ");
    scanf("%d", &number); 
    printf("The entered number is: %d\n", number);
    return 0;
}

Output:

Enter the number: 20
The entered number is: 20

Conclusion
In this article, you will learn what is format specifiers in C language and you will also learn how to use these all format specifiers in C with printf function. In addition, you will also learn how to use format specifiers with scanf function.

FAQs:

1. What is the purpose of format specifiers in C?
Format specifiers are used to specify the type and formatting of data that is input or output through standard input and output streams in C. They ensure that data is read or displayed correctly and avoid issues with the program’s behavior.

2. What happens if the format specifier in C does not match the data type?
If the format specifier does not match the type of data being processed, the results may be unpredictable, leading to runtime errors, data corruption, or other issues.

3. Can format specifiers be used with other functions besides printf() and scanf() in C?
Format specifiers are typically used with functions such as printf(), scanf(), and fprintf() in C to format data for output or input. However, they can also be used with other functions that accept formatted input or output, such as sscanf(), sprintf(), and snprintf().

4. What is the difference between the %i and %d format specifiers in C?
In C, the %i and %d format specifiers are used to format integer values. The main difference between the two is that %i supports input in octal and hexadecimal notation, whereas %d does not. Both format specifiers output integers in decimal notation.

5. Can you use multiple format specifiers in a single printf() statement in C?
Yes, you can use multiple format specifiers in a single printf() statement in C. For example, you can use "%d %f %c" to print an integer, a floating-point value, and a character, respectively.

Leave a Reply

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