c data types
C data types - Storage format for a variable to store data to perform a specific operation.
Basic Data type in C language
Primitive Data Type: Inbuilt data type provided by C language
- int, float, double, char and void
Non primitive data type : These are derived from the primary data type
- arrays , pointers
User Defined Data types
- Structure, Union, Enumeration
Other way of classifying
Primary
Character – char, signed char , unsigned char
Integer
signed – int, short int ,long int
unsigned – int, short int, long int
Float - float , double , long doulbe
Void
Derived
Array , pointer, structure, union enum
1)
a)Integer
Integer data type is used to store numeric values in a variable.
int is the keyword used to refer integer data type.
The storage size of int data type is 2 , 4 or 8 bytes
This depends on the upon the processor in the CPU that we use. For a 16 bit processor, 2 bytes (16 bit) of memory will be allocated for int data type.
For 32 bit processor 4 bytes or 32 bits and for 64 bit processor 8 byte or 64 bits are allocated for int datatype.
Signed and Unsigned Variables
Signed variables, such as signed integers is used represent numbers both in the positive and negative ranges.
Integer Type
Consider a 16 bit machine
Consider a 32 bit machine
Refer: testint.c
b) Char
It stores a single character and requires a single byte of memory in almost all compilers.
Char is a data type which is used in C programming for storing characters like letters and punctuation marks. However, it still remains to be an integer type. This is due to the reason that char type technically stores integers and not characters.
It makes use of a numerical code which represents characters by using integers. For example – ASCII code which is one of the most commonly used codes for carrying out such interpretations.
The transformation of char into int values is done automatically by C. However, it is still dependent on the machine which decides that the result would be negative or not. The upper case A is equivalent to integer value of 65.
The terms unsigned and signed used with char means that if the content of these eight bits is interpreted into an integer then these terms can make some difference. Character data type is usually of type unsigned by default. However, In C++ and ANSI C mode, there is an option to explicitly declare them as signed or unsigned char.
In unsigned char 8 bits are used as data bits, whereas in memory representation of signed char 1 bit (most significant bit) is used for signed bit and 7 bits are used as data bits. If the signed bit is 0 it means that number is positive. If signed bit is 1 then number is negative.
The char data type can be either signed or unsigned. It depends on the compiler and platform.
While the character data type is commonly used to
represent a character (and that's where it gets its name) it is also
used when a very small amount of space, typically one byte, is needed to
store a number. A signed char can store a number from -128 to 127, and
an unsigned char can store a number from 0 to 255. When used to
represent a character, however, that signed or unsigned
is irrelevant
Refer : testchar.c
c) float and double
float and double are two different data types in C for storing real numbers. double occupies twice the memory occupied by float
The size of float (single precision float data type) is 4 bytes. And the size o
float: It is used to store decimal numbers (numbers with floating point value) with single precision.( Occupies 32 bits of memory.)
double: It is used to store decimal numbers (numbers with floating point value) with double precision. ( Occupies 64bits of memory )
2) Enumerated Data type
This is a user defined data type in C.
It can have only one value
3) void
Specifies that no value is available . It is used in the following cases
1. When a function does not return any value void test_fun(int input_val)
2. When a function does not take any input argument int test_fun(void)
3. pointer to void
A void pointer is one which does not have any data type associated with , what is means is that it can be assigned a value of any type.
It is capable of storing address of any data type.
void *ptr;
int intval; char charval;
ptr = &intval; //ptr changes to integer pointer as address of integer is assigned to it
ptr = &charval; //ptr changes to character pointer as address of character is assigned to it
Note: Null pointer is different from void pointer
A null pointer is one which is not pointing to anything, i.e. it is assigned a null value.
If there is no address to assign to a pointer, it is better to set it to null.
Syntax: <data type> *<variable name> = NULL;
Example: int *ptr = NULL;
char *ptr = '\0';
ref: testvoidptr.c
Derived data types: Pointer, array, structure,
We will see in further sessions
C data types - Storage format for a variable to store data to perform a specific operation.
Basic Data type in C language
Primitive Data Type: Inbuilt data type provided by C language
- int, float, double, char and void
Non primitive data type : These are derived from the primary data type
- arrays , pointers
User Defined Data types
- Structure, Union, Enumeration
Other way of classifying
Primary
Character – char, signed char , unsigned char
Integer
signed – int, short int ,long int
unsigned – int, short int, long int
Float - float , double , long doulbe
Void
Derived
Array , pointer, structure, union enum
| Basic/Primary Type/ primitive |
int, char, float, double |
| Enumerated Type |
Enum |
| Void Type |
void |
| Derived Type |
pointer, array, structure, union |
1)
a)Integer
Integer data type is used to store numeric values in a variable.
int is the keyword used to refer integer data type.
The storage size of int data type is 2 , 4 or 8 bytes
This depends on the upon the processor in the CPU that we use. For a 16 bit processor, 2 bytes (16 bit) of memory will be allocated for int data type.
For 32 bit processor 4 bytes or 32 bits and for 64 bit processor 8 byte or 64 bits are allocated for int datatype.
Signed and Unsigned Variables
Signed variables, such as signed integers is used represent numbers both in the positive and negative ranges.
Unsigned variables, such as
unsigned integers, is used to represent numbers in the positive
Two's complement Method
Two's complement is
the most common method of representing signed integers in computers
In two’s complement form, a negative number is the 2’s complement of its positive number
How to get 2's complement of a number
Assume we are using 8 bit form and want to represent -28 in binary form.
a) 28 in binary form is : 00011100
b) Then we invert the digits. 0 becomes 1, 1 becomes 0.: 11100011
c) Then we add 1. 11100100
That is how one would write -28 in 8 bit binary.
In two’s complement form, a negative number is the 2’s complement of its positive number
How to get 2's complement of a number
Assume we are using 8 bit form and want to represent -28 in binary form.
a) 28 in binary form is : 00011100
b) Then we invert the digits. 0 becomes 1, 1 becomes 0.: 11100011
c) Then we add 1. 11100100
That is how one would write -28 in 8 bit binary.
For a 8 bit signed
integer
127 is represented
as 01111111
-127 is represented
as 2’s complement of positive 127, ie 10000001
Get the Max and min values of signed and unsigned
Unsigned data types:
Get the Max and min values of signed and unsigned
Unsigned data types:
int max = pow(2, number of bits assigned to data types) — 1;
Signed data types:
int min = ( pow(2, number of bits assigned to data types) / 2) * -1;
int max = (pow(2, number of bits assigned to data types) / 2) - 1;
For 16 bit
Unsigned – > 2 power 16 - 1 = 65536 – 1 = 65535
Signed
int min = (2 power 16 ) /2 * (-1) = 65536/2 *(-1) = -32768
int max = ( 2 power 16 / 2) -1 = 32767Integer Type
Consider a 16 bit machine
| Type | Size (Bytes) |
Range |
int or signed int |
2 |
-32,768 to 32767 |
| unsigned int |
2 |
0 to 65535 |
short int or signed short int |
1 |
-128 to 127 |
unsigned short int |
1 |
0 to 255 |
| long int or signed long int | 4 |
-2,147,483,648 to 2,147,483,647 |
| unsigned long int | 4 |
0 to 4,294,967,295 |
Consider a 32 bit machine
| Type |
Size(Bytes | Range |
| int |
4 |
|
| short int |
2 |
|
| long int |
8 |
Refer: testint.c
b) Char
It stores a single character and requires a single byte of memory in almost all compilers.
Char is a data type which is used in C programming for storing characters like letters and punctuation marks. However, it still remains to be an integer type. This is due to the reason that char type technically stores integers and not characters.
It makes use of a numerical code which represents characters by using integers. For example – ASCII code which is one of the most commonly used codes for carrying out such interpretations.
The transformation of char into int values is done automatically by C. However, it is still dependent on the machine which decides that the result would be negative or not. The upper case A is equivalent to integer value of 65.
The terms unsigned and signed used with char means that if the content of these eight bits is interpreted into an integer then these terms can make some difference. Character data type is usually of type unsigned by default. However, In C++ and ANSI C mode, there is an option to explicitly declare them as signed or unsigned char.
In unsigned char 8 bits are used as data bits, whereas in memory representation of signed char 1 bit (most significant bit) is used for signed bit and 7 bits are used as data bits. If the signed bit is 0 it means that number is positive. If signed bit is 1 then number is negative.
| Type | Storage Size | Value Range |
| Char | 1 byte | -128 to 127 or 0 to 255 |
| unsigned char | 1 byte | 0 -255 |
| Signed char | 1 byte | -128-127 |
The char data type can be either signed or unsigned. It depends on the compiler and platform.
Refer : testchar.c
c) float and double
float and double are two different data types in C for storing real numbers. double occupies twice the memory occupied by float
The size of float (single precision float data type) is 4 bytes. And the size o
f double (double precision float data type) is 8 bytes. float: It is used to store decimal numbers (numbers with floating point value) with single precision.( Occupies 32 bits of memory.)
double: It is used to store decimal numbers (numbers with floating point value) with double precision. ( Occupies 64bits of memory )
2) Enumerated Data type
This is a user defined data type in C.
It can have only one value
3) void
Specifies that no value is available . It is used in the following cases
1. When a function does not return any value void test_fun(int input_val)
2. When a function does not take any input argument int test_fun(void)
3. pointer to void
A void pointer is one which does not have any data type associated with , what is means is that it can be assigned a value of any type.
It is capable of storing address of any data type.
void *ptr;
int intval; char charval;
ptr = &intval; //ptr changes to integer pointer as address of integer is assigned to it
ptr = &charval; //ptr changes to character pointer as address of character is assigned to it
Note: Null pointer is different from void pointer
A null pointer is one which is not pointing to anything, i.e. it is assigned a null value.
If there is no address to assign to a pointer, it is better to set it to null.
Syntax: <data type> *<variable name> = NULL;
Example: int *ptr = NULL;
char *ptr = '\0';
ref: testvoidptr.c
Derived data types: Pointer, array, structure,
We will see in further sessions
No comments:
Post a Comment