C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

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;


    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;


    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");
		printf("Decimal: %d, Octal: %o\n",arr[i],arr[i]);
	return 0;


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

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.