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