Home > Backend Development > C++ > Format specifiers in C language

Format specifiers in C language

PHPz
Release: 2023-09-03 14:17:05
forward
1027 people have browsed it

Format specifiers in C language

The format specifiers are used in C for input and output purposes. Using this concept the compiler can understand that what type of data is in a variable during taking input using the scanf() function and printing using printf() function. Here is a list of format specifiers.

Format Specifier Type
%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 = &#39;B&#39;;
   printf("%c</p><p>", ch); //printing character data
   //print decimal or integer data with d and i
   int x = 45, y = 90;
   printf("%d</p><p>", x);
   printf("%i</p><p>", y);
   float f = 12.67;
   printf("%f</p><p>", f); //print float value
   printf("%e</p><p>", f); //print in scientific notation
   int a = 67;
   printf("%o</p><p>", a); //print in octal format
   printf("%x</p><p>", a); //print in hex format
   char str[] = "Hello World";
   printf("%s</p><p>", str);
   printf("%20s</p><p>", str); //shift to the right 20 characters including the string
   printf("%-20s</p><p>", str); //left align
   printf("%20.5s</p><p>", str); //shift to the right 20 characters including the string, and print string up to 5 character
   printf("%-20.5s</p><p>", str); //left align and print string up to 5 character
}
Copy after login

输出

B
45
90
12.670000
1.267000e+001
103
43
Hello World
Hello World
Hello World
Hello
Hello
Copy after login

我们可以以相同的方式使用这些格式说明符来使用scanf()函数。因此,我们可以像上面打印的那样从scanf()中获取输入。

The above is the detailed content of Format specifiers in C language. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template