Declaration of variable

After deciding a suitable name for the variable, we have to declare them to the compiler.

What does declaration do in c program?

It tells the compiler what the variable name is. It specifies what types of data the variable will hold. There are two types of declaration of variables.

  1. Primary declaration
  2. User-defined declaration

1.Primary declaration:-
       In primary type declaration, A declaration statement begins with the type, end by the name of one or more variables. A declaration statement must end be with a semicolon.
Syntax of declaration sentences.

Syntax data_type variable_1, variable_2, .....,variable_n; 

Declaration of multiple variables of the same data types can be done in one statement.

For Example,

              int a;

              int b;

              int c;

can be also written as

              int a, b, c;

             When variables are declared inside a function, they are called local variables. When the variables are declared in the definition of function parameters, these variables are called formal parameters. when the variables are declared outside all functions, they are called global variables.

Example:

#include <stdio.h>
#include <conio.h>
int a; //Global Variable
int main()
{
    printf("Enter your Global variable: ");
    scanf("%d",&a);
    add1();
    add2();
    getch();
}

add1()
{
   int b,sum1; //Local Variable
    printf("Enter a number for Local variable: ");
    scanf("%d",&b);
    sum1=a+b; //Here you don't need to again declare 'a' as it is Globally declared
    printf("Your addition of Global & Local variable is %d",sum1);
    getch();
}
add2()
{
     int c,sum2; //Local Variable
    printf("\n Enter a number for Local variable: ");
    scanf("%d",&c);
    sum2=a+c; //Here you don't need to again declare 'a' as it is Globally declared
    printf("Your addition of Global & Local variable is %d",sum2);
    getch();
}
//Here you can note that we have declared 'a' once only but using many times. 
// It is called Global variable/Global declaration.
Global variables can be used anywhere in the program while the local variable is not.
The initial value in the Global variable assigned is 'Zero' while the initial value assigned in the local variable is 'Garbage value'

This Garbage value can be anything.

2. User-Defined declaration:-
             C language supports a feature known as "type definition" which allows defining an identifier that would represent an existing data type. the user-defined data type identifier can later be used to declare a variable.

Syntax of user-defined type declaration is,

Syntax:  typedef type identifier;

Where type refers to existing data types and identifier refers to the new name given to the data type. typedef cannot create a new type.

Some Examples,

               typedef int marks;

               typedef float sum;

Here, marks symbolize int and float symbolize float. they can be used to declare variable later. as follows,

              marks batch1, batch2;

              sum sum1, sum2;

where marks and sum represent integer type and float type value.

we don't have to declare their datatype again.

               Another user-defined data type is the enumerated data type provided by  ANSI.

Syntax: enum identifier{v1, v2, v3, ...vn};

Example:

                 enum day {mon, tues,.....sun};

The compiler automatically assigns integer digits beginning with 0 to all enumeration constants.here, the enumeration constant mon is assigned 0 Tues is assigned 1 and so on.

Example:

Program:
#include 
#include<stdio.h>

enum week{Mon=2, Tue, Wed, Thur, Fri, Sat, Sun};

int main()
{
    enum week day;
    day = Wed;
    printf("%d",day);
    return 0;

Output:

4

we can also assign a value.

Example:

Program:

#include 
#include<stdio.h>
/
enum week{Mon, Tue, Wed, Thur, Fri, Sat, Sun};

int main()
{
    enum week day;
    day = Wed;
    printf("%d",day);
    return 0;

Here the constant mon is assigned the value of 2. the remaining

constant assigned continuously value like 3,4,5.....n.

Output:

2

Note: The declaration of variables must be done before they are used in the program.

Comments

Popular posts from this blog

Getting input in C

Unary Operator