C Input and Output Statements

    0 Votes


In the previous chapters of this C tutorial, you've learned the basic building blocks and concepts in C programming language. In this chapter, let's learn about how to perform input/output operations in C program. Input/output statements defines how we can read input from different input devices or write into different output devices. By input operation, we mean user providing values for the program through an external device (like keyboard) and by output operation, we mean writing results of computation to an external device, like monitor, printer etc. An input/output statement is nothing but standard function provided by C and each of them have a list of arguments enclosed in parentheses.

Standard Input/ Standard Output/ Standard Error

When you run a C program, operating system opens 3 files and provides it to the program; they are standard input, standard output and standard error.

  1. Standard input file is used to take inputs to the program provided by user. Standard input device is your keyboard.
  2. Standard output file is used to write the output that your program creates. Standard output device is your monitor.
  3. Standard error file is used to write the errors encounter during program execution. Your monitor serves as the standard error file.

You might think that why keyboard, monitor etc. are referred as file here. This is because the operating system treats these devices as files and manipulate them using standard file operations. For example, when you want to write something onto a monitor, you write them to the file referring to the monitor. Operating system will handle this through the abstraction layer provided by your hardware and write them onto the screen.

Character Input/Output

As mentioned in the earlier chapters, character data type can holds only one character. To read a single character data type from the input device, C has getchar() function. The function can be used as given below

char c = getchar();

Here c is a character variable. This function will wait for one input character, read it when it's available from standard input device (like a keyboard) and assign it to the variable (here 'c').

We can write a single character to the standard output device (like a monitor) using another standard function provided by C called putchar(). This function can be used as given below

putchar(c);

Here 'c' is a character variable and its content will be written to the output device.

Example of Character Input/Output

#include<stdio.h>
void main()
{
   char c;
   printf("Enter a character: ");
   c = getchar();

   printf("You have entered:");
   putchar(c);
}

Output:
Enter a character: b
You have entered: b

The above program will wait for you to enter a character at line 6. Once you've entered it, program reads it from the keyboard and writes it back into the monitor.

Also Read - Top C Interview Questions and Answers for Freshers & Advanced C Interview Questions and Answers

String Input/Output

The string is nothing but an array of characters. Similar as character I/O, C have standard library functions for input/output operations of strings. They are gets(char *s) and puts(const char *s).

To read a string from standard input, use gets()as give below.

	char string[20];
	gets(string);
 

gets reads string into the passed character array until a newline or EOF is encountered in the standard input. For instance, it reads all the characters and save it in the passed character array until you press "enter" key.

To write a string to the standard output, use puts()as give below.

	char string[] = "This is a string.";
	puts(string);
 

puts writes a string to standard output until a string terminator is encountered. (When initializing a string as given above, string terminator would be appended automatically. More details about string can be found in chapter about strings.)

Example of String Input/Output

#include<stdio.h>
void main()
{
    char  s[100];
	printf("Enter a string: ");
	gets(s);
    puts(s);
}

Output:
Enter a string: This is a string
This is a string

The above program reads a line as input and prints the line into the screen.

Also Read - Top Embedded C Interview Questions and Answers & C++ Interview Questions and Answers

General Input/Output Statement

Above mentioned functions are used only for specific data types, ie; character and string. They cannot be used with other data type. What if you need to read a float or double value as input? C has functions for general input/output operations to handle this situation. These functions are printf and scanfand they have following syntax.

printf( "format", value_1, value_2, ...value_n);
scanf("format", address_1, address_2, ...address_n);

Output Using printf

printf is used to perform generic output operation. The first parameter, 'format' is a string that specifies how the output needs to be formatted. Other parameters are optional for printf. The 'format' string can contain format specifiers (conversion characters) to specify how the other parameters needs to be handled. Each data type has an associated format specifier. For example, to print an integer value, you can use '%d' as format specifier, '%f' can be used for float etc. With the format specifiers, the printf statement would look like

int value = 10;
printf( "%d", value);

If the first argument doesn't have any conversion characters, it will be written as it is to the standard output.

Input Using scanf

To read a data of specific type, scanf can be used. Same as printf, conversion characters can be used with 'format' string of scanf to specify the format of input data. For example, we can read an integer variable as given below.

int value;
scanf("%d", &value);

Note that, for scanf we need to pass the address of variable to which you want to read the data.

More about format specifiers is in another chapter of this tutorial.

Example of printf and scanf

#include <stdio.h>

int main(void) {
	int a;
	float b;
	// No conversion characters in the first argument, 
	// so it will be printed as it is
	printf("Enter an int number: ");
	scanf("%d", &a);
	printf("Enter a float number: ");
	scanf("%f", &b);
	
	printf("int you've entered is: %d and float you've entered is: %f", a, b);
	
	return 0;
}

Output:

Enter an int number: 10
Enter a float number: 99.5
int you've entered is: 10 and float you've entered is: 99.500000

Related Articles

C Overview and Basic Syntax
Data Types in C Language
C Qualifiers - Constant and Volatile type Qualifier
C Type Casting with examples - float to int / int to char / int to long
C Constants and Literals
C Operators - Arithmetic, Assignment, Relational and Logical
C Operators - Bitwise Operators and Conditional Operators
C Operator Precedence and Associativity Table with Examples
C Format Specifiers for printf and scanf
C Decision Making Control Structures - 'if', 'if - else', nested 'if', 'switch'
Loops in C - for, while and do...while Loops
C 'break', 'continue' and 'goto' Statements to Control Program Flow
C Preprocessor Directives and Macros
C Functions Tutorial with Examples
C Arrays - Dynamic, 2d and Multidimensional with Examples
C Strings - Internal Working and Functions with example
C Pointers basics explained with examples
Structures in C Programming with Example
Unions in c programming with example
C Storage Class Specifiers - auto, register, static and extern

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know