C throws an error if the argument is not specified on the command line

In C, how can I create an error if no arguments are given on the command line? I do not use int main(int argc , * char[] argv). I have no input, so I get my variable withscanf("%d", input)

+4
source share
3 answers

Based on the code:

#include <stdio.h>

int main() {
  int input;
  int rc = scanf("%d", &input);
}

We can verify that we were scanf()able to successfully get some input from the user by checking its return value. Only when the rc == 1user correctly gave us a valid input.

If you want to know more, I recommend reading the scanf documentation .

+1
source

: , main argc argv.

main , :

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

, arc 1. :

int main(int argc, char *argv[]) {
    if (argc < 2) {
        printf("error: missing command line arguments\n");
        return 1;
    }
    ...
}

main int main(void), . .

+3

, main :

  • argc -
  • argv -

, , argc ( ) ; -)

int main(), .

0
source

Source: https://habr.com/ru/post/1628735/


All Articles