printf problem

This is a discussion on printf problem within the C Programming forums, part of the General Programming Boards category; I am making a simple program that outputs a random number but when I put a printf in it triggers ...

You're a little confused on the value of argc. The name of the program is always the first thing in argv (argv[0]), making argc always at least 1. If I call a program with no parameters, argc is 1, if I call it with 2 parameters, argc is 3:

Code:

$ ./foo
argc = 1
$ ./foo 10 20
argc = 3

That means, when you call it with no parameters, and argc is 1, and you try to atoi(argv[1]), which doesn't exist. When you call it with one parameter, it tries to atoi(argv[2]), which doesn't exist, and crashes. Fix your if check at the top to be argc < 2 || argc > 3, and fix your statement below to be argc == 2.