Variable and Its Data Type
Variable and Its Data Type
Identifiers
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
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;
Variable Initialization
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 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 defines some standard data type. A data type that is predefines in the language
is called standard 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 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
It is used to store integer values. It takes two bytes in memory. Its range from -
32768 to 32767.
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
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
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.
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.
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.
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.
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
✓ 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.