Format Specifiers in C

The format specifiers in C language notify the compiler about the data type of a variable during input and output operations. The compiler can determine what kind of data is in a variable by using the format specifier in C when using the scanf() and printf() function to read and print data in the console. In C, the format specifiers for char use the character ‘s’ in different formats.



Format Specifiers in C

This format specifier is used for input and output operations such as printf, scanf, fprintf, fscanf. The first argument of the printf and scanf helps you to define the format specifiers such as %s,%d and so on, in addition to constant characters. The first argument will be printed exactly as defined in the printf function. The variable or value is defined in the second and subsequent arguments.

Syntax of the command

printf(const char *format, arg1, arg2, …)
scanf(const char *format, arg1, arg2, …)
fprintf(FILE *fptr, const char *format, arg1, arg2, …)
fscanf(FILE *fptr, const char *format, arg1, arg2, …)

Example

#include <stdio.h>

int main(int argc, char** argv) {
	int a = 16;
	printf("int value : %d\n", a); 
	printf("oct value : %o\n", a); 
	printf("hex value : %x\n", a); 
	return 0;
}

Output

int value : 16
oct value : 20
hex value : 10


Format Specifiers format

Format Specifier often starts with percentage (%) and followed by one or two special characters informing about the data type of the data. Additional characters are used to construct a printing format along with format specifier.

The optional characters are minus(-), dot (.) and number.

  • A number defines the minimum data width. If the number of characters is less than the number specified, the format specifier prefixes the space. If the number of characters is lower, it will print as it is.
  • A minus(-) symbol shows the left data alignment
  • A dot(.) represents the precision of the decimal values. If it is a string, the number of the character to be written.


String, Char Format Specifiers in C

#include <stdio.h>

int main(int argc, char** argv) {
	char s[] = "YawinTutor"; 
	printf("value actual                : [%s]\n", s); 
	printf("value with number           : [%15s]\n", s); 
	printf("value with minus+number     : [%-15s]\n", s); 
	printf("value with number+dot       : [%15.5s]\n", s); 
	printf("value with minus+number+dot : [%-15.5s]\n", s); 

	printf("value with dot              : [%.5s]\n", s); 
	printf("value with minus            : [%-s]\n", s); 
	printf("value with minus+dot        : [%-.5s]\n", s); 
	return 0;
}

Output

value actual                : [YawinTutor]
value with number           : [     YawinTutor]
value with minus+number     : [YawinTutor     ]
value with number+dot       : [          Yawin]
value with minus+number+dot : [Yawin          ]
value with dot              : [Yawin]
value with minus            : [YawinTutor]
value with minus+dot        : [Yawin]


Float Number Format Specifier in C

#include <stdio.h>

int main(int argc, char** argv) {
	float f = 12345.67891; 
	printf("value actual                : [%f]\n", f); 
	printf("value with number           : [%15f]\n", f); 
	printf("value with minus+number     : [%-15f]\n", f); 
	printf("value with number+dot       : [%15.2f]\n", f); 
	printf("value with minus+number+dot : [%-15.2f]\n", f); 

	printf("value with dot              : [%.2f]\n", f); 
	printf("value with minus            : [%-f]\n", f); 
	printf("value with minus+dot        : [%-.2f]\n", f); 
	return 0;
}

Output

value actual                : [12345.678711]
value with number           : [   12345.678711]
value with minus+number     : [12345.678711   ]
value with number+dot       : [       12345.68]
value with minus+number+dot : [12345.68       ]
value with dot              : [12345.68]
value with minus            : [12345.678711]
value with minus+dot        : [12345.68]



Related Articles

Leave a Reply

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