0% found this document useful (0 votes)
73 views6 pages

Lab 2 Data Types, Variable, Literals Objective

This document provides an overview of various data types, variables, literals, and type casting in C programming. It defines different data types like char, int, float, their storage sizes, and value ranges. It also describes format specifiers, escape sequences, keywords, separators, identifiers, variables, literals, and type casting. An example program is included to demonstrate variable declaration and usage.

Uploaded by

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

Lab 2 Data Types, Variable, Literals Objective

This document provides an overview of various data types, variables, literals, and type casting in C programming. It defines different data types like char, int, float, their storage sizes, and value ranges. It also describes format specifiers, escape sequences, keywords, separators, identifiers, variables, literals, and type casting. An example program is included to demonstrate variable declaration and usage.

Uploaded by

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

Lab 2

Data Types, Variable, Literals


Objective
1. Data type
2. Type Casting
3. Identifier(Variable)
4. Literal(Constant)
5. Keyword
6. Separators

Data Types
A data type is an amount of storage allocated to variables. The Data type determines the permissible
operations on variables. A program usually contains different types of data types like integer, float, character
etc. and need to store the values being used in the program. The variables should be declared by specifying
the data type.

Type Storage size Value range


Char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
int 4 bytes -2,147,483,648 to 2,147,483,647
unsigned int 2 or 4 bytes 0 to 65,535 or 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
Float 4
Double 8

Format Specifiers
These are the operators used in printf() function to print the data which is referred by an object or a
variable. The Format specifier is the sequence passed as the formatting string argument.
Following are the most common used format specifiers.

Data Type Format Specifiers


signed decimal integer %d
single character %c
string %s
Float %f
Double %lf
unsigned decimal integer %u
unsigned hexadecimal integer %x

Escape Sequence
The character combinations, which comprise a backslash (\) followed by some character. They are also
called white spaces because they are not displayed on the screen.

Escape Sequence Represents


\n New line
\t Horizontal tab
\v Vertical tab
\r Carriage Return
\' Single quotation mark
\" Double quotation mark
\\ Backslash

Keywords
Keywords are reserved words that have a specific meaning for the compiler. They cannot be used as
identifiers. For Example int, float, main, void etc.

Separators
Separators are symbols that indicate the division and arrangement of groups of code. The structure and
function of code is generally defined by the separators. The separators used in C are as follows:
 parentheses ( )
 braces { }
 brackets [ ]
 semicolon ;

Identifiers
Identifiers are names provided by you. These can be assigned to variables, methods, functions etc. to
uniquely identify them to the compiler.

Variables
A variable is a meaningful name of data storage location in computer memory. Or it can be considered as
a box that can hold a single value. When using a variable, refer to memory address of computer.

Naming variables:
The name of variable can be called identifier or variable name. The name can contain letters, digits and the
underscore. The length of name can be up to 247 characters long but 31 characters are usually adequate,
keywords cannot be used as a variable name.
Declaration of variables:

In order to use a variable first declare it by specifying which data type is it to be. The syntax to declare a
new variable is to write the specifier of the desired data type like int, float followed by a valid variable
identifier. For example: int a; float nfc; int a,b,c;
Initializing of variables:
In declaration of a variable, its value is by default undetermined. To store a concrete value to a variable
is called the initialization of a variable.
Example: int a = 10; fload nfc=1.9; char ch = 'NFCIET';

In the example the declaration statement int qty; selects a piece of memory with the name qtycalled
variable which is of integer type.

In the above example qty is a variable, changes its value as the execution of program progresses.
Line 1: The keyword int selected a piece of memory with a name qty to store an integer value
Line 3: An integer value 45 is assigned (stored) to the variable qty using assigning operator (=)
Line 4: The value of qty is incremented by 10 and assigned to the same variable, which will be
overwriting on the previous value.

LITERALS (Constant)
A constant is a value of any type that has the same value and can never change. The constants can be
any of the basic data types i.e. they can be int, float or char. Constants are also known as literals i.e.
constants and literals are one and the same. The Character constants are enclosed between single
quotes.

Example: 1, 2, 3, 4 OR ’a'
Declaration and Initialization of variable with data type
Integer :
int is used to define integer numbers
Example:
{
int a;
a = 5;
}
Float :
float is used to define floating point numbers.
{
float nfc;
nfc = 5.6;
}
Double :
double is used to define BIG floating point numbers. It reserves twice the storage for the number. On
PCs this is likely to be 8 bytes.
{
double Atoms;
Atoms = 2500000;
}
Character :
char defines characters.
{
char Letter;
Letter = 'x';
}
Void :
Void type has no values therefore it cannot be declare as variable as we did in case of integer and float.
It is usually used with function to specify its type.
The Address Operator (&) and scanf
An address of operator returns the memory address of a variable. These addresses returned by the
address-of operator are known as pointers, because they point to the variable in memory. The & operator
tells the scanf ( ) function where to find each variable into which it is to store a new value. If the & were
omitted, the scanf ( ) would know only a variable’s current value, not its location in the memory.
Example:
{
int i;
scanf("%d", &i);
printf("number is: %d", i);
}

Program example #include <stdio.h>


#include <conio.h>
void main( ) #include <stdio.h>
{ #include <conio.h>
int a=5, b=10; void main( )
clrscr( ); {
printf("%d",c); int a;
getch( ); a = 10000;
} printf("My salary is %d.", a);
getch( );
}

#include <stdio.h>
#include <conio.h>
void main( )
{
int a;
clrscr( );
printf("Enter a number\n");
scanf("%d", &a);
printf("You entered %d\n", a);
getch();
}
Type Casting
A type cast is basically a conversion from one type to another. There are two types of type conversion:

Implicit Type Conversion


Also known as ‘automatic type conversion’.
1. Done by the compiler on its own, without any external trigger from the user.
2. Generally takes place when in an expression more than one data type is present. In such
condition type conversion (type promotion) takes place to avoid lose of data.
3. All the data types of the variables are upgraded to the data type of the variable with largest data
type.
char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -
> long double

Explicit Type Conversion


This process is also called type casting and it is user defined. Here the user can type cast the result to
make it of a particular data type.
The syntax in C:
(type) expression

Advantages of Type Conversion


1. This is done to take advantage of certain features of type hierarchies or type representations.
2. It helps us to compute expressions containing variables of different data types.

You might also like