Basic Input and Output in C
Last Updated :
16 Jul, 2025
In C, there are many input and output for different situations, but the most commonly used functions for Input/Output are scanf() and printf() respectively. These functions are part of the standard input/output library <stdio.h>. scanf() takes user inputs (typed using keyboard) and printf() displays output on the console or screen.
Basic Output in C
The printf() function is used to print formatted output to the standard output stdout (which is generally the console screen). It is one of the most commonly used functions in C.
Syntax
C
printf("formatted_string", variables/values);
Where,
- Formatted String: string defining the structure of the output and include format specifiers
- variables/values: arguments passed to
printf()
that will replace the format specifiers in the formatted string.
Examples
The following examples demonstrate the use of printf for output in different cases:
Printing Some Text
C
#include <stdio.h>
int main() {
// Prints some text
printf("First Print");
return 0;
}
Explanation: The text inside "" is called a string in C. It is used to represent textual information. We can directly pass strings to the printf() function to print them in console screen.
Printing Variables
C
#include <stdio.h>
int main() {
int age = 22;
// Prints Age
printf("%d\n", age);
return 0;
}
Here, the value of variable age is printed. You may have noticed %d in the formatted string. It is actually called format specifier which are used as placeholders for the value in the formatted string.
You may have also noticed '\n' character. This character is an escape sequence and is used to enter a newline.
Printing Variables Along with String
C
#include <stdio.h>
int main() {
int age = 22;
// Prints Age
printf("The value of the variable age is %d\n", age);
return 0;
}
OutputThe value of the variable age is 22
The printf function in C allows us to format the output string to console. This type of string is called formatted string as it allows us to format (change the layout the article).
fputs()
The fputs() function is used to output strings to the files but we can also use it to print strings to the console screen.
Syntax:
C
fputs("your text here", stdout);
Where, the stdout represents that the text should be printed to console.
Example
C
#include <stdio.h>
int main() {
fputs("This is my string", stdout);
return 0;
}
scanf() is used to read user input from the console. It takes the format string and the addresses of the variables where the input will be stored.
Syntax
C
scanf("formatted_string", address_of_variables/values);
Remember that this function takes the address of the arguments where the read value is to be stored.
Examples of Reading User Input
The following examples demonstrate how to use the scanf for different user input in C:
Reading an Integer
C
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
// Reads an integer
scanf("%d", &age);
// Prints the age
printf("Age is: %d\n", age);
return 0;
}
Output
Enter your age:
25 (Entered by the user)
Age is: 25
Explanation: %d is used to read an integer; and &age provides the address of the variable where the input will be stored.
Reading a Character
C
#include <stdio.h>
int main() {
int ch;
printf("Enter a character: \n");
// Reads an integer
scanf("%c", &ch);
// Prints the age
printf("Entered character is: %d\n", ch);
return 0;
}
Output
Enter a character:
a (Entered by the user)
Entered character is: a
Reading a string
The scanf() function can also be used to read string input from users. But it can only read single words.
C
#include <stdio.h>
int main() {
char str[100]; // Declare an array to hold the input string
printf("Enter a string: ");
scanf("%s", str); // Reads input until the first space or newline
printf("You entered: %s\n", str);
return 0;
}
Output:
Enter a String:
Geeks (Entered by the user)
Entered string is: Geeks
The scanf() function can not handle spaces and stops at the first blanksspace. to handle this situation we can use fgets() which is a better alternative as it can handle spaces and prevent buffer overflow.
fgets()
fgets() reads the given number of characters of a line from the input and stores it into the specified string. It can read multiple words at a time.
Syntax
C
where buff is the string where the input will be stored and n is the maximum number of characters to read. stdin represents input reading from the keyboard.
Example:
C
#include <stdio.h>
#include <string.h>
int main() {
// String variable
char name[20];
printf("Enter your name: \n");
fgets(name, sizeof(name), stdin);
printf("Hello, %s", name);
return 0;
}
Output
Enter your name:
John (Entered by User)
Hello, John
For reading a single character, we use getchar() in C.