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:where v is the variable of character type. For example:
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:
where v is the variable of character type. For example:
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:
where v is the variable of character type. For example:
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:
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:
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:
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:
or
scanf("control strings",&v1,&v2,&v3,................&vn);
The scanf() format code (spedifier) is as shown in the below table:
Format Code | Meaning |
%c | To read a single character |
%d | To read a signed decimal integer (short) |
%ld | To read a signed long decimal integer |
%e | To read a float value exponential |
%f | To read a float (short0 or a single precision value |
%lf | To read a double precision float value |
%g | To read double float value |
%h | To read short integer |
%i | To read an integer (decimal, octal, hexadecimal) |
%o | To read an octal integer only |
%x | To read a hexadecimal integer only |
%u | To read unsigned decimal integer (used in pointer) |
%s | To 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();
}
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:
or
printf("Message line or text line");
The control strings use some printf() format codes or format specifiers or conversion characters.
These all are discussed in the below table as:
Format Code | Meaning |
%c | To read a single character |
%s | To read a string |
%d | To read a signed decimal integer (short) |
%ld | To read a signed long decimal integer |
%f | To read a float (short0 or a single precision value |
%lf | To read a double precision float value |
%e | To read a float value exponential |
%g | To read double float value |
%o | To read an octal integer only |
%x | To read a hexadecimal integer only |
%u | To 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();
}
30 comments
Click here for commentsLinux Trainng in Chennai
ReplyRed Hat Trainng in Chennai
Replyvery good information provided you can read the best c Language Interview Questions
Good Posting...
ReplyReal Estate Consultant in Chennai
Real Estate Brokers in Chennai
Real Estate Agents in Chennai
Thanks
ReplyInformative 👌
Reply
ReplyIn 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
Cool txs for sharing.
ReplyHealrun 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.
Replyso. 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
Replyhttps://www.supplementsforfitness.com/
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.
ReplyIt's too useful
Reply
ReplyAlthough 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
ReplySuplementarios >>> 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/
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
Replyi have also webiste related to the c and c++ programming languges and hacking
c programming and hacking
Tanx..... t hz helped mch...
Replynice article for beginners.thank you.
Replyjava tutorial
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.
Replyhttps://www.nutrifitweb.com/vialift-xl/
https://www.nutrifitweb.com/
Thanks
ReplyAwesome tutorials...
ReplyHellio 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.
Replyhttps://www.smore.com/2wgm0-gomovies-2020
You explained the best
Replyhire me i will teach you C language C Language Teach
Replyhire me i will teach you C language C Language Teach
ReplyDownload
ReplyIf anyone interested to learn Input Output Statement in C in deep to kindly visit it.
ReplyIn 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.
ReplyKeto Advanced Fat Burner
SAP BO Training In Noida
ReplyNice article keep posting.
ReplyTesting tools institute in hyderabad
>Ivory County Gold Sector 115 Noida The project offers 4, 5 BHK luxury apartments with world-class aminities.
ReplyDLF SCO Plots offer an terrific opportunity for commercial growth in Garden City Central, Sector 84, Gurgaon. Designed for Shop-Cum-Office areas, these plots feature prime vicinity benefits, cutting-edge infrastructure, and top notch connectivity. Perfect for groups and traders, they ensure high footfall and visibility, making them a clever investment desire in Gurgaon's thriving enterprise hub.
ReplyConversionConversion EmoticonEmoticon