-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormatspecifiers.c.save
More file actions
141 lines (108 loc) · 4.19 KB
/
Formatspecifiers.c.save
File metadata and controls
141 lines (108 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*@Shyed Shahriar Housaini
Copyright: @uthor*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <windows.h>
///#include <threads.h>
#include <conio.h>
#include <dos.h>
#include <direct.h>
#include <math.h>
#include <stdbool.h>
#include<ctype.h>
int main(void)
{
/**
https://www.google.com/search?client=opera&hs=mbc&sxsrf=ALeKk01i4Whd3cvlBusztiHVr2rdN98thw%3A1596615022635&ei=bmkqX8C2JomGyAOU8Ii4Cg&q=scanf+and+printf+of+double+and+float+in+c&oq=scanf+and+printf+of+double+and+float+in+c&gs_lcp=CgZwc3ktYWIQAzoECAAQRzoHCCMQsAIQJzoICAAQCBAHEB46CAgAEA0QBRAeOgQIIRAKUN-jAVjx8wFg-vkBaABwAXgAgAHJAogBlB2SAQgwLjE2LjMuMZgBAKABAaoBB2d3cy13aXrAAQE&sclient=psy-ab&ved=0ahUKEwjApZG6zoPrAhUJA3IKHRQ4AqcQ4dUDCAs&uact=5
https://stackoverflow.com/questions/19952200/scanf-printf-double-variable-c#:~:text=For%20variable%20argument%20functions%20like,and%20%25lf%20for%20double*%20.
https://www.tutorialspoint.com/format-specifiers-in-c
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 = 'B';
printf("%c\n", ch); //printing character data
//print decimal or integer data with d and i
int x = 45, y = 90;
printf("%d\n", x);
printf("%i\n", y);
float f = 12.67;
printf("%f\n", f); //print float value
printf("%e\n", f); //print in scientific notation
int a = 67;
printf("%o\n", a); //print in octal format
printf("%x\n", a); //print in hex format
char str[] = "Hello World";
printf("%s\n", str);
printf("%20s\n", str); //shift to the right 20 characters including the string
printf("%-20s\n", str); //left align
printf("%20.5s\n", str); //shift to the right 20 characters including the string, and print string up to 5 character
printf("%-20.5s\n", str); //left align and print string up to 5 character
}
Output
B
45
90
12.670000
1.267000e+001
103
43
Hello World
Hello World
Hello World
Hello
Hello
We can use these format specifiers for the scanf() function also in the same manner. So we can take the input from scanf() like above how we have printed.
**/
char ch = 'B';
printf("%c\n", ch); //printing character data
//print decimal or integer data with d and i
printf(R"( //print decimal or integer data with d and i )");
printf("\n\n");
int x = 45, y = 90;
printf("%d\n", x);
printf("%i\n", y);
float f = 12.676767;
printf("%.3f\n", f); ///print float value /// Printing to only 3rd position after . decimal position;
printf("%f\n", f); //print float value
printf("%e\n", f); //print in scientific notation
int a = 67;
printf("%o\n", a); //print in octal format
printf("%x\n", a); //print in hex format
char str[] = "Hello World";
printf("%s\n", str);
printf("%20s\n", str); //shift to the right 20 characters including the string
printf("%-20s\n", str); //left align
printf("%20.5s\n", str); //shift to the right 20 characters including the string, and print string up to 5 character
printf("%-20.5s\n", str); //left align and print string up to 5 character
return 0;
}