![]() |
C Input Output Function in C Programming Language |
C Input-Output Function in C Programming Language
C input-output function the
formatted input-output functions read and write, respectively, all types of
data values. They require format string to produce formatted results. They can
be used for reading and writing all data values.
The formatted functions return
values after execution.
The return value is equal to the number of variables
successfully read/written.
Using this value the
user can find the error that occurred during the reading or writing of data.
Example of formatted Function:
Printf()
Scanf()
What do u understand by formatted input output identifier in C programming?
Unformatted input-output Function in C
The unformatted input/output
functions work only with the character data type.
They do not require format
conversion symbols for the formatting of data types because they work only with character data types. There is no need to convert data.
In case values of other data types are passed to these functions, they are treated as character data.
Example: putch(),getch(), getche(),getchar(), gets(),
puts(),putchar(),putch()
Formatted Printf() Function
C input-output function Printf(): This function is used to print any text as well as the value of the variables on the standard output device/Console (monitor screen), printf is a fundamental library function in the c language that is declared in stdio.h header file. The printf() function prints all types of data values to the console. It requires a format conversion symbol or format string and variable names to print the data. The format string symbol.
syntax: printf(“message text”); printf(“message text+ format-string”,variable-list); |
Note: First printf() style prints the simple text on the monitor, while the second printf() prints the message with values of the variable list.
#include <stdio.h> int main() { printf("C Language"); printf("Codeswithbaby"); printf("C Programming Language"); return 0; } *****OUTPUT***** C language Codeswithbaby C Programming Language |
Printf() Function To Print Values of The Variables
·
To print values of the variables,
you need to understand that format strings are the special characters followed
by the % sign, which are used to print values of the variable s from the variable list.
·
Format specifiers
·
Here is the list of some of the format
specifiers, use them in printf() & scanf() to format & print values of
the variables:
- Character (char) %c
- Integer (int) %d
- Insigned integer (unsigned int) %ld
- Long (long) %ld
- Unsigned long (unsigned long) %lu
- Float (float) %f
- Octal Value (octal value) %o
- Hexadecimal Value (hex value) %x
- String (char[]) %s
- Use ‘u’ for unsigned type modifier, and ‘l’ for long.
- Double (double) %lf
#include <stdio.h> int main() { int num=100; float val=1.23f; char gender='F'; printf("Output1:"); printf("%d",num); printf("%f",val); printf("%c", gender); printf("\nOutput2:"); // \n: for new line in c printf("%d,%f,%c",num,val,gender); return 0; } *****OUTPUT***** Output1:1001.230000F Output2:100,1.230000,F |
Return Type And the value of Printf() Function
printf returns an integer value, which is the total number
of printed characters.
For example: if you are printing "Hello" using
printf, printf will return 5.
Write a Program Total Character Printed in C Language
#include <stdio.h> int main() { int result; result = printf("Hello\n"); printf("Total Printed Characters are: %d\n",result); return 0; } *****OUTPUT***** Hello The total Printed Characters are: 6 |
Formatted Scanf() Function
C Input-Output Function Scanf(): This Formatted function is used to get (input) value from the keyboard. We pass format specifiers, in which format we want to take input.
Syntax: scanf(“format-specifier”, &var-name); scanf(“fromat-specifier-list”, &var-name-list); |
The first type of scanf() takes the single value for the
variable and the second type of scanf() will take the multiple values for the
variable list.
Write a Program Using scanf() Function in C Language
#include<stdio.h> #include<conio.h> int main() { int a; float b; char c; printf("Enter an integer number:"); scanf("%d",&a); printf("Enter a float number:"); scanf("%f",&b); printf("Enter a character:"); fflush(stdin); scanf("%c",&c); printf("\na=%d,b=%f,c=%c " ,a,b,c); getch(); } *****OUTPUT***** Enter an integer number: 1234 Enter a float number: 1.4345 Enter a character: G a=1234,b=1.234500,c=G |
Note: Here, G will not store into the c variable, because we are not
flushing the input buffer here. So either you will have to take the input of c first or
you will have to read the value of c separately.
Return Type And Value of Scanf() Function
scanf is a library function of stdio.h, it is used to take
input from the standard input device (keyboard). scanf returns an integer
value, which is the total number of inputs.
For example: if you are reading two values from scanf, it will return 2.
#include <stdio.h> int main() { int a,b; int result; printf("Enter two numbers: "); result=scanf("%d%d",&a,&b); printf("Total inputs are: %d\n",result); return 0; } *****OUTPUT***** Enter two numbers: 10 20 Total inputs are: 2 |
Unformatted Function Character I/O Function
Character Function: The getchar() function reads character-type data from the input. The getchar() function reads one character at a time till the user presses the enter key.
#include <stdio.h> //header file section #include <conio.h> int main() { char c; printf("Enter a character : "); c = getchar(); printf("\nEntered character : %c ", c); return 0; } *****OUTPUT***** Enter a character: X Entered character: y |
Note:
getchar() reads the input from the user and displays it back to
the user.
Getch Function: The getch() function reads the alphanumeric character input from the user. But, the entered character will not be displayed.
#include <stdio.h> #include <conio.h> int main() { printf("\nHello, press any alphanumeric character to exit "); getch(); return 0; } *****OUTPUT***** Hello, press any alphanumeric character to exit |
Note: The above program will run until you press one of many
alphanumeric characters. The key pressed by you will not be displayed.
Getche() Function: getche() function reads the alphanumeric character from the user input. Here, the character you entered will be echoed to the user until he/she presses any key.
#include <stdio.h> //header file section #include <conio.h> int main() { printf("\nHello, press any alphanumeric character or symbol to exit \n "); getche(); return 0; } *****OUTPUT***** Hello, press any alphanumeric character or symbol to exit R |
Note: The above program will run until you press one of many
alphanumeric characters. The key pressed by you will be echoed.
Putchar() Function: putchar() function prints only one character at a time.
#include <stdio.h> //header file section #include <conio.h> int main() { char c = 'R'; putchar(c); return 0; } *****OUTPUT***** |
Note: variable c is assigned to a character 'R'. The variable c is displayed by the putchar(). Use a single quotation mark ' ' for a character.
UnFormatted Function String I/O Function
Gets() Function: The gets() function can read a full string even blank spaces presents in a string.But, the scanf() function leaves a string after a blank space is detected. The gets() function is used to get any string from the user.
#include <stdio.h> #include <conio.h> int main() { char c[25]; printf("Enter a string : "); gets(c); printf("\n%s is awesome ",c); return 0; } *****OUTPUT***** Enter a string: C language C language is awesome |
Puts() Function: The puts() function prints the charater array or string on the console. The puts() function is similar to printf() function, but we cannot print other than characters using puts() function.
#include <stdio.h> //header file section #include <conio.h> int main() { char c[25]; printf("Enter your Name : "); gets(c); puts(c); return 0; } *****OUTPUT***** Enter your Name: Codeswithbaby Codeswithbaby |
Commonly Used Library Function
Clrscr() Function: Clrscr() is an inbuilt library function that is used to clear the previous output displayed on a screen. clrscr() is defined in #include <conio.h> header file.
#include <stdio.h> #include <conio.h> int main() { printf("Before clrscr"); clrscr(); printf("clrscr() will clear the screen"); return 0; } *****OUTPUT***** clrscr() will clear the screen |
Exit() Function: exit() is an inbuilt library function that is used to terimate the program irrespective of the statements followed by it. exit() is defined in #include <stdlib.h> header file.
#include <stdio.h> //header file section #include <conio.h> int main() { printf("This statement is before exit(); function "); exit(0); printf("It will not display "); return 0; } This statement is before exit(); function |
Sleep() Function: sleep() is an inbuilt library function that is used to delay the program's output. sleep() is defined in #include <unistd.h> header file.
#include <stdio.h> #include <conio.h> int main() { printf("Countdown... "); printf("\n 3"); sleep(1); printf("\n 2"); sleep(1); printf("\n 1"); sleep(1); printf("\n Celebration Time "); return 0; } *****OUTPUT***** Countdown... 3 2 1 Celebration Time |
Difference Between printf And Puts Function
Both functions are
declared in the studio. h and used to send text/character stream to output stream
printf can print the
value of the mixed type of variables but puts can’t print, puts has a single
parameter that is a character array (character pointer).
printf prints whatever you provide, it may be text, text + values, etc without adding a new line after the text while adding one more character that is a new line character (\n) just after the text/character stream.
#include <stdio.h> #include <conio.h> int main() { printf("Using printf...\n"); printf("C language is good."); printf("C Language with this Website is awesome."); printf("\n\n"); printf("Using puts...\n"); puts("C Language is good."); puts("C Language with this Website is awesome."); printf("End of main...\n"); return 0; } *****OUTPUT***** Using printf... C Language is good. C Language with this Website is awesome. Using puts... C Language is good. C Language with this Website is excellent. End of main... |
Types of Token C Programming Language
C Token: C tokens are the basic building blocks in the C
language that are constructed together to write a C program.
Each and every smallest
individual unit in a C program is known as a C token.
C Token Are of 6 Types in C Programming Language
1. Keywords (eg: int,
while),
2. Identifiers (eg:
main, total),
3. Constants (eg: 10,
20),
4. Strings (eg: “Total”,
“hello”),
5. Special symbols (eg:
(), {}),
6. Operators (eg: +, /,-,*)
Write a Program C Token Program in C Language
int main() { int x, y, total; x = 10, y = 20; total = x + y; printf ("Total = %d \n", total); } |
• main – identifier
• {,}, (,) – delimiter
• int – keyword
• x, y, total –
identifier
• main, {, }, (, ), int,
x, y, total – tokens
Find The Number of Tokens in Statement Printf in C Language
The number of tokens in the given statement is: 1. printf 2. ( 3. "i = %d, &i = %x" 4. , 5. i 6. , 7. & 8. i 9. ) 10. ; Total 10 Tokens |
Read Also Post C Programming Language
Introduction to C Programming Language
Data Types In C Programming Language With Example
Data Conversion Type Casting Implicit and Explicit in C Language