Compiti ed esercitazioni VERIFICATO

argv+argc accorgimenti logici e teorici prof: Caglioti

Politecnico di Milano ingegneria dell'automazione 2018
70 visualizzazioni
Nessun voto ancora
Condividi: WhatsApp Telegram
Anteprima pagina 1 — 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.c as 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 in argc = 1 and argv[1] = "two args".

Questo appunto è gratis. Registrati in 30 secondi per leggere tutte le pagine e scaricarlo.

Altri appunti di FONDAMENTI DI INFORMATICA

Condividi questi appunti

WhatsApp Telegram