PROGRAMMING I
CHAPTER 3.
INPUT/OUTPUT
CENGİZ RİVA
Input and Output
• When we say Input, it means to feed some data into a program.
• An input can be given in the form of a file or from the command
line.
• C programming provides a set of built-in functions to read the
given input and feed it to the program as per requirement.
• When we say Output, it means to display some data on screen,
printer, or in any file.
• C programming provides a set of built-in functions to output the
data on the computer screen as well as to save it in text or
binary files.
The Standard Files
• C programming treats all the devices as files.
• Every file has a pointer.
• So devices such as the display are addressed in the same way as
files and the following three files are automatically opened
when a program executes to provide access to the keyboard and
screen.
getchar Function
• The int getchar(void) function reads the next available
character from the screen and returns it as an integer.
• It is defined inside the <stdio.h> header file.
• This function reads only single character at a time. You can use
this method in the loop in case you want to read more than one
character from the screen.
• Generally, getchar() will also display the character you type
to the screen automatically.
• Syntax is:
variable = getchar( );
• variable here must be of type char:
The getchar and putchar()
Functions
Example:
#include <stdio.h>
int main( )
{
char ch;
printf( "Enter a character :");
ch = getchar( );
printf( "You entered: %c",ch);
}
• When the above code is compiled and executed, it waits for you to
input some text.
• When you enter a text and press enter, then the program proceeds
and reads only a single character and displays it as follows
The getchar Function
putchar() Functions
• The int putchar(int var) or putchar(char
var) function puts the passed character on the screen and
returns the same character.
• It is defined inside the <stdio.h> header file.
• This function puts only single character at a time.
• You can use this method in the loop in case you want to display
more than one character on the screen.
• Syntax is:
putchar( variable );
putchar() Function
Example: #include <stdio.h>
int main( )
{
char ch;
printf( "Enter a character :");
ch = getchar( );
printf( "You entered: ");
putchar(ch);
}
Output:
Formatted Output/Input
• In addition to the simple console I/O functions, the standard C
library contains 2 functions that perform formatted output and
input: printf() and scanf().
• Formatted I/O refers to the fact that these functions may format
the information under your direction.
• Remember, the simple routines, getchar() and putchar(), will
only input and display data in raw form.
• We have already used printf() function when displaying
different variables.
• The complement of this function is scanf() which allows
reading of various data types from the keyboard.
Formatted Output
• The output function which translates internal values to
characters is printf.
• The description here covers most typical uses.
• printf converts, formats, and prints its arguments on the
standard output under control of the format.
• It returns the number of characters printed.
int printf(“text %format1 %format2 text”, arg1, arg2, ...);
• Each conversion specification begins with a % followed by
format type special character.
• Format type characters are given in the next table.
Formatted Output
Formatted Output
• printf examples:
#include <stdio.h>
#include <float.h>
int main()
{
int x, num;
char ch;
float fl;
x=5;
num=175000;
ch= 'a';
fl=1.26878;
printf("integer var x = %i\n",x );
printf("integer var num = %d\n",num );
printf("char var ch = %c\n",ch );
printf("float var f = %f\n",fl );
printf("string is= %s\n", " Hello World ! ");
}
Formatted Output
• When we execute
Formatted Output
• Floating number is displayed with 0 at the end. Actually printf
function allows us to control number of digits to display
• For example %.5f will display max 5 characters after decimal
point.
printf("float var f = %.5f\n",fl );
• If we decrease decimal poi nt digit to 2, it will aproximate the
number as:
printf("float var f = %.2f\n",fl );
Formatted Output
• Let’s change the line of printing the character as:
printf("char var ch = %i\n",ch );
• When we execute:
• This time it prints the value of 97 which is the ASCII code of the
character 'a'.
Character Constants
• There are certain characters in C when they are preceded by a
backslash they will have special meaning and they are used to
represent like newline (\n) or tab (\t).
• Here, the list of some of such escape sequence codes:
Character Constants
• Let’s use some of them.
• First try to print '\' character
#include <stdio.h>
int main ()
{
printf("\");
}
• Compiler gives error
Character Constants
• We have tu use escape characte '\' character
#include <stdio.h>
int main ()
{
printf("\\");
}
• This time it is successfully printed
Character Constants
• Let’s use some of them.
• First try to print '\t' character
#include <stdio.h>
int main ()
{
printf("text1\ttext2");
}
• Output will be:
Formatted Input - Scanf
• The function scanf() is the input analog of printf, providing
many of the same conversion facilities in the opposite
direction.
int scanf("%format", &var)
• scanf() reads characters from the standard input, interprets
them according to the specification in format, and stores the
results through the remaining arguments.
• The format argument is describes the type of data like in printf.
• Second arguement must be a pointer indicating where the
corresponding converted input should be stored.
• % and & signs must be used in front of these two arguements.
Formatted Input - Scanf
• scanf stops when it exhausts its format string, or when some
input fails to match the control specification.
• It returns as its value the number of successfully matched and
assigned input items.
• This can be used to decide how many items were found.
• On the end of file, EOF is returned;
• Note that this is different from 0, which means that the next
input character does not match the first specification in the
format string.
• The next call to scanf resumes searching immediately after the
last character already converted.
Basic Scanf Conversions
The scanf() and printf() Functions
• For now let us proceed with a simple example :
#include <stdio.h>
int main( )
{
char ch;
int x;
float fl;
printf( "Enter a character :");
scanf("%c", &ch);
printf( "You entered the character: %c\n ", ch);
printf( "Enter an integer:");
scanf("%i", &x);
printf( "You entered the integer: %i\n ", x);
printf( "Enter a float :");
scanf("%f", &fl);
printf( "You entered the integer: %f\n ", fl);
}
The scanf() and printf() Functions
• Output:
LAB Exercises
1. Read the following values from keyboard to your own variables
and print them in a C program:
25
87.358
\
t
string
2. Write a C program to print the following output.
" is a special character in C like "\"
LAB Exercises
3. Write a C program to print the given matrix in the matrix form
using variables.
a11=1, a12=x, a13=0.25
a21=y, a22=19246850, a23=0
a31=2934527571 a32=8, a33=c
1 x 0.25
y 19246850 0
2934527571 8 c
The scanf() and printf() Functions
• For now let us proceed with a simple example :
#include <stdio.h>
int main( )
{
printf( "\" is a special character in C like \"\\\" ");
}
LAB Exercises
#include <stdio.h>
#include <float.h>
int main()
{
int a11,a22,a23,a32;
char a12,a21,a33;
long int a31;
float a13;
a11=1;
a12='x';
a13=0.25;
a21='y';
a22=19246850;
a23=0;
a31=2934527571;
a32=8; a33='c';
printf("%i\t\t%c\t\t%.2f\n",a11,a12,a13);
printf("%c\t\t%i\t%i\n",a21,a22,a23);
printf("%li\t%i\t\t%c\n",a31,a32,a33);
}