I want to read a number from stdin. I don't understand why scanf requires the use of & before the name of my variable: int i; scanf("%d", &i); Why does scanf need the address of the variable?
The & in C is an operator that returns the address of the operand. Think of it this way, if you would simply give scanf the variable a without the &, it will be passed to it by-value, which means scanf will not be able to set its value for you to see. Passing it by-reference (using & actually passes a pointer to a) allows scanf to set it so that the calling functions will see the change too ...
scanf(" %c", &c); The blank in the format string tells scanf to skip leading whitespace, and the first non-whitespace character will be read with the %c conversion specifier.
10 From scanf: On success, the function returns the number of items successfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens. In the case of an input failure before any data could be successfully read, EOF is returned.
So scanf("%*d %d", &i); would read two integers and put the second one in i. The value that was output in your code is just the value that was in the uninitialized i variable - the scanf call didn't change it.
char str[25]; And you cannot use scanf to read sentences--it stops reading at the first whitespace, so use fgets to read the sentence instead. And in your last printf, you need the %c specifier to print characters, not %s. You also need to flush the standard input, because there is a '\n' remaining in stdin, so you need to throw those ...
Third, when using scanf() function, the result is completely wrong because first character apparently has a -52 ASCII value. For this, I have no explanation. Now I know that gets() is discouraged to use, so I would like to know if I can use fgets() here so it doesn't read (or ignores) newline character.
I have to create and call a function from main. Then I have to call scanf to read two integers and print out the bigger one. Then I have to do another scanf, but this time with doubles instead of
The first scanf() doesn't return when a user has hit space, even though that's where the number ends (because at that point the line is still in the terminal's line buffer); and the second scanf() doesn't wait for you to enter another line (because the input buffer already contains enough to fill the %s conversion).
C maneja buffers, tanto de entrada como de salida. Lo que hace la función scanf es ir al buffer de entrada stdin y tratar de retirar el formato que le indicaste (en este caso, un char). En el primer caso, como el buffer está vacío, el programa se va a quedar esperando que ingreses algo por teclado, en tu caso ingresaste una a y presionaste la tecla enter la cual fue interpretada como un \n ...