⌨️ Input and Output in C Programming
Learn printf() and scanf() With Examples
How Programs Communicate With Users 🚀
🚀 Introduction
A computer program is not useful if it only performs fixed tasks. Programs need to communicate with users.
This communication happens through:
📥 Input
Data provided by the user to the program.
📤 Output
Information displayed by the program.
🖥️ printf() Function in C
The printf() function is used to display output on the screen.
printf("Hello World");
Output:
Hello World
🔤 Format Specifiers in C
| Specifier | Data Type | Example |
|---|---|---|
| %d | Integer | 25 |
| %f | Float | 10.5 |
| %c | Character | A |
| %s | String | Hello |
⌨️ scanf() Function in C
The scanf() function is used to take input from the user.
scanf("%d",&number);
The & symbol gives the memory address of the variable.
💻 Example: Add Two Numbers
#include <stdio.h>
int main()
{
int a,b,sum;
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
sum = a + b;
printf("Sum = %d",sum);
return 0;
}
🔄 Program Execution Flow
User Input
⬇️
scanf()
⬇️
Processing
⬇️
printf()
⬇️
Output Display
⬇️
scanf()
⬇️
Processing
⬇️
printf()
⬇️
Output Display
🖥️ Output Example
Enter first number: 10
Enter second number: 20
Sum = 30
Enter second number: 20
Sum = 30
⚠️ Common Mistakes Beginners Make
- Forgetting & symbol in scanf()
- Using wrong format specifier
- Missing semicolon (;)
- Incorrect variable type
🚀 Practice Programs
- Take user's name and display it
- Calculate area of rectangle
- Convert Celsius to Fahrenheit
- Create a simple calculator
- Calculate student marks percentage
🎯 Conclusion
Input and Output are essential parts of every programming language. With printf() and scanf(), your programs can interact with users.
No comments:
Post a Comment