Format and C syntax highlighting on code snippets (#25114)
parent
d327a5c36b
commit
15f51af38b
|
@ -6,7 +6,7 @@ title: Format Specifiers
|
||||||
Format specifiers define the type of data that is to be printed on standard output. We need format specifiers in order to take the formatted input or print the formatted output. Format specifiers are also called as format string. Format specifier is used during input and output. It is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf(). Some examples are %c, %d, %f, etc.
|
Format specifiers define the type of data that is to be printed on standard output. We need format specifiers in order to take the formatted input or print the formatted output. Format specifiers are also called as format string. Format specifier is used during input and output. It is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf(). Some examples are %c, %d, %f, etc.
|
||||||
|
|
||||||
Character format specifier : %c
|
Character format specifier : %c
|
||||||
|
```c
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -14,12 +14,13 @@ int main()
|
||||||
printf("%c\n", ch);
|
printf("%c\n", ch);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Output:
|
Output:
|
||||||
A
|
A
|
||||||
|
|
||||||
Integer format specifier : %d, %i
|
Integer format specifier : %d, %i
|
||||||
|
```c
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -28,13 +29,13 @@ int main()
|
||||||
printf("%i\n", x);
|
printf("%i\n", x);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
```
|
||||||
Output:
|
Output:
|
||||||
45
|
45
|
||||||
45
|
45
|
||||||
|
|
||||||
Double format specifier : %f, %e or %E
|
Double format specifier : %f, %e or %E
|
||||||
|
```c
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -43,13 +44,13 @@ int main()
|
||||||
printf("%e\n", a);
|
printf("%e\n", a);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
```
|
||||||
Output:
|
Output:
|
||||||
12.670000
|
12.670000
|
||||||
1.267000e+01
|
1.267000e+01
|
||||||
|
|
||||||
Unsigned Octal number for integer : %o
|
Unsigned Octal number for integer : %o
|
||||||
|
```c
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -57,12 +58,14 @@ int main()
|
||||||
printf("%o\n", a);
|
printf("%o\n", a);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Output:
|
Output:
|
||||||
103
|
103
|
||||||
|
|
||||||
Unsigned Hexadecimal for integer : %x, %X
|
Unsigned Hexadecimal for integer : %x, %X
|
||||||
|
|
||||||
|
```c
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -70,12 +73,14 @@ int main()
|
||||||
printf("%x\n", a);
|
printf("%x\n", a);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Output:
|
Output:
|
||||||
f
|
f
|
||||||
|
|
||||||
String printing : %s
|
String printing : %s
|
||||||
|
|
||||||
|
```c
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -83,6 +88,7 @@ int main()
|
||||||
printf("%s\n", a);
|
printf("%s\n", a);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Output:
|
Output:
|
||||||
nitesh
|
nitesh
|
||||||
|
@ -93,6 +99,7 @@ scanf(char *format, arg1, arg2, …)
|
||||||
|
|
||||||
decimal integer : %d
|
decimal integer : %d
|
||||||
|
|
||||||
|
```c
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -101,12 +108,14 @@ int main()
|
||||||
printf("%d\n", a);
|
printf("%d\n", a);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
output:
|
output:
|
||||||
45
|
45
|
||||||
|
|
||||||
Integer may be octal or in hexadecimal : %i
|
Integer may be octal or in hexadecimal : %i
|
||||||
|
|
||||||
|
```c
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -117,13 +126,14 @@ int main()
|
||||||
printf("%d\n", a);
|
printf("%d\n", a);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
```
|
||||||
output:
|
output:
|
||||||
15
|
15
|
||||||
15
|
15
|
||||||
|
|
||||||
Floating data type : %f, %e(double), %lf(long double)
|
Floating data type : %f, %e(double), %lf(long double)
|
||||||
|
|
||||||
|
```
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -132,12 +142,14 @@ int main()
|
||||||
printf("%f\n", a);
|
printf("%f\n", a);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Output:
|
Output:
|
||||||
0.000000
|
0.000000
|
||||||
|
|
||||||
String input : %s
|
String input : %s
|
||||||
|
|
||||||
|
```c
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -146,12 +158,13 @@ int main()
|
||||||
printf("%s\n", str);
|
printf("%s\n", str);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Output:
|
Output:
|
||||||
nitesh
|
nitesh
|
||||||
|
|
||||||
Character input : %c
|
Character input : %c
|
||||||
|
```c
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
@ -160,6 +173,7 @@ int main()
|
||||||
printf("%c\n", ch);
|
printf("%c\n", ch);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
output:
|
output:
|
||||||
A
|
A
|
||||||
|
|
Loading…
Reference in New Issue