Lecture 04
Lecture 04
Constants in C
COSC 11023 / COST 11023
4
Rules for Variable Names (Identifiers)
The first letter of a variable name must be either a
letter or an underscore(_).
● totalSum ● is_valid
● price2Day ● user name
● 2ndPlace ● _userID
● total-sum ● counter#
● float ● xCoordinate
9
Declaring Variables
10
Declaring Variables (Continuation)
10101010
11
Data Types in C
There are four basic data types in C.
1. int
used to represents whole numbers
2. float
used to represents real numbers
3. double
used to represents real numbers
4. char
used to represents characters
12
int Data Type
13
float or double Data Types
14
char Data Type
Represents a single byte (8 bits) of storage.
16
Variable Assignment
After variables are declared, they must (should) be given
values. This is called assignment and it is done with the
‘=‘ operator.
Examples:
float a, b;
int c;
b = 2.12;
c = 200;
17
Type Casting (Type Conversions)
18
Implicit Type Casting
Example:
int a;
float f, g;
g = a + f;
19
Explicit Type Casting
Example:
a = (int) c;
b = (double) d + c;
20
Constants in C
What is a Constant?
Constant is a value that cannot change during
the execution of a program.
22
Integer Constants in C
23
Real/Floating point Constants
Example:
Decimal form: 0.254, +32.0, 2.95
Exponential form: 0.218e6 0.42e-32
24
Character Constants
25
Syntax for Constant Declarations
or
or
or
or
#define pi 3.14;
(This should go before the main() function)
27