What is the difference between getchar()
, gets()
and scanf()
?
getchar reads one character at a time, gets reads one whole line at a time, and scanf is a generic function that does different things according to the format string you use. These functions do very different things and it is better to choose the most appropriate one instead of worrying about micro optimizations (the cost of doing input and output operations and calls to the operating system is probably much greater than that.)
For the rest of your questions, I suggest you learn how to read the executable files generated by your compiler. In the case of gcc you can use the -S flag to get an assembly language version of the executable generated from your program:
gcc -S meuprograma.c -o meuprograma.S
The compiler has enough freedom to change the structure of your program, as long as the result is the same as the original. I think you'll be amazed at the result in some cases :) Just a tip: To simplify things, just write a function with the code of your interest - leave the input and output aside.
Note that this all depends on the compiler you use, the level of optimization (-O0, -O1, -O2) and your processor architecture (x86, x86-64, ARM, etc.)
1) What is the implementation of < = or > =? What are the differences from & lt ;, > and ==? The comparison > = would be two separate functions or one?
Do not worry about it. Your processor will probably spend the same time for any of these comparisons, and even if it were different, your compiler would probably account for doing the micro-optimizations itself (for example, if(a < b){ XXX }else{ YYY }
is the same as if(a >= b){ YYY }else{ XXX }
) .
3) What is the implementation of do..while (), while () and for ()? The last two seem to be the same thing but in a different way to put the parameters. Would it be the same implementation? How efficient is it to use for () instead of while () and vice versa?
It's all equally efficient. your compiler will convert all structured control structures (if, while, for, etc) into a lower-level execution flow graph, and in the end will spit out a soup of% unstructured%.
Just a warning for your adventures: It's hard to guess how long the computer will take to do each operation and it's even harder to predict which part of your program is the most sensitive in terms of performance (no use nothing double the speed of a stretch responsible for 1% of the total run time). Always use a profiler to take empirical measures of the time spent and remember that the CPU does not perform all operations at the same speed (for example, nowadays the speed of memory access tends to be a bottleneck much larger than the number of operations performed by the CPU)