Difference between scanf() and fgets()

There are multiple differences. Two crucial ones are: fgets() can read from any open file, but scanf() only reads standard input. fgets() reads ‘a line of text’ from a file; scanf() can be used for that but also handles conversions from string to built in numeric types. Many people will use fgets() to read a … Read more

printf not printing on console

Output is buffered. stdout is line-buffered by default, which means that ‘\n’ is supposed to flush the buffer. Why is it not happening in your case? I don’t know. I need more info about your application/environment. However, you can control buffering with setvbuf(): setvbuf(stdout, NULL, _IOLBF, 0); This will force stdout to be line-buffered. setvbuf(stdout, … Read more

C – scanf() vs gets() vs fgets()

Never use gets. It offers no protections against a buffer overflow vulnerability (that is, you cannot tell it how big the buffer you pass to it is, so it cannot prevent a user from entering a line larger than the buffer and clobbering memory). Avoid using scanf. If not used carefully, it can have the … Read more

Does sscanf(“123456789123456789123456789”, “%d”, &n) have defined behavior?

From the standard, 7.21.6.2p10 ((f)scanf, applies to the whole family): … If this object does not have an appropriate type, or if the result of the conversion cannot be represented in the object, the behavior is undefined. Looks like another reason to be very cautious with the scanf family. The strtoXX functions have a fully … Read more

Why is scanf() causing infinite loop in this code?

scanf consumes only the input that matches the format string, returning the number of characters consumed. Any character that doesn’t match the format string causes it to stop scanning and leaves the invalid character still in the buffer. As others said, you still need to flush the invalid character out of the buffer before you … Read more

Format specifiers for uint8_t, uint16_t, …?

They are declared in <inttypes.h> as macros: SCNd8, SCNd16, SCNd32 and SCNd64. Example (for int32_t): sscanf (line, “Value of integer: %” SCNd32 “\n”, &my_integer); Their format is PRI (for printf)/SCN (for scan) then o, u, x, X d, i for the corresponding specifier then nothing, LEAST, FAST, MAX then the size (obviously there is no … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)