0

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

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this really helps a noob like me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.