Answering the first question:
There are no differences in the implementation question, possibly the comparator >= or <= "exert the function" of 2 comparators, because it first checks whether the value in question is equal and then checks whether the value is higher or lower.
Answering the second question:
The GETCHAR() function reads a character and returns an integer which is:
- the character code, or
- the -1 value that corresponds to the file.
The GETS function, from the standard C library (stdio) can generate a big problem for the programmer who uses it: as this function does not limit the number of characters to be read from the standard input (stdin), there may be memory leakage, or even worse, injecting malicious code into the program.
The solution is to use FGETS, which limits the read buffer.
The function used to read "generic" values (receives any primitive type) is the SCANF function.
Answering the third question:
WHILE X FOR:
FOR performs a limited and fixed number of steps, while WHILE can do this indefinitely.
For example, if I make a loop to add 1 to a variable, already declared, n times, the loop will always run n times. The initial and final result will always be the same.
while must be used when it is the user who will set the initial value of this variable, and the control only serves the maximum value.
Being clearer(Exemplifying):
The program declares, at the beginning, x as 1. And then executes the FOR(loop) 10 times, and in each of them the value will be added 1. The final result will always be 11.
If it is a WHILE loop, written so that this variable x reaches 10 (while x<=10), the maximum result will always be 10. But if the user puts x initially as 9, the result will be 10. And the loop will only run 1 time.
If the program is fixed, without user intervention, the most indicated is the is. For example, a program that always provides squares of integer numbers from 1 to 10.
But if it is a program that calculates the squares of the integer numbers within a defined range, and typed, by the user (from x to y, for example), the while is more indicated.
DO WHILE X WHILE:
The repetition structure DO WHILE assumes that something must be done first and then compare a variable to see if the loop will be executed once more.
SUMMARY:
The FOR is simpler to implement, which can reduce the errors that the programmer may make.
The speed depends on the size of the loop, and there is no significant difference between them, considering an equal number of executions. FOR has more parameters, that is, more resources than WHILE, because in addition to the comparator (that both WHILE AND FOR have), FOR has the initialization of a variable and the increment of it.
WHILE can be implemented in functions that perform an undefined (initially) number of steps. Therefore, it can be faster if the user sets a small amount of steps. If it is a fixed number of runs, it is simpler to use a.
The only way is to read the source code of the compiler and see what it generates. But for a quick answer: 1) very likely each of these will be a single machine instruction, all with the same performance; 2) I have no idea; 3) depends a lot on the architecture on which the program is compiled, but there should be no significant difference. The most common is
se a condição X for falsa faça um desvio pra instrução A
during the test andfaça um desvio incondicional pra instrução B
at the end of the loop. Already in ado..while
there is no such last deviation, and the condition is tested at the end (for true in this case).– mgibsonbr
+1 pro comment from mgibsonbr. And for mathematical operators, I think it’s more interesting to see how they work at the hardware level.
– Oralista de Sistemas
One more question. Where do I find the GCC source code? I found several sites with "releases" versions but I don’t know which one I can trust. Grateful.
– ViniciusArruda