Variables and Data Types in C
sum = a + b
where we know the value of a and b. We have to find the value of sum. How can we do it by C Programming?
According to Algebra, we know
b are called
variable and ‘
+’ is an
operator which operate the sum of variable
b. In c programming those are the same as Algebra.
Variable is the name of data where we can store the value and this value is changeable. In C Programming, every variable has an unique memory address of system and this memory address is the location of variable we declare in program. When we declare a variable in our program to run or compile computer keeps a memory address for this variable.
Variable Declaration in C
There has a little difference of variable declaration between algebra and C Program.
In Algebraic variable declaration it is not required to declare of its data type. Such as, is it integer variable or float variable? But in C Program, it is required to declare data type alongside variable before using any variable in program. So the variable declaration in C will be:
int sum;Similarly integer number, we can declare variable for fractional number, character and words or sentence (String).
For fractional the data type are
Question: What is the difference between float and double in C? Ans:For character and string is char.
Question: What is the difference between character and string in C?
Ans: String is the sequence of character. So more than one character makes string.
Question: Can I declare same data type variable alongside another variable declaration?
Ans: Yes. You can declare more variable same data type in line and you have to separate by comma (,) between those variables. Example:
int sum, int a, int b;or
int sum, a, b;Don’t forget to put semicolon after finishing statement.
But the another good news is that you are allowed to declare different data type variable declaration in line. Example:
int sum, float amount;
Initialization: Assigning or Putting Value to Variable
We have learned how to declare variable but we don’t know how to put or assign value to variable.
If we want to put 2017 to an integer type variable
year then we can do it as below:
int year = 2017; /*If we don’t declare data type before */
year = 2017; /*If we declare data type before as int year; */
Most of lesson to write a program to calculate the sum of two variable whose value is given.
So we can write a program like this:
int a, b, sum;
/*ASSIGN VALUE OT VARIABLE*/
a = 30, b = 20;
sum = a + b;
/*PRINT THE RESULT*/
printf("Result = %dn",sum);
return 0;/*END OF PROGRAM*/
Run the program and your output will be –
Result = 50
1. In variable declaration we have declared three variables in line.
2. In variable initialization or assignment, suppose we have two given value 30 for a and 20 for b. We have assigned those values to its related variable in line and separated by comma and it is legal and easy. We could also as below:
a = 30;
b = 20;
3. In printing the calculation of sum, we have used
printf() function. Since we want to show the result after printing “
Result =”, we put this string withing double quotation in
printf() function. Here,
%d is for printing the integer value. It is called format specifier. Some format specifier:
%f for float, %c for character, %s for string, %lf for double etc.
Another the most important observation about
prinft() function in this program is, we use comma after double quotation and put the variable name of sum. Yes, we have to specify which variable’s value we want to print.
If we would want to print the value of variable a then we had to replace
Question: Can I use anything for variable name declaration, such as
sum1, 2sum, #sum, div&as, 3242 etc?
Though, the name of data is called variable, to define or name of variable there has some rules. You can’t use whatever you want to name of your selected variable.
Rules of Variable Name
- First character must be underscore or an alphabet. Without this you are not allowed to use another.
- You can use only digit, underscore and alphabet in your variable.
- You are not allowed to use keyword as variable name.
- You can’t put space in variable name.
Exercise1. Find out the valid variable name from the following.
Value2_, var-name, amount_for, com+pro+math, _value, #NUM, Number of Loan.
2. Assume variable
var2 are declared before. Which initializations are true.
Var1 = 20; var2 = 30;
Var1 = 20, var2 = 30;
Var1 = 20;
var2 = 30;
Var1 = 20,
var2 = 30;
3. Write a program to calculate the division of two variable, where
a = 100 and
b = 50.