Format Specifiers in C (2024)

/ #C
Format Specifiers in C (1)

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:

SpecifierUsed For
%ca single character
%sa string
%hishort (signed)
%hushort (unsigned)
%Lflong double
%nprints nothing
%da decimal integer (assumes base 10)
%ia decimal integer (detects the base automatically)
%oan octal (base 8) integer
%xa hexadecimal (base 16) integer
%pan address (or pointer)
%fa floating point number for floats
%uint unsigned decimal
%ea floating point number in scientific notation
%Ea 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:

  1. %c: Represents a single character.

    printf("%c\n", 'f');  // Output: f
  2. %s: Represents a string.

    printf("%s\n", "freeCodeCamp");  // Output: freeCodeCamp
  3. %hi and %hu: Used for short (signed) and short (unsigned) integers, respectively.

  4. %Lf: Represents a long double.

  5. %n: Prints nothing, used for position tracking in printf().

  6. %d and %i: Decimal integer format specifiers.

    printf("%d\n", 2015);  // Output: 2015
    printf("%i\n", 2020);  // Output: 2020
  7. %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
  8. %o: Octal integer format specifier.

    printf("%o\n", 31);  // Output: 37
  9. %x: Hexadecimal integer format specifier.

    printf("%x\n", 28);  // Output: 1c
  10. %p: Represents an address (or pointer).

  11. %u: Unsigned decimal format specifier.

  12. %e: Floating-point number in scientific notation.

  13. %%: 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.

Format Specifiers in C (2024)
Top Articles
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 6707

Rating: 4.9 / 5 (59 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.