Lecture 04
Lecture 04
Sachintha Pitigala
© 2023
What is a Variable?
● Variables in C have the same meaning as variables in
algebra. That is, they represent some unknown, or
variable, value. x
x = a+b
5
z + 2 = 3 ( y - 5)
● In programming, a variable is a location in memory
where a value can be stored for use by a program.
● There is no rule on how long a variable can be. However, only the
first 31 characters of a variable are checked by the compiler.
6
Naming Conventions
● C programmers generally agree on the following conventions for naming
variables.
● Separate “words” within identifiers with underscores or mixed upper and lower
case.
Examples: surfaceArea surface_Area
surface_area
● Be consistent!
7
Case Sensitivity
● C is case sensitive language
9
Declaring Variables
● The declaration statement includes the
data type of the variable.
age
● Visualization of the declaration:
int age; garbage
11
1000101
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
● Represents a signed integer (Whole numbers or Counting
numbers) of typically 4 or 8 bytes (32 or 64 bits).
13
float and double Variable Types
● Represent typically 32 and/or 64 bit real numbers.
14
char Variable Type
● Represents a single byte (8 bits) of storage.
● This (=) operator does not denote equality. It assigns the value of the
righthand side of the statement (the expression) to the variable on the
lefthand side.
● Examples:
float a, b;
int c;
b = 2.12;
c = 200;
17
Type Casting (Type Conversions)
● Type casting is a way to convert a variable from one
data type to another data type.
● Example:
int a;
float f, g;
g = a + f;
19
Explicit Type Casting
● In explicit type casting (automatic transformation) data
type of a variable will be explicitly converted into a
different data type by the programmer.
● 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.
21
Numeric Constants
● Numeric constants can be categorized into two categories.
1. Integer Constants
2. Real or Floating point constant
● Example:
○ Decimal form: 0.254, +32.0, 2.95
○ Exponential form: 0.218e6 0.42e-32
23
Character Constants
● Any letter or character enclosed in single apostrophe is
call character constant.
■ Example: ‘y’ ‘$’ ‘+’
25
Example Constant Declarations
const int SIZE = 10;
or
char const GRADE = ‘A’;
or
#define pi 3.14;
(This should go before the main() function)
26