Lab overview
Lab 01
Lab 02
Lab 03
Lab 04
Lab 05
Lab 06
Lab 07
Lab 08
Lab 09
Lab 10
Lab 11
Lab 12
Lab 13
Review

[Course home page]

CSC 209 lab 10 solutions, week of 19 July 2022

Question 8:

Problem:
"./a.out num1 num2" outputs the sum of the two numbers.

Code:

int main(int argc, char **argv)
{
    if (argv[2] == NULL) {
        printf("two integers required on the command line");
        return(55);
    }
    printf("%d", atoi(argv[1]) + atoi(argv[2]));
}

Solution:

int main(int argc, char **argv)
{
    if (argc != 3) {
	fprintf(stderr, "usage: %s num1 num2\n", argv[0]);
        return(1);
    }
    printf("%d\n", atoi(argv[1]) + atoi(argv[2]));
    return(0);
}

Problems fixed above:


[press 'back' in your web browser to return to the problems]