Format Specifiers in C

 

💻 Format Specifiers in C (With Easy Examples)

In C (developed by Dennis Ritchie), format specifiers are used in functions like printf() and scanf() to tell the compiler:
👉 what type of data you are printing or reading


🔹 What is a Format Specifier? 🤔

A format specifier starts with % and defines the data type of a variable

👉 Example:

printf("%d", a);

Here %d means → print an integer


🔹 Common Format Specifiers 📊

Data TypeSpecifierMeaning
int%dInteger
float%fDecimal number
double%lfDouble precision
char%cSingle character
string%sText/string
unsigned int%uPositive integer
long int%ldLong integer
long long int%lldVery large integer

🔹 Examples of Each Specifier

🔸 1. Integer (%d)

#include <stdio.h>

int main() {
int a = 10;
printf("%d", a);
return 0;
}

👉 Output: 10


🔸 2. Float (%f)

float b = 5.5;
printf("%f", b);

👉 Output: 5.500000


🔸 3. Double (%lf)

double pi = 3.14159;
printf("%lf", pi);

👉 Output: 3.141590


🔸 4. Character (%c)

char ch = 'A';
printf("%c", ch);

👉 Output: A


🔸 5. String (%s)

char name[] = "Raj";
printf("%s", name);

👉 Output: Raj


🔸 6. Unsigned Integer (%u)

unsigned int x = 20;
printf("%u", x);

👉 Output: 20


🔸 7. Long Integer (%ld)

long int y = 100000;
printf("%ld", y);

👉 Output: 100000


🔸 8. Long Long Integer (%lld)

long long int z = 1234567890;
printf("%lld", z);

👉 Output: 1234567890


🔹 Multiple Values Example 🧠

#include <stdio.h>

int main() {
int a = 10;
float b = 5.5;
char c = 'A';

printf("Value: %d %f %c", a, b, c);
return 0;
}

👉 Output:

Value: 10 5.500000 A

🔹 Format Specifiers in scanf() 📥

int a;
scanf("%d", &a);

👉 &a means address of variable


🔹 Important Tips ⚠️

%d → int
%f → float
%lf → double
%c → char
%s → string

❗ Wrong specifier = wrong output or error


🔹 Final Concept 🔥

👉 Format specifier = type batata hai
👉 printf = output
👉 scanf = input


📝 Summary

  • %d → integer
  • %f → float
  • %lf → double
  • %c → character
  • %s → string

Comments

Popular posts from this blog

Introduction to Computer

History of Computer

Computer Generation