top

Search

C Tutorial

.

UpGrad

C Tutorial

Format Specifiers in C

Overview:

Format specifiers in the C programming language are a specific class of characters used for input and output processing, i.e., they are a sort of data used to publish the data on standard output. These specifiers are typically used with the printf and scanf functions to publish the output data that any variable applies to. As a result, if we wish to display the value of a variable on the standard output, we use the scanf() function. This function allows us to apply format specifiers for certain data types to print in the appropriate manner. The "%" sign is often followed by characters for certain data kinds in these format specifiers in C.

Why Do We Use C Format Specifiers?

Format specifiers in C are used to receive inputs and print the output based on the input type. Every format specifier uses the symbol %. Format specifiers in C specify the types of data that must be input and the types of data that must be displayed on the screen.

Most Commonly Used Format Specifiers in C

There are many format specifiers in C, and each is utilized for a certain data type. Here are a few of the C format specifiers that are often used:

%d (Decimal Integer) Format Specifier:

In C programming, %d is a format specifier in C for double that accepts integer values as signed decimal integers, including both positive and negative values. However, values must be represented in decimals, or a trash value would be shown. The following is the syntax for the %d specifier for input and output: 

scanf("%d...", ...); \\for input
printf("%d...", ...); \\ for output

Let's look at this format specifiers in c with examples. The printf() function prints an integer number with the %d operator in this illustration.

#include <stdio.h>
int main()
{
//declare an integer type value
  int num = 20;
  //Use format specifier as %d to print the above integer value
  printf("This variable's value is printed as: %d\n", num);
  return 0;
}

 Output: 

20

%c (Character) Format Specifier:

The format specifier in the C language for the char data type is the %c. It may be used in the C language for both formatted input and output. In order to print the character stored in a variable, it is used in conjunction with the printf() function. When printing character data, you must use the %c format specifier.

// C Program to define the %c format specifier.

#include <stdio.h>
int main()
{
    char c;
    // using %c for character input
    scanf("Enter the character: %c", &c);
    // using %c for character output
    printf("The entered character is: %c", &c);
    return 0;
}

%f (Floating Point) Format Specifier:

The %f format specifier is implemented for representing all the fractional values. For printing the floating-point or fractional value kept in the variable, it is implemented within the printf() function. Anytime you need to print any fractional or floating-point data, you must use the %f format specifier. 

printf("%%f is : %f\n", 1.0/3.0); //0.333333

%e (Floating Pointer Number) Format Specifier:

There must be a floating-point value in the argument. The value is changed into a string with the following format: "-d.ddd...E ddd". If the result is a negative number, the string begins with a minus sign. The decimal point is always followed by one digit. The precision specifier in the format string specifies the total number of digits in the output string (including the one before the decimal point); if no precision specifier is present, a default precision of 15 is assumed. In the resultant string, the "e" exponent character is always followed by a plus or minus sign and at least three numbers. 

printf("%e\n", 5.5); //5.500000e 00

%s (String) Format Specifier:

The argument must be a character type, a string, or a char* value. The format specifier is replaced with the string format specifier in c or character. The precision specifier specifies the maximum length of the output string if it appears in the format string. If the argument is a string, it is truncated if it exceeds this length. 

#include <stdio.h>
int main()
{
    char s[10]="writing";
    printf("The value of this string is %s \n",s);
    return 0;
}

Output: 

The value of this string s is writing

%lf (Double) Format Specifier:

It is used to print the double-precision floating number or long float.

#include <stdio.h>

int main()
{
    // declaring the double variables
    double c_temp, f_temp;
    printf(" Enter temperature in the format of Celsius: ");

    // taking the input
    scanf (" %lf", &c_temp);

    f_temp  = (c_temp * 1.8)32;

    // displaying the output
    printf (" The temperature converted in the Fahrenheit is: %lf," f_temp);
    return 0;
}

