×

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

Octal Literals in C programming Language

Octal literals in C language: Here, we are going to learn about the octal literals in C language, how to use octal literals, how to assign octal literal to a variable etc?
Submitted by IncludeHelp, on February 11, 2019

Octal literals in C

Octal numbers are the technique to represent the numbers which use the base-8 number; it uses 7 digits 0, 1, 2, 3, 4, 5, 6, and 7. (Read: Computer number systems)

In C programming language, we can use octal literals in any expressions; we can assign octal numbers to the variables. To use octal literals, we use 0 (zero) as a prefix with the number. For example: 010 is an octal number, which is equivalent to 8 in the decimal number system.

Printing octal number in decimal format

To print an octal number in decimal format, we use %d format specifier.

Printing octal number in octal format

To print an octal number or other type of numbers, we use %o format specifier.

C Code to printing octal literal in decimal and octal format

#include <stdio.h>

int main(){

    //printing octal number in decimal format
    printf("%d\n", 010);
    //printing octal number in octal format 
    printf("%o\n", 010);
    //printing other format number in octal format
    printf("%o\n", 8);

    return 0;
}

Output

8
10
10

Using octal literals in the expressions

#include <stdio.h>
int main(){
	
    //adding two octal numbers
    printf("%d\n", (010+020));
	//adding octal, decimal numbers 
    printf("%d\n", (010+020+30+40));
    
    return 0;
}

Output

24
94

Assigning octal literals to the variables

#include <stdio.h>
int main(){
	
    int a = 010;
    int b = 076541;
    
    printf("a in octal: %o, and in decimal: %d\n", a, a);
    printf("b in octal: %o, and in decimal: %d\n", b, b);
    
    return 0;
}

Output

a in octal: 10, and in decimal: 8
b in octal: 76541, and in decimal: 32097

Input a number in Octal format

To input a number in an octal format, we use %o format specifier in the scanf() function

#include <stdio.h>
int main(){
	
    int a;
    
    printf("Enter value of a in octal: ");
    scanf("%o", &a);
    printf("a in octal: %o, and in decimal: %d\n", a, a);
    
    return 0;
}

Output

Enter value of a in octal: 73411
a in octal: 73411, and in decimal: 30473

Error while using an invalid digit with octal literal

If we use any other digits except 0-7, program returns a compilation error "invalid digit 'x' in octal constant"

#include <stdio.h>
int main(){
	
    int a = 010;
    int b = 0129; //9 is not a valid oct digit
    
    printf("a in octal: %o, and in decimal: %d\n", a, a);
    printf("b in octal: %o, and in decimal: %d\n", b, b);
    
    return 0;
}

Output

main.c: In function 'main':
main.c:5:13: error: invalid digit "9" in octal constant
     int b = 0129;
             ^

Read more...

C Language Tutorial »





Comments and Discussions!

Load comments ↻





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