format specifiertype
%c character
%d signed integer
%e or %e scientific notation of floats
%f float values
%g or %g similar as %e or %e
%hi signed integer (short)
%hu unsigned integer (short)
%i unsigned integer
%l or %ld or %li long
%lf double
%lf long double
%lu unsigned int or unsigned long
%lli or %lld long long
%llu unsigned long long
%o octal representation
%p pointer
%s string
%u unsigned int
%x or %x hexadecimal representation
%n prints nothing
%% prints % character
these are the basic format specifiers. we can add some other parts with the format specifiers. these are like below −
a minus symbol (-) sign tells left alignment
a number after % specifies the minimum field width. if string is less than the width, it will be filled with spaces
a period (.) is used to separate field width and precision
example live demo
#include <stdio.h>main() { char ch = 'b'; printf("%c
", ch); //printing character data //print decimal or integer data with d and i int x = 45, y = 90; printf("%d
", x); printf("%i
", y); float f = 12.67; printf("%f
", f); //print float value printf("%e
", f); //print in scientific notation int a = 67; printf("%o
", a); //print in octal format printf("%x
", a); //print in hex format char str[] = "hello world"; printf("%s
", str); printf("%20s
", str); //shift to the right 20 characters including the string printf("%-20s
", str); //left align printf("%20.5s
", str); //shift to the right 20 characters including the string, and print string up to 5 character printf("%-20.5s
", str); //left align and print string up to 5 character}
输出b459012.6700001.267000e+00110343hello worldhello worldhello worldhellohello
我们可以以相同的方式使用这些格式说明符来使用scanf()函数。因此,我们可以像上面打印的那样从scanf()中获取输入。
以上就是在c语言中的格式说明符的详细内容。