2

I cant figure out why either of the following code fails to operate as expected. There both compiled into execution files.

Outputs:

a.out  , prints 1, expected "no value"
a.out 1, prints 2, expected 1 
a.out 2, prints 2, expected 2

Using a case:

void main(int in)
{
 int a = in ;
 printf("In function if\n");
 if ( in == 1 )
   printf("1\n");
 else
   if ( in == 2)
     printf("2\n");
   else
     printf("wrong value\n");
}

Using a switch:

void main(int in)
{
  switch( in )
    {
    case  1: printf("1\n");                 break;
    case  2: printf("2\n");                 break;
    default: printf("wrong value\n"); break;
    }
};

I'm trying to get the following LISP functionality in C code:

(cond ((= in 1) 1)
      ((= in 2) 2)
      (t        nil))

Thank you for your assistance.

Dan
  • 55
  • 1
  • 4
  • 2
    `void main(int in)` is illegal in C. I don't know about Lisp, what are you trying to achieve? Specifically, you expect `in` to come from command line argument, or standard input? – Yu Hao Nov 21 '14 at 05:57
  • I'm trying to get either a case statement to behave as expected. – Dan Nov 21 '14 at 06:04
  • I seem to be getting the wrong answers from a case and an if statement. I replaced void with int in my code and there was no change. I'm not expecting return values just for it to print the correct line for the functioning input. This is just test code to see if i can get it working – Dan Nov 21 '14 at 06:12

2 Answers2

7

main doesn't accept the input from the command line as direct arguments, you are getting the argument count in there which is 1 if there are no arguments, and 2 if there is one argument, which causes the strange behavior.

main should be defined as int main( int argc, char *argv[] ) or something similar. To get the input, you need to first check if it exists by testing argc (the argument count, plus one for the executable path), and then converting argv[1] to an integer. atoi can be used to convert a string to an integer.

mukunda
  • 2,908
  • 15
  • 21
2

Note that the first parameter to main is argc, the total count of app name + parameters given on the command line. There is a second argument on main, char *argv[] for receiving the command line parameters.

The reason you get the behaviour described is because your parameter in replaces the purpose of argc. i.e. when you execute the App with no command line params, the argc count is 1, with one param, it will be 2, etc - this is the value passed to in. Since you don't have a second main parameter for the command line parameters (argv), you won't receive the actual parameters at all.

To fix this, you'll need to parse the second args array for your in integer parameter:

#include <stdlib.h>

int main(int argc, char *argv[]) {
    if (argc < 2)
       return EXIT_FAILURE; // ... print out the correct command line usage to user

    char *endPointer = NULL;
    long in;

    in = strtol(argv[1], &endPointer, 10);

    if (endPointer != NULL) {
      switch( in ) {
         case  1: 
            // ... same code as above
        }
       return EXIT_SUCCESS;
    }
    return EXIT_FAILURE; // User hasn't provided a number for 1st param
}

Ide One Sample Here

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285