### Constants in C language:

The literal values inserted in the code are called constants because we can’t change these values. In C language we have primary and secondary constants and C also allows to define our own constants. So we can say that C has infinite types of constants. In this session we will discourse only primary constants used in C language.

### Integer Constants:

The numbers with no fractional part are called integer constants. C accepts integer in three numbering systems that are decimal, octal and hexadecimal. When an integer has no prefix then C considers it as decimal integer, When it has 0 (zero) prefix then C considers it as Octal and when it has 0x (zero ex) as prefix then considers it as hexadecimal integer.

#### Rules in writing integer constants:

- No spaces and commas are allowed
- It can be +ve/-ve but by default it is +ve
- No fractional part

**Guess which are valid and invalid:**

1) 4567

2) 23,456

3) +5689

4) 34 67

5) -4567

6) 567.0

7) -045

8) 06702

9) 0x3E5

10) 03486

11) -0x3B2A

12) 045A8

1) valid

2) invalid (must not have commas)

3) valid (using + is optional)

4) invalid (no spaces are allowed)

5) valid

6) invalid (fractional part is not allowed)

7) invalid (-ve octal numbers are not allowed)

8) valid

9) valid

10) invalid (Octal number must have digits combination from 0 to 7 only)

11) invalid (-ve hexadecimals are not allowed)

12) invalid ( A hexadecimal constant must be prefixed with 0x)

### Integer numbering systems conversions:

We use int or long type of variable to store any integer of any numbering system. C understands as decimal, octal or hexadecimal according to the prefix we supply.

int x,y,z; x=456; /* treats it as decimal number*/ y=0456; /* treats it as octal number */ z=0x456; /*treats it as hexadecimal */

In C language printf() and scanf() are so intelligent so that they automatically convert from one system to another according to the formatting characters we use.

%o | to represent or convert to octal |

%x or %X | to represent or convert to hexadecimal. %x display in small case %X display in upper case |

%d | to represent or convert to decimal |

**Example 1:**

#include<stdio.h> int main() { int a=10; printf("%d\n%o\n%x\n%X",a,a,a,a); return 0; }

Output:

10

12

a

A

Example explained:

Decimal constant 10 is automatically converted into octal and hexadecimal constants

**Example 2:**

#include<stdio.h> int main() { printf("%d\n%d\n%d\n%d",10,12,0xa,0XA); return 0; }

Output:

10

12

10

10

Example explained:

octal and hexadecimal constants are automatically converted into decimal constants

**Example 3:**

#include<stdio.h> int main() { printf("%x\n%o",34.56,3.78);/*can't be done*/ return 0; }

Output:

147ae148

10020243656

Example explained:

Real constants can’t be converted into octal and hexadecimal

**Example 4:**

#include<stdio.h> int main() { printf("%x\n%o",078,0x2G);/* Invalid numbers */ return 0; }

Output:

demo3.c: In function ‘main’:

demo3.c:4:17: error: invalid digit “8” in octal constant

printf(“%x\n%o”,078,0x2G);

^

demo3.c:4:21: error: invalid suffix “G” on integer constant

printf(“%x\n%o”,078,0x2G);

^

Example explained:

The above program doesn’t compile because 078 is invalid octal and 0x2G is invalid hexadecimal number

**Example 5:**

#include<stdio.h> int main() { int n; printf("Enter any octal number:"); scanf("%o",&n); printf("Decimal equal: %d",n); printf("\nHexadecimal equal: %X",n); return 0; }

Execution1:

Enter any octal number:12

Decimal equal: 10

Hexadecimal equal: A

Execution2:

Enter any octal number:0x7A

Decimal equal: 0

Hexadecimal equal: 0

In first execution octal number is converted into equal decimal and hexadecimal numbers. But during the second execution, produced invalid output because given input is invalid octal number

**Specification:**

Accept any decimal number and print their equal octal and hexadecimal numbers

Program:

#include<stdio.h> int main() { int n; printf("Enter any decimal number:"); scanf("%d",&n); printf("Octal %o",n); printf("\nHexadecimal %X",n); return 0; }

Execution:

Enter any decimal number:9567

Octal 22537

Hexadecimal 255F

### Numbering systems in arithmetic expression:

In an arithmetic expression integer constants may be in any numbering system, converts implicitly into decimal equivalents before participating in arithmetic operations

#include<stdio.h> int main() { int a; a=015 + 0x71 +5; printf("%d",a); return 0; }

Output:

131

Here the sum of 015(13), 0x71(113) and 5 is 131

#include<stdio.h> int main() { int a,b,c,s; a=015; /* assigns 13 */ b=0x71; /* assigns 113 */ c=5; s=a+b+c; printf("%d",s); return 0; }

Output:

131

We may assign integer constants in any numbering system but, stores their decimal equivalents

We will see both real and single character constants in the next session