C Programming + Tutorials in C for Beginners

C Input/Output Statements

Home  »  C Programming  »  C Input/Output Statements


        In C Language input and output function are available as C compiler function or C library provided with each C compiler implementation. These all functions are collectively known as Standard I/O Library function. Here I/O stands for Input and Output used for different inputting and outputting statements. These I/O functions are categorized into three prcessing functions. Console input/output function (deals with keyborad and monitor), disk input/output function (deals with floppy or hard disk) and port input/output function (deals with serial or parallet port). As all the input/output statements deals with the console, so these are also Console Input/Output functions. Console Input/Output function access the three major files before the execution of a C Program. These are as:

stdin: This file is used to receive the input (usually is keyborad file, but can also take input from the disk file).

stdout: This file is used to send or direct the output (usually is a monitor file, but can also send the output to a disk file or any other device).

stderr: This file is used to display or store error messages.

Contents

Input Ouput Statement

    Input and Output statement are used to read and write the data in C programming. These are embedded in stdio.h (standard Input/Output header file). There are mainly two of Input/Output functions are used for this purpose. These are discussed as:

Unformatted I/O functions

   There are mainly six unformatted I/O functions discussed as follows:

a) getchar()
b) putchar()
c) gets()
d) puts()
e) getch()
f) getche()

a) getchar()

    This function is an Input function. It is used for reading a single character from the keyborad. It is buffered function. Buffered functions get the input from the keyboard and store it in the memory buffer temporally until you press the Enter key.

The general syntax is as:
v = getchar();

where v is the variable of character type. For example:
char n;
n = getchar();

A simple C-program to read a single character from the keyboard is as:

/*To read a single character from the keyboard using the getchar() function*/
#include
main()
{
char n;
n = getchar();
}


b) putchar()

    This function is an output function. It is used to display a single character on the screen. The general syntax is as:

putchar(v);

where v is the variable of character type. For example:
char n;
putchar(n);

A simple program is written as below, which will read a single character using getchar() function and display inputted data using putchar() function:

/*Program illustrate the use of getchar() and putchar() functions*/
#include
main()
{
char n;
n = getchar();
putchar(n);
}


c) gets()

    This function is an input function. It is used to read a string from the keyboar. It is also buffered function. It will read a string, when you type the string from the keyboard and press the Enter key from the keyboard. It will mark nulll character ('\0') in the memory at the end of the string, when you press the enter key. The general syntax is as:

gets(v);

where v is the variable of character type. For example:
char n[20];
gets(n);

A simple C program to illustrate the use of gets() function:

/*Program to explain the use of gets() function*/
#include
main()
{
char n[20];
gets(n);
}


d) puts()

    This is an output function. It is used to display a string inputted by gets() function. It is also used to display an text (message) on the screen for program simplicity. This function appends a newline ("\n") character to the output.


The general syntax is as:
puts(v);
or
puts("text line");

where v is the variable of character type.
A simple C program to illustrate the use of puts() function:

/*Program to illustrate the concept of puts() with gets() functions*/
#include
main()
{
char name[20];
puts("Enter the Name");
gets(name);
puts("Name is :");
puts(name);
}

OUTPUT IS:
Enter the Name
Laura
Name is:
Laura


e) getch()

    This is also an input function. This is used to read a single character from the keyboard like getchar() function. But getchar() function is a buffered is function, getchar() function is a non-buffered function. The character data read by this function is directly assign to a variable rather it goes to the memory buffer, the character data directly assign to a variable without the need to press the Enter key.
Another use of this function is to maintain the output on the screen till you have not press the Enter Key. The general syntax is as:

v = getch();

where v is the variable of character type.
A simple C program to illustrate the use of getch() function:

/*Program to explain the use of getch() function*/
#include
main()
{
char n;
puts("Enter the Char");
n = getch();
puts("Char is :");
putchar(n);
getch();
}

