-4

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?

Nulle
  • 1,301
  • 13
  • 28
  • 5
    That's not really how command-line arguments works in C. The second argument to `main` is an *array of strings*. – Some programmer dude Sep 15 '17 at 12:54
  • 1
    What makes you think `value` = 2? Did you try to print `value` from in your program? – Scott Hunter Sep 15 '17 at 12:54
  • https://stackoverflow.com/questions/2108192/what-are-the-valid-signatures-for-cs-main-function/ – dbrank0 Sep 15 '17 at 12:56
  • 3
    @Someprogrammerdude: .... an *array of **pointer to `char`***. – alk Sep 15 '17 at 12:56
  • 1
    @alk ... If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, – Sourav Ghosh Sep 15 '17 at 12:57
  • 1
    Not sure why people are downvoting this. It's a perfectly valid question from someone who doesn't understand how command line arguments work in C. – Max Sep 15 '17 at 12:58
  • @Max Probably because just abut any [beginners book or tutorial](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) should have told the OP about this? – Some programmer dude Sep 15 '17 at 13:00
  • Thank you Max. I was thinking exactly the same. This might be very trivial to someone who is experienced with C. But for us who has just begun how should we know that the arguments to the main function is apparently a locked case. But this is to be expected of stackoverflow in my experience. – Nulle Sep 15 '17 at 13:01
  • @alk Ahh, I missed Joachim's comment, i took that you're pointing out my answer, NVM. – Sourav Ghosh Sep 15 '17 at 13:02
  • 1
    @alk A pointer to pointer to `char`. – too honest for this site Sep 15 '17 at 13:03
  • I am actually voting to close this because you're not telling what are the *expected* results, you're just asking "what's happening" (and the answer is undefined behaviour). – Antti Haapala -- Слава Україні Sep 15 '17 at 13:03
  • @Max we are not a tutoring site. This shows no research at all. Passing commandline arguments is explained in every beginners' C book. – too honest for this site Sep 15 '17 at 13:04
  • I assure you i spend alot of time researching this before posting here. fact is stackoverflow is apparently not the place to be if you are a beginnner. – Nulle Sep 15 '17 at 13:10

2 Answers2

3

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)
    ^~~~
Max
  • 21,123
  • 5
  • 49
  • 71
  • Okay thank you very much. I wasn't aware that the arguments to the main function was a locked case. So I take it i need to convert from string to long long for this to work. – Nulle Sep 15 '17 at 13:06
  • But why does the gcc compiler not error if what i am doing is not valid? – Nulle Sep 15 '17 at 13:23
  • Generally C compilers only error if there is no way they can compile the code. There are (rare) cases where casting between different types is perfectly valid, so in this case it gives you the benefit of the doubt and just prints a warning. – Max Sep 15 '17 at 13:35
0

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.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261