GET EDUCATE

Contents
Basics of C Programming

⯈Basic C Programs

Constants in C

A constant is something whose value doesn’t change during the execution of the program. 

There are two types of constants at the highest level. They are:

  1. Numeric constants.
  2. Character constants.

Types of constants in C

Constants in c

Numeric Constants

These constants contain only numeric values. There are two types of numeric constants, they are:

  1. Integer constants
  2. Real constants

Integer constants

Integer constants are the one which don’t contain the decimal point.

Ex: 2, +3, -4, etc. 

There are three types of integer constants.

  1. Decimal integer constant.
  2. Octal integer constant.
  3. Hexa-decimal integer constant.

We will discuss each of these in the coming sections.

Decimal integer constants

Decimal integer constants are the numbers which have base – 10. Numbers from 0 to 9 can be used to form any number. The number may be positive or negative.

Ex: +123, -89, 45

Octal integer constants

Octal integer constants are the numbers which have base – 8 system, it means we can use number from 0 to 7. The numbers may be positive or negative.

If we write the number, 45, the number can be both decimal as well as octal. This is ambiguity. To resolve this, octal numbers begin with 0. 

Therefore 45 is decimal, whereas 045 is octal.

Ex: 022, -017

Note: 0 is octal in C programming language but is decimal in Java.

The following are invalid octal constants.

  1. 018 (contains 8, octal numbers are formed from 0 – 7 only)
  2. 15 (don’t start with 0)

Hexa decimal integer constants

Hexa decimal constants have the base 16. 16 different values can be used. It uses the numbers from 0-9 and A-F. Hexa-decimal numbers may be positive or negative.

A – 10 ,  B – 11,  C – 12, D-13, E-14, F-15

The alphabets A-F can either be written in upper case or lower case.

If we write the number 45, this number can be decimal or hexa-decimal. This is case of ambiguity. To overcome this hexa-decimal numbers, begin with 0x. 

Therefore, 45 is decimal whereas 0x45 is hexa-decimal.

Ex: 0x00, 0x12EA, 0x45, -0x46

Real Constants

Real constants are one which have decimal point. Real constants can be positive or negative.

Ex: 3.14, 9.24, -8.4, 2.0

Note:

  1. 2 is integer, whereas 2.0 is real constant.
  2. we can write 2.0 as 2. (note that after decimal point, we didn’t write anything)

Real constants can also be written in scientific notation.

Ex:

  1. 123.45 in scientific notation in mathematics is written as, 1.2345*10². In C programming we write it as, 1.2345E2. Here E represents 10 to the power.
  2. 0.0034 in scientific notation in mathematics is written as,  3.4*10-3  . In C programming we write it as, 3.4E-3. Here E represents 10 to the power.
  3. -53.5 can be written as as -5.3E1

Character constants

Character constants are made up of alphabets, digits and special characters. There are two types of character constants.

  1. Single character constants.
  2. String constants.

Single character constants

Anything written in single quotes and is of single in length is called as single character constants.

Ex: ‘a’, ‘Q’, ‘4’, ‘%’

The following are not single characters constants.

  1. a (not written in single quotations)
  2. ‘ab’ (Even though written in single quotes, but is not of single in length)

String constants

Anything written in double quotes becomes a string. The formal definition of string is an array of characters terminated with NULL.

Ex: “BHARAT”, “INDIA”, “123”, “A1”, “123@abc”

To Summarize

If we write,

4  — It is an integer constant.

4 .0 — It is a real constant. (has decimal point)

‘4’  — It is a single character constant. (written in single quote)

“4”  — It is a string constant.

Scroll to Top