Monday, August 14, 2017

Variables and Data Types in C

We saw in Hello World, how can we print a words, numbers, message, sentence etc. Now if we want to calculate an equation like below:
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 sum, a & b are called variable and ‘+’ is an operator which operate the sum of variable a and 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.
Variables and Data Types in C

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: data_type variable_name; Example: int sum; Similarly integer number, we can declare variable for fractional number, character and words or sentence (String).
For fractional the data type are float & double.
Practical example of variables and data types in c
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?
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?
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:
#include <stdio.h>
int main()
    int a, b, sum;
    a = 30, b = 20;
    sum = a + b;
    printf("Result = %d\n",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 sum with a.
Question: Can I use anything for variable name declaration, such as sum1, 2sum, #sum, div&as, 3242 etc? 
Ans: No.

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

  1. First character must be underscore or an alphabet. Without this you are not allowed to use another.
  2. You can use only digit, underscore and alphabet in your variable.
  3. You are not allowed to use keyword as variable name.
  4. You can’t put space in variable name.
Exercise 1. 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 Var1 and var2 are declared before. Which initializations are true.
a. Var1 = 20; var2 = 30;
b. Var1 = 20, var2 = 30;
c. Var1 = 20; 
var2 = 30;
d. Var1 = 20, 
var2 = 30;
3. Write a program to calculate the division of two variable, where a = 100 and b = 50.

Looking for more effecive Course?

C Programming For Beginners by Udemy icon
$15 (Original $145)
30-Day Money-Back Guarantee
  • 6 hours on-demand video
  • 1 Article
  • 2 Supplemental Resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion