Monday, June 15, 2015

Introduction to C Language and Programming (Part II How To Use Variables)

How to use Variables



When you are programming, you want to show and store information, to do that, you have to have Variables, you can input data and store in variables. In programming, Few Variables types are available, there are some choices, it will depend on the data type which you are going to use and input to a variable.

Ex:

Int     = Int can store integers (Numbers can be stored but it cannot store decimal numbers)
Char  = Char and store single character
Float  = float can store number with decimal

You tell the compiler that I’m going to use this variable as this type or that type or whatever, then only compile can identify about the variable.


Syntax of the declaring a variable:

<Variable type> <Name of the variable>;

Ex :
int myFrist;
int xy123;
char my1, my2, myName;

·        Here we have declared three variables by dividing commas, you can have multiple variables, but names must be different.
           char my1, my2, myName;

When we are naming the variables, you have to use camel case. It means when we are declaring a variable, we have to start the name of the variable by using Simple letter, but we can combine two words by using Capital letter as like below, you will see the camel :)

helloWorld
youAre
sriLanka

I hope you could have an idea about it, and also we must know, we cannot declare a variable starting from a number like below.

Int 123xy;
Char 124gh;

Those are wrong and cannot use as variables, after declare the variable, you have to close it by using semicolon, and then it will ready for using.

#include <stdio.h>
int main()
{
    int x;
   x = 10;
    printf( "The Number is %d: ", x);
   
    getchar();

    return 0;
}


When you are declaring a variable, you must know, you have to declare it first, or else it won’t be able to show the information that you have stored in the variable, and also there will be an error, because you cannot use a variable name without declare it first, try below example and you will understand.

Below program is not valid that is only an example:

#include <stdio.h>
int main()
{
 
    printf( "The Number is %d: ", x);
  int x = 10; 
    getchar();

    return 0;

}

0 comments:

Post a Comment

Facebook Fan Page

Popular Posts

Powered by Blogger.