[go: up one dir, main page]

0% found this document useful (0 votes)
8 views8 pages

Labsheet 2

Uploaded by

KUSUMITA PAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views8 pages

Labsheet 2

Uploaded by

KUSUMITA PAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

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 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

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, 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:

Variables can be initialized in their declaration or afterwards. 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 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:

The #define Preprocessor:

We can use #define for 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
defned using #define directive.

Exercise:

1. Write a C program to know the size of different floating point


datatypes.
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 stores an integer in hexadecimal format and
output in the deimal format. Your program should be able to take
the hexadecimal integer as input.
4. Write a program to print your name and registration number by
defining it in a constant using #define directive.

You might also like