argv+argc accorgimenti logici e teorici prof: Caglioti
Stai vedendo l'anteprima delle prime pagine. Il file completo è gratis: registrati per leggerlo tutto.
Di cosa parla
- Command-line arguments allow passing information to a program from the command line, with
-o myprog myprog.cas an example for GCC where -o, myprog, and myprog.c are arguments. - Main function now takes two parameters: argc (argument count) and argv (argument vector), which is an array of strings representing the arguments passed to the program. Each string in argv corresponds to one argument, with
argv[0]being the name of the program itself. - The following C program demonstrates handling command-line arguments:
#include <stdio.h> int main (int argc, char *argv[]) { int count; printf ("This program was called with \"%s\".\n", argv[0]); if (argc > 1) { for (count = 1; count < argc; count++) { printf("argv[%d] = \"%s\"\n", count, argv[count]); } } else { printf("The command had no other arguments.\n"); } return 0; } - Passing an argument with spaces requires enclosing it in double quotes, e.g.,
./myprog "two args", which results inargc = 1andargv[1] = "two args".
Questo appunto è gratis. Registrati in 30 secondi per leggere tutte le pagine e scaricarlo.