Worked solution

Activité 3: Paramètres en Ligne de Commande

This exercise set focuses on programming in C using command line parameters. It tests the student's ability to handle command line arguments, convert string inputs to numeric types, perform basic loops and arithmetic, and manipulate strings by counting characters.

Based on the document Activité 3: Paramètres en Ligne de Commande

This article was generated from the source document, then verified before publication.

Activité 3: Paramètres en Ligne de Commande

Source document

Activité 3: Paramètres en Ligne de Commande

Computer Science - C Programming · PDF · 3 pages

Show document preview

View the original document

This exercise set focuses on programming in C using command line parameters. It tests the student's ability to handle command line arguments, convert string inputs to numeric types, perform basic loops and arithmetic, and manipulate strings by counting characters.

Activité 3: Paramètres en Ligne de Commande

This activity introduces how to access and display command line parameters passed to a C program.

The program receives two arguments in the main function:

  • argc: the number of arguments on the command line, including the executable name.
  • argv: an array of strings containing the command name at index 0 and the parameters at subsequent indices.

The example program below prints the number of parameters (excluding the executable name) and then lists each parameter on its own line.

#include <stdio.h>

// main(int argc, char *argv[])
// ARGUMENTS :
// argc = number of arguments on the line (including executable name)
// argv = array of strings with argv[0] as command name and others as parameters
// RETURNS : 0 on normal execution

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

  printf("les %d parametres de la commande %s sont :\n", argc-1, argv[0]);

  nb = 1;
  while(nb < argc){
    printf("\t%s\n",argv[nb]);
    nb++;
  }

  return 0;
}

For example, running ./test 1 15.3 bonjour A -39 outputs:

les 5 parametres de la commande ./test sont :
  1
  15.3
  bonjour
  A
  -39

Convert String Parameters to Numbers

This section explains how to convert command line string parameters to numeric values using atoi, atol, or atof from stdlib.h. It also emphasizes checking that the correct number of parameters is provided.

The example program calculates the sum of the first N integers, where N is passed as a command line parameter. It checks that exactly one parameter is given (besides the executable name), converts it to an integer, and verifies it is not zero before computing the sum.

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

int main(int argc, char *argv[])
{
  int somme, n_premiers_entiers, indice ;

  if(argc != 2){
    printf("usage : somme N\n");
    exit(1);
  }

  n_premiers_entiers = atoi(argv[1]);
  if(n_premiers_entiers == 0){
    printf("vous n'avez pas entre un chiffre ou vous avez entre 0\n");
    exit(1);
  }

  printf("la somme des %d premiers entiers est ", n_premiers_entiers);

  indice = 0;
  somme = 0;
  while(indice <= n_premiers_entiers){
    somme += indice;
    indice++;
  }
  printf("%d\n", somme);

  return 0;
}

Example runs:

  • ./test 5 outputs la somme des 5 premiers entiers est 15
  • ./test a outputs vous n'avez pas entre un chiffre ou vous avez entre 0
  • ./test 3 4 outputs usage : somme N
  • ./test outputs usage : somme N
  • ./test 0 outputs vous n'avez pas entre un chiffre ou vous avez entre 0
  • ./test 3 outputs la somme des 3 premiers entiers est 6

Exercise: Count the Number of Letters in a Word Passed as a Command Line Parameter

The task is to write a program that counts the number of letters in the word passed as the first parameter on the command line (excluding the executable name).

The program must verify that exactly one parameter is provided. Then it counts characters until it reaches the string terminator '\0'.

Example runs:

  • ./compte_lettre_mots abracadabra outputs le mot abracadabra contient 11 lettres
  • ./compte_lettre_mots abricot outputs le mot abricot contient 7 lettres
  • ./compte_lettre_mots zoo outputs le mot zoo contient 3 lettres
  • ./compte_lettre_mots ce outputs le mot ce contient 2 lettres
  • ./compte_lettre_mots a outputs le mot a contient 1 lettres

Solution Code

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

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

  if(argc != 2){
    printf("erreur");
    exit(1);
  }

  indice = 0;
  while(argv[1][indice] != '\0'){
    indice++;
  }

  printf("le mot %s contient %d lettres\n", argv[1], indice);

  return 0;
}

Method: Techniques Rewarded and Mistakes Punished

  • Proper use of argc and argv: The program must correctly check the number of parameters and handle errors gracefully by printing usage messages or error notices.
  • String to number conversion: Using atoi, atol, or atof correctly and verifying the converted value is valid (not zero when zero is invalid).
  • Looping constructs: Using while loops to iterate over parameters or characters in a string, with correct loop conditions and index increments.
  • Output formatting: Clear and correct output messages matching the specification.
  • Error handling: Exiting the program with exit(1) on incorrect input or argument count.
  • Attention to detail: Counting characters until the null terminator '\0' rather than relying on fixed sizes.
  • Common mistakes to avoid: Not checking argument count, failing to convert strings to numbers before arithmetic, off-by-one errors in loops, and ignoring invalid inputs.

All study guides