Datatypes and Variables

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Birla Institute of Technology & Science Pilani, Hyderabad

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:

2. More on Data Types

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 illustrates the use of the operator:

The following piece of the program demonstrates 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 a 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

By executing the above program, you will get the output

Value of sum: 116


Here the value of sum is 116 because the compiler is doing integer promotion (type
casting) and converting the value of ‘c’ to ASCII before performing the addition operation.

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, the 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 a comma. Here are examples of some valid declarations:

Variables can be initialized in their declaration or afterward. The initializer consists of an


equal sign followed by a constant expression as follows:

type variable_name = value

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:

An integer constant or literal can be a decimal, octal, or hexadecimal constant. A prefix


specifies the base or radix: 0x for hexadecimal, 0 for octal, and nothing for decimal.

Different types of integer literals:

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 character (like ‘a’), or an escape sequence (like ‘\t’).
When a character in C is preceded by a backslash 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
sequences:

Example:

The #define Preprocessor:

We can use #define for defining a constant. Please take a look


at the following example:
Observe the output of the program. Note that the constants LENGTH, WIDTH, and NEWLINE are
defined using #define directive.

Exercise:

1. Write a C program to store a real number with 8 decimal digits in double type and cast
it in type float. Output both double and float numbers with 8 precision value, i.e., 8
digits after the decimal point. What is the interpretation of the output?

2. Write a program that stores a 8 digit integer and cast it in type ‘short’. (a) What is the
output of your program? (b) How can you interpret the result?

3. Write a program that has a constant decleared for PI (3.14159) and a variables radius.
Calculate area, and circumference of the circle and output the values with 4 precision,
i.e., 4 digits after the decimal point.

4. Write a program that stores an integer in octal format and outputs in the decimal
format. Your program should be able to take the octal integer as input.

You might also like