user241221
user241221

Reputation: 399

If-Statement in C memory address

Well,l I am sorry. I think this question has already been answered somewhere else, but I could not find a solution for this. I have a variable of type int and an if-statement in C and I don't know what this statement checks.

int a;
if(a==0x6634522)
{
  //do some stuff
}

I am really sorry if this is already answered somewhere else..

Upvotes: 1

Views: 615

Answers (4)

haccks
haccks

Reputation: 105992

a==0x6634522 checks whether a is equivalent to the hexadecimal 0x6634522 ( (6634522)16 = (107169058)10) ) or not. It is checking the value stored in a, not the address of a (&a).

Upvotes: 4

Ned
Ned

Reputation: 957

The if statement is nothing more than a test to determine whether the uninitialized variable a has the hexadecimal integer value, 0x6634522. It is unlikely to ever be true. Your code sample is equivalent to:

int a;
if(a==107169058)
{
  //do some stuff
}

...because 0x6634522 in hexadecimal (base 16) notation is 107169058 in decimal (base 10) notation.

It is true that memory addresses assigned to pointer variables are typically assigned using hexadecimal numbers, but that is a reflection of the historical convention adopted because memory address spaces have almost always ranged over addresses which were a power of two and hexadecimal notation provides a more compact representation. But there is no requirement in C to use decimal, hexadecimal, or even octal number literals for number or pointer values.

Upvotes: 3

HAL9000
HAL9000

Reputation: 3751

If a equals to 107169058 it does some stuff, but since a has not been initialized it will unlikely do any stuff.

Upvotes: 1

unxnut
unxnut

Reputation: 8839

It is just checking if a is the same as hexadecimal number 6634522. Whenever you put 0x in front of an integer constant, C interprets it as a hexadecimal number.

Upvotes: 3

Related Questions