The document discusses various C data types including primary, derived, and user defined data types. It describes integer, floating point, character, array, structure, and enum data types. Integer types store whole numbers, floating point types store decimal numbers, and character types store single characters. Arrays allow storing multiple values of the same type, structures group different data types together, and enums define a new data type with named integer constants. Multidimensional arrays and accessing structure members are also explained with code examples.
2. Data Types
The basic definition of a data type is a data storage
format that can contain a specific type or range of
values.
When computer programs store data in variables, each
variable must be assigned a specific data type.
3. Types of Data Types
Primary Data Types
They are arithmetic types and are further classified into:
(a) Integer types, (b) Character and (c) Floating-point types.
Derived Data Types:
They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union
types and (e) Function types.
User Defined Data Types:
They include TypeDef and ENUM.
4. Integer Type
Integers are a commonly used data type in computer programming. An
integer is a whole number (not a fraction) that can be positive, negative, or
zero.
It is denoted by “int” .
Type Storage Size Value Range
Int 2 or 4 bytes -32,768 to 32,767 or -
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
5. Floating Point Type
As the name implies, floating point numbers are numbers that contain
floating decimal points. For example, the numbers 5.5, 0.001, and -
2,345.6789 are floating point numbers.
Denoted by keyword “float”.
Type Storage size Value Range Precision
float 4 byte 1.2E-38 to
3.4E+38
6 decimal places
double 8 byte 2.3E-308 to
1.7E+308
15 decimal places
long double 10 byte 3.4E-4932 to
1.1E+4932
19 decimal places
6. Character Data Types
Character data type allows a variable to store only one character.
“char” keyword is used to refer character 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
7. Example Code:
#include <stdio.h>
void main()
{
int i = 20;
float f = 3.1415;
char c;
c = 'd';
printf("This is my float: %f n", f);
printf("This is my integer: %d n", i);
printf("This is my character: %c n", c);
}
8. VOID DATA TYPE
The void type has no values therefore we cannot declare it as variable as we
did in case of integer and float.
The void data type is usually used with function to specify its type.
9. User Defined Data Types
User defined data types are those data types which are created by the
user to characterize the existing data types.
This user defined data type can later be used to declare variables.
In other words we are redefining the name of existing data types.
10. User Defined Data Types-Enum:
An enumeration is a user-defined data type consists of integral constants and
each integral constant is give a name. Keyword enum is used to defined
enumerated data type.
Syntax: enum type_name{ value1, value2,...,valueN };
Here, type_name is the name of enumerated data type or tag. And value1,
value2,....,valueN are values of type type_name.
By default, value1 will be equal to 0, value2 will be 1 and so on but, the
programmer can change the default value.
12. User Defined Data Types-typedef
The C programming language provides a keyword called typedef, which you can
use to give a type, a new name.
Syntax:
typedef <type><identifier>;
Example:
typedef int score;
Score player1,player2;
13. Derived Data Types: ARRAY
Array is a kind of data structure that can store a fixed-size sequential collection
of elements of the same type.
An array is used to store a collection of data, but it is often more useful to think
of an array as a collection of variables of the same type.
All arrays consist of contiguous memory locations.
First Element Last Element
a[1]a[0] a[2] a[3] a[4] a[5] a[6] a[7] a[n]
14. Declaring Arrays
To declare an array in C, a programmer specifies the type of the elements
and the number of elements required by an array as follows :
type arrayName [ arraySize ];
For example we can declare a 10 element array like:
int marks [10];
15. Initializing Array
We can initialize an array in C either one by one or using a single
statement as follows:
double price[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
The number of values between braces { } cannot be larger than the
number of elements that we declare for the array between square brackets
[ ].
16. Example:
#include <stdio.h>
int main ()
{
int n[ 10 ];
/* n is an array of 10 integers */
int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i < 10; i++ ) {
n[ i ] = i + 100;
/* set element at location i to i + 100 */
}
/* output each array element's value */
for (j = 0; j < 10; j++ ) {
printf("Element[%d] = %dn", j, n[j] );
}
return 0;
}
Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109
Output
17. Multidimensional Arrays
Array having more than one subscript variable is called multidimensional array.
Multidimensional array is also called as matrix
To declare a two-dimensional integer array of size [x][y], you would write
something as follows:
type arrayName [ x ][ y ];
To initialize multidimensional array we specify bracketed value of each row; for
example:
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
18. Example:
#include <stdio.h>
int main () {
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8} };
int i, j;
/* output each array element's value */
for ( i = 0; i < 5; i++ )
{
for ( j = 0; j < 2; j++ ) {
printf("a[%d][%d] = %dn", i,j, a[i][j] );
}
}
return 0;
}
a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8
Output :
19. Structures
Arrays allow to define type of variables that can hold several data items of the
same kind. Similarly structure is another user defined data type available in C
that allows to combine data items of different kinds.
Structures are used to represent a record. Suppose you want to keep track of
your books in a library.
You might want to track the following attributes about each book like:
Title
Author
Subject
Book ID
20. Defining Structures
To define a structure, you must use
the struct statement.
The format is as follows:
struct [structure tag] {
member definition;
member definition;
...
member definition;
} [one or more structure variables];
Example:
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
}
book;