C Input and Output (I/O) (JNNC Technologies)


As we all know the three essential functions of a computer are reading, processing and writing data. Majority of the programs take data as input, and then after processing the processed data is being displayed which is called information. In C programming you can use scanf() and printf() predefined function to read and print data.
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
The above program scanf() is used to take input from user, and respectively printf() is used to display output result on the screen.

Managing Input/Output

I/O operations are useful for program to interact with users. stdlib is the standard C library for input output operations. While dealing with input-output operations in C, there are two important streams that play their role. 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 for giving output to a device such as a monitor. For using I/O functionality, programmers must include stdio header-file within the program.

Reading Character In C

The easiest and simplest of all I/O operations is taking character as input by reading that character from standard input (keyboard). getchar() function can be used to read a single character. This function is alternate 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 which 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 standard output.
Syntax:
int putc(int c, FILE *stream);

Formatted Input

It refers to an input data which has been arranged in a specific format. This is possible in C using scanf(). We have already encountered this and familiar with this function.
Syntax:
scanf("control string", arg1, arg2, ..., argn);
The field specification for reading integer inputted number 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 var1= 1234;
 scanf("%2d %5d", &var1, &var2);
}
Input data items should have to be separated by spaces, tabs or new-line and the punctuation marks are not counted as separators.

Reading and Writing Strings in C

There are two popular library functions gets() and puts() provides to deal with strings in C.
gets: The char *gets(char *str) reads a line from stdin and 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 stdoutbut it does not include null characters. A new line character need to be append to the output. The declaration is:
Syntax:
int puts(const char *str)
where str is the string to be written in C.

0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();