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 functions scanf()
and printf()
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: 12 3 The addition of two number is:15
In the above program, the scanf()
function takes input from the user, and the printf()
function displays the output result on the screen.
Managing Input/Output
I/O operations are helpful for a program to interact with users. C stdlib
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 or stdin
is used for taking input from devices such as the keyboard as a data stream. Standard output or stdout
is used to give output to a device such as a monitor. For I/O functionality, programmers must include the stdio
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 to scanf()
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 to getchar()
, 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 function putc()
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 using scanf()
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 sd
Here 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()
and puts()
.
gets: The char *gets(char *str)
reads a line from stdin
, keeps the string pointed to by the str
, and is terminated when the new line is read, or EOF
is reached. The declaration of gets()
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 to stdout
, but it does not include null
characters. A new line character needs to be appended to the output. The declaration is:
Syntax:
int puts(const char *str)
where the str
is the string to be written in C.