This Blog is for New Computer Prgrammers

Friday, 9 December 2016

Variables in C - Language


Variables
Variables are named memory locations which are used to store program's input data and its computational Results during program execution.

The Variables are created in the RAM, therefore the data stored in variables is temporarily saved. Every variables has following attributes.



  • Variable Name
  • Address
  • Value
  • Data Type

Variable Name.

Variable name could be any name that follows the C - Language Variable Naming Rules
  • A variable name can consists of letters, digits and underscore character.
  • The first character of the name must be letter (Alphabet), The underscore is also allowed but not recommended
  • C - Language is case sensitive, so be careful in naming variables, C understands Capital A and small a differently.
  • C Keywords can't be used as variable names.
  • For many compilers, a variable name must not exceed from 31 letters, if it is so, first 31 letters are accepted and others are ignored.
  • Blank spaces, or special characters are not allowed except underscore.
  • Variable name in a module must be identical.
Address.
Every variable is assigned a unique memory address composed with Hexa Decimal Numbers you can display the address of variable using following printf command

printf("%x",&varibalename);

Value.
You can store any one value from the variable's permitted range, that is told when we create variable (will discuss later), Only one value can be stored in a variable at one time, but it could be changed during the program execution.

Data Type.
Data type means set of values and a set of operations on those values (will discuss later).

Declaring a Varibale in C - Language.

C is a strongly typed language so all variables must be declared before using them, Varibale declaration means you are telling the compiler that i am going to use this number of variables, with these names and respective data types.

datatype variablename;

Defining a Variable.

Remember declaration means only data type and variable name, but no memory assigned to this variable, but When we assign any value to a variable it takes space in  memory, it is called Defining the variable.

Note: in C - Language when we declare a variable C automatically define it, and some garbage is stored in that variable.

Initialization of Variable.
We can declare and define variable in a single statement, this is called initialization of variable

datatype variablename=value;



Share:

5 comments:

Total Pageviews