0% found this document useful (0 votes)
22 views7 pages

Variable and Its Data Type

The document provides an overview of basic data types and variables in C and C++, including identifiers, keywords, variable declaration, initialization, and rules for naming variables. It details standard and user-defined data types, along with specific data types for integers, floating points, and characters, highlighting their memory requirements and ranges. Additionally, it distinguishes between primitive, derived, and user-defined data types in C++.

Uploaded by

Extra Test
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)
22 views7 pages

Variable and Its Data Type

The document provides an overview of basic data types and variables in C and C++, including identifiers, keywords, variable declaration, initialization, and rules for naming variables. It details standard and user-defined data types, along with specific data types for integers, floating points, and characters, highlighting their memory requirements and ranges. Additionally, it distinguishes between primitive, derived, and user-defined data types in C++.

Uploaded by

Extra Test
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/ 7

Basic data types and variables

BS(IT) 1st Semester

Identifiers

✓ Identifiers refer to the names of variable, functions and arrays.


✓ These are user defines names and consists of sequence of letters and digits.
✓ Both upper case and lowercase letters are permitted to make the identifier
Rules of identifiers
✓ First character must be alphabet
✓ Must not contain white space.
✓ Only first 31 characters are significant.
✓ Cannot use keyword as a identifier

Keyword
✓ Every C word is classified as either keyword or identifier.
✓ All keywords have fixed meanings and you cannot change its meaning.
✓ C language has total 32 keywords.

List of keywords in C

auto else goto static struct


break enum if sizeof switch
case extern do signed typedef
char float double short union
const for int return unsigned
continue default long register void
volatile while
Variable:

A variable is a named memory location or memory cell. It is used to store programs input
data and its computational results during execution. The value of the variable may change during
program execution. The variables are created in RAM. RAM is temporary memory. That is why
the data stored in variables is also temporary. It can only be used and processed during the
execution of program.

Variable Declaration

The process of specifying the variable name and its type is called variable
declaration. All variables must be declared before they are used in program. A variable can be
declared only for one data type. Once variable declared, its data type cannot be changed during
program execution.

Syntax

data_type variable_name;

data_type it indicates type of data that can be store in variable.

variable_name: It refers to the memory location of the variable.

Example: int marks;

Variable Initialization

The process of assigning a value to a variable at the time of declaration is known


as variable initialization. The equal = sign is used to initialize a variable. Variable name is written
on the left side and the value is written on the right side of equal sign.

The compiler automatically allocated the required memory for the variable when it
is declared. The memory location may already contain some data that is meaningless for the
program. This meaningless data is called garbage value. It may produce unexpected and wrong
results during computation.

Syntax

data_type variable_name = value;

Example: int marks=10;


Rules for naming variable

1. Variable may include letters, numbers and underscore.


2. The first character of variable must be a letter or underscore.
3. Blank spaces are not allowed in variable names. E.g my var
4. Both uppercase and lowercase are allowed. The uppercase and lowercase letters are
considered different in C.
5. The special symbols such as @,$,% are not allowed in variables names.
6. Reserved words cannot be used as variable name.
7. A variable can be up to 31 characters long for many compilers. If a variable consists of
more than 31 characters, only 31 characters will be used.
8. A variable can be declared only for one data type.

Data Types

The data type specifies the type of data that can be stored in a variable. It also defines a set
of operations on the data. The compiler allocated memory space for each variable according to its
data type.

C language provide the following data types

Data Type Process


int To store numeric values
float To store real values
double To store large real values
char To store character value

Categories of Data types

Standard Data type:

C language defines some standard data type. A data type that is predefines in the language
is called standard data type.

User defined data type:

C also allows the user to define his own data types known as user defined data type.
Different data types to store an Integer data

➢ int data type

Int data type is used to represent integer values. An integer variable stores a numeric
value with no fractional; part such as 10, 24 etc. It takes 2 or 4 bytes in memory depending on the
computer and the compiler being used. Its range from -32768 to 32767

➢ short data type

It is used to store integer values. It takes two bytes in memory. Its range from -
32768 to 32767.

➢ unsigned int data type

It is used to store only positive integer values. It takes 2 bytes in memory. Its range
from 0 to 65,535 which is equal to 0 to 216-1

➢ long data type

It is used to store large integer values. It takes 4 bytes in memory. Its range is from
-2,147,483,648 to 2,147,483,647

➢ unsigned long int

It is used to store large positive integer values. It takes 4 bytes in memory. Its range
is from 0 to 4,294,967,295.

Types of Floating Point

➢ float data type

It is used to store real values. It takes 4 bytes in memory. Its range from 3.4*10 -38
to 3.4*1038 It provide the precision of 6 decimal places.

➢ double data type

It is used to store large real values. It takes 8 bytes in memory. Its range from
-308
1.7*10 to 1.7*10308 .It provide the precision of 15 decimal places.

➢ long double data type


It is used to store very large real values. It takes 10 bytes in memory. Its range from
-4932
1.7*10 to 1.7*104932. It provide the precision of 19 decimal places.

Character Data Type

char data type is used to store character value. It takes 1 byte in memory. It is used to
represent a letter, numeric, punctuation mark and a fewer other symbol.

Character values are written in single quotes. It can represent individual characters such as
‘a’, ‘x’ etc.

The characters can be added, subtracted or compared. The character are stores in
ASCII code ASCII stands for American National Standard Code for Information Interchange.
The ASCII code values of character are used when they are added, subtracted and compared.

C++ Datatypes
variables use data-type during declaration to restrict the type of data to be stored.
Therefore, we can say that data types are used to tell the variables the type of data it can store.
Whenever a variable is defined in C++, the compiler allocates some memory for that variable
based on the data-type with which it is declared. Every data type requires a different amount of
memory.

Data types in C++ is mainly divided into three types:

✓ Primitive Data Types:

These data types are built-in or predefined data types and can be used
directly by the user to declare variables. example: int, char , float, bool etc. Primitive
data types available in C++ are:

o Integer
o Character
o Boolean
o Floating Point
o Double Floating Point
o Valueless or Void
o Wide Character
✓ Derived Data Types:

The data-types that are derived from the primitive or built-in datatypes are
referred to as Derived Data Types. These can be of four types namely:

o Function
o Array
o Pointer
o Reference
✓ Abstract or User-Defined Data Types:

These data types are defined by user itself. Like, defining a class in C++ or a
structure. C++ provides the following user-defined datatypes:

o Class
o Structure
o Union
o Enumeration
o Typedef defined DataType

primitive data types available in C++.

✓ Integer: Keyword used for integer data types is int. Integers typically requires 4 bytes
of memory space and ranges from -2147483648 to 2147483647.

✓ Character: Character data type is used for storing characters. Keyword used for
character data type is char. Characters typically requires 1 byte of memory space and
ranges from -128 to 127 or 0 to 255.

✓ Boolean: Boolean data type is used for storing boolean or logical values. A boolean
variable can store either true or false. Keyword used for boolean data type is bool.

✓ Floating Point: Floating Point data type is used for storing single precision floating
point values or decimal values. Keyword used for floating point data type is float. Float
variables typically requires 4 byte of memory space.

✓ Double Floating Point: Double Floating Point data type is used for storing double
precision floating point values or decimal values. Keyword used for double floating
point data type is double. Double variables typically requires 8 byte of memory space.
✓ void: Void means without any value. void datatype represents a valueless entity. Void
data type is used for those function which does not returns a value.

✓ Wide Character: Wide character data type is also a character data type but this data
type has size greater than the normal 8-bit datatype. Represented by wchar_t. It is
generally 2 or 4 bytes long.

You might also like