As we all know, the three essential functions of a computer are reading, processing, and writing data. Most C programs take data as input, and then after processing, the processed data is displayed, which is called information. This tutorial will teach you various predefined C functions to read and print data.
To read and print data, you can use the predefined functionsscanf() andprintf() in C programs.
Example:
#include <stdio.h>void main(){ int a, b, c; printf("Please enter any two numbers: \n"); scanf("%d %d", &a, &b); c = a + b; printf("The addition of two number is: %d", c);}Output:
Please enter any two numbers:123The addition of two number is:15
In the above program, thescanf() function takes input from the user, and theprintf() function displays the output result on the screen.
Managing Input/Output
I/O operations are helpful for a program to interact with users. Cstdlib is the standard C library for input-output operations. Two essential streams play their role when dealing with input-output operations in C. These are:
- Standard Input (stdin)
- Standard Output (stdout)
Standard input orstdin is used for taking input from devices such as the keyboard as a data stream. Standard output orstdout is used to give output to a device such as a monitor. For I/O functionality, programmers must include thestdio header file within the program.
Reading Character In C
The easiest and simplest of all I/O operations are taking a character as input by reading that character from standard input (keyboard).getchar() function can be used to read a single character. This function is an alternative toscanf() function.
Syntax:
var_name = getchar();Example:
#include<stdio.h>void main(){ char title; title = getchar();}There is another function to do that task for files:getc() which is used to accept a character from standard input.
Syntax:
int getc(FILE *stream);Writing Character In C
Similar togetchar(), there is another function that is used to write characters, but one at a time.
Syntax:
putchar(var_name);Example:
#include<stdio.h>void main(){ char result = 'P'; putchar(result); putchar('\n');}Similarly, there is another functionputc() which is used for sending a single character to the standard output.
Syntax:
int putc(int c, FILE *stream);Formatted Input
It refers to input data that has been arranged in a specific format. This is possible in C usingscanf() function. We have already encountered this and are familiar with this function.
Syntax:
scanf("control string", arg1, arg2, ..., argn);The field specification for reading integer input numbers is:
%w sdHere the% sign denotes the conversion specification;w signifies the integer number that defines the field width of the number to be read.d defines the number to be read in integer format.
Example:
#include<stdio.h>void main(){ int var1= 60; int var2= 1234; scanf("%2d %5d", &var1, &var2);}Input data items should have to be separated by spaces, tabs, or a new line, and the punctuation marks are not counted as separators.
Reading and Writing Strings in C
There are two popular library functions for dealing with strings in C:gets() andputs().
gets: Thechar *gets(char *str) reads a line fromstdin, keeps the string pointed to by thestr, and is terminated when the new line is read, orEOF is reached. The declaration ofgets() function is:
Syntax:
char *gets(char *str);Where str is a pointer to an array of characters where C strings are stored.
puts: The function -int puts(const char *str) is used to write a string tostdout, but it does not includenull characters. A new line character needs to be appended to the output. The declaration is:
Syntax:
int puts(const char *str)where thestr is the string to be written in C.