Variables & Constants

Variables
  • Variable in C Programming is called as container to store the data.
  • Variable name may have different data types to identify the type of value stored.
  • Suppose we declare variable of type integer then it can store only integer values.
  • Variable is considered as one of the building block of C Programming which is also called as identifier.
  • A Variable is a name given to the memory location where the actual data is stored.

Following are the types of variable in C :

  • Local Variables
  • Global Variables

Local Variables

  • Local Variable is Variable having Local Scope.
  • Local Variable is accessible only from function or block in which it is declared.
  • Local variable is given Higher Priority than the Global Variable.

Local variables are declared within a function definition and could only be accessed from anywhere in that function in which it is declared.

Thus, local variables have their scope local to the function in which they are declared and are unknown to other functions outside their own function.

Example :

#include <stdio.h>
    int main ()
    {
    /* local variable declaration */
    int x, y, z;

    /* actual initialization */
    x = 1;
    y = 2;
    z = x + y;

    printf ("Value of x = %d, y = %d and z = %d\n", x, y, z);

    return 0;
    }

Output :

        Value of x = 1, y = 2 and z = 3

Explanation :

Here the variable xyz are local variable. And the scope of these variables is within the main() function.

Global Variables

  • Global Variable is Variable that is Globally available.
  • Scope of Global variable is throughout the program [ i.e in all functions including main() ]
  • Global variable is also visible inside function , provided that it should not be re-declared with same name inside function because “High Priority is given to Local Variable than Global”
  • Global variable can be accessed from any function.

Global variables in c have their declaration outside the function definition of all functions used within the program.

Example :

#include <stdio.h>

    /* global variable declaration */
    int z;
    int main ()
    {
    /* local variable declaration */
    int x, y;

    /* actual initialization */
    x = 10;
    y = 20;
    z = x + y;

    printf ("Value of x = %d, y = %d and z = %d\n", x, y, z);
    return 0;
    }

Output :

Value of x = 10, y = 20 and z = 30

Explanation :

The variable z is a global variable, and it can be accessed by any function.

Example :

#include <stdio.h>

    /* global variable declaration */
    int a = 20;

    int main ()
    {
    /* local variable declaration */
    int a = 10;
    printf ("Value of a = %d\n", a);
    return 0;
    }

Output :

Value of a = 10

Explanation :

Variable a is locally as well as globally defined, but the local variable value will be preferred.

NOTE :

  • If the local and global variable have same name then value of local variable will take preference.

Rules to define Variable

  1. Characters allowed : Underscore(_), Capital Letters ( A – Z ), Small Letters ( a – z ), and Digits ( 0 – 9 ).
  2. Blank spaces and commas are not allowed.
  3. No Special Symbols other than underscore(_) are allowed.
  4. First Character should be alphabet or Underscore.
  5. Variable name Should not be Reserved Keywords.

Keywords

Keywords are reserved words which have standard, predefined meaning in C. They cannot be used as program-defined identifiers

*Generally all keywords are in lower case although uppercase of same names can be used as identifiers.

List of C keywords are as follows :

Keywords in C Programming
auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while

Data Types

  • DataTypes are used for declaring variables And functions of different types.
  • When Program store data in variables, It is necessary that each variable must be assigned a specific data type.
Following are the list of Data Types in C :
Keyword Memory Range
char or signed char 1 Byte -128 to 127
unsigned char 1 Byte 0 to 255
int or signed int 2 Byte -32,768 – 32,767
unsigned int 2 Byte 0 to 65535
short int or signed short int 1 Byte -128 to 127
unsigned short int 1 Byte 0 to 255
long or signed long 4 Bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 Bytes 0 to 4,294,967,295
float 4 Bytes 3.4E – 38 to 3.4E + 38
double 8 Byte 1.7E – 308 to 1.7E + 308
long double 10 Bytes 3.4E-4932 to 1.1E + 4932

Declaration of variable :

main()
    {
    /* declaration */

    //long int is datatype and amount is variable name
    long int amount;

    //int is datatype and code is variable name
    int code;

    char c;

    double average;

    float x,y;

    }

 

Constants

A constant is an identifier with an associated value which cannot be altered by the program during execution.

How to declare constant variable ?

We can declare constant variable using const keyword.

Example of declaring constant variable

//Syntax for declaring constant variable
    data type const variable_name = value;

    //float constant
    float const pi = 3.14;

    //integer constant
    int const a = 5;

    //character constant
    char const yes = 'y';

Symbolic Constants

Symbolic Constant is a name that substitutes for a sequence of a characters or a numeric constant, a character constant or a string constant.

The syntax is as follow :

#define name text

where,

name implies symbolic name in a caps
text implies value or the text.

For Example :

    #define printf print
    #define MAX 100
    #define TRUE 1
    #define FALSE 0
    #define SIZE 0

The # character is used for preprocessor commands. A preprocessor is a system program, which comes into action prior to Compiler, ans it replaces the replacement text by the actual tet. This will allow correct use of the statement printf