OUTPUT IS:
Enter the Char
Char is L
f) getche()

    All are same as getch(0 function execpt it is an echoed function. It means when you type the character data from the keyboard it will visible on the screen. The general syntax is as:

v = getche();

where v is the variable of character type.
A simple C program to illustrate the use of getch() function:

/*Program to explain the use of getch() function*/
#include
main()
{
char n;
puts("Enter the Char");
n = getche();
puts("Char is :");
putchar(n);
getche();
}

OUTPUT IS:
Enter the Char L
Char is L



Formatted I/O functions

    Formatted I/O functions which refers to an Input or Ouput data that has been arranged in a particular format. There are mainly two formatted I/O functions discussed as follows:

a) scanf()
b) printf()

a) scanf()

    The scanf() function is an input function. It used to read the mixed type of data from keyboard. You can read integer, float and character data by using its control codes or format codes. The general syntax is as:

scanf("control strings",arg1,arg2,..............argn);
or
scanf("control strings",&v1,&v2,&v3,................&vn);
Where arg1,arg2,..........argn are the arguments for reading and v1,v2,v3,........vn all are the variables.
The scanf() format code (spedifier) is as shown in the below table:

Format CodeMeaning
%cTo read a single character
%dTo read a signed decimal integer (short)
%ldTo read a signed long decimal integer
%eTo read a float value exponential
%fTo read a float (short0 or a single precision value
%lfTo read a double precision float value
%gTo read double float value
%hTo read short integer
%iTo read an integer (decimal, octal, hexadecimal)
%oTo read an octal integer only
%xTo read a hexadecimal integer only
%uTo read unsigned decimal integer (used in pointer)
%sTo read a string
%[..]To read a string of words from the defined range
%[^]To read string of words which are not from the defined range


/*Program to illustrate the use of formatted code by using the formatted scanf() function */ #include
main()
{
char n,name[20];
int abc;
float xyz;
printf("Enter the single character, name, integer data and real value");
scanf("\n%c%s%d%f", &n,name,&abc,&xyz);
getch();
}

b) printf()

    This ia an output function. It is used to display a text message and to display the mixed type (int, float, char) of data on screen. The general syntax is as:

printf("control strings",&v1,&v2,&v3,................&vn);
or
printf("Message line or text line");
Where v1,v2,v3,........vn all are the variables.
The control strings use some printf() format codes or format specifiers or conversion characters.


These all are discussed in the below table as:

