Labsheet 2
Labsheet 2
Campus
Computer Programming (CS F111)
2021-22 Second semester
Lab-2
Topics to be covered:
1. Keywords
2. More on Data Types
3. Variables
4. Constants and literals
1. Keywords
The list below shows the reserved words in C. These words cannot be used as constant or
variable or any other identifier names:
Following table gives the details of integer types with storage size and value ranges:
Type Storage size Value range
Char 1 byte -128 to 127
Unsigned char 1 byte 0 to 255
Int 4 bytes -2,147,483,648 to
2,147,483,647
Unsigned int 4 bytes 0 to 4,294,967,295
Short 2 bytes -32,768 to 32,767
Unsigned short 2 bytes 0 to 65,535
Long 4 bytes -2,147,483,648 to
2,147,483,647
Unsigned long 4 bytes 0 to 4,294,967,295
We can use the sizeof operator for knowing the size of a type or a variable on a
particular platform. The following program illustrate the use of the operator:
The following piece of program demonstrate the ranges of different types of floating
point numbers:
2.1 Type Casting
Converting one datatype into another is known as type casting or, type conversion.
For example, if you want to store a ‘long’ value into an simple integer then you can type cast
‘long’ to ‘int’. You can convert the values from one type to another explicitly using the cast
operator as follows.
(type_name) expression
3. Variables
A variable definition means to tell the compiler where and how much to create the storage
for the variable. A variable definition specifies a data type and contains a list of one or
more variables of that type as follows:
type variable_list
Here, type must be a valid C data type including char, int, float, double etc. or any user
defined objects, and variable_list may consist of one or more identifier names separated by
comma. Here are example of some valid declaration:
Examples:
4. Constants and literals
The constants refer to fixed values that the program may not alter during its execution.
These fixed values are also called literals.
Constants can be of any of the basic data types like an integer constant, floating point
constant, a character constant, or a string constant.
Integer constants:
Character Constants:
Character literals are enclosed in a single quote, e.g, ‘a’ and can be stored in a simple
variable of char type.
A character literal can be a plain plain character (like ‘a’), or an escape sequence (like ‘\t’).
When a character in C is preceded by a backskash they will have special meaning and they
are used to represent like newline (\n) or tab (\t). Here is a list of some of such escape
sequence:
Example:
Exercise: