-   Programming (
-   -   Handline passing arguments in C (

AMMullan 03-20-2004 09:33 PM

Handling passing arguments in C
Hey all :-)

I''m playing round with a few things at the moment and want to know how the following could be done:


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
typedef enum boolean {FALSE, TRUE} boolean;
boolean test_values(int a, int b) {
  return((boolean) (a==b));
int main(int argc, char *argv[]) {
  boolean equal;
  int x, y;
  printf("Argument #1: %s\n", argv[1]);
  printf("Enter 2 numbers (i.e. 2,4): ");
  scanf("%d,%d", &x, &y);
  equal = test_values(x, y);
  if(equal) {
  } else {
return 0;

Input: echo 1,1 | ./a
Output: Enter 2 numbers (i.e. 2,4): Same

What I want is just for the answer to be shown...

Does anyone know the best way for this to be done... Thanks in advance :)

jinksys 03-21-2004 10:55 PM

Judging by the 29+ people whove seen this thread and not posted. . .I can safely say no one knows what you are talking about....could you elaborate?

aluser 03-21-2004 11:22 PM

lol your judgement is impeccible. Count me among the 29.

AMMullan: could you tell us exactly what the program is supposed to do? e.g. an example of what the output *should* look like for a given input

AMMullan 03-21-2004 11:35 PM

Ok I'm sory - I thought I had done this (i.e the input output etc lol)...

K I have a program called a and this works out if numbers are even or odd...

$ ./a
Enter 2 numbers (i.e. 2,4): 1,2

$ ./a
Enter 2 numbers (i.e. 2,4): 1,1

What I want to be able to do is to use it like this:

$ echo 1,1 | ./a

$echo 1,2 | ./a

So now do you get it? (Sorry, even I know i should have put this right from the start - but I spose it happens to the best of us lol)...

Thanks guys

EDIT: I know it does work already but I wanted to cut out the Enter 2 numbers (i.e. 2,4): if it was used like this...

aluser 03-21-2004 11:46 PM

There's an easy way and a hard way :) The easy way is to just have a command line argument that says to turn off the prompt, e.g.


int main(int argc, char** argv)
    int prompt = 1;
    if (argc > 1 && strncmp(argv[1], "-q", 2) == 0)
        prompt = 0;

So now you can say "echo 1,1 | ./a -q" and it doesn't print the prompt. Ok, so that's a little ugly. If you want exactly the results you're looking for, you need to check if stdin comes from a terminal. There is probably a right way to do this which I don't know. The wrong way, which would work, is to use the readlink() function (see its man page) on /proc/$$/fd/0, where $$ is your pid which you can get with getpid(), and see if the link points toward something beginning with "/dev/pts/". If so, you're reading from a terminal so you should present the prompt.

Other systems might have different naming conventions, too.

I'm assuming you're on linux. If you're on *bsd you might have to do something special as root to even get /proc. If you're on something else, well, sorry :)

Oh yeah, another horribly ugly solution would be to select() on stdin for a short time, and if it fails to find waiting input assume you're reading from a terminal. That's reaaaly ugly though.

AMMullan 03-21-2004 11:55 PM

the prompt idea is a really good one... Might just create a getopt() function to handle this and add a help function :-)

Thanks 4 ya help :D

jinksys 03-22-2004 01:16 AM

This program takes two args, or no args...

$ ./prog 2 3

Numbers are different.
First number is even.
Second number is odd.

$ ./prog

Enter first number: 2
Enter second number: 5

Numbers are different.
First number is even.
Second number is odd.


#include <stdio.h>

#define FETCH_STDIN 0x00
#define FETCH_CMDLINE 0x11

__inline__ void num_test(int a, int b, unsigned int mode);

int main(int argc, char *argv[])
                num_test(atoi(argv[1]),atoi(argv[2]), FETCH_CMDLINE);

        return 0;

__inline__ void num_test(int a, int b, unsigned int mode)

        int same, aodd, bodd;


        printf("\nEnter first number: ");
        scanf("%i", &a);
        printf("Enter second number: ");
        scanf("%i", &b);




        printf("\nNumbers are ");

        printf("First number is ");

        printf("Second number is ");

Hopefully this helps...

aluser 03-22-2004 01:30 AM

heh, yeah that's the more reasonable way to do it. What I get for blindly sticking to the "spec".

aluser 03-22-2004 01:31 AM

On an unrelated note, what's the difference between __inline__ and inline?

jinksys 03-22-2004 01:37 AM

GCC will accept include or __inline__, with the latter being ISO C compliant.

You can read up on it here,

All times are GMT -5. The time now is 05:17 AM.