-5

So i am writing a program that takes in an int, and returns something to do with the bits that represent the int. However, when I put an int in the command line and immediately print it out it, it prints 2.

Why is this happening, and how can I fix it?

~ ./a.out 8

I can only take in one parameter for main:

#include<iostream>
#include<cstdlib>
using namespace std;

void foo (int ii, int x){
   static int p;
   k= x<<ii;
   .
   .

}

int main(int x) {
    cout << x << endl;
.
.
.
return 0
}
who
  • 1
  • 1
  • 6
    Where is your code? – Yu Hao Feb 14 '15 at 13:06
  • 1
    I have a hunch what's happening here, but I need to see your code to confirm it. – Wintermute Feb 14 '15 at 13:06
  • C or C++? Writing a multi-language source file is hard work! – pmg Feb 14 '15 at 13:06
  • Congratulations, you've written a program that outputs `2`. Now explain to us why this was undesirable. –  Feb 14 '15 at 13:07
  • 4
    `int main(int argc, char **argv) { printf("%d\n", argc); return 0; }` – pmg Feb 14 '15 at 13:08
  • This is pretty serious. The only way to fix it is by learning some C/C++. Try consulting a textbook, or one of the many, many online introductions which you should have consulted before coming here. – Beta Feb 14 '15 at 13:11
  • [What should main() return in C and C++?](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) - For a comprehensive breakdown of why `int main(int x)` is wrong. –  Feb 14 '15 at 13:11

1 Answers1

3

Parameter passing does not work the way you appear to believe it does.

There are precisely two signatures for main that are guaranteed to work:

int main()

and

int main(int argc, char *argv[])

The latter is the one you use when you want to parse command line arguments. Here argc is the argument count, i.e. the number of passed arguments (including the name of the command itself) and the length of argv. argv is an array of C-style strings that holds the arguments.

So, when you call

./a.out 8

then argc is 2, argv[0] is "./a.out", argv[1] is "8", and argv[2] is a null pointer. If you want to use the first command line argument as a number, you have to parse argv[1] with std::istringstream or atoi or similar, as in

#include <iostream>
#include <sstream>

int main(int argc, char *argv[]) {
  if(argc < 2) {
    std::cerr << "No argument was given.\n";
    return -1;
  }

  std::istringstream parser(argv[1]);
  int x;

  if(parser >> x) {
    std::cout << x << " * 2 = " << x * 2 << '\n';
  } else {
    std::cerr << "The argument was not a number.\n";
    return -2;
  }
}

As a side note, because this is not something you can depend upon, this also explains why you get the output 2. When you write

int main(int x) {

x takes the value of argc, and what would have been argv is ignored, for low-level reasons that have to do with the way the arguments are laid out in memory before the jump to main. When you call the program with ./a.out 8, that value is 2, and so x is 2 in your code. This is not guaranteed to work by the C++ standard, but it is likely to happen on all platforms as it did on yours.

Wintermute
  • 42,983
  • 5
  • 77
  • 80