Difference Between printf() and scanf() in C (Simple Guide)
When learning C programming, two functions you will use again and again are printf() and scanf().
At first, they may look confusing, but once you understand them, everything becomes much easier.
What is printf()?
printf() is used to display output on the screen.
It simply prints whatever you want to show.
Example:
#include <stdio.h>
int main() {
printf("Hello World");
return 0;
}👉 Output:
Hello WorldWhat is scanf()?
scanf() is used to take input from the user.
It allows the user to enter data while the program is running.
Example:
#include <stdio.h>
int main() {
int num;
scanf("%d", &num);
return 0;
}👉 Here, the user can enter a number.
Key Difference Between printf() and scanf()
| Feature | printf() | scanf() |
|---|---|---|
| Purpose | Output | Input |
| Work | Displays data | Takes data |
| Symbol | No & | Uses & |
Simple Program Using Both
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d", num);
return 0;
}Final Thoughts
Both printf() and scanf() are very important in C programming.
Once you understand how to use them, you can easily create interactive programs.
Keep practicing, and things will become clear step by step 🙂
Comments
Post a Comment