Welcome to the world of C Programming! Today, we'll delve into one of the most essential aspects of this language: 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!
%cUsed to print a single character.
#include <stdio.h>
int main() {
char c = 'A';
printf("Character: %c\n", c);
return 0;
}%d or %iUsed to print decimal integers. They can print both positive and negative numbers.
#include <stdio.h>
int main() {
int num = 123;
printf("Integer: %d\n", num);
return 0;
}%fUsed to print floating-point numbers (float or double).
#include <stdio.h>
#include <math.h>
int main() {
double pi = M_PI;
printf("Pi: %.2f\n", pi);
return 0;
}%sUsed to print strings.
#include <stdio.h>
int main() {
char str[20] = "Hello, World!";
printf("String: %s\n", str);
return 0;
}%oUsed to print octal numbers.
#include <stdio.h>
int main() {
int num = 10;
printf("Octal: %o\n", num);
return 0;
}%x or %XUsed to print hexadecimal numbers in lowercase and uppercase, respectively.
#include <stdio.h>
int main() {
int num = 10;
printf("Hexadecimal (lowercase): %x\n", num);
printf("Hexadecimal (uppercase): %X\n", num);
return 0;
}%uUsed to print unsigned integers.
#include <stdio.h>
int main() {
unsigned int num = 4294967295;
printf("Unsigned Integer: %u\n", num);
return 0;
}%e and %EUsed to print scientific notation of floating-point numbers with e or E suffix.
#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;
}What is used to print a single character in C?
Keep learning and happy coding! ✅