Search code examples
cargv

Is it possible to write a C program that functions differently according to argv[0]?


Is it possible to write a C program that functions differently according to argv[0]?

In fact, I am working on an exercise from a C textbook. The exercise is to write a program that converts uppercase to lower or lowercase to upper, depending on the name it is invoked with, as found in argv[0].

[Edit] FYI, the exercise is from K&R's textbook

enter image description here


Solution

  • Is it possible to write a C program that functions differently according to argv[0]?

    Yes, it is possible, very simply. For example:

    if (0 == strcmp(argv[0], "say_hello")) {
        printf("hello\n");
    } else if (0 == strcmp(argv[0], "say_bye")) {
        printf("bye\n");
    }