Home » C programming language

How to assign binary value in a variable directly?

We have assigned Hexadecimal, Decimal, and Octal values in a variables in our many previous solutions.

Decimal value can be assigned directly (int a=100 ;), Octal value can be assigned by using "0" notation (int b=0144 ;) and Hexadecimal value can be assigned by using "0X" or "0x" notation (int c=0x64 ;).

First consider the following example, which is assigning Decimal, Octal and Hexadecimal values in variable and printing their values in Decimal format.

#include <stdio.h>
int main()
	int a=100;
	int b=0144;
	int c=0x64;
	printf("a= %d\n",a);
	printf("b= %d\n",b);
	printf("c= %d\n",c);
	return 0;


    a= 100
    b= 100
    c= 100

The values of a, b and c are 100 because, 0144 is the octal value of 100 and 0x64 is the Hexadecimal value of 100.

How to assign Binary value in a variable?

Binary value can be assigned in a variable by using "0b" notation (we can say it format specifier too), this is a new feature which was introduced in C99 (not a standard feature, some compilers may not support this feature).

Let’s consider the following program

#include <stdio.h>
int main()
	int num=0b1010;
	printf("Num: %d\n",num);
	return 0;


    Num: 10

Remember: Read this post before using 0b notation to represent binary value

assign binary toa variable

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions

We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.