Wednesday 29 November 2017

CONSTANTS in C Programming

  • C constants are like normal variables, but the only difference is their value can't be modified by the program once defined.
  • Constants have the fixed values.
  • The constants are also called as Literals.
  • Constants may be belonging to any of the datatypes.
  • There are a few types of constants;
    1. Numeric Constant.
    2. Non-numeric Constant.

1. Numeric Constant:

  • Numeric constant stands for a number.
  • The number can be integer, fraction etc.
  • Real numbers are also considered as the numeric constants.
  • Real constants are the combination of integer and fraction.

a. Integer Constant:

  • Integers are the natural numbers including zero.
  • Integer constants may be decimal, octal, hexadecimal constants. 
    • Decimal: {0,1,2,3,4,5,6,7,8,9}
    • Octal: {0,1,2,3,4,5,6,7,8,9}
    • Hexadecimal: {0,1,2,3,4,5,6,7,8,9, A, B,C,D,E,F}
  • Integers may be Signed Integer and Unsigned Integer.
  • Signed Integer refers to the -ve and +ve values including zero.
  • Unsigned Integer refers to the +ve values including zero.
  • The largest integer number that can be stored in a 16-bit computer is 215 - 1.
  • The largest integer number that can be stored in a 32-bit computer is 231 - 1.
  • The Octal constants are written with a leading zero.
    • Ex: 0234
  • The Hexadecimal constants are written in a leading 0x.
    • Ex: 0x49B

Rules for Integer Constant:

  1. An integer constant must have at least one digit.
  2. It must not have a decimal point.
  3. It can be either positive or negative. 
  4. If no sign precedes an integer constant, it is assumed to be positive.
  5. Commas or blanks are not allowed within an integer constant.

b. Floating-Point Constant:

  • A fraction is used for scientific notation or exponent form.
  • The floating point number has two parts.
    • One is a decimal part and another part is a fractional part.
    • While representing in fractional form, we have to include the decimal point.
    • And sometimes we have to use e or E.
  • Ex: 
    • 3.234
    • 3234E3
  • Both the examples have the same value but presented in two ways.

2. Non-numeric Constant:

  • Non-numeric constants are the constants except the number.
  • The non-numeric constants may be Character or String constants.

a. Character Constant:

  • Character constants are always written in the single quoted form.
    • Ex: 's'
  • Character constants can be a single character or an escape sequence or a universal character.
    • Plain Character: 'a', 'b', 'x' etc.
    • Escape Sequence: '\n', '\t', '\a' etc.
    • Universal Character: '\u02C0',

b. String Constant:

  • A collection of characters is known as a string.
  • These are written in double-quoted form.
  • Always a string constant is terminated with a special character called known as null character.
    • Ex: "Welcome"

Constant Defining:

There are two ways to define CONSTANTS in C Programming-
  1. Using const keyword.
  2. Using #define preprocessor.

a. Using const Keyword:

Syntax: const type variablename = value;
Example:

#include<stdio.h>

void main()
{
     const int op1 = 10;
     const int op2 = 15;
     int addition;
     addition = op1 + op2;
     printf("The addition is = %d", addition);
}

Output: The addition is = 25

b. Using #define Preprocessor:

Syntax: #define variablename value
  • We don't need to put a semicolon (;) for the termination.
Example:

#include<stdio.h>

#define op1 10
#define op2 15

void main()
{
     int addition;
     addition = op1 + op2;
     printf("The addition is = %d", addition);
}

Output: The addition is = 25



No comments:

Post a Comment