LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-20-2004, 09:33 PM   #1
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

Rep: Reputation: 30
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:

Code:
#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) {
    printf("Same\n");
  } else {
    printf("Different!\n");
  }
                                                                                                         
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

Last edited by AMMullan; 03-21-2004 at 12:48 AM.
 
Old 03-21-2004, 10:55 PM   #2
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 417

Rep: Reputation: 31
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?
 
Old 03-21-2004, 11:22 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
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
 
Old 03-21-2004, 11:35 PM   #4
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

Original Poster
Rep: Reputation: 30
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...

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

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

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

$ echo 1,1 | ./a
Same

$echo 1,2 | ./a
Different!


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...

Last edited by AMMullan; 03-21-2004 at 11:48 PM.
 
Old 03-21-2004, 11:46 PM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
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.

Code:
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.
 
Old 03-21-2004, 11:55 PM   #6
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

Original Poster
Rep: Reputation: 30
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
 
Old 03-22-2004, 01:16 AM   #7
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 417

Rep: Reputation: 31
This program takes two args, or no args...
Code:
$ ./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.
Code:
#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[])
{
        if(argc>1)
                num_test(atoi(argv[1]),atoi(argv[2]), FETCH_CMDLINE);
        else
                num_test(0,0,FETCH_STDIN);

        return 0;
}


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

{
        int same, aodd, bodd;

        if(mode==FETCH_STDIN)
        {

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

        if(a==b)
                {same=1;}
         else
                {same=0;}

         if(a%2)
                {aodd=1;}
         else
                {aodd=0;}

         if(b%2)
                {bodd=1;}
         else
                {bodd=0;}

        printf("\nNumbers are ");
        if(same)
                printf("same.\n");
        else
                printf("different.\n");

        printf("First number is ");
        if(aodd)
                printf("odd.\n");
        else
                printf("even.\n");

        printf("Second number is ");
        if(bodd)
                printf("odd.\n");
        else
                printf("even.\n");
}
Hopefully this helps...

Last edited by jinksys; 03-22-2004 at 01:18 AM.
 
Old 03-22-2004, 01:30 AM   #8
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
heh, yeah that's the more reasonable way to do it. What I get for blindly sticking to the "spec".
 
Old 03-22-2004, 01:31 AM   #9
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
On an unrelated note, what's the difference between __inline__ and inline?
 
Old 03-22-2004, 01:37 AM   #10
jinksys
Member
 
Registered: Aug 2003
Location: 63123
Distribution: OpenSuSE/Ubuntu
Posts: 417

Rep: Reputation: 31
GCC will accept include or __inline__, with the latter being ISO C compliant.

You can read up on it here, http://gcc.gnu.org/onlinedocs/gcc/Inline.html
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing arguments to a shell script subu_s Programming 3 09-02-2005 05:13 AM
Problem with passing arguments in Perl bahadur Programming 1 05-30-2005 01:47 AM
passing a list of arguments to a command hdagelic Linux - General 2 05-09-2005 09:30 AM
Passing Arguments into the Thread Function George_gk Programming 2 01-31-2005 05:03 AM
Issue with recursive script passing arguments gauge73 Linux - Newbie 0 01-06-2004 07:16 PM


All times are GMT -5. The time now is 01:35 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration