Basic Input and Output in C (original) (raw)

Last Updated : 13 May, 2025

In C programming, input and output operations refer to reading data from external sources and writing data to external destinations outside the program. C provides a standard set of functions to handle input from the user and output to the screen or to files. These functions are part of the standard input/output library ****<stdio.h>**.

In C, there are many functions used for input and output in different situations, but the most commonly used functions for Input/Output are scanf() and printf(), respectively.

Basic Output in C

In C, there are many functions used for output in different situations, but the most commonly used function for output is printf().

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,

**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;

}

`

Output

The 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; }

`

Basic Input in C

These functions provide ways to read data from the user and use it in the programs . Among the most commonly used input functions are scanf() for reading formatted data and getchar() for reading a single character.

**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 `

fgets(str, n, stdin);

`

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:
**Shubham Kumar _(Entered by User)
Hello, Shubham Kumar