How to use switch in C?

1

I have to understand this code that my advisor passed to me.

How much% of% after opt is required?

The command switch too? Why does not it appear after the options break and case '?' ?

if( argc <= 1) {
    fprintf(stderr, "\nparametros:\n-e arquivo de entrada\n-s arquivo de saida\n");
    exit(EXIT_FAILURE);
}
while ((opt = getopt(argc, argv, "e:s:")) != -1) {
    switch (opt) {

        case 's':
            saida=optarg;
            printf("Nome do arquivo de saida: %s\n", saida);
            errS++;
            break;

        case 'e':
            entrada=optarg;
            printf("Nome do arquivo de entrada: %s\n", entrada);
            errE++;
            break;


        case '?':
            fprintf(stderr, "\nFALTA PARAMETROS\n");

        default:
            fprintf(stderr, "%s \nparametros:\n-e arquivo de entrada\n-s arquivo de saida\n", argv[0]);
            exit(EXIT_FAILURE);
    }
    
asked by anonymous 13.03.2018 / 19:26

4 answers

3
  

Is the "opt" after "switch" required?

Well, no. There you need an expression that gives some value that can be compared to the case options. Usually it's a variable, but it does not have to be, much less it needs to be opt . It may, but it does not make sense to have a literal, if you already know the value you do not have to compare.

switch always takes this value and finds which of the case s that fits in it, only one can be executed directly. But others can run in sequence indirectly, so the case that it enters because its literal (and case must always have a literal) is equal to the value of switch will be first, then it keeps coming in in all% s of% s following.

This helps because you often want to make a kind of case , that is, more than one value is accepted, whichever one it enters must execute all of the following execution blocks. It is common that in these cases only the last or that should do something have a code, the others are empty, like this:

switch (variavel) {
case 1:
case 2:
case 3:
    printf("entrou");
}
  

Is the "break" command too?

But doing this is not very useful, you will want it to stop running at some point, you can not have it done because there the case is no longer useful, you are not selecting anything.

This is where switch comes in, you use it to say that this block should be closed. Note that the moment you find break break everything is terminated. That's better:

switch (variavel) {
case 1:
case 3:
    printf("impar");
    break;
case 2:
case 4:
    printf("entrou");
}
  

Why does not it appear after the 'case' options? ' "and" default "?

You see that I did not use switch at the end. Why would I use it? In the end it closes itself. No need. But many people even put it in order to organize, to make it more readable, to identify the intention and to facilitate a change that adds some block without the risk of forgetting to put the break that now becomes necessary.

The same goes for break , you do not have to have something closed down.

You may wonder why the last default did not need case and break exists then. The default is special, it only enters it if it does not enter the previous blocks, it is an execution OU, there is no way to enter it if it entered some default . It's another name, the compiler knows that it has closed before.

No case nobody puts default does not make sense because no point of view is.

    
14.03.2018 / 01:32
1

switch(opt) informs that the variable opt will be used to compare with the values after each case .

Each break signifies that the control should exit out of block switch . So, while not mandatory, it would be nice if you had break at the end of the case '?' commands.

default is used for expressions not covered by the cases declared above. As the latter case, you do not need break to exit switch block.

    
13.03.2018 / 19:58
1

Is the "opt" after "switch" required?

Yes, it is mandatory, first because without it the command does not work, second because the variable "opt" contains the value that will eventually correspond to the value contained in any of the cases. If the values match, the code block runs.

The "break" command too? Why does not it appear after the "case" and "default" options?

The break command is not required, but it is used to break the execution stream. The default is enabled only if there is no match, and the "?" is enabled only when data entry is invalid. In both cases there is no need to interrupt the execution of the program.

    
13.03.2018 / 19:55
0

The opt operator is a variable, yes it is necessary as if it were a if () when putting it there it will get the value of this variable and compare it with the CASE in case you do not find anything will fall into the DEFAULT

Break is and is not necessary, it serves to bar the process example:

switch(num)
{

case 1:
x = i;
printf("Olha eu aqui");

case 2:
x = 20;
printf("Olha eu aqui");
break;
}

In this case the first case 1 will continue pro case 2 and will drop two printf because there is no Break in case 1 p>     

13.03.2018 / 19:57