6. input/output operation take places as a stream
of character
#include<stdio.h> includes the contents of
standard input-output file, stdio.h at compile
time (contains definitions of stdin, stdout,
stderr)
6
7. C provides standard functions, scanf() and
printf() for performing formatted input and
output
Input data may contain a mixed mode data:
7
DATA TYPES EXAMPLE
float floating point 101.7, 0.5, 2.36
double floating point 2.365, 75.666, 0.12222
int decimal number 225, 3, 75, 9662
char character a, f, apple, 15DET09F1002
8. printf() and puts()
printf() function
used for formatted output to standard output
device that is screen
general form:
printf(“format_specifier”,variable_list1,variable_list2,…)
e.g:
printf(“%d%d%dn”,456,456,456)
Output: 456456456
8
9. 9
Codes Meaning
b Back space
f Form feed
n New line
r Carriage return
t Horizontal tab
” Double quote mark
’ Single quote mark
0 Null
Back slash
v Vertical tab
a Alert
o Octal constant
x Hexadecimal constants
10. puts()
to display a string contained in a string
variable
general form:
e.g:
char str[20]=“I am going to England”;
puts(str);
if str has the string “I am going to Englad”, puts(str) would
display the entire string “I am going to England” and
moves the cursor to the beginning of the next line
10
int puts (char*)
11. scanf() and gets()
scanf() function
used for formatted input from standard input
device that is keyboard
general form:
scanf(“format specifier”,variable_list1,variable_list2,…)
e.g:
printf(“Please enter a number”);
scanf(“%d”,&number1)
Output: Please enter a number 15
11
12. gets()
to accept string up to a new line character into
a string variable
general form:
12
char*gets (char *);
13. To illustrate gets() and puts()
13
#include<stdio.h>
#include<conio.h>
void main()
{
char words[40];
clrscr();
printf(“Enter a line of text: “);
gets (words);
printf(“nThe line of text entered is “);
puts (words);
}
Enter a line of text: I love programming
The line of text entered is I love programming
14. 14
printf() STATEMENT OUTPUT
printf(“Line 1”);
printf(“Line 2”);
Line1Line2Line3
printf(“Line 3”);
The newline character (n) must be include whenever
the cursor have to move to the next line
printf() STATEMENT OUTPUT
printf(“Line 1n”);
Line1
printf(“Line 2n”);
Line2
printf(“Line 3n”);
Line3
printf() STATEMENT OUTPUT
printf(“Line 1”);
Line1
printf(“nLine 2n”);
Line2
printf(“Line 3”);
Line3
15. Specifies the field in which the data has to
be entered
15
code meaning
%c Single character
%d Decimal integer
%e Floating point value
%f Floating point value
%o Octal value
%x Hexadecimal value
%s A string
%u Unsigned integer
16. int d;
to accept a value into a variable n of int type
scanf(“%d”,&n); Which is %d is the format specifier for int
16
Variable n
char c;
to accept a value into a variable c of char type
scanf(“%c”, &c); Note that the format specified used is %c
Variable c
17. float f;
to accept a floating point value into the variable f
scanf(“%f”, &f); Note that the format specified used is %f
17
Variable f
To read values into three variable i, f, and c of
type int, float and char using a single statement
scanf(“%d%f%c”, &i,
&f, &c);
Note that the order of the format
specifiers matches with that of the
variable
18. int d;
To display the value of the variable n of int type
printf(“%d”,n); Which is %d is the format specifier for int
18
Variable n
char c;
To display the character stored in the variable c
printf(“%c”,c); Note that the format specified used is %c
Variable c
19. float f;
to display the floating point value stored in the
variable f
printf(“%f”,f); Note that the format specified used is %f
19
Variable f
To display the value stored in three variable i, f,
and c of type int, float and char using a single
statement
printf(“%d%f%c”,i,f,
c);
Note that the order of the format
specifiers matches with that of the
variable
20. To illustrate simple printf() and scanf()
Enter first number : 15
Enter second number :65
Total for first and second number :80
20
#include<stdio.h>
void main()
{
int number_1, number_2, total;
printf(“Enter first number: “);
scanf(“%d”, &number_1);
printf(“Enter second number: “);
scanf(“%d”, &number_2);
total= number_1 + number_2;
printf(“nTotal for first and second number : %d”, total);
}