Aashiq
Aashiq

Reputation: 497

Cant pass command line arguments in C programming from the terminal

I'm trying to pass two command line arguments from the terminal for a count down program, which is like:

#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>

int main(int argc, char* argv[]){

    int disp, count;

    if(argc < 2){
        printf("You mush enter the length of the count \n on the the command line. Try again.\n");
        exit(1);
    }

    if(argc == 3 && !strcmp(argv[2], "display")) disp = 1;
    else disp = 0;

    for(count = atoi(argv[1]); count; count--)
        if(disp) printf("%d\n", count);
    
    putchar('\a');
    printf("Done.");
    return 0;
}

in the command line , I compiled as, >$cc countdown.c 4 display

It throws a compilation error saying:

clang: error: no such file or directory: '4'
clang: error: no such file or directory: 'display'

I even tried passing in double quotes, but the error is same. For more details my ,cc --version goes like:

Apple clang version 13.0.0 (clang-1300.0.29.3)
Target: arm64-apple-darwin20.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Upvotes: 0

Views: 511

Answers (1)

kaylum
kaylum

Reputation: 14044

You need to compile first then run. They are two separate steps.

cc -o countdown countdown.c
./countdown 4 display

The first command compiles the C code into a countdown binary. Assuming that succeeds, the second command runs the binary with the required arguments.

Upvotes: 4

Related Questions