CODe
CODe

Reputation: 2301

Recursive Printing Function in C

The program doesn't output anything currently. The program is meant to take an integer command line value and then print "Test" that number of times using a recursive printing function. I'm new to C and can't figure out why the program isn't working, I'm not getting any compilation errors. (Still working on getting familiar with gdb)

#include <stdio.h>

void myfunc(int num)
{
    if(num <= 0)
    {
        return;
    }
    else
    {
        printf("%s \n", "Test");
        myfunc(num-1);
        return;
    }
}

int main (int argc, char *argv[])
{
    int i;
    i = atoi(argv[0]);
    myfunc(i);
}

Upvotes: 1

Views: 747

Answers (2)

zubergu
zubergu

Reputation: 3706

argv[0] holds name of the executable, so when You run your executable:

program.out 1 2

argv[0] will be "program.out", (they are all strings)
argv[1] will be "1",
and argv[2] will be "2".

Just to be complete, argc will hold the number of elements in argv, so in this case argc will be 3 (integer 3, not string"3").

Upvotes: 2

Grijesh Chauhan
Grijesh Chauhan

Reputation: 58271

Because you are not passing an int:

i = atoi(argv[0]);
              ^
             argument 0 is name of executable 

may be your need:

i = atoi(argv[1]);

Upvotes: 6

Related Questions