+ 2

Anyone can explain me the uses of fgets,fputs,sprintf,sscanf specifically in a easy way

I think that the tutorial is over complicated and precise.

18th May 2019, 11:57 AM
徐振羽
1 Respuesta
+ 6
fgets() - is most often used to get a fixed-length string as input from stdin (but can fetch strings from files too.) This function also saves the '\n' character to the string (which is not always what you want). fputs() - outputs a string to any file you choose (most often stdout). sscanf() - looks for data in a string and assigns it to variable(s) with a matching type. ex: nr_read = sscanf(input, "%d%d", &num1, &num2); sprintf() - works similar to printf() it creates a formatted string but unlike printf() it stores it into a string. This is useful when you want to convert numbers to a string representation, ex: for time where you have hours, minutes and seconds. sprintf(time_string, "%02d:%02d:%02d", hour, min, sec); sprintf() - should be used with caution since it doesn't know the size of the string it writes to - which can lead to buffer overflows, so make sure the string is large enough to hold the data. For C99 there's snprintf() which checks the size.
20th May 2019, 4:27 PM
Komodo64
Komodo64 - avatar