Variables and Constants in C Programming

Variables and Constants in C Programming

In this tutorial, you are going to learn about Variables and Constants in C Programming.

Variables

  • It is often referred to as a named memory location. This implies it is the name of a memory location and is used to store some data.
  • The value or data stored can be changed, and can be reused any number of times.
  • The syntax to declare a variable is: <data type> <variable name> = <value>
  • Some examples to make the concept of declaring more clear: int a=50;

Rules for defining variables

  • A variable can be an alphabet, digit or an underscore (_) sign.
  • A variable name cannot start with a digit. It can only start with an alphabet or an underscore (_) sign.
  • A variable name should not contain any white spaces in between.
  • A variable name should never be same as any keyword, e.g. double, int, float, etc.
  • Some examples of valid variable names: abc, ab_1, pro_33, etc.
  • Some examples of invalid variable names: 1ab, abc d, double, etc.

Types of Variables in C

  1. Local Variable: Those variables which are declared inside the function or block are called local variables. These variables must be declared at the start of any block and must be initialized before being used.
  2. Global Variable: Those variables which are declared outside the function or block are called global variables. These variables are accessible for all functions i.e. any function can change the value of global variable and must be declared at the start of the block.
  3. Static Variable: Those variables which are declared using the static keyword are called static variables. These variables retain their value between multiple function calls.
  4. Automatic Variable: All the variables in C that are declared inside the block are automatic variables by default. An automatic variable can be explicitly declared using the auto keyword.
  5. External Variable: An external variable is used to share a variable in multiple C source files and can be declared using external keyword.

Constants

  • A value or variable that cannot be changed in the program are called constants.
  • Examples are- 5, 9,’c’,” Code”, etc.
  • There are many types of constants like integer, float, octal, hexadecimal, character constants, etc.
  • C constants can be divided into two major categories: Primary Constants and Secondary Constants.

Integer Constants

These refer to the sequence of digits with no decimals. They are of three types:

  • Decimal constant: Decimal numbers are set of digits from 0 to 9 with +ve or –ve sign. Example: 99, -99.
  • Octal constant: Octal numbers are any combination of digits from set 0 to 7 with leading 0. Example: 0546, 0342.
  • Hexadecimal constant: Hexadecimal numbers are the sequence of digits preceding 0x or 0X. Example: 0xBD23, 0X632.

Rules for Integer Constants:

  • The name of an integer constant must contain at least a digit.
  • The name should not contain any comma or blanks.
  • The name should not have a decimal point.

Real or Floating Point Constants

They are numeric constants with decimal points. These are further classified as:

  • Fractional Real constant: These are the set of digits from 0 to 9 with decimal points. Example: 160.2, 0.543.
  • Exponential Real Constant: In the exponential form the constants are represented in the form of Mantissa E exponent. Example: 12345.67=1.234567 E4, where E4=10^4.

Rules for Real or Floating point Constants:

  • The name of an integer constant must contain at least a digit.
  • The name should not contain any comma or blanks.
  • The name should have a decimal point.

Character constant

These are the set of alphabets enclosed in single quotes. For example: ‘A’,’I’, etc.

Rules for Character Constants: The maximum length of a character constant can be one character.

Declared constant

  • Creation of new constants whose values cannot be altered if once defined.
  • Example: const int b=100;
  • This signifies that b will have a constant integer value of 100.


String constant

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

Difference between Variables and Constants

Constants Variable
A value that cannot be altered throughout the program. A named memory location used to store a value.
It cannot be modified by the program once defined. A storage area holds data.
Cannot be changed. Can be changed according to the need.
Its value is fixed. Its value varies.


This post on Variables and Constants in C Programming is written by Divyanshu Shekhar (BTech CS, Chandigarh Engineering College). If you like TheCode11, then do follow us on Facebook, Twitter and Instagram.

Previous Post Next Post

Contact Form