C: command-line argument as binary not identical to local variable as binary

384 views Asked by At

I have c program that takes in a long long argument. When i try to convert this long to its binary representation then it turns out all wrong. But if i set a long long variable that is equal to the value of the long long argument then i get the correct binary representation.

  int main(int argv, long long value)
{
        long long i = value; // value = 2
        long long e = 2;
        printBits(sizeof(e), &e); // 0000000000000000000000000000000000000000000000000000000000000010
        printBits(sizeof(i), &i); // 0000000000000000011111111111110000010000111010101110101000001000
        return 0;
}

So as you can see above the argument retrieved through the console as input is no where near to what it should be.

printBits() is a copy paste from this solution.

Can someone clearify what is happening?

2

There are 2 answers

1
Sourav Ghosh On

The signature of main() in your code

 int main(int argv, long long value)

is invalid and wrong for hosted environments. The second argument is supposed to be of type char **. By defining the argc equivalent as long long, you're trying to receive a char ** type into a long long type, which causes undefined behavior, as they are not compatible types, anyway.

To put it in other words, the supplied command line arguments, are passed as pointer to strings. You need to apply proper conversion to get them back to long long or any other compatible type. strtoll() is just made for that purpose.

Quoting C11, chapter §5.1.2.2.1,

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;10) or in some other implementation-defined manner.

and, paragraph 2,

— If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.

— If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.

3
Max On

The signature for main is

int main(int argc, char** argv)

Unfortunately C compilers can be pretty permissive, so what is likely happening is that your pointer to char* is being silently cast to long long, resulting in nonsense.

You need to actually parse the argument e.g. using strtoll.

Working with C without the -Wall compiler flag is asking for trouble. With all warnings enabled you would have found the issue immediately:

warning: second argument of ‘main’ should be ‘char **’ [-Wmain]
int main (int argc, long long value)
    ^~~~