Data Types, Variables and Constants
Data Types, Variables and Constants
Facilitators
Mr. Luyima Alex Cedric
[email protected]
0772 302775 / 0708 045 305
1.5.1 Literals
Literals are values that appear directly in a program. Numeric constants are an uninterrupted sequence
of digits (possibly containing a period). Examples of numerical values are 123, 10000 and 99.78
int age = 23; // 23 is a numeric literal
Character constants represents a single character and it is surrounded by single quotation mark ( ‘).
Characters such as ‘a’, ‘A’, ‘$’ and ‘4’ are examples of Character Constants.
1.5.2 Identifiers
Identifiers are referred to as names given to variables. There are four major rules that must be
considered when coming up with identifiers (variable names):
i. An identifier must begin with a letter and the rest can be letters, digits or underscores. An
identifier must not start with a number / digit. e.g. 2Age and 23sum cannot be used as identifiers.
ii. Identifiers are case sensitive, therefore the identifier abc is different from ABC or Abc.
iii. C reserved / key words such as int, float, double, void, main, include etc. cannot be used as
identifiers. Programmers cannot use any of the keywords as variable names.
iv. Identifiers should never have spaces between them e.g. an identifier cannot be “age of student”
but can be “age_of_student”.
Sometimes a C compiler may consider only the first 32 characters in an identifier. While defining
identifiers programmers should follow some of the naming standards for better readability of the
program. One such standard is the use of underscores symbol (_) to combine two words (example:
sub_total).
• Primitive (or basic) data types –these are the fundamental data types supported by the language.
These can be classified as integer types, floating point types and character types.
• User defined data types – based on the fundamental data types users can define their own data
types. These include type defined data types (using typedef keyword) and enumerated types (using
enum keyword).
• Derived data types – programmers can derive data types such as arrays, structures, unions and
pointers by combining several data types together.
1.5.3.1 Primitive Data Types
The C language supports five primitive data types; namely integers (int) floating point numbers (float),
double precision floating point numbers (double), characters (char) and void (void). Many of these data
types can be further extended as long int and long double. Each of these data types requires different
storage capacities and has different range of values (depending on the hardware) as shown in Table 2.2
below.
Table 2.4 –Basic C data types and modifiers (on a 32-bit machine)
1.5.4 Variables
A variable is a value that can change. A variable is described as a reserved memory location that can
hold a value of a certain data type.
Programmers refer to a variable by its name (identifier) so that it can be accessed during the course of
the program. Programmers cannot use any of the keywords as variable names as stated in the rules that
govern the formulation of identifiers.
int age = 23; // age is a variable
1.5.4.1 Declaring Variables
In order to use a variable in C, the programmer must first declare it specifying the data type. The most
important restriction on using a variable in C is that they have to be declared at the beginning of the
program.
Declaring a variable refers to reserving named space in memory where data of a specified data type
is stored.
The syntax to declare a new variable is to first write the data type then followed by a valid variable
identifier as given in the following examples:
inta;
float total;
unsignedint number_of_students;
int a;
int b;
float total;
float sub_total;
int a, b;
const affects all variables on the declaration line. In the last line, debt and tax_rate are symbolic
constants. If your program tries to modify a const variable, the compiler generates an error message, as
shown here:
const int count = 100;
count = 200; /* Does not compile! Cannot reassign or alter */
/* the value of a constant. */
END
THANK YOU