This document is written as a support material for the group L3-Inter.
It should be used together with the TP6 subject available on the page Moodle of the course Advanced C Programming.
Exercise 1
Read the description of the exercice carefully and explain in a few words what are the "subtilités de scanf"
Take few minutes to read the man page of scanf ($: man scanf) and explain the differences among
scanf, fscanf and sscanf
Exercise 2
Compile and test the program given in the exercise with integer values and non integer values.
Observe and explain what are the problems.
Pay attention to the line (void)scanf("%d\n",&i);
Take few minutes and propose a solution by using fgets and sscanf.
The idea is that we want to read all the input data in an array and then process the data.
Improve this solution to allow the user to enter two integers separated by a space and read the two values.
To explore : we can also use gets() to read an input stream to a char pointer;
however, you will get an warning message by gcc saying that the usage of gets() is not safe. Why ?
Exercise 3
Identify the format of the two expressions. The following information can be found in the man page of scanf
d : Matches an optionally signed decimal integer; the next pointer must be a pointer to int.
s : Matches a sequence of non-white-space characters; the next pointer must be a pointer to the initial element of a character array that is long enough to hold the input sequence and the termi‐
nating null byte ('\0'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first.
c : Matches a sequence of characters whose length is specified by the maximum field width (default 1); the next pointer must be a pointer to char, and there must be enough room for all the char‐
acters (no terminating null byte is added). The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format
[ : Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in
the string, plus a terminating null byte. The usual skip of leading white space is suppressed. The string is to be made up of characters in (or not in) a particular set; the set is defined
by the characters between the open bracket [ character and a close bracket ] character. The set excludes those characters if the first character after the open bracket is a circumflex (^).
To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. The hyphen character - is also special; when
placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, [^]0-9-]
means the set "everything except close bracket, zero through nine, and hyphen". The string ends with the appearance of a character not in the (or, with a circumflex, in) set or when the
field width runs out.
f : Matches an optionally signed floating-point number; the next pointer must be a pointer to float.
In the previous exercise, we have seen the usage of sscanf : n = sscanf(chaine,"%d",&i).
Propose a solution for this exercise by using the same technique.