Variables and
Constants in C
COSC 11023 / COST 11023
Ⓒ 2024 - Sachintha Pitigala 1
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
z + 2 = 3 ( y - 5) 5
In programming, a variable is a location in memory
where a value can be stored for use by a program.
A variable is best thought of as a container for a
value.
2
Variable Declaration
Before using a variable, you must give the
compiler some information about the variable;
i.e., you must declare it.
A variable declaration specifies the type, the
name and possibly the initial value of the
variable.
A variable name in C also called as an identifier.
3
Identifiers
Identifiers refer to the names of variables,
functions and arrays.
These are user defined names and consists of
sequence of letters, digits and underscore.
Both uppercase and lowercase letters are permitted
to make the identifier.
4
Rules for Variable Names (Identifiers)
The first letter of a variable name must be either a
letter or an underscore(_).
A variable name can have letters (both uppercase and
lowercase letters), digits and underscore only.
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.
Variable name in C must not be a C reserved word
(Keyword).
5
Reserved Words (Keywords) in C
● auto ● double ● int ● struct
● break ● else ● long ● switch
● case ● enum ● register ● typedef
● char ● extern ● return ● union
● const ● float ● short ● unsigned
● continue ● for ● signed ● void
● default ● goto ● sizeof ● volatile
● do ● if ● static ● while
6
Be
Naming Conventions consistent!
C programmers generally agree on the following
conventions for naming variables.
1. Begin variable names with lowercase letters
2. Use meaningful identifiers
3. Separate “words” within identifiers with
underscores or mixed upper and lower case.
Examples: surfaceArea surface_Area
surface_area 7
Case Sensitivity
C is case sensitive language
It matters whether an identifier, such as a variable
name, is uppercase or lowercase.
Example:
area
Area
AREA
ArEa
are all seen as different variables by the compiler.
8
Which are the legal identifiers?
● totalSum ● is_valid
● price2Day ● user name
● 2ndPlace ● _userID
● total-sum ● counter#
● float ● xCoordinate
9
Declaring Variables
The declaration statement includes the data type of
the variable.
Examples of variable declarations:
int age;
float area;
10
Declaring Variables (Continuation)
When we declare a variable;
● Space is set aside in memory to hold a value of
the specified data type
● That space is associated with the variable name
● That space is associated with a unique address
Visualization of the declaration: age
int age;
garbage
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
Represents a signed integer (Whole numbers or
Counting numbers) of typically 4 or 8 bytes (32 or
64 bits).
Precise size is machine-dependent.
13
float or double Data Types
Represent typically 32 and/or 64 bit real numbers.
How these are represented internally and their
precise sizes depend on the machine.
14
char Data Type
Represents a single byte (8 bits) of storage.
Can be signed or unsigned.
Internally char is just a number.
Numerical value is associated with character via a
character set.
ASCII character set used in ANSI C.
15
Additional Qualifier Names
Note that other data types can be constructed
using the modifiers:
short, long, signed, unsigned
The precise sizes of these types is
machine-specific.
To find out the meaning of short int, etc. on a
given system, use <limits.h>
16
Variable Assignment
After variables are declared, they must (should) be given
values. This is called assignment and it is done with the
‘=‘ operator.
This (=) operator does not denote equality. It assigns the
value of the right hand side of the statement (the
expression) to the variable on the left hand 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.
There are two ways of achieving the type casting.
There are:
1. Implicit Type Casting
2. Explicit Type Casting
18
Implicit Type Casting
In implicit type casting (automatic
transformation) variable in a smaller data type is
transformed into a larger data type automatically.
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.
Constants in C can be divided into two main types.
1. Numeric Constants
2. String or Character Constants
21
Numeric Constants in C
Numeric constants can be categorized into two
categories.
1. Integer Constants
2. Real or Floating point constant
22
Integer Constants in C
An integer constant is a signed or unsigned whole
number.
C language supports an integer constant in octal
(base 8), decimal (base 10) and hexadecimal (base
16). The default number system follows in C
language is decimal (base 10).
23
Real/Floating point Constants
Any signed or unsigned number with fractional part
is called real or floating point constant. A real
constant can be written in decimal or exponential
form.
Example:
Decimal form: 0.254, +32.0, 2.95
Exponential form: 0.218e6 0.42e-32
24
Character Constants
Any letter or character enclosed in single
apostrophe is call character constant.
Example: ‘y’ ‘$’ ‘+’
Any sequence of characters consisting of letters,
digits and symbols enclosed in double quotes is
called string constant.
Example: “uok” “value is 0”
25
Syntax for Constant Declarations
const data_type variable_name;
or
data_type const variable_name;
or
#define variable_name value;
26
Examples for Constant Declarations
const int SIZE = 10;
or
char const GRADE = ‘A’;
or
#define pi 3.14;
(This should go before the main() function)
27