Output: 

 Enter the temperature in the format of Celsius: 37
 The temperature in Fahrenheit is: 98.600000

%o (octal integer) Format Specifier:

An integer value must be used as the parameter. The value is changed into an octal-digit string. If a precision specifier is included in the format string, it means that the resulting string must have at least that many digits; otherwise, the string is left-padded with zeroes.

#include <stdio.h> 
int main() 
{ 
    int data = 65;
    printf("%o\n", data); 
    return 0; 
}

Output: 

101

%x (Hexadecimal Integer) Format Specifier:

A Hexadecimal number is represented in C programming language by antedating with" 0x" or" 0X". Therefore the value in Hexadecimal can be written as" 0x64".

Hexadecimal values may be stored in any integral form of data type (char, short, or int) since they are integer values. Hence there is no particular type of data type for storing them in C programming.

We are storing "FAFA" in the int variable and "64" in an unsigned char variable (64 is a tiny number and may be stored in a Byte).

#include <stdio.h>
int main()
{
    unsigned char b;
    int a;
    printf("Enter value of b: ");
    scanf("%x",&b);
    printf("Enter value of a: ");
    scanf("%x",&a);
    printf("Value of b: Hex: %X, Decimal: %d\n",b,b);
    printf("Value of a: Hex: %X, Decimal: %d\n",a,a);
    return 0;
}

Output:

 Enter the value of a: 64
 Enter the value of b: FAFA
 Value of a: Hex: 64, Decimal: 100 
 Value of b: Hex: FAFA, Decimal: 64250

%p (Prints Memory Address) Format Specifier:

The C language likewise provides the format specifier to output the address and pointers. In C, we may use %p to display addresses and pointers.

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

Input and Output Formatting:

The C programming language has certain capabilities that allow us to format input and output. Some of them include the following and are often positioned between the sign and the format specifier symbol:

  • The negative (-) symbol indicates left alignment.

  • If the letters fit inside the field's minimum width, the leftover space will be filled with white space; if not, they will be printed without truncation. A number following % designates the minimum field width to be printed.

  • The field width is precisely separated using the period (.) character.

  • Precision indicates the least and maximum number of characters in a string, the number of digits after the decimal place in a floating value, and the minimum and maximum number of digits in an integer.

Conclusion

This article briefly overviews the format specifier used in the C programming language. Different format specifiers in C exist in C for various data types, and they may be used to print values stored in variables using the printf() function as well as to accept these variable values as input by using the scanf() function, which also makes use of format specifiers. Numerous specifiers, including integer (%d), char (%c), string (%s), and float (%f), are covered.

FAQs

1. What is the main purpose of different format specifiers in C?

Format specifiers in C are used to specify the type and formatting of input or output data through standard input and output streams in C. By doing this, and they guarantee that data is received or shown accurately and prevent software behavior problems.

2.Can you use format specifiers in C functions other than printf() and scanf()?

In order to prepare data for output or input, format specifiers are commonly used with C functions like printf(), scanf(), and fprintf(). They can also be used with other functions like sscanf(), sprintf(), and snprintf() that take structured input or output.

3. What is the distinction between C's %d and %i format specifiers?

In C, integer values are formatted using the %d and %i format specifiers. The primary distinction between the two is that input in octal and hexadecimal notation is supported by %i, whereas it isn't supported by %d. Integers are printed by both format specifiers using decimal notation.

4. Can we utilize several format specifiers in a single printf() statement in C?

A single printf() statement in C can contain several format specifiers. For example, you may print an integer, a floating-point number, and a character using the notation "%d %f %c."

5. How to print a floating-point number with a precision % of 6.2f?

While printing float values using precision %6.2 f format specifier, the final output should be of length 6. As 6.2 f indicates, the total length of the output should be 6 and 2 values after "." The remaining area is covered in blank spaces. After the dot, there will be two decimal numbers in the output, and we still require three more values to get the overall length to six.

Leave a Reply

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