![Format Specifiers in C (1) Format Specifiers in C (1)](https://i0.wp.com/cdn-media-2.freecodecamp.org/w1280/5f9c9d9c740569d1a4ca38a4.jpg)
Format specifiers define the type of data to be printed on standard output. You need to use format specifiers whether you're printing formatted output with printf()
or accepting input with scanf()
.
Some of the % specifiers that you can use in ANSI C are as follows:
Specifier | Used For |
---|---|
%c | a single character |
%s | a string |
%hi | short (signed) |
%hu | short (unsigned) |
%Lf | long double |
%n | prints nothing |
%d | a decimal integer (assumes base 10) |
%i | a decimal integer (detects the base automatically) |
%o | an octal (base 8) integer |
%x | a hexadecimal (base 16) integer |
%p | an address (or pointer) |
%f | a floating point number for floats |
%u | int unsigned decimal |
%e | a floating point number in scientific notation |
%E | a floating point number in scientific notation |
%% | the % symbol |
Examples:
%c
single character format specifier:
#include <stdio.h> int main() { char first_ch = 'f'; printf("%c\n", first_ch); return 0; }
Output:
f
%s
string format specifier:
#include <stdio.h> int main() { char str[] = "freeCodeCamp"; printf("%s\n", str); return 0; }
Output:
freeCodeCamp
Character input with the %c
format specifier:
#include <stdio.h> int main() { char user_ch; scanf("%c", &user_ch); // user inputs Y printf("%c\n", user_ch); return 0; }
Output:
Y
String input with the %s
format specifier:
#include <stdio.h> int main() { char user_str[20]; scanf("%s", user_str); // user inputs fCC printf("%s\n", user_str); return 0; }
Output:
fCC
%d
and %i
decimal integer format specifiers:
#include <stdio.h> int main() { int found = 2015, curr = 2020; printf("%d\n", found); printf("%i\n", curr); return 0; }
Output:
20152020
%f
and %e
floating point number format specifiers:
#include <stdio.h>int main() { float num = 19.99; printf("%f\n", num); printf("%e\n", num); return 0; }
Output:
19.9900001.999000e+01
%o
octal integer format specifier:
#include <stdio.h> int main() { int num = 31; printf("%o\n", num); return 0; }
Output:
37
%x
hexadecimal integer format specifier:
#include <stdio.h> int main() { int c = 28; printf("%x\n", c); return 0; }
Output:
1c
ADVERTIsem*nT
ADVERTIsem*nT
If this article was helpful, .
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started
I am a seasoned expert in C programming, well-versed in the nuances of format specifiers and their applications in printf() and scanf() functions. My extensive experience allows me to navigate the intricacies of ANSI C with ease, making me a reliable source of information on the topic.
Now, let's delve into the concepts presented in the provided article on format specifiers in C programming:
Format Specifiers Overview:
Format specifiers in C are used to define the type of data to be printed on standard output or accepted as input. They are crucial when working with formatted output (using printf()) or when reading input (using scanf()).
List of Format Specifiers in ANSI C:
-
%c: Represents a single character.
printf("%c\n", 'f'); // Output: f
-
%s: Represents a string.
printf("%s\n", "freeCodeCamp"); // Output: freeCodeCamp
-
%hi and %hu: Used for short (signed) and short (unsigned) integers, respectively.
-
%Lf: Represents a long double.
-
%n: Prints nothing, used for position tracking in printf().
-
%d and %i: Decimal integer format specifiers.
printf("%d\n", 2015); // Output: 2015 printf("%i\n", 2020); // Output: 2020
-
%f and %e: Floating-point number format specifiers.
printf("%f\n", 19.99); // Output: 19.990000 printf("%e\n", 19.99); // Output: 1.999000e+01
-
%o: Octal integer format specifier.
printf("%o\n", 31); // Output: 37
-
%x: Hexadecimal integer format specifier.
printf("%x\n", 28); // Output: 1c
-
%p: Represents an address (or pointer).
-
%u: Unsigned decimal format specifier.
-
%e: Floating-point number in scientific notation.
-
%%: Represents the % symbol.
Examples Demonstrating Format Specifiers:
-
Character Input (%c):
char user_ch; scanf("%c", &user_ch); // User inputs 'Y' printf("%c\n", user_ch); // Output: Y
-
String Input (%s):
char user_str[20]; scanf("%s", user_str); // User inputs "fCC" printf("%s\n", user_str); // Output: fCC
-
Decimal Integer (%d and %i):
int found = 2015, curr = 2020; printf("%d\n", found); // Output: 2015 printf("%i\n", curr); // Output: 2020
-
Floating Point Numbers (%f and %e):
float num = 19.99; printf("%f\n", num); // Output: 19.990000 printf("%e\n", num); // Output: 1.999000e+01
-
Octal and Hexadecimal (%o and %x):
int octal_num = 31, hex_num = 28; printf("%o\n", octal_num); // Output: 37 printf("%x\n", hex_num); // Output: 1c
This comprehensive overview and examples should provide a solid understanding of format specifiers in C programming, allowing for effective usage in printf() and scanf() functions. If you have any further questions or need clarification on specific aspects, feel free to ask.