Different Data Types And Variables In C
For making a program you have to deal with lots of different types of data and variables. So, our today’s C programming tutorial for beginners lesson is to familiarize with various types of data and variables. You must know all the different types of Data & Variables for accurate coding with C language. If you don’t know the data types and variables correctly. Your programming life will be much more difficult. You must use proper data types and variables for a specific purpose while making a C program.
Different Types Of Data In C:
1st of all during programming you have to store and operate with different types of data. Suppose you are given two values a and b you have to show their sum as output. So, you must take these a and b as input and print their sum. For this case, we consider a and b are integers. Now, suppose you have to check an alphabet whether it is vowel or consonant. So, in this case, you have to deal with the character type of data. Then suppose, you have given the buy rate and sell rate of a product. Now, you have to show the percentage of benefits. For, this case you have to deal with fractional type data. So, I think you got the basic idea about what are data types actually means. Now some important data types are shown below:
1)Integer Type Data:
Integer type data means numeric data without fractions. You have to define a integer type data as int . Format specifier for integer type of data is %d. It takes 4 bytes of storage in the memory. And the range of this data type is (-2*10^9 to +2*10^9). However, integer type data actually 2 types.
i) Signed Integer:
Signed integer means all the positive and negative integers from -2*10^9 to +2*10^9. Actually, integer data type means signed integer. Format specifier %d.
ii)Unsigned Integer:
Unsigned integer means only positive integers ranging from 0 to 4*10^9. Format specifier %u.
2) Long Long Integer:
When we need to deal with a large integer data which is greater than 2*10^9. Then we have to use long long integer types of data. Have to define as long long int. The format specifier is %lld. This type of data needs 8-byte memory storage. long long int type of data range is -9*10^18 to +9*10^18. long long int also called signed long long int. Unsigned long long int type of data value range is 0 to +18*10^18.
3) Float Data Type:
Float type of data means fractional types of data like 75.88. This type of data takes 4 byte storage. The format specifier is %f. It can hold at most 6 digit integer having a fraction. You must declare this type of data as float.
4)Double And Long Double:
We can use the double type of data for the representation of all integer types of data with fractions. You can say the double type of data is the long float type of data. It took 8-byte storage. The format specifier is %lf. long double type of data used to represent all long long int type of data having fraction. It took 16 bytes of memory. Format specifier is %Lf.
5)Character Type Data:
When you need to deal with an alphabet and other characters you have to use the character type of data. It doesn’t mean you can only use the alphabet A-Z and a-z. Besides them, you can use other characters having Ascii values -128 to 127. These are also called signed character types of data. The unsigned character type of data represents the Ascii values from 0 to 255. Format specifier for both types of character data is %c. Declaration of this type of data as char. The character type of data took 1 byte of storage in the memory.
Those are the general types of data in c. But we can use user-defined data types like under one variable we can use more than 1 type of data as structure. We will learn this later when we reach that level.
However, to deal with a particular type of data we must use a variable to store them.
Now, your question is what is a variable? Let’s clear it.
What Are The Types Of Variables in C:
Actually, variable means giving a name to a particular memory location to access the data contained by this particular storage using that name. For example:
int a,b,sum;
These a,b, and sum are 3 integer types of variables. For better understanding follow this example:
int a=5, b= 11, sum;
sum=a+b;
Each integer type of variable a,b and sum contains 4 bytes of storage in the memory. 5 is stored in the memory as labeled a, 11 as b. And then sum stores in the memory as the summation of a and b means 16.
Another example is:
char ch=’a’ or ch=97;
In this example, ch is a variable that only can store character type of data. And we assign a as a value under ch variable. ch=’a’ and ch=97 actually mean the same. Because we know char types of data actually represent the Ascii value range from -128 to 127. And Ascii value of a is 97. So, ch=’a’ or ch=97. Both store a in the 1-byte memory location labeled as ch.
So, if you want to deal with a particular data you must use a variable to store that data in the memory specified by the variable. And you can use the variable instead of the value throughout the program.
Variables are actually 2 types:
1)Local Variable In C:
Local variables are the variables that are used under a particular function not throughout the whole program. They are actually declared and used only under that particular function.
2) Global Variable In C:
These types of variables are generally declared at the starting inside the main function. They are used throughout the whole program.
Naming Rules For Variables In C:
However, there were some rules for declaring variables they are:
- Variable name must start with an alphabet or underscore.
- Variables must contain only characters A to Z and a to z, numbers 0 to 9, and a special character underscore(_). It should not contain any other special characters like @,&,$,%, etc.
- The variable should not be keywords like int,#define, void, etc.
- A variable name can be 1 to at most 32 digits. Not more than that.
- Variable name must not contain white space. Like ” we 3″ is an invalid variable and “we_3” or “we3” is a valid variable.
- Variables are case-sensitive. Sum and sum are two different variables not the same.
In conclusion, to write a program you must deal with the above type of data using particular variables. That is it for today. In the next tutorial, we will try to learn how to take input and give output in the c program and also the usage of variables and different types of data.