Format Specifiers in C
Introduction
Format specifiers in C are simply types of strings or operators that are primarily used when implementing user input and when printing something to the console. The data type of the input or output to the compiler is what they want to specify. Integer, string, float, and other types of data are considered data types. The format specifiers differ for each type of data. a% is the default symbol at the beginning of a format specifier before a string of characters. They work with the input and output functions in the C language, respectively, scanf and printf.
Why Do We Use C Format Specifiers?
In C, format specifiers are employed to accept inputs and print a type's output. Each of the format specifier uses the symbol %. The kind of data which must be provided as input as well as the kind of information that must be displayed on the display, are both specified by format specifiers to the compiler.
Given that you have a basic understanding of format specifiers within C, let's move on to some lists of format specifiers.
Different Format Specifier Types
Let's now examine the various kinds of format specifiers which are frequently used in C. The list of all C format specifiers can be noticed below.
Symbol | Name | Description |
%d or %i | Signed Integer Format Specifier | It is employed if the data type has been signed int, and it stores values that are integers such as 1 or 2. |
% you | Unsigned Integer Format Specifier | When a data type called not signed int, which stores unsigned integer values, is utilized, this is the data type used. |
%f | Floating Point Format Specifier | It is used if the data type is float, which can store a decimal values that are floating points like 5.5 and 4.5. |
%s | String Format Specifier | This is utilized whenever the data type is of the string variety, capable of holding strings like "Example." |
%c | Character Format Specifier | It is used when a data type is of the Char variety, which stores single characters such as an orb. |
%p | Address Printing Format Specifier | The address of a variable or pointer can only be printed when using it. |
%ld | Long Integer Format Specifier | If the information type is long int, which can store values in the [2,147,483,647, +2,147,483,647] range, it is used. |
%lld | Long Long Integer Format Specifier | When a type of data called long long int is utilized, a long long integer value with up to 64 bits can be stored. |
%lf | Double floating Format Specifier | While a double data type is used to store exact floating-point data. |
Example 1
#include <stdio.h>
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
printf(" The value of a , b and c is %d %d %d", a,b, c);
return 0;
}
Output
![Format Specifiers in C](https://static.tutorialandexample.com/c/format-specifiers-in-c1.png)
Explanation
We first declared all three integer variables, a, b, and c. Then we utilized scanf to collect user input for these variables via the %d format specifier since they are all int variables. Finally, we applied printf to print the user-provided values to the console while also informing the compiler of the data type of the variable it will be printing using %d.
Let's now talk about and examine in more detail how a few frequently employed format specifiers are implemented in C.
Format Specifiers of different integer types
Different integer types used in the C language include int, unsigned int, long int, and long long int. Every one of these data types has a unique format specifier. Now we'll see how some integer data types, such as int, unsigned int, long long int, and long int, are implemented for input and output. Below given is the corresponding code.
Example
#include<stdio.h>
int main() {
int x;
unsigned int y;
long int z;
long long int t;
scanf("%d %u %ld %lld", &x, &y, &z, &t);
printf("The value of x,y,z and t are %d %u %ld %lld", x, y, z, t);
return 0;
}
Output
![Format Specifiers in C](https://static.tutorialandexample.com/c/format-specifiers-in-c1.png)
Explanation
In the code above, we use the correct format specifier for each variable based on the data type.
Format specifiers for various floating point data types
They primarily occur in the form of two types: float and double, which are used whenever we need to accept or output data in decimal form. The only distinction is that the double data type is more precise than the float data type. They both have distinctive Format Specifiers. Let's take a look at the code and its execution.
Example
#include<stdio.h>
int main() {
float var1;
double var2;
scanf("%f %lf", &var1, &var2);
printf("The values of var1 and var2 are %f %lf", var1, var2);
return 0;
}
Output
![Format Specifiers in C](https://static.tutorialandexample.com/c/format-specifiers-in-c1.png)
The appropriate format specifier for every variable in the code above is used based on the variable's data type.
Format Specifiers for char and string data types:
- Based on whether we want to input or output only one character or a string of characters (a char array), we utilize a different Format Specifier.
- As many of us already know, strings are characteristic arrays in the C programming language, so taking input like this in a single line of syntax is a little distinct from other types. Take a look at how that is put into practice as well.
Example
#include<stdio.h>
int main() {
char a;
char str[50];
scanf("%c", &a);
scanf("%s", str);
printf("%c \n", a);
printf("%s",str);
return 0;
}
Output
![Format Specifiers in C](https://static.tutorialandexample.com/c/format-specifiers-in-c1.png)
Explanation
The appropriate format specifier for each variable in the example code above is used based on the variable's data type.
Address Printing Format Specifier –
The Address-Based Printing Format Specifier remains an individual format specifier that is only used to print the address of a variable or pointer. This is how it was done; see below.
- The format specifier is %p in the C programming language and is utilized to print addresses for memory.
- A pointer's value, or whatever memory address it holds, is printed when the corresponding format specifier is combined using the printf function. Here is an explanation with additional details and an illustration
Example
#include<stdio.h>
int main()
{
int x;
float y;
char z;
printf("Address of x: %p\n", &x);
printf("Address of y: %p\n", &y);
printf("Address of y: %p\n", &z);
return 0;
}
Output
![Format Specifiers in C](https://static.tutorialandexample.com/c/format-specifiers-in-c1.png)
Explanation
The address is not required to be the same every time. Each compilation's version is going to be unique.
We are done with all of the significant and widespread Format Specifiers. Let's get started by taking a look at a few more format specifiers within the C language that are not frequently used but are still useful to know about.
Octal Integer Format Specifier (%o)
- This type of Format specifier is mainly employed for printing or implementing input that uses an octal integer, that is, a Base 8 integer. It is shown as %o.
Example
#include <stdio.h>
int main() {
int a=34;
printf("Decimal value of a is : %d\n", a);
printf("Octal value of a is : %o\n", a);
return 0;
}
Output
![Format Specifiers in C](https://static.tutorialandexample.com/c/format-specifiers-in-c1.png)
Hexadecimal Integer Format Specifier (%x)
- This type of Format Specifier is mainly employed to print or accept input that is a hexadecimal unsigned integer value. The notation %x is used to represent this.
- Each alphabet is always displayed in small letters only in this kind of Format Specifier.
Example
#include <stdio.h>
int main() {
int n = 200;
printf("Decimal: %d\n", n);
printf("Hexadecimal value of n is (lowercase): %x\n", n);
printf("Hexadecimal value of n is (uppercase): %X\n", n);
return 0;
}
Output
![Format Specifiers in C](https://static.tutorialandexample.com/c/format-specifiers-in-c1.png)
Signed Short and Unsigned Short Format Specifier
For printing and scanning signed shorts, we utilize the %hi symbol, and for not-signed shorts, we use the %hu symbol. Both of these specifiers are implemented in the same way as other specifiers.
Example
#include <stdio.h>
int main() {
short k = -678;
printf("Signed Short of value k is: %hd\n", k);
return 0;
}
Output
![Format Specifiers in C](https://static.tutorialandexample.com/c/format-specifiers-in-c1.png)
Example 2
#include <stdio.h>
int main() {
unsigned short k = 890;
printf("Unsigned Short of value k is: %hu\n", k);
return 0;
}
Output
![Format Specifiers in C](https://static.tutorialandexample.com/c/format-specifiers-in-c1.png)
So since we have covered all of the C language's format specifiers, we are able to implement them. We have learned about all of the different types of specifiers, their instances, and how to use them in code. That concludes this topic.
Conclusion
Both scanf and printf in the programming language C support the use of format specifiers.
In the C programming language, format specifiers are a kind of operator or string that informs a compiler of the data types of the input and output objects.
Each data type has a different Format Specifier.