Thread: how to ignore * function

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    13

    how to ignore * function

    i am creating a basic calculator program for a class and and stuck on the multiplication. when the third argument is * it goes all funky and give a segmentation fault. my only guess is that this is because in the shell * is a wild card.

    Code:
     
    #include<stdio.h>
    #include <stdlib.h>
    #include<string.h>
    int main(int argc, char *argv[]){
      int a;
      int b;
      int c;
    
      if  (argc  == 4 || (strcmp (argv[4], "dec") == 0 )){
    
      if (strcmp (argv[2], "+") == 0){
          a = atoi(argv[1]);
          b = atoi(argv[3]);
          c= a+b;
          printf("%d\n", c);}
    
      if (strcmp (argv[2], "-")==0){
        a = atoi(argv[1]);
        b = atoi(argv[3]);
        c= a-b;
        printf("%d\n", c);}
    
      if (strcmp (argv[2], "*")==0){
        a = atoi(argv[1]);
        b = atoi(argv[3]);
        c= a*b;
        printf("%d\n", c);}
    
    
    
    
    }
      return 0;
    }
    our teacher warned us of this and said "Remember what * means in the shell. How do you protect it? " as a tip

    how to i protect it

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Use single quote.
    ./a.out ' 3 * 2 '

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laxkrzy View Post
    our teacher warned us of this and said "Remember what * means in the shell. How do you protect it? " as a tip

    how to i protect it
    Use the standard math symbol X for multiplication.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Depends on the shell. If it's unix-ish, try ./a.out 3 \* 2. Or maybe ./a.out 3 '*' 2. You can't put the whole thing in one set of quotes because then it'll be lumped into argv[1], spaces and all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  4. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  5. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM