1

Coming back to programming after a number of years, currently fiddling with a simple version of Euclid's algorithm. It currently works if I assign values to the integers within the program, so I'm assuming that the issue lies with how I'm passing the integers from the command line.

Any help would be greatly appreciated.

#include <stdio.h>

//Defining variables
int x,y, rem;

//Defining function gcd
int gcd(int x, int y);
//Where x>= y and y>=0

int main(int argc, char *argv[])
{
    //Read in command line arguments
    x= sscanf(argv[1], "%d", &x);
    y= sscanf(argv[2], "%d", &y);

    gcd(x,y);
}

//Define recursive gcd function
int gcd(int x, int y)
{
    while(y !=0)
    {
        rem=x%y;
        x=y;
        y=rem;
        gcd(x,y);
    }
    return x;
} 
5
  • man sscanf will help you. Commented Jul 30, 2013 at 23:14
  • Don't tag with c++ if u mean c and vice versa Commented Jul 30, 2013 at 23:16
  • Apologies for the mis-tag. Commented Jul 30, 2013 at 23:50
  • Brief code review while we're here: I would have declared gcd() to be static so that its symbol doesn't get exported (unless you intend to export it) -- that's just a style thing; it keeps the namespace less cluttered and can help with debugging. Declaring x,y,rem to be global is unecessary, clutters the namespace, prevents recursion, makes the code thread-unsafe, and limits the optimizations the compiler can do. Better to declare x,y in the body of main() and rem in the body of gcd(). Finally, main() should return a value; I'm surprised that the compiler didn't squawk. Commented Jul 31, 2013 at 0:01
  • The call gcd(x,y); in int gcd(int x, int y) appears to serve no purpose. Commented Jul 31, 2013 at 0:12

3 Answers 3

8

Along with it's sibling functions (printf, sprintf etc) sscanf returns the number of arguments correctly parsed - one in this case. You need to remove assignments of x and y to sscanf.

sscanf(argv[1], "%d", &x);
sscanf(argv[2], "%d", &y);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks suspectus, but I had originally tried that. Perhaps it's an issue with how I'm inputting the command line arguments. I will have a fiddle.
Sam - works for me after adding a printf to output the gcd result.
Ah! I was being dim and expecting the return function to print it for me. Has been a long time, sorry. Thank-you for your help, much appreciated.
1

You may wish to avoid using sscanf() altogether. The functions atoi(), atof(), and atol() are standard C library functions specifically designed to convert strings into numerical values. For instance, the prototype for atoi(), "ascii to integer", is as follows:

#include <stdlib.h>

int atoi(const char *str);

this should work for your code:

x = atoi(argv[1]);

etc.

Just remember to # include the stdlib.h header at the beginning of your program.

Here is a link to a decent reference for the standard C library: http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.13.html#atoi

Comments

0

You seem to be assigning x and y the return value of sscanf, which means that after it has read the input and stored the values to the appropriate variables, you are then overwriting them. Try removing x= and y= from those lines.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.