C Input / Output
C programming has several in-built library functions to perform input and output tasks.
Two commonly used functions for I/O (Input/Output) are printf() and scanf().
The scanf()
function reads formatted input from standard input (keyboard) whereas the printf()
function sends formatted output to the standard output (screen).
Example #1: C Output
#include <stdio.h> //This is needed to run printf() function.
int main()
{
printf("C Programming"); //displays the content inside quotation
return 0;
}
C Programming
How this program works?
- All valid C program must contain the
main()
function. The code execution begins from the start ofmain()
function. - The
printf()
is a library function to send formatted output to the screen. Theprintf()
function is declared in"stdio.h"
header file. - Here,
stdio.h
is a header file (standard input output header file) and#include
is a preprocessor directive to paste the code from the header file when necessary. When the compiler encountersprintf()
function and doesn't findstdio.h
header file, compiler shows error. - The
return 0;
statement is the "Exit status" of the program. In simple terms, program ends.
Example #2: C Integer Output
#include <stdio.h>
int main()
{
int testInteger = 5;
printf("Number = %d", testInteger);
return 0;
}
Output
Number = 5
Inside the quotation of printf()
function, there is a format string "%d"
(for integer). If the format string matches the argument (testInteger in this case), it is displayed on the screen.
Example #3: C Integer Input/Output
#include <stdio.h>
int main()
{
int testInteger;
printf("Enter an integer: ");
scanf("%d",&testInteger);
printf("Number = %d",testInteger);
return 0;
}
Enter an integer: 4 Number = 4
The scanf()
function reads formatted input from the keyboard. When user enters an integer, it is stored in variable testInteger.
Note the '&'
sign before testInteger; &testInteger gets the address of testInteger and the value is stored in that address.
Example #4: C Floats Input/Output
#include <stdio.h>
int main()
{
float f;
printf("Enter a number: ");
// %f format string is used in case of floats
scanf("%f",&f);
printf("Value = %f", f);
return 0;
}
Enter a number: 23.45 Value = 23.450000
Example #5: C Character I/O
#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c",&chr);
printf("You entered %c.",chr);
return 0;
}
Enter a character: g You entered g.
%c
is used in case of character types.And when we displayed that value using "%c"
text format, the entered character is displayed.
Example #6: C ASCII Code
#include <stdio.h>
int main()
{
char chr;
printf("Enter a character: ");
scanf("%c",&chr);
// When %c text format is used, character is displayed in case of character types
printf("You entered %c.\n",chr);
// When %d text format is used, integer is displayed in case of character types
printf("ASCII value of %c is %d.", chr, chr);
return 0;
}
Enter a character: g You entered g. ASCII value of g is 103. The ASCII value of character 'g' is 103. When, 'g' is entered, 103 is stored in variable var1 instead of g.
You can display a character if you know ASCII code of that character. This is shown by following example.
Example #7: C ASCII Code
#include <stdio.h>
int main()
{
int chr = 69;
printf("Character having ASCII value 69 is %c.",chr);
return 0;
}
Character having ASCII value 69 is E.
More on Input/Output of floats and Integers
Integer and floats can be displayed in different formats in C programming.
Example #8: I/O of Floats and Integers
#include <stdio.h>
int main()
{
int integer = 9876;
float decimal = 987.6543;
// Prints the number right justified within 6 columns
printf("4 digit integer right justified to 6 column: %6d\n", integer);
// Tries to print number right justified to 3 digits but the number is not right adjusted because there are only 4 numbers
printf("4 digit integer right justified to 3 column: %3d\n", integer);
// Rounds to two digit places
printf("Floating point number rounded to 2 digits: %.2f\n",decimal);
// Rounds to 0 digit places
printf("Floating point number rounded to 0 digits: %.f\n",987.6543);
// Prints the number in exponential notation(scientific notation)
printf("Floating point number in exponential form: %e\n",987.6543);
return 0;
}
4 digit integer right justified to 6 column: 9876 4 digit integer right justified to 3 column: 9876 Floating point number rounded to 2 digits: 987.65 Floating point number rounded to 0 digits: 988 Floating point number in exponential form: 9.876543e+02