Saturday, 23 November 2013

Variables In C

A variable is a name given to a storage area . Each variable in C has a type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
c programming
c programming


The name of a variable is a identifier . It must begin with a letter or an underscore. Based on the basic types explained in previous post, there will be the following basic variable types:

Type      Description
char        Typically a single character. This is an integer type.
int        The most natural size of integer for the machine.
float         A single-precision  value.
double A double-precision  value.
void          Represents the absence of type.

C programming language also permits to define various other types of variables, which we will cover in subsequent chapters like Enumeration, Pointer, Array, Structure, Union, etc.

Variable Definition in C:

A variable definition is a way to tell the compiler where and how much to create the storage for the variable. A variable definition contain a data type and contains a list of one or more variables of that type as follows:

    type variable_list;

Here, type must be a valid C data type including char,  int, float, user-defined object, double or a bool etc., and variable_list may has one or more than one identifier names.for example:

 int    a, b, c;
 char   cr, af;
 float  g, salary;
 double h;
The line int a, b, c; both declares and defines the variables a, b and c; which instructs the compiler to create variables named a, b and c of type int.

Variables can be initialized (assigned an initial value) in  declaration. The initialization consists of an equal sign followed by a constant expression .

type variable_name = value;
Some examples are:

extern int d = 3, f = 5;    // declaration of d and f.
int d = 3, f = 5;           // definition and initializing d and f.
byte z = 22;                // definition and initializes z.
char x = 'x';               // the variable x has the value 'x'.

For definition without an initialization: variables with static storage duration are implicitly initialized with NULL (all bytes have the value 0); the initial value of all other variables is undefined.

Variable Declaration in C:

Declaration of a  variable  provides assurance to the compiler that there is one variable existing with the given type and name so that compiler proceed for further compilation without checking complete detail about the variable. A declaration has its meaning  only for   compilation time, compiler use this  variable declaration at the time of linking of the program.

A variable declaration is more efficient  when you are using multiple files .You define a variable in one of the files which will be available at the time of linking of the program. You can use extern keyword to declare a variable at any place.
Example
Try the example,

#include <stdio.h>

// Variable declaration:
extern int a, b;
extern int c;
extern float f;

int main ()
{
  /* variable definition: */
  int a, b;
  int c;
  float f;

  /* actual initialization */
  a = 10;
  b = 20;
  
  c = a + b;
  printf("value of c : %d \n", c);

  f = 70.0/3.0;
  printf("value of f : %f \n", f);

  return 0;
}
The  output is :

value of c : 30
value of f : 23.333334

Same concept is applied on a function declaration where you provide a function name at the time of its declaration and its actual definition can be given anywhere in the program.

// function declaration
int func();

int main()
{
    // function call
    int i = func();
}

// function definition
int func()
{
    return 0;
}

Lvalues and Rvalues in C:

C has  two kinds of expressions i:

lvalue : An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.

rvalue : An expression that is an rvalue may appear on the right- but not left-hand side of an assignment.

Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so they may not be assigned.The valid statement is:

int g = 20;

But following will generate compile-time error:
10 = 20;
C - Overview
C - Basic Syntax
C - Data Types
C - Variables
C - Constants
C - Storage Classes
C - Operators
C - Decision Making
C - Loops
C - Functions
C - Scope Rules
C - Arrays
C - Pointers
C - Strings
C - Structures
C - Unions
C - Bit Fields
C - Typedef
C - Input & Output
C - File I/O
C - Preprocessors
C - Header Files
C - Type Casting
C - Error Handling
C - Recursion
C - Variable Arguments
C - Memory Management
C - Command Line Arguments

0 comments:

Post a Comment