Format CodeMeaning
%cTo read a single character
%sTo read a string
%dTo read a signed decimal integer (short)
%ldTo read a signed long decimal integer
%fTo read a float (short0 or a single precision value
%lfTo read a double precision float value
%eTo read a float value exponential
%gTo read double float value
%oTo read an octal integer only
%xTo read a hexadecimal integer only
%uTo read unsigned decimal integer (used in pointer)


/*Below the program which show the use of printf() function*/ #include
main()
{
int a;
float b;
char c;
printf("Enter the mixed type of data");
scanf("%d",%f,%c",&a,&b,&c);
getch();
}




<< Previous Topic
Next Topic >>



Previous
Next Post »

29 comments

Click here for comments
Unknown
admin
16 October 2017 at 22:32 ×


very good information provided you can read the best c Language Interview Questions

Reply
avatar
Unknown
admin
16 May 2018 at 20:10 ×

Informative 👌

Reply
avatar
Unknown
admin
24 August 2018 at 00:54 ×


In the last few months we've seen a lot of Health Care Reform rules and regulations being introduced by the Health and Human Services Department. Every time that happens, the media gets hold of it and all kinds of articles are written in the Wall Street Journal, the New York Times, and the TV network news programs talk about it. All the analysts start talking about the pros and cons, and what it means to businesses and individuals. Health is God


Reply
avatar
Unknown
admin
27 October 2018 at 23:14 ×

Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website. Such a nice blog you are providing ! Kindly Visit Us big data training in Velachery | Hadoop Training in Chennai | big data Hadoop training and certification in Chennai

Reply
avatar
Anonymous
admin
1 November 2018 at 01:38 ×

Cool txs for sharing.

Reply
avatar
thrtyjtyk
admin
3 January 2019 at 00:43 ×

Healrun is a health news blog we provide the latest news about health, Drugs and latest Diseases and conditions. We update our users with health tips and health products reviews. If you want to know any information about health or health product (Side Effects & Benefits) Feel Free To ask HealRun Support Team.

Reply
avatar
Keto Primal
admin
5 January 2019 at 13:04 ×

so. Although I see a great use of Duromine, especially for those who want to lose around 24 pounds during the allowed 12-week regimen, it is dangerous. On the other hand, it has been clinically proven
https://www.supplementsforfitness.com/

Reply
avatar
VGSARE
admin
5 January 2019 at 22:56 ×

Pilpedia is supplying 100 percent original and accurate information at each moment of time around our site and merchandise, and the intent is to improve the usage of good and pure health supplement. For More Info please visit Pilpedia online store.

Reply
avatar
29 March 2019 at 05:39 ×


Although improve Overall Fitness With Andro Testo Pro is one of the most lost parts of Andro Testo Pro is Best Choice For Healthy Muscle. Let's pretend that there was somewhere you could go to in order to learn everything as that respects Improve Muscle Power With Andro Testo Pro. I admit, that's pretty darn hot. I needed to pull strings a lot to get that completed. I'm not advocating that anybody stop using Improve Muscle Power With Andro Testo Pro.

http://la-grange-aux-creations.over-blog.com/2019/02/andro-testo-pro-does-it-really-work.html

Reply
avatar
2 May 2019 at 04:15 ×


Suplementarios >>> La autenticidad de nuestro sitio web es claramente visible a través de los blogs de moda de salud con el nombre mencionado en él. Tenemos una tendencia a ofrecer nuestro mejor apoyo a los huéspedes que buscan detalles de los suplementos y las comparaciones entre ellos. Para más información amable >>> http://suplementarios.es/

http://suplementarios.es/erozon-max/

http://suplementarios.es/reduslim/

https://www.facebook.com/Suplementarios-2251727545101681/

Reply
avatar
5 May 2019 at 04:51 ×

nice information given by you sir i am serching on this topic from 2 hourse but finnaly i get anser bro thanx for providing knowlegeble content

i have also webiste related to the c and c++ programming languges and hacking


c programming and hacking

Reply
avatar
Unknown
admin
26 July 2019 at 13:23 ×

Tanx..... t hz helped mch...

Reply
avatar
suman
admin
4 August 2019 at 05:39 ×

nice article for beginners.thank you.
java tutorial

Reply
avatar
nutrifitweb
admin
3 October 2019 at 21:50 ×

Male Enhancement is very suggested by me. Vialift Xl Whereas these kinds of Male Enhancement might be ideal for some, they're not advisable for others. I reckon Male Enhancement is kind of cool. Granted, that was where some novice Male Enhancement fans build their solely mistake. That got rather heated. This discusses every nuance. I've not applied any reasonably structure to my Male Enhancement goals.

https://www.nutrifitweb.com/vialift-xl/

https://www.nutrifitweb.com/

Reply
avatar
Kc Funs
admin
20 January 2020 at 00:09 ×

Awesome tutorials...

Reply
avatar
merry jose
admin
24 May 2020 at 04:47 ×

Hellio It is a great job, I love your posts and wish you all the very best. And I hope you continue doing this job well.
https://www.smore.com/2wgm0-gomovies-2020

Reply
avatar
Unknown
admin
3 June 2020 at 07:13 ×

You explained the best

Reply
avatar
shaffu
admin
11 August 2020 at 09:50 ×

hire me i will teach you C language C Language Teach

Reply
avatar
shaffu
admin
11 August 2020 at 09:52 ×

hire me i will teach you C language C Language Teach

Reply
avatar
Use My Notes
admin
8 May 2021 at 23:00 ×

If anyone interested to learn Input Output Statement in C in deep to kindly visit it.

Reply
avatar
Candyce Herr
admin
2 August 2021 at 04:34 ×

In the event you need to see several Keto Advanced Fat Burner here they are. Otherwise, Keto Advanced Fat Burner has a couple of other features. I'm getting into one stop shopping for Keto Advanced Fat Burner. Why is Keto Advanced Fat Burner crucial to me? I, reputably, can grasp Keto Advanced Fat Burner. This is how I became a Keto Advanced Fat Burner expert. There's some doubt that there is a Keto Advanced Fat Burner like it. It is the essence of Keto Advanced Fat Burner.


Keto Advanced Fat Burner

Reply
avatar
Thanks for your comment