C Format Specifiers 🎯

beginner
9 min

C Format Specifiers 🎯

Welcome to the world of C Programming! Today, we'll delve into one of the most essential aspects of this language: Format Specifiers. 💡

What are Format Specifiers? 📝

In C, Format Specifiers are used in the printf() function to format output. They tell the compiler how to convert the arguments passed to the function. Let's get started with the basics!

Basic Format Specifiers 💡

%c

Used to print a single character.

c
#include <stdio.h> int main() { char c = 'A'; printf("Character: %c\n", c); return 0; }

%d or %i

Used to print decimal integers. They can print both positive and negative numbers.

c
#include <stdio.h> int main() { int num = 123; printf("Integer: %d\n", num); return 0; }

%f

Used to print floating-point numbers (float or double).

c
#include <stdio.h> #include <math.h> int main() { double pi = M_PI; printf("Pi: %.2f\n", pi); return 0; }

%s

Used to print strings.

c
#include <stdio.h> int main() { char str[20] = "Hello, World!"; printf("String: %s\n", str); return 0; }

Advanced Format Specifiers 💡

%o

Used to print octal numbers.

c
#include <stdio.h> int main() { int num = 10; printf("Octal: %o\n", num); return 0; }

%x or %X

Used to print hexadecimal numbers in lowercase and uppercase, respectively.

c
#include <stdio.h> int main() { int num = 10; printf("Hexadecimal (lowercase): %x\n", num); printf("Hexadecimal (uppercase): %X\n", num); return 0; }

%u

Used to print unsigned integers.

c
#include <stdio.h> int main() { unsigned int num = 4294967295; printf("Unsigned Integer: %u\n", num); return 0; }

%e and %E

Used to print scientific notation of floating-point numbers with e or E suffix.

c
#include <stdio.h> #include <math.h> int main() { double num = 123456789.123456; printf("Scientific Notation (lowercase): %.2e\n", num); printf("Scientific Notation (uppercase): %.2E\n", num); return 0; }

Quiz 📝

Quick Quiz
Question 1 of 1

What is used to print a single character in C?

Keep learning and happy coding! ✅