Chp_3 - Console Input Output
Chp_3 - Console Input Output
CHAPTER 3.
INPUT/OUTPUT
CENGİZ RİVA
Input and Output
• 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
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
• 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 );
• 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("\");
}
#include <stdio.h>
int main ()
{
printf("\\");
}
#include <stdio.h>
int main ()
{
printf("text1\ttext2");
}
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);
}