C Inputs and Outputs in C Programming

Whenever a program is written, it will mainly have certain input values from the users for which the program will do some operations/calculations and its result will be displayed to the user. There should be some devices to enter the value to the program (system or keyboard or files) and there should be some devices to accept the result (system, screen or files). But C treats all these devices as files and calls it as standard files. It terms input devices / input files as standard input file or stdin file and output files as standard output file or stdout file. Like any other programs, C programs can also have errors. When compiler encounters error, it displays them on the screen. This error files are termed as standard error files or stderr file in C.

The user inputs can be anything – may be integer values, float / double values, character/string values. The code should be strong enough to accept any of these kinds of inputs from the user and should able to differentiate its type. These entered input values are accepted by using the functions getchar, scanf or gets functions. Similarly, the result of operations inside the program can be anything from numbers to string. This can be displayed on the screen using putchar and printf functions. Both input and output functions are defined in the header file stdio.h. Hence we need to add this preprocessor directive before starting the main function in any C program.

getchar ()

This function is used to read one character from the standard input (stdin). That means, this function accepts only one character (may be alphabets, numbers or any symbols) from the user. Even though it accepts multiple characters from the user, it can store only first entered character in it. Actually it waits for the user to press ‘Enter’ after they enter single character. If user does not press ‘Enter’, it will keep on waiting for them to press ‘Enter’. Hence it allows users to enter as many characters as they enter but it stores only initial character. Below snippet shows the how getchar accepts inputs for different set of entries.

#include  

int main () 
{
	char i;
	printf ("Please enter one character :"); 
	i = getchar (); // Accepts only one character from the user

	printf ("Entered character is: %c", i);
	
	return 0;
}

 

scanf ()

Even though getchar function accepts the inputs from the user, it accepts only one character. We will not be able enter any multiple digit numbers, fractional number or any string values. Either we need to enter these values by writing getchar functions as many times as the length of the digits or string. But this is not feasible. Hence we have scanf function, which accepts any number of characters – alphabets, numbers and symbols. But here we need to specify the datatype of the input values to differentiate them from strings, digits and fraction numbers. Usually ‘%d’ is used for integer values, ‘%f’ is used for float values, ‘%ld’ is used for double, ‘%c’ for characters. More details about the same can be found at topic ‘Datatypes’.

#include  

int main()
{
	int intId;
	char chrName[15];
	char chrAddr [20];

	printf ("Please Enter Student Id:");
	scanf ("%d", &intId); // Reads integer value from stdin/user

	printf("Please Enter Student Name:");
	scanf("%s", chrName); // Reads string value from stdin/user

	printf ("Please Enter Student Address:");
	scanf("%s", chrAddr);

	//Displays the result
	printf("\n\n Entered Student Details are:\n");
	printf("---------------------------------\n");
	printf("%d", intId);
	printf("\t %s", chrName);
	printf("\t %s", chrAddr);
	return 0;
}

gets()

This is the function used only to input the string values. It accepts all the alphanumeric characters from the user. This accepts line of string values and ends when newline character (‘Enter’ key) is entered or end of file is reached. We can even enter numbers, but it considers it as its ASCII value of alphanumeric characters.

#include  

int main()
{
	int intId;
	char chrName[15];
	char chrAddr[20];

	printf("Please Enter Student Id:");
	scanf("%d", &intId); // Reads integer value from stdin/user

	printf("Please Enter Student Name:");
	gets(chrName); // Reads string value from stdin/user

	printf("Please Enter Student Address:");
	gets(chrAddr);

	//Displays the result
	printf("\n\nEntered Student Details are:\n");
	printf("---------------------------------\n");
	printf("%d", intId);
	printf("\t %s", chrName);
	printf("\t address:%s", chrAddr);
	return 0;
}

putchar ()

This function is used to write one character into the standard output – stdout. That means, it displays only one character at a time on the screen for the user. The output character can be any alphanumeric character.

#include  

int main()
{
	char chrName = 'C';

	printf("Value of chrName is:");
	putchar(chrName);
	return 0;
}

printf ()

This function will display any alphanumeric value of any length of a variable. Like we do in scanf, here also we need to specify the format for the datatype of the variable so that compiler can understand what type of value is being displayed. It can also be used to display any message to the user.

#include 

int main ()
{
    printf ("This is an example for printf function!");
   
    return 0;
}

puts ()

This is meant for displaying a line of string value. It can display any alphanumeric values.

#include  

int main()
{
	char chrName[15] = "C Program!";

	printf("Value of chrName is:");
	puts(chrName); // Displays the value of chrName
	return 0;
}


Next > < Prev
Scroll to Top