Unit 3 : Declarations, Input and Output Functions - Tin học đại cương (IT1110) | Trường Đại học Bách khoa Hà Nội

Unit 3 : Declarations, Input and Output Functions

lOMoARcPSD| 27879799
Downloaded by VietJack TV Official (vietjackvideos@gmail.com)
lOMoARcPSD| 27879799
2
Declaraons
Constants and variables must be declared before they can be
used.
A constant declaraon species the type, the name and the
value of the constant.
A variable declaraon species the type, the name and
possibly the inial value of the variable.
When you declare a constant or a variable, the compiler:
1. Reserves a memory locaon in which to store the value of
the constant or variable.
2. Associates the name of the constant or variable with the
memory locaon. (You will use this name for referring to
the constant or variable.)
lOMoARcPSD| 27879799
3
Constant declaraons
Constants are used to store values that never change during the
program execuon.
Using constants makes programs more readable and
maintainable.
Syntax: const <type> idener = <expression>;
Examples:
const double US2HK = 7.8;
//Exchange rate of US$ to HK$
const double HK2TW = 3.98;
//Exchange rate of HK$ to TW$
const double US2TW = US2HK * HK2TW;
//Exchange rate of US$ to TW$
lOMoARcPSD| 27879799
4
-3.14
lOMoARcPSD| 27879799
5
Variable declaraons
A variable has a type and it can contain only values of that
type. For example, a variable of the type int can only hold
integer values.
Variables are not automacally inialized. For example, aer
declaraon
int sum; the value of the variable sum can be
anything (garbage).
Thus, it is good pracce to inialize variables when they are
declared.
Once a value has been placed in a variable it stays there unl
the program deliberately alters it.
lOMoARcPSD| 27879799
Constant declaraons
A convenient way to associate constant values with
names is using the #dene statement.
#dene
Examples
#dene TRUE 1
#dene TABLESIZE 100
Constants can be declared by const declaraon.
They appear inside funcons.
const data_type const_name = value const int a=2, b=5;
6
lOMoARcPSD| 27879799
7
Statements and Blocks
An expression such as x = 0 or i++ or prin(. . .) becomes
a statement when it is followed by a semicolon.
Block group any number of data denions,
declaraons, and statements into one statement.
compound statement group statements into one
statement
Use a blocks or compound statements wherever a single
statement is allowed.
We use braces {} to build block or compound
statements
lOMoARcPSD| 27879799
8
Input and output in C
C programming provides a set of built-in funcons to
read the given input and feed it to the program as per
requirement.
C also provides a set of built-in funcons to output the
data on the computer screen as well as to save it in text
or binary les.
Typical input output funcons are
In <stdio.h>
The scanf() and prin() Funcons
The getchar() and putchar() Funcons
In <conio.h>
The getch() and putch() Funcons
The gets() and puts() Funcons
lOMoARcPSD| 27879799
9
Funcons prin, scanf
lOMoARcPSD| 27879799
10
is usually called the
control string
lOMoARcPSD| 27879799
11
lOMoARcPSD| 27879799
12
lOMoARcPSD| 27879799
13
with scanf.
lOMoARcPSD| 27879799
14
Acon:
Change the values stored in the parts of
memory that is associated with variables.
Process the control string from le to right
Each me it reaches a specier : interpret
what has been typed as a value.
Input mulple values : separated by white
space (spaces, newline or tabs)
lOMoARcPSD| 27879799
15
lOMoARcPSD| 27879799
16
lOMoARcPSD| 27879799
17
putch
lOMoARcPSD| 27879799
18
s
lOMoARcPSD| 27879799
19
| 1/19

Preview text:

lOMoAR cPSD| 27879799
Downloaded by VietJack TV Official (vietjackvideos@gmail.com) lOMoAR cPSD| 27879799 Declarations
• Constants and variables must be declared before they can be used.
• A constant declaration specifies the type, the name and the value of the constant.
• A variable declaration specifies the type, the name and
possibly the initial value of the variable.
• When you declare a constant or a variable, the compiler:
1. Reserves a memory location in which to store the value of the constant or variable.
2. Associates the name of the constant or variable with the
memory location. (You will use this name for referring to the constant or variable.) 2 lOMoAR cPSD| 27879799 Constant declarations
• Constants are used to store values that never change during the program execution.
• Using constants makes programs more readable and maintainable. Syntax: const identifier = ; Examples: const double US2HK = 7.8; //Exchange rate of US$ to HK$ const double HK2TW = 3.98; //Exchange rate of HK$ to TW$
const double US2TW = US2HK * HK2TW; //Exchange rate of US$ to TW$ 3 lOMoAR cPSD| 27879799 - 3.14 4 lOMoAR cPSD| 27879799 Variable declarations
• A variable has a type and it can contain only values of that
type. For example, a variable of the type int can only hold integer values.
• Variables are not automatically initialized. For example, after declaration
int sum; the value of the variable sum can be anything (garbage).
• Thus, it is good practice to initialize variables when they are declared.
• Once a value has been placed in a variable it stays there until
the program deliberately alters it. 5 lOMoAR cPSD| 27879799 Constant declarations
A convenient way to associate constant values with
names is using the #define statement. #define Examples #define TRUE 1 #define TABLESIZE 100
Constants can be declared by const declaration. They appear inside functions.
const data_type const_name = value const int a=2, b=5; 6 lOMoAR cPSD| 27879799 Statements and Blocks
• An expression such as x = 0 or i++ or printf(. . .) becomes
a statement when it is followed by a semicolon.
• Block group any number of data definitions,
declarations, and statements into one statement.
• compound statement group statements into one statement
• Use a blocks or compound statements wherever a single statement is allowed.
• We use braces {} to build block or compound statements 7 lOMoAR cPSD| 27879799 Input and output in C
• C programming provides a set of built-in functions to
read the given input and feed it to the program as per requirement.
• C also provides a set of built-in functions to output the
data on the computer screen as well as to save it in text or binary files.
• Typical input output functions are • In
• The scanf() and printf() Functions
• The getchar() and putchar() Functions • In
• The getch() and putch() Functions
• The gets() and puts() Functions 8 lOMoAR cPSD| 27879799 Functions printf, scanf 9 lOMoAR cPSD| 27879799
is usually called the control string 10 lOMoAR cPSD| 27879799 11 lOMoAR cPSD| 27879799 12 lOMoAR cPSD| 27879799 with scanf. 13 lOMoAR cPSD| 27879799 Action:
• Change the values stored in the parts of
memory that is associated with variables.
• Process the control string from left to right
• Each time it reaches a specifier : interpret
what has been typed as a value.
• Input multiple values : separated by white
space (spaces, newline or tabs) 14 lOMoAR cPSD| 27879799 15 lOMoAR cPSD| 27879799 16 lOMoAR cPSD| 27879799 putch 17 lOMoAR cPSD| 27879799 s 18 lOMoAR cPSD| 27879799 19