Variables and constant in C programming

In this tutorial we will learn about variable and constant in C programming. To better understand this topic you must have knowledge on keywords and identifier.  

fig: varailbe and constant

Variables

Variables are the storage box. We can take it as a container to hold the data program needs. 

Variables are the unique (Identifiers) name. Each variable represents the address in memory. 

int number = 99;

Here number is a variable of Integer type. The variable is assigned int value of 99.

Rules for Naming Variable

  • Variable name must start with letter or underscore ( _ ).
  • Variables are case sensitive i.e uppercase variable name will differ from lowercase.
  • They can only contain digits, letters and underscore ( _ ) .
  • Keywords cannot be used as variable name.

Initializing Variable

  • Variables should be declared before use.
  • Memory space is not allocated for a variable while declaration. It happens only on variable definition.

Types of Variables in C

1. Local Variable

The scope of local variables will be within the function only. Variables are declared within the function and can’t be accessed outside the function.

#include<stdio.h>
void test();
 
int main()
{
   int m = 22, n = 44;
   printf("value of  m = %d and n = %d", m, n);
   test();
}
 
void test()
{
   int a = 50, b = 80;
   printf("value of a = %d and b = %d", a, b);
}

Output

value of m = 22 and n = 44
value of a = 50 and b = 80

In above example,  m, n are local variables of main function. These are not visible to test function. If you try to access a and b in this function,  you will get ‘a’ undeclared and ‘b’ undeclared error

2. Global Variable

The scope of global variables will be throughout the program. These variables can be accessed from anywhere in the program.

#include<stdio.h>
void test();
int m = 22, n = 44;
int a = 50, b = 80;
 
int main()
{
   printf("All variables are accessed from main function");
   printf("\nvalues: m=%d:n=%d:a=%d:b=%d", m,n,a,b);
   test();
}
 
void test()
{
   printf("\n\nAll variables are accessed from" \
   " test function");
   printf("\nvalues: m=%d:n=%d:a=%d:b=%d", m,n,a,b);
} 

Output

All variables are accessed from main function
values : m = 22 : n = 44 : a = 50 : b = 80
All variables are accessed from test function
values : m = 22 : n = 44 : a = 50 : b = 80

3. Environmental Variables

Environment variable is a variable that will be available for all C  applications and C programs. We can access these variables from anywhere in a C program without declaring and initializing in an application or C program.

Function used to access, modify and assign an environmental variable.

  1.  setenv()
  2.  getenv()
  3.  putenv()

1. setenv()

 sets the value for environment variable.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
setenv("FILE", "/usr/bin/test.c",50);
printf("File = %s\n", getenv("FILE"));
return 0;
}

Output

File = /usr/bin/test.c

2. getenv()

gets the current value of the environment variable.

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
printf("env = ",getenv("DIR"));
return 0;
}

Output

usr/bin/live/

3. putenv()

modifies the value for environment variable. 

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
setenv("DIR", "/usr/bin/example/",50);
printf("Directory name before modifying = " \
"%s\n", getenv("DIR"));
putenv("DIR=/usr/home/");
printf("Directory name after modifying = " \
"%s\n", getenv("DIR"));
return 0;
}

Output

Directory name before modifying = /usr/bin/example/
Directory name after modifying = /usr/home/

Constants

Constants are the fixed values in the program whose values remain same throughout the program. There are different types of constant.

Types of constant in C

1. Integer Constant

Integer constants refer to the sequence of digits with no decimal points. The three kinds of integer constants are:

  • Decimals constant: Decimal numbers are set of digits from 0 to 9 with +ve or -ve sign. For example: 11, -11 etc.
  • Octal constant: Octal numbers are any combination of digits from set 0 to 7 with leading 0. For example: 0213, 0123 etc.
  • Hexadecimal constant: Hexadecimal numbers are the sequence of digits preceding 0x or 0X. For example: 0xBD23, OX543 etc.

Rules for constructing Integer Constants

  • Name of an integer constant must have at least one digit.
  • Comma or blanks are not allowed in the name of an integer constant.
  • It should not have a decimal point.

2. Real or Floating point Constants

They are numeric constants with decimal points. The real constants are further categorized as:

  • Fractional Real constant: These are the set of digits from 0 to 9 with decimal points. For example: 123.3, 0.765 etc.
  • Exponential Real constant: In the exponential form, the constants are represented in two form:

Mantissa E exponent: where the Mantissa is either integer or real constant but the exponent is always in integer form.

For example, 12345.67 = 1.234567 E4, where E4 = 104.

Rules for constructing Real or Floating point Constants

  • Name of a real constant must have at least one digit.
  • Comma or blanks are not allowed in the name of a real constant.
  • It should have a decimal point.

3. Character Constant

Character constants are the set of alphabet enclosed in single quotes. For example: ‘B’, ‘g’, ‘k’ etc.

Rules for constructing Character Constant

  • The maximum length of a character constant can be one character.

4. Declared Constant

As same as declaring a variable using the prefix const we can create new constants whose values can’t be altered once defined.

For example:

const int i = 5;

This signifies that i will have a constant integer value of 5. Not only integer, by using prefix we can declare character constant and string constant as well.

5. String Constant

String constants are the sequence of characters enclosed in a pair of double quotes (” “).

For example: “C programming”

6. Enumereated Constant

Enumerated constant creates set of constants with a single line using keyword enum.

Syntax

enum type_name{ var1, var2, var3 };

Here var1var2 and var3 are the values of enumerated datatype type_name.

By default the var1var2 and var3 will have values 0, 1 and 2.

By using assignment operators, we can assign any value to var1var2 and var3.

For example:

enum COLOR {YELLOW= 6, GREEN, WHITE = 8};

This code sets yellow to 6 and white to 8.

The member without assigned value will possess the 1 higher value than the previous one. In this case, greenwill be set to 7.

Difference Between Variables and Constants.

VariablesConstants
value of variable can be change during program executionvalue of the constant remains same throughout the execution of program
Variable can be declared many times in a program. It can happen only one time for a variable in a program.