Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 17

National Institute of Electronics & Information Technology

Gorakhpur Center
Ministry of Electronics & Information Technology (MeitY), Government of India

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Contents to be covered
• Managing Input and Output with examples

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Input/output Statement
 printf() and scanf() functions are inbuilt library functions in C programming
language which are available in C library by default. 
 These functions are declared and related macros are defined in “stdio.h”
which is a header file in C language.
 We have to include “stdio.h” file in C program to make use of printf() and
scanf() library functions in C language.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


printf() Function
 printf() function is use to display something on the console or to display the
value of some variable on the console.
The general syntax for printf() function is as follows
printf(<”format specifier”>,<list of variables>);

 We use printf() function with %d format specifier to display the value of an


integer variable.

 Similarly %c is used to display character, %f for float variable, %s for string


variable, %lf for double and %x for hexadecimal variable.

 To generate a newline, we use “\n” in C printf() statement.


http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
scanf() Function
 scanf() function is use to read data from keyboard and to store that data in the
variables.
The general syntax for scanf() function is as follows.
Scanf(“format specifier”,&variable);

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Key Points to remember in printf() & scanf()
 printf() is used to display the output and scanf() is used to read the inputs.
 printf() and scanf() functions are declared in “stdio.h” header file in C library.
 All syntax in C language including printf() and scanf() functions are case
sensitive.

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


First C Program
#include <stdio.h>  
#include <conio.h>  
void main()
{  
printf(“Welcome to NIELIT");  
getch();  
}
Description of the above C Program:
#include <stdio.h> includes the standard input output library functions. The printf() function is
defined in stdio.h .
#include <conio.h> includes the console input output library functions. The getch() function is
defined in conio.h file.
void main() The main() function is the entry point of every program in c language. The void
keyword specifies that it returns no value.
printf() The printf() function is used to print data on the console.
getch() The getch() function asks for a single character. Until you press any key, it blocks the
screen.
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Example 1
Problem: Program to input any to number and print sum of that numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("enter any two no");
scanf("%d%d",&a,&b);
c=a+b;
printf("%d",c);
getch();
}
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Example 2
Problem: Program to input any five digit number and print average of its.
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c,d,e,f ;
printf("enter any five no");
scanf("%f%f%f%f%f",&a,&b,&c,&d,&e);
f=(a+b+c+d+e)/5;
printf("%f",f);
getch();
}
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Example 3
Problem: Program to input the two numbers in two variables and interchange their
value.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter any two number \n");
scanf("%d%d",&a,&b);
c=a+b;
a=c-a;
b=c-b;
printf("Value of A__%d\n",a);
printf("Value of B__%d\n",b);
getch();
} http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Quiz
Time

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

1. What is the type of the following assignment expression if x is of type float and y is of
type int?
y = x + y;
a)Int b) float
c) there is no type for an assignment expression d) double Answer: a

2. What will be the final value of c in the following C statement? (Initial value: c = 2)
c <<= 1;
a) c = 1; b) c = 2;
c) c = 3; d) c = 4; Answer: d

3. Which of the following is an invalid assignment operator?


a) a %= 10; b) a /= 10;
c) a |= 10; d) None of the mentioned Answer: d
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Quiz
Time

4. Which keyword is used to prevent any changes in the variable within a C program?
a) immutable b) mutable
c) const d) volatile Answer: c

5. Which of the following statement is false?


a) A variable defined once can be defined again with different scope
b) A single variable cannot be defined with two different types in the same scope
c) A variable must be declared and defined at the same time
d) A variable refers to a location in memory Answer: c

6. A variable declared in a function can be used in main().


a) True b) False
c) True if it is declared static d) None of the mentioned Answer: b

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Quiz
Time

7. Which of the following operators has an associativity from Right to Left?


a) <= b) <<
c) == d) += Answer: d

8. Which of the following operator has the highest precedence in the following?
a) () b) sizeof
c) * d) + Answer: a

9. Which of the following method is accepted for assignment?


a) 5 = a = b = c = d; b) a = b = c = d = 5;
c) a = b = 5 = c = d; d) None of the mentioned Answer: b

10. Which of the following is possible with any 2 operators in C?


a) Same associativity, different precedence b) Same associativity, same precedence
c) Different associativity, different precedence d) All of the mentioned Answer: d
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
Assignments
 The length & breadth of a rectangle and radius of a circle are input through the
keyboard. Write a program to calculate the area & perimeter of the rectangle, and
the area & circumference of the circle.
 Employee’s basic salary is input through the keyboard. His dearness allowance is
40% of basic salary, and house rent allowance is 20% of basic salary. Write a
program to calculate his gross salary.
 If the marks obtained by a student in five different subjects are input through the
keyboard, find out the aggregate marks and percentage marks obtained by the
student. Assume that the maximum marks that can be obtained by a student in
each subject is 100.
 The distance between two cities (in km.) is input through the keyboard. Write a
program to convert and print this distance in meters, feet, inches and centimeters.
 Two numbers are input through the keyboard into two locations a and b. Write a
program to interchange the contents of a and b.
http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia
References

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia


Thank You
Any Query

http://www.nielit.gov.in/gorakhpur /GKP.NIELIT @GKP_NIELIT /NIELITIndia /school/NIELITIndia

You might also like