# Working with octal values in C programming language

Octal value has 8 digit values from 0 to 7, with the base 8. (Read more about Computer number systems), here we will learn how to work with octal values in c programming language?

## Representation of octal numbers in C programming

In C programming language, an octal number is represented by preceding with "0", thus the value in octal can be written as "0123" (which is equivalent to 83 in Decimal).

### Assigning the octal number in a variable

There is no special type of data type to store octal values in C programming, octal number is an integer value and you can store it in the integral type of data types (char, short or int).

Let suppose, we have a value in octal "0123" (83 in Decimal).

We are storing "0123" in an unsigned char variable (83 is small value and can be stored with in a Byte).

Consider the following statements

`unsigned char a=0123;`

### Printing the number in octal format

To print integer number in octal format, "%o" is used as format specifier in printf() statement.

Consider the code, which is printing the value of a...

```#include <stdio.h>

int main()
{
unsigned char a=0123;

printf("value of a (in octal)  : %o\n", a);
printf("value of a (in decimal): %d\n", a);

return 0;
}
```

Output

```    value of a (in octal)  : 123
value of a (in decimal): 83
```

### Reading value in octal format

"%o" can be used with scanf() statement to read the value in octal format from the user.

Consider the following code

```#include <stdio.h>

int main()
{
unsigned int num;

printf("Input value in octal: ");
scanf("%o", &num);

printf("Entered value is in octal format  : %o\n", num);
printf("Entered value is in decimal format: %d\n", num);

return 0;
}
```

Output

```    Input value in octal: 12345
Entered value is in octal format  : 12345
Entered value is in decimal format: 5349
```

### Declaring integer array by assigning octal values

Consider the following example, where integer array is declaring with the octal values and printing in both formats Decimal and Octal.

```#include <stdio.h>

int main()
{
int arr[]={ 0100, 0101, 0123, 0761, 10};
int i;

printf("Array elements are\n");
for(i=0;i<5;i++)
printf("Decimal: %d, Octal: %o\n",arr[i],arr[i]);

return 0;
}
```

Output

```    Array elements are
Decimal: 64, Octal: 100
Decimal: 65, Octal: 101
Decimal: 83, Octal: 123
Decimal: 497, Octal: 761
Decimal: 10, Octal: 12
```

Read more...

TOP Interview Coding Problems/Challenges

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.