This post explains you about Input output in c programming language.
Note: In this article we are not discussing anything about file io and its functions.
Input Output in c
Input refers to inputting data to the computer. It can be achieved through various input devices. For a typical c program we can input through standard input device i.e. keyboard or we can use command line arguments or even we can input from a file.
Output refers to displaying data from computer. It can be achieved through various output devices. For a typical c program we can output through standard output device i.e. display monitor.
How to Perform Input Output in c?
C programming provides standard header file stdio.h
to How to perform Input output which contains all the required functions to perform IO.
File Descriptors Associated in UNIX Based Systems
For performing io operations in unix based systems c opens a file descriptors viz. stdin,stdout,stderr which are defined in stdio
header file.
File Descriptor | Device | Type |
stdin | Keyboard | Input |
stdout | Display console | Output |
stderr | Display console | Error |
Functions for Input Output in c
Type | Function Name | Prototype |
Input | scanf | int scanf(const char *format, params...) |
gets | char *gets(char *c) | |
getchar | int getchar(void) | |
Output | printf | int printf(const char *format, params...) |
puts | int puts(const char *c) | |
putchar | int putchar(int c) |
Input Output in c with functions
scanf in c
scanf in c is used to perform input operation. It returns an int of number of characters it read. Inorder to read a value we have to pass the symbol associated with the datatype ( to know more on datatypes and the symbols associated read this article) and the address of variable.
Prototype of scanf
#include<stdio.h>
int scanf(const char *format, params...)
Example program for scanf
#include<stdio.h>
int main(int argc, char const *argv[])
{
int var;
scanf("%d",&var);
return 0;
}
gets in c
gets in c is used to read a string ( array of character ). It takes a pointer to array of character. It stops reading until EOF ( End Of File ) is encountered.
Prototype of gets
#include<stdio.h>
char *gets(char *c)
Example program for gets
#include<stdio.h>
int main(int argc, char const *argv[])
{
char s[100];
gets(s);
gets( s );
puts(s);
return 0;
}
getchar in c
getchar in c is used to read a character. It stores the read input at an int.
Prototype of getchar
#include<stdio.h>
int getchar(void)
Example program for getchar
#include<stdio.h>
int main(int argc, char const *argv[])
{
int x=getchar();
putchar(x);
return 0;
}
printf in c
printf in c is used to output formatted data to the console.
It returns an int of number of characters outputted to console. Inorder to output a variable we have to pass the symbol associated with the datatype ( to know more on datatypes and the symbols associated read this article) and the of variable as parameter.
Prototype of printf
#include<stdio.h>
int printf(const char *format, params...)
Example program for printf
#include<stdio.h>
int main(int argc, char const *argv[])
{
printf("Hello world");
return 0;
}
puts in c
puts in c is used to output a string to the console.
Prototype of puts
#include<stdio.h>
int puts(const char *c)
Example program for puts
#include<stdio.h>
int main(int argc, char const *argv[])
{
char s[100];
gets(s);
gets( s );
puts(s);
return 0;
}
putchar in c
putchar in c is used to output a single character to console.
Prototype of puts
#include<stdio.h>
int putchar (int c)
Example program for putchar
#include<stdio.h>
int main(int argc, char const *argv[])
{
int x=getchar();
putchar(x);
return 0;
}
Learning is a continuous process. Read other c programming tutorials.
People also ask
What is input output statement in C?
Input output staments are used to perform reading and writing operations. C programming has stdio.h
header file which contains all the required input output statements.
What is formatted input output in C?
Formatted inpput output refers to conversion of data in readable format. C programming allows to perform formatted input-output operations using various functions found in stdio.h header file.
What is the output of C statement?
C programming language provides statements to write to console or we can write to a file, these statements are called as output statements in c programming.
What is the input statement?
C programming language provides statements to read from console or we can read from a file, these statements are called as input statements in c programming.
What is basic input statement?
Simple way of reading data from user is the basic input statement. We ususally use scanf for this purpose in c programming.