Data Types in C
Data type refers to the type of data. We have already learnt about variables in C which refers to the memory location of any data stored but what kind of data that memory location can store , this is determined by assigning a type to the variable using some keywords called data types. It determines the space occupied in the memory location , type of values that can be stored in that memory location and much more.
In C programming language variables are declared before it can be used in the program, and data types are used to define a variable before its use.
For Example-
Keyword used– void
Void data type is used to represent a null value. We will learn more about its uses in our upcoming tutorials.
Syntax-
By default the first value in the braces (value1) is assigned a numeric value 0 ,value 2 is assigned 1, value 3 is 2 and so on..
But if we assign some integer value to the very first element in the braces say value1=20 then the values assigned to the next elements will be Value2=21, value3=23 and so on..
The above enums are similar to defining integer constants like-
this line creates a new data type called weekdays.This new data type has 7 values written in the braces.As explained above the numerically assigned values for mon is 0,tue is 1,wed is 2 and so on..
This statement declares a new variable wd of type weekdays and wd can be initialized with any of the values given in the new data type braces like-
wd = tue;
If we write code
In C programming language variables are declared before it can be used in the program, and data types are used to define a variable before its use.
Classification of Data Types in C-
C data types are classified into four categories –Category | Types |
---|---|
Basic Data Type | int,char,double,float |
Derived Data Type | Array,structure,pointer,union |
Void | void |
Enumerated data type | enum |
1.C Basic Data Types-
C basic data type are arithmetic data types and int,char,double,float comes under this category.1.1 Integer data type In C-
Values Stored | Integer values are stored |
Keyword used | int |
Storage size | 2 or 4 byte |
Value range | -32768 to +32767 (for 2 byte) 2,147,483,648 to +2,147,483,647 (for 4 byte) |
Points to remember-
- Decimal values cannot be stored in an int data type.
- If we store decimal values in an int type variable then we will get only whole numbers and decimal values will be truncated
1.2 Char Data type in C-
Values Stored | Only a single character can be stored |
Keyword used | char |
Storage size | 1 byte |
Value range | 128 to 127 or 0 to 255 |
1.3 Floating Data type in C
Floating data type is further classified into 3 categories – 1. float 2. double 3. long double1.3.1 float data type-
Values Stored | Decimal values are stored |
Keyword used | float |
Storage size | 4 byte |
Value range | 1.2E-38 to 3.4E+38 |
Precision | up to 6 decimal places |
1.3.2 Double data type-
Values Stored | Decimal values are stored |
Keyword used | double |
Storage size | 8 byte |
Value range | 2.3E-308 to 1.7E+308 |
Precision | upto 15 decimal places |
1.3.3 long double data type-
Values Stored | Decimal values are stored |
Keyword used | long double |
Storage size | 10 byte |
Value range | 3.4E-4932 to 1.1E+4932 |
Precision | up to 19 decimal places |
How to get the Exact size of a datatype on any platform in C-
In C programming language you can use size of operator to get the exact size of any data type on that platform.For Example-
sizeof(int)
2. Derived data type in C-
Arrays , structure , Union are the derived datatypes in C. We will learn in detail about derived data types in our upcoming tutorials.3.Void data type in C-
void data type is used for no value available.Keyword used– void
Void data type is used to represent a null value. We will learn more about its uses in our upcoming tutorials.
4. Enumerated data type
With its help you can define your own data type, enum is a user defined data type. It consist a list of named integer type constant.Syntax-
enum new_datatype_name {value1, value2, value3, value4,….. valuen}
By default the first value in the braces (value1) is assigned a numeric value 0 ,value 2 is assigned 1, value 3 is 2 and so on..
But if we assign some integer value to the very first element in the braces say value1=20 then the values assigned to the next elements will be Value2=21, value3=23 and so on..
The above enums are similar to defining integer constants like-
#define value1 0 #define value2 1 #define value3 2 and so on…For Example-
enum weekdays{mon,tue,wed,thu,fri,sat,sun}; enum weekdays wd;In above Example-
enum weekdays{mon,tue,wed,thu,fri,sat,sun};
this line creates a new data type called weekdays.This new data type has 7 values written in the braces.As explained above the numerically assigned values for mon is 0,tue is 1,wed is 2 and so on..
enum weekdays wd;
This statement declares a new variable wd of type weekdays and wd can be initialized with any of the values given in the new data type braces like-
wd = tue;
If we write code
printf(“%d”,wd);It will print the numeric value of wd which is 1.
A simple C program printing the numeric values of enum-
#include <stdio.h> void main() { int n; enum weekdays {mon,tue,wed,thu,fri,sat,sun }; for(n=mon;n<=sun;n++) printf("%d",n); }Output-
0123456
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment