0% found this document useful (0 votes)
17 views

CMPE113_Lecture 5_Input_Output_Operations

The document provides an overview of input and output operations in the C programming language, including the structure of a C program, variable declarations, data types, and the use of printf and scanf functions for output and input operations. It explains the syntax and usage of different data types such as int, double, and char, as well as constants and assignment statements. Additionally, it covers the importance of placeholders in printf for displaying variable values and the address of operator in scanf for inputting data into variables.

Uploaded by

armangezerer1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

CMPE113_Lecture 5_Input_Output_Operations

The document provides an overview of input and output operations in the C programming language, including the structure of a C program, variable declarations, data types, and the use of printf and scanf functions for output and input operations. It explains the syntax and usage of different data types such as int, double, and char, as well as constants and assignment statements. Additionally, it covers the importance of placeholders in printf for displaying variable values and the address of operator in scanf for inputting data into variables.

Uploaded by

armangezerer1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Overview of C

Input / Output Operations

CMPE113
Computer Programming - I

Atilim University

10/30/2021 CMPE113 Lecture Notes 2021-2022 Fall Semester


Recall:
General Form of a C Program

© 2016 Pearson Education, Ltd. All rights reserved. 2


Recall: First C program

3
Recall: Function main
• Every C program has a main function.
int main (void)
• declarations
• the part of a program that tells the compiler the names of
memory cells in a program
• executable statements
• program lines that are converted to machine language
instructions and executed by the computer

© 2016 Pearson Education, Ltd. All rights reserved. 4


Recall: Variable declarations
• We introduce variables that we are going to use with the help of
declaration statements.
• We will learn about data types and how to declare them in the next
section.
• Examples:
double kilograms; // type double means real numbers.
int number; // type int means integer numbers

5
Recall: Statements
• The body of a program contains statements which are actually the
commands that we are going to give to our computer in order to
perform the tasks that we want.
• Examples:
printf("Enter weight in pounds: ");

scanf("%lf", &pounds);

kilograms = pounds * 0.453592;

Please note that each C Statement ends with a semi-colon!


6
Recall: Data Types
• int
• a whole number
• 435, -230
• double
• a real number with an integral part and a fractional part separated by a
decimal point
• 3.14159
• char
• an individual character value
• enclosed in single quotes
• ‘A’, ‘z’, ‘2’, ‘9’, ‘*’, ‘!’, ‘1’

© 2016 Pearson Education, Ltd. All rights reserved. 7


Data types
• You have to declare all of the variables you need to use in your
program, before using them
• In a variable declaration, you must indicate the name of the variable
and the type of the data you will store in that variable
Type Size Range Applications
(bits)
int integer values
char 8 -128 to 127 Full PC Character set
double floating point
int 32 –2 billion through 2 billion Counting, relatively
char character 31
2 – 1 » 2 billion small numbers, loop
(1 bit is used for the sign) control
double 64 1.7 x 10 -308 to 1.7 x 10 Scientific
308

8
int type
• Integer numbers are whole numbers, such as –3, 0, 15.
• They do not contain a decimal point, and they may be positive or negative.
• A positive integer can be preceded or not with a plus sign. E.g., 15 and +15 are the same
numbers. On the other hand, minus sign must be used before a negative number. E.g., -
15.
• Leading zeros are ignored. E.g., 015, +00015, 0000015 are all the same number: 15.
• An integer can not contain a comma, even if it contains too many digits. E.g., 1,000,000 is
wrong, it should be written as 1000000.
• Decimal points can not be used when writing integers. E.g., 15 and 15.0 have the same
numeric value, but 15.0 is not of type integer (but double).
• If we want to declare a variable in which we will store an integer, e.g. year, we declare it
as int year;
• If we want to declare more than one integer variables, we can list them separating with
commas: int year, age, number;
9
double type
• double type: This type is used to store real numbers. A real number has an
integral part and a fractional part which are separated by a decimal point, such as
3.0, 0.75, -2.47.
• A real number can be positive or negative, and the rules for + and – signs are the
same as for the integers. E.g., 0.75 = +0.75  -0.75.
• Both leading and trailing zeros are ignored. E.g., -02.47, -2.4700, -002.470 are all
equal to -2.47.
• The zero before or after the decimal point can be omitted. Thus, 3.0 can also be
written as 3.. But do not forget the decimal point, otherwise it becomes an
integer.
• Notice that the arithmetic values of 3.0 and 3 are the same, but the type of these
two numbers are different: First one is double, second one is int.
• Similarly, 0.75 can be written as .75 in C language.

10
double type
• We can use exponential notation, especially for very small or very
large real numbers. This notation is very similar to scientific notation
in arithmetics.
• You know from arithmetics that 6.5x102 = 650 and 0.00012 = 1.2x10-4.
These values can be expressed as 6.5E+2 (or 65E+1) and 1.2E-4 (or
12E-5) respectively. This notation is referred as exponential notation.
• Read the letter E as “times 10 to the power”. E.g., read 1.2E-4 as “1.2
times 10 to the power minus 4”.
• If we want to declare a variable to contain a real number, e.g. weight,
we declare it as double weight;
11
char type
• char type: This data type is used to store a single character value, such as a
letter, a digit, or a special symbol.
• A character value must be enclosed in single quotes (‘) in a program,. E.g.,
‘A’, ‘z’, ‘2’, ‘5’, ‘*’, ‘:’, ‘ ‘ (blank character).
• It is very important to understand the difference between 5 and ‘5’
appearing in a program. The first one is an integer, thus, it has an
arithmetic value, and it can be used in arithmetic operations. On the other
hand, the second one is a character, and has no arithmetic value. It can not
be used in arithmetic operations.
• If we want to declare a variable to contain a character, e.g. option, we will
declare it as char option;

