I have this piece of code in C:
int x = 52706108;
if(argc >= 2){
int val = *argv[1];
int xor = x^val;
printf("The xor value between %d and %d is %d in decimal\n",x,val,xor);
}
I’m compiling it like this:
gcc -m32 -g -o a5_1 a5_1.c
Running it like this:
./a5_1 12
And this is my output:
The xor value between 52706108 and 49 is 52706061 in decimal
I can’t understand why I’m passing the parameter “12” but the machine is reading 49 instead.
That
49is the ASCII code point of the1in your string argument12. That’s becauseargvis an array ofcharpointers, each of which points to a C string containing the argument. So, it’s as if you’ve definedargv[1]to be{'1', '2', '\0').If you want to convert the argument to an integer, use something like:
or, preferably with error checking and to avoid undefined behaviour in the event the number is out of range:
Full example:
The output of that program (when given
12as an argument) is: