Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can integer variable be initialized with the binary number in C? [duplicate]

Tags:

c

We initialize octal by putting 0 as prefix and hexadecimal as 0x. How can we initialize an int variable in binary number? Is there any access specifier in C for binary number? e.g %o for octal and %x for hexadecimal number.

like image 730
Harsh Avatar asked Sep 03 '13 13:09

Harsh


People also ask

How do you initialize a variable in C?

Different ways of initializing a variable in Cint a, b; a = b = 10; int a, b = 10, c = 20; Method 5 (Dynamic Initialization : Value is being assigned to variable at run time.)

How do you assign a binary number to a variable?

To assign value in binary format to a variable, we use the 0b suffix. It tells the compiler that the value (suffixed with 0b) is a binary value and assigns it to the variable. Note: To print value in binary format, we use bin() function.

Is it possible to initialize a variable at the time it was declared in C?

INITIALIZING DATA VARIABLES AT DECLARATION TIME Unlike PASCAL, in C variables may be initialized with a value when they are declared.

Why do we initialize variables in C?

While declaring variables, it tells compilers the type of data it holds. Variables tell compilers the name of the variables that are being used in the program. As variables specify storage, compilers do not have to worry about the variables' memory location until they are declared.


1 Answers

Recent versions of GCC provide an extension to the C standard. Use 0b or 0B to prefix a bit series like:

int i = 0b0101010;
like image 109
alk Avatar answered Oct 02 '22 20:10

alk