Input & Output Functions in C

 

💻 Input & Output Functions in C (printf() and scanf())

In C (developed by Dennis Ritchie), input and output functions are used to:

  • Take data from user 📥
  • Display data on screen 📤

🔹 1. printf() Function (Output) 📤

👉 printf() is used to display output on the screen


✅ Syntax

printf("format specifier", variables);

✅ Example 1

#include <stdio.h>

int main() {
printf("Hello, World!");
return 0;
}

👉 Output:

Hello, World!

✅ Example 2 (with variables)

int a = 10;
printf("Value of a = %d", a);

👉 Output:

Value of a = 10

🔹 Multiple Values Example

int a = 10;
float b = 5.5;
printf("a = %d, b = %f", a, b);

👉 Output:

a = 10, b = 5.500000

🔹 2. scanf() Function (Input) 📥

👉 scanf() is used to take input from user


✅ Syntax

scanf("format specifier", &variable);

👉 & = address of variable


✅ Example 1

#include <stdio.h>

int main() {
int a;
scanf("%d", &a);
printf("You entered: %d", a);
return 0;
}

👉 Input:

5

👉 Output:

You entered: 5

🔹 Example with Multiple Inputs

int a;
float b;

scanf("%d %f", &a, &b);
printf("a = %d, b = %f", a, b);

🔹 Important Format Specifiers 📊

Data TypeSpecifier
int%d
float%f
char%c
double%lf
string%s

🔹 Important Rules ⚠️

✔ For printf()

  • No & needed
  • Used to display output

✔ For scanf()

  • Must use & (except for strings)
  • Used to take input

❗ Special Case (String)

char name[20];
scanf("%s", name); // no &

🔹 Complete Example Program 🧠

#include <stdio.h>

int main() {
int age;
float salary;
char grade;

printf("Enter age, salary, grade: ");
scanf("%d %f %c", &age, &salary, &grade);

printf("Age: %d\n", age);
printf("Salary: %f\n", salary);
printf("Grade: %c\n", grade);

return 0;
}

🔹 Real-Life Understanding 🤔

👉 scanf() = asking question ❓
👉 printf() = giving answer 📢


🔹 Final Summary 📝

  • printf() → output 📤
  • scanf() → input 📥
  • Format specifiers define data type
  • & used in scanf() (except string)

Comments

Popular posts from this blog

Introduction to Computer

History of Computer

Computer Generation