INSTITUTE - UIE
DEPARTMENT- ACADEMIC UNIT-2
Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Introduction to Problem Solving
Code:22CSH-101
Input Output Functions DISCOVER . LEARN . EMPOWER
Introduction to
Problem Solving
Course Objectives
The course aims to provide exposure to problem-
solving through programming.
The course aims to raise the programming skills
of students via logic building capability.
With knowledge of C programming language,
students would be able to model real world
problems.
2
Course Outcomes
CO Course Outcome
Number
CO1 Remember the concepts related to fundamentals of C language,
draw flowcharts and write algorithm/pseudocode.
CO2 Understand the way of execution and debug programs in C
language.
CO3 Apply various constructs, loops, functions to solve mathematical
and scientific problem.
CO4 Analyze the dynamic behavior of memory by the use of pointers.
CO5 Design and develop modular programs for real world problems
using control structure and selection structure.
3
Scheme of Evaluation
4
Contents
I/O Formatted & Importance Of Need Of Header
statements Unformatted I/O Indentation Files
5
I/O Statements
I/O Statements
Help to load values into declared
variables
Values can be printed onto standard
output device in a desired format
One of the most important part of
programming
6
I/O
I/O Types
types
Formatted I/O : In formatted I/O functions format specifiers are used to
format the I/O before reading or printing it.
Unformatted I/O : Format specifiers are not allowed to use.
7
Formatted I/O Functions
Functions Description
This is formatted input function used to read one or
scanf()
many Inputs from standard input device keyboard
This is formatted output function used to print one or
printf()
many Values on standard output device monitor .
8
Scanf()
Scanf()
scanf(<control string>, &address1,&address2, . .&addressn);
& (ampersand) is used with each variable to access its address
It is a formatted input function , format specifiers are provided in
control string
9
Scanf()
printf()
printf(“ Text ”);
printf(“Text <control string> Text”, Variable_name);
It is a formatted output function , format specifiers are provided in
control string.
10
Format Specifiers
Functions Description
Used for signed and unsigned int variables
%d and %u
respectively
Used for signed and unsigned long variables
%ld and %lu
respectively
%c and %s
Used for character and string variables respectively
%lf and %Lf Used for double and long double variables
respectively
11
Example1- Use Of printf() and scanf()
#include <stdio.h> // header file used for standard I/O
int main()
{
int age; //Integer variable to store age of student
int roll; // Integer variable to store roll number of student
printf("Enter the data");
scanf("%d%d", &age, &roll); //format specifier
printf("Age=%d\n",age); // Print age on standard Output
printf("Roll No.=%d\n",roll); // Print Roll. No. on standard
Output
return 0; // return to C system from where main() was called
}
12
Indentation In C
Indentation in programming is used to make program easier
to read and understand .
#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
printf("Hello World"); printf("Hello World");
return 0; return 0;
} }
13
Header Files
•Header file is that which consists of functions , macros and variable
declarations .
•It is stored with .h extension .
•Every program includes at least one header file named <stdio.h> it
includes declarations of standard input and output functions scanf()
and printf()
14
Types Of Header Files
predefined header files : are those which are inbuilt into C
system like stdio.h. Inbuilt header files are included in < >
pair as :
#include<stdio.h>
User Defined Header Files : User can write their own
header files and can include into C programs . The user
defined header files are included in “ “ (Double quotes).
15
Unformatted I/O Functions
Functions Description
This function is used to read a single character from keyboard .
getchar()
This function is used to print a single character on standard O/P
putchar()
screen.
gets() It can be used to read strings .scanf() is not capable to read spaces but
gets() can
puts() string read by gets () can be printed using puts() function .
16
Use Of getchar() and putchar()
#include <stdio.h>
int main()
{ char ch;
printf("\n press y /Y for yes or n/N for no ");
ch = getchar(); // Read single char from keyboard
//scanf("%c",&ch);
putchar(ch);
printf("\n");
//printf("%c",ch);
return 0;
}
17
Role of Format specifiers in reading
#include <stdio.h>
int main()
{
int x; float y; char z;
scanf("%3d%4f", &x, &y);
printf("%3d\n%.2f",x,y);
return 0;
}
18
Role of Format specifiers in printing
#include <stdio.h> Output:
main()
{ /987.361000/ //when no precision is specified,the default precision is 6
places after decimal
float a = 987.361;
/ 987.361000/ // 2 blank spaces introduced before the output
printf("/%f/\n", a); because space is of 12 chars
printf("/%12f/\n", a); /987.361000 / // 2 blank spaces introduced after the output
printf("/%-12f/\n", a); /987.361000/ // The width specifier 4 is ignored because chars
printf("/%4f/\n", a); are more
/987.3610/ // precision is controlled to 4 , default of 6 is overridden
printf("/%.4f/\n", a);
/987.4/ // when precision is overridden rounding of value will occur
printf("/%.2f/\n", a);
}
19
Use Of gets() and puts()
#include <stdio.h>
int main()
{
char str[81];
puts("Enter a line of text:\n");
gets(str);
puts("You entered:\n");
//printf("%s",str);
puts(str);
}
20
Summary
Are used to read values from keyboard and write values on
I/O Statements
standard O/P screen.
Formatted and Format specifiers are allowed in formatted I/O but not in
Unformatted I/O unformatted I/O
Format specifiers Are used to format input and output according to user
requirements
Indentation Makes program easier to read and understand
Header Files Are used include functions, macros and variable declarations
21
Q2: Can we define our own header files ?
Q1: : Why &(ampersand) is used with variables
in scanf() but not in printf() ?
Ans: Printf() is a function in which variables are Ans: Yes , we can create our own header
passed via call by value method but in scanf() files .Include it in “ “ (Double Quotes)
variables are passed via call by reference . like #include”fact.h” .
FAQs
Q4 : I read two int values using scanf()
Q3: When I try to print real value it print six
statement I provide space in two %d then it did
digits after decimal points , it is too long .Can i
not read the values properly , why ?
control it ?
Ans: space is not allowed in two format
Ans: Yes it is too long we can control it with the
specifiers while using scanf() statement it tries
help of format specifiers like float a=1.2
to assign second value to space and store
printf(“%f”,a); // 1.200000 garbage value in the second variable .So, avoid
printf(“%1.3f”,a); // 1.200 space in format specifiers .
22
Q6: Multiline comments make programs bulky
in size. Is it good practice to use these in C
programs ?
Q5: : Is indentation mandatory ?
Ans: Whenever necessary like in the beginning
Ans: No, indentation is not mandatory but to
provide program description in multiline
make program easy to read and understand use
comment at other places use single line
indentation into your programs .
comment .
FAQs
Q7: I store my city population in int variable it
is 50000 but when I print it was some garbage Q8 : Can header files include main() function ?
value why it happened ?
Ans: No, header files are always included into C
Ans: You worked on 16 bit compiler. int take programs and one program cannot have two
two bytes and can store value in range -32768 main functions .
to 32767 so 50000 cannot be stored in this
variable you can take long to store this value .
23
Test Yourself
Int a=145;
printf(“%5d”,a); Char str[35];
printf(“%-5d”,a); scanf(“%[ABCD ]s”,str);
Int a=12654;
printf(“hello””hi”); float b=2.3509
Printf(“Hello”+”Hi”); printf(“%2d”,a);
printf(“%.1f”,b);
24
References
S.No Title Content link
1 Book Programming in C by Reema Thareja.
2 Book Programming with C (Schaum's Outline Series) by Byron
Gottfried Jitender Chhabra, Tata McGraw Hill.
3 Book The C Programming Language by Brian W. Kernighan, Dennis
Ritchie, Pearson education.
4 Book Programming in ANSI C by E. Balaguruswamy, Tata McGraw
Hill.
5 Weblink https://www.tutorialspoint.com/cprogramming/c_operators.htm
6 Weblink https://www.programiz.com/c-programming
7 Weblink https://fresh2refresh.com/c-programming/
References
S.No Title Content link
8 Weblink https://www.studytonight.com/c/
9 Weblink https://www.javatpoint.com/c-operators
10 Video Link https://www.youtube.com/watch?v=cEfuwpbGi1k
11 Video Link https://www.youtube.com/watch?v=qmd5sVFOgpg
12 Online Course Link https://www.coursera.org/
13 Online Course Link https://www.udemy.com/
14 Online Course Link https://www.niit.com/
THANK YOU