You are on page 1of 27

INSTITUTE - UIE

DEPARTMENT- ACADEMIC UNIT-2


Bachelor of Engineering (Computer Science & Engineering)
Subject Name: Fundamentals of Computer programming
Code:21CSH-101

Input Output Functions DISCOVER . LEARN . EMPOWER


Fundamentals of
Computer
programming
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
Scheme of Evaluation

Sr. Type of Assessment Weightage of actual Frequency of Task Final Weightage in Internal Remarks
No. Task conduct Assessment (Prorated
Marks)

1. Assignment* 10 marks of One Per Unit 6 marks As applicable to


each assignment course types depicted
above.
2. Time Bound 12 marks for each One per Unit 4 marks As applicable to
Surprise test course types
Test depicted above.
3. Quiz NA NA Non Graded: Engagement As applicable to
Task course types
depicted above.
4. Mid-Semester Test** 20 marks for one 2 per semester 10 marks As applicable to
MST. course types
depicted above.
5. Presentation***     Non Graded: Engagement Only for Self Study
Task MNGCourses.

6. Homework NA One per lecture topic Non-Graded: Engagement As applicable to


(of 2 Task course types
questions) depicted above.
7. Discussion Forum 4 marks One per 4 marks As applicable to
semester course types depicted
above.
8. Attendance and NA NA 2 marks  
Engagement Score
on BB
3
Contents

I/O Formatted & Importance Of Need Of Header


statements Unformatted I/O Indentation Files

4
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

5
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.

6
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 .

7
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

8
Scanf()
printf()

printf(“ Text ”);


printf(“Text <control string> Text”, Variable_name);

It is a formatted output function , format specifiers are provided in


control string.

9
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

10
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
}

11
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;
} }

12
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()

13
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).

14
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 .


15
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;
}

16
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;
}

17
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);
}

18
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);
}

19
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

20
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 files
passed via call by value method but in scanf() .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 .
21
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 .
22
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);

23
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
Indentation In C

Indentation in Indent can be invoked on command prompt by using any one of the
programming is used to following form .
make program easier to indent [options] [file-name]
 
read and understand .
indent [options] [source file] [-o destination-file]
In C , indentation can  
understands C syntax and In first form original file will be replaced by indented file and in the second
can convert one form of C form source file will be indented and store into output file .
program to other example :
 
indent fact.c -o fact.out
 
It will indent the file fact.c and store the indented file into fact.out

27

You might also like