C programming variables and constants

In this tutorial, you will get familiar with c programming variables and constants.

c programming variables and constants

C Programming Variables


Actually variable is the location of a place in computer’s memory where the data is stored. To put it up simply, we can say a variable is a box in which we can store data.

Each variable is given a unique name called Identifier. So variables like num1, num2 and others actually correspond to a location in computer memory.

For example:

int num1;

Here num1 is a variable of integer type.

It is called variable because it can hold different values at a different time.

For example, let’s suppose we assign value 3 to num1 then the memory location with name num1 will contain 3 and as soon we assign another value 5 to num1 the value at memory location num1 will be overwritten by 5.

Rules for Naming Variables

  • The first letter of a variable name must be alphabet (underscore is also allowed).
  • Variable can only contain letters, digits, and underscores.
  • White space is not allowed.
  • Keywords cannot be used as an identifier to name variables.
  • Since C is case sensitive language variable name written in uppercase will differ from lowercase.

Difference between variable and constant


Difference between variable and constant

C Programming Constants


Constants are the expressions with fixed values that do not change during the execution of the program. To put it up simply constant means whose value cannot be changed.

Following are the different types of constants we can use in C.

[adsense1]

1. Integer Constants


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: ‘A’, ‘f’, ‘i’ etc.

For example: ‘A’, ‘f’, ‘i’ 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 b = 100;

This signifies that b will have a constant integer value of 100. 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: “Hello”

6. Enumerated Constant


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

Syntax

enum type_name{ var1, var2, var3 };

Here var1, var2 and var3 are the values of enumerated datatype type_name.

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

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

Visit C programming operators to learn about different operators used in C.

For example:

enum COLOR {RED = 5, GREEN, WHITE = 8};

This code sets red to 5 and white to 8.

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