12
Constants
• A constant is a value that does not change during the execution of a
program.
• You can use constant values in many different statements in your
programs. As you have already learned, you can also give names to
some constant values using #define preprocessor directive, and use
those names in the statements.
• Eg.:
• #define PI 3.14159
• #define MAX 1000
• #define BLANK ' ’

13
Assignment Statements
• Assume we declare an integer variable:
int age;
• Once we have declared our variables, we can put values in them by means
of assignment statements:
int age=20;
• In an assignment, the movement of data is from the right of the
assignment symbol to the left.
• If the variable already had a value, that value is overwritten after the
assignment statement.
age = 25;

14
Assignment Statements
• It is also possible to assign the value of a variable to another variable.

salary1 = 15.780;
salary2 = salary1;

• The variable on the left hand side and the value on the right must
match in data type. Real numbers must be assigned to double
variables, integers to integer variables, and characters to character
variables.

15
Assignment Statements

• Assignments to a character variable require the character value to be


enclosed in single quotation marks.
• Example: char gender = ‘M’;

16
Input/Output Statements

17
Input/Output statements
• The input statement in C language is scanf,
• The output statement in C language is printf.
• Both are functions supplied as part of the C standard input/output
library to which we gain access through the preprocessor directive

18
Output Operation: printf ()
• printf () displays the format string on the screen.
• printf () is used to write formatted output to console

19
Output Operation: printf ()
• Syntax:
printf(format string);
printf(format string, variable list);

• Format string is enclosed within double quotes and may contain


messages that we want to display

20
Example: The printf Function
• print list
• in a call to printf, the variables or expressions whose values
are displayed
• placeholder
• a symbol beginning with % in a format string that indicates
where to display the output value

printf(“That equals %f kilometers. \n”, kms);

function name
© 2016 Pearson Education, Ltd. All rights reserved. 21
Output Operation: printf ()
• If you want to display it on a new line, you should put \n either at the
beginning of the format string in the second printf statement, or at
the end of the format string in the first printf statement.
Output screen

printf(" CMPE 113");


printf(" Computer Programming ");

printf(" CMPE 113\n");


printf(" Computer Programming ");

22
\n -- new line
Formatting Characters:
“\n”: Newline escape sequence
Moves to the first column of next line
“\b”: Moves to left one column
“\f”: Moves to the first column of next page
“\t”: Skips to next tab position
“\\”: Prints one backslash

10/30/2021 CMPE113 Lecture Notes 2021-2022 Fall Semester


Placeholders
• When we want to display the contents of variables, we must use
placeholders in the format string.
• A placeholder is a symbol beginning with % in a format string that
indicates where and how to display the value of a variable.
• The placeholder we should use depends on the type of the variable
Type Placeholder for printf
int %d
float %f
double %lf
char %c
string %s
24
Placeholders
• If the variable list contains more than one variable, the format string
should contain the same number of placeholders. C matches the
variables with placeholders in left-to-right order.
Output screen

int a=5
double b=8.13; a=5 b=8.130000
printf("a=%d b=%f",a,b);

25
Placeholders
• It is possible to display the result of an arithmetic expression using
printf.

Output screen

int a=5, b=-8;


Total=-3
printf("\nTotal = %d", a + b);

26
Input Operation: scanf()
• Copies data from the standard input device (usually
the keyboard) into a variable (stored in memory).

double miles;
scanf(“%lf”, &miles);

© 2016 Pearson Education, Ltd. All rights reserved. 27


Input Operation: scanf()
• scanf takes input from the standard input device (keyboard) and puts
it into a variable.

• Syntax: scanf(<format string>, <input list>);

28
Memory
Address of operator & 4001
4002
4003 2000 birth_year
• The name of the input variable is preceded by & 4004
symbol, which is called as address of operator. It 4005
tells the computer the address of the variable 4006
into which the input will be placed. 4007
4008

Output screen
int birth_year;
printf("Enter your birthyear: "); Enter your birthyear: 2000
scanf("%d", &birth_year); You are 21 years old
printf("You are %d years old", 2021-birth_year);

The input will be placed into the


memory location 29
Address of operator &
• If the scanf statement contains addresses of more than one variable,
the user must enter data in the same order as the variables in the list.

scanf("%d%lf%d", &a, &b, &c);

• Here, the user should enter three numbers separated by blanks: first
an integer, then a double, lastly an integer, e.g.: 15 3.5 765.
• If the placeholder and the data type of the corresponding variable do
not match, it will cause an error.

30
Input/ Output Operations: Examples-1

Output screen

31
Input/ Output Operations: Examples-2
Memory
4001
4002 C letter1
4003
4004
4005
4006 A Letter2
4007
4008
Output screen
4009 N letter3
4010
4011
4012
32
Thank you

Any Questions

10/30/2021 CMPE113 Lecture Notes 2021-2022 Fall Semester

You might also like