π₯Input and Output in C Programming
π§π» Input and Output in C Programming
π¨️ printf(), ⌨️ scanf(), π° main() Explained in Full Detail
✨ Introduction
After learning Variables and Data Types, the next most important step in C programming is learning how a program communicates with the user. This communication is done using Input and Output (I/O) functions provided by C.
When I started learning C, understanding input and output felt confusing at first. This blog explains it in the simplest way I wish I had learned it.
In this professional beginner‑friendly guide, you will learn:
π The first line of a C program
π The main() function
π¨️ Output using printf()
⌨️ Input using scanf()
π§Ύ Format specifiers (with table)
π§ͺ Complete working programs
π§© Introduction to basic functions
1️⃣ π First Line of Every C Program — #include <stdio.h>
#include <stdio.h>
π What does this line mean?
#include➜ Instructs the compiler to include additional features<stdio.h>➜ Standard Input Output header file
❓ Why is it required?
Functions like
printf()andscanf()are defined insidestdio.hWithout this line, input and output operations will not work
π‘ Analogy: Just like importing tools before starting work.
2️⃣ π main() Function — Starting Point of a C Program
#include <stdio.h> // Header file for input and output functions
int main() { // Main function: entry point of the program
// Your logic starts here
return 0; // Program ends successfully
}
π§ Explanation:
Every C program starts execution from
main()int➜ Return type of the functionreturn 0;➜ Indicates successful program execution
✅ Important Points:
A program cannot run without
main()Statements written outside
main()will not execute
3️⃣ π¨️ Output in C — printf() Function
π What is printf()?
printf() is used to display output on the screen.
π§Ύ Syntax:
printf("text or format", variables);
π§ͺ Example 1: Simple Output
printf("Hello World"); // Displays Hello World message on the output screen
π€ Output:
Hello World
π§ͺ Example 2: Printing Variable Value
int age = 20; // Declares an integer variable 'age' and assigns value 20
printf("My age is %d", age); // Prints the value of age using %d format specifier
π€ Output:
My age is 20
4️⃣ π§Ύ Format Specifiers in C (With Examples & Output)
printf() and scanf() which type of data is being used and how it will be displayed on the screen.| Data Type | Format Specifier | Description |
|---|---|---|
| int | %d | Integer value |
| float | %f | Decimal value |
| char | %c | Single character |
| string | %s | Group of characters |
π§ Explanation of the Table
Data Type ➜ Type of variable used in the program
Format Specifier ➜ Symbol used inside printf() / scanf()
Example Code ➜ How the format specifier is used in a real program
Output ➜ What appears on the screen after execution
Data Type ➜ Type of variable used in the program
Format Specifier ➜ Symbol used inside printf() / scanf()
Example Code ➜ How the format specifier is used in a real program
Output ➜ What appears on the screen after execution
| Data Type | Format Specifier | Example Code | Output |
|---|---|---|---|
| int | %d | int a = 10; |
10 |
| float | %f | float b = 3.14; |
3.140000 |
| char | %c | char c = 'A'; |
A |
| string | %s | char name[] = "Krishna"; |
Krishna |
π Important Note:
Float values print 6 digits after the decimal point by default when using
%f
5️⃣ ⌨️ Input in C — scanf() Function
π What is scanf()?
scanf() is used to take input from the user via keyboard.
π§Ύ Syntax:
scanf("format", &variable); // Reads input according to the format and saves it at the variable's address
❓ Why use & (Ampersand)?
It provides the address of the variable
Required so
scanf()can store the input value
π§ͺ Example: Taking Integer Input
int number; // Declares an integer variable named 'number'
scanf("%d", &number); // Takes integer input from the user and stores it in 'number'
printf("Number = %d", number); // Displays the value stored in 'number'
π§ͺ Taking Multiple Inputs
int a, b; // Declares two integer variables 'a' and 'b'
scanf("%d %d", &a, &b); // Takes two integer inputs and stores them in 'a' and 'b'
⚠️ Common Mistakes to Avoid
❌ Forgetting the
&symbol❌ Using incorrect format specifier
❌ Missing
#include <stdio.h>
6️⃣ π§ͺ Complete Input–Output Program Example
#include <stdio.h> // Header file for input and output functions
int main() { // Main function: program execution starts here
int a, b, sum; // Declares three integer variables: a, b, and sum
printf("Enter two numbers: "); // Prompts the user to enter two numbers
scanf("%d %d", &a, &b); // Takes two integer inputs and stores them in a and b
sum = a + b; // Adds a and b and stores the result in sum
printf("Sum = %d", sum); // Displays the sum of the two numbers
return 0; // Ends the program successfully
}
π Explanation:
⌨️ Takes two numbers as input
➕ Adds the numbers
π¨️ Displays the result
- The image below gives a quick visual overview of input and output concepts covered in this blog.
Figure: Overview of input and output in C programming using printf(), scanf(), and main().
- The image below gives a quick visual overview of input and output concepts covered in this blog.
7️⃣ π Practice Questions (With Answers)
✅ Question 1: Print Your Name
Program:
#include <stdio.h> // Header file for input and output functions
int main() { // Main function: program execution starts here
printf("My name is Krishna Popat"); // Prints the given text on the output screen
return 0; // Ends the program successfully
}
π§ Line-by-Line Explanation:
#include <stdio.h>➜ Includes standard input-output functions likeprintf()int main()➜ Execution of the program starts here{➜ Beginning of the main function blockprintf("My name is Krishna Popat");➜ Prints the given text on the screenreturn 0;➜ Ends the program successfully}➜ End of the main function
π€ Output:
My name is Krishna Popat
✅ Question 2: Take an Integer and Print It
Program:
#include <stdio.h> // Header file for input and output functions
int main() { // Main function: program execution starts here
int num; // Declares an integer variable 'num'
printf("Enter a number: "); // Asks the user to enter a number
scanf("%d", &num); // Reads an integer input and stores it in 'num'
printf("You entered: %d", num); // Displays the entered number
return 0; // Ends the program successfully
}
π§ Line-by-Line Explanation:
#include <stdio.h>➜ Required forprintf()andscanf()int main()➜ Program execution starts hereint num;➜ Declares an integer variable to store inputprintf("Enter a number: ");➜ Asks the user for inputscanf("%d", &num);➜ Takes integer input and stores it innumprintf("You entered: %d", num);➜ Displays the entered numberreturn 0;➜ Ends the program successfully
π₯ Sample Input:
5
π€ Output:
Enter a number: 5
You entered: 5
✅ Question 3: Add Two Numbers Using Input
Program:
#include <stdio.h> // Header file for input and output functions
int main() { // Main function: program execution starts here
int a, b, sum; // Declares integer variables a, b, and sum
printf("Enter two numbers: "); // Prompts the user to enter two numbers
scanf("%d %d", &a, &b); // Takes two integer inputs and stores them in a and b
sum = a + b; // Adds the two numbers and stores the result in sum
printf("Sum = %d", sum); // Displays the sum of the two numbers
return 0; // Ends the program successfully
}
π§ Line-by-Line Explanation:
#include <stdio.h>➜ Enables input and output functionsint main()➜ Entry point of the programint a, b, sum;➜ Declares variables to store numbers and their sumprintf("Enter two numbers: ");➜ Prompts user to enter two numbersscanf("%d %d", &a, &b);➜ Takes two integer inputssum = a + b;➜ Adds the two numbersprintf("Sum = %d", sum);➜ Displays the resultreturn 0;➜ Terminates the program
π₯ Sample Input:
3 7
π€ Output:
Enter two numbers: 3 7
Sum = 10
✅ Conclusion
In this blog, you learned:
π️ Structure of a C program
π¨️ Output using
printf()⌨️ Input using
scanf()π§Ύ Format specifiers
π§© Basics of functions
✨ Written by Krishna Popat
π± Founder, Learning Growth Hub
Comments
Post a Comment