C Programming Input Output (I/O): printf() and scanf()
This tutorial focuses on two in-build functions printf() and scanf() to
perform I/O task in C programming. Also, you will learn how you write
a valid program in C.
C programming has several in-build 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;
}
Output
C Programming
How this program works?
All valid C program must contain the main() function.
The code execution begins from the start of main() function.
The printf() is a library function to send formatted output to the
screen.
The printf() function is declared in "stdio.h" header file.
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 encounters printf() function and doesn't
find stdio.h header file, compiler shows error.
The return 0; statement is the "Exit status" of the program. In simple
terms, program ends.
2.2 A Simple C Program:Printing a Line of Text
1 /* Fig. 2.1: fig02_01.c
2 A first program in C */
3 #include <stdio.h>
4
5 int main()
6 {
7 printf( "Welcome to C!\n" );
8
9 return 0;
10 }
Welcome to C!
• Comments
– Text surrounded by /* and */ is ignored by computer
– Used to describe program
• #include <stdio.h>
– Preprocessor directive
• Tells computer to load contents of a certain file
5 – <stdio.h> allows standard input/output operations
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;
}
Output
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 #3: 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;
}
Output
Enter a number: 23.45 Value = 23.450000
The format string "%f" is used to read and display formatted in case of
floats.
Example #4: C Character I/O
#include <stdio.h>
int main()
{
char var1;
printf("Enter a character: ");
scanf("%c",&var1);
printf("You entered %c.",var1);
return 0;
}
Output
Enter a character: g You entered g.
Format string %c is used in case of character types.
1 /* Fig. 2.5: fig02_05.c
2 Addition program */ Outline
3 #include <stdio.h>
4 1. Initialize variables
5 int main()
6 { 2. Input
7 int integer1, integer2, sum; /*
declaration
8 */ 2.1 Sum
9 printf( "Enter first integer\n" ); /*
prompt
10 */
scanf( "%d", &integer1 ); /* read 3. Print
an integer
11 */ "Enter second integer\n" );
printf( /*
prompt
12 */
scanf( "%d", &integer2 ); /* read
an integer
13 sum = */
integer1 + integer2; /*
assignment
14 of "Sum
printf( sum */
is %d\n", sum ); /*
print sum */
15
16 return 0; /* indicate that program ended
successfully
17 } */
Enter first integer Program Output
45
Enter second integer
72
Sum is 117
10
2000 Prentice Hall, Inc. All rights reserved.
Another Simple C Program: Adding Two Integers
• = (assignment operator)
– Assigns a value to a variable
– Is a binary operator (has two operands)
sum = variable1 + variable2;
sum gets variable1 + variable2;
– Variable receiving value on left
• printf( "Sum is %d\n", sum );
– Similar to scanf
• %d means decimal integer will be printed
• sum specifies what integer will be printed
– Calculations can be performed inside printf
statements
printf( "Sum is %d\n", integer1 +
integer2 );
11
Little bit on ASCII code
When a character is entered in the above program, the character itself is not
stored. Instead a numeric value(ASCII value) is stored. 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() { int var1 = 69;
printf("Character having ASCII value 69 is %c.",var1);
return 0; }
Output
Character having ASCII value 69 is E.
Example #5: C ASCII Code
#include <stdio.h> int main() { char var1; printf("Enter a character: ");
scanf("%c",&var1); // When %c text format is used, character is
displayed in case of character types printf("You entered %c.\n",var1);
// When %d text format is used, integer is displayed in case of
character types printf("ASCII value of %c is %d.", var1, var1); return 0; }
Output
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
Example #7: 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;
}
Output
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