Input & Output Explain?
Can someone give me an explanation on Input & Output functions? So far I only know how to use scanf() & printf(), but I don't know how they work exactly.. I just know the syntax :( .
The book I have doesn't really go through the inner workings of the functions, just basically shows how they are used.
Someone please explain to me all functions that I can use to take an input from a user and functions to output on screen etc.. I wanna know how these work and how they differ from each other.
Thanks so much. :)
The whole idea with functions is that you simply know how to call them(ie., what type they are, what data they ask for, what they do with that data, what values they return for which circumstances), and don't have to know their inner workings, unless you actually have to modify them.
If you ever want to know an exact definition of a function, especially one like printf that is part of the standard, just google for its name; there are several pages that have variously detailed explanations of all C keywords, libraries/functions, macros, etc.
If you're looking for an actual in-depth explanation of C syntax, or an exhaustive list of IO functions, a book or a good tutorial is really your best bet; while we want to help, we're not going to take hours to explain everything to everyone. However, if you have specific questions along the way, or need help with a particularly tanglesome concept, that's what this forum is for.
Can someone then explain why scanf() cannot be used to take an input that has a "space" while fgets() can?
Because scanf interprets space-separated strings as different input arguments. This short and horrible program demonstrates what I'm talking about:
If you type "abcdefg 123456 ABCDEFG" when the program runs, printf will put "abcdefg" in myarray, "123456" into myarray2, and "ABCDEFG" into myarray3.
The %d, by the way, illustrates that scanf's return value is the number of strings successfully read. So in this case, if you typed out what I said when the program ran, you'd see something like
printf("%s\n%s\n%s\n%d\n", myarray, myarray2, myarray3, scanf("%s%s%s", myarray, myarray2, myarray3));
as the output.
is the same as
scanf("%s%s%s", myarray, myarray2, myarray3);
so a more readable version of the program would be this:
(Well, there is one difference: the second you have to press Enter after "abcdefg", "123456", and "ABCDEFG", but with the first you just separate them with spaces...)
int i = 0;
i+= scanf("%s", myarray);
i+= scanf("%s", myarray2);
i+= scanf("%s", myarray3);
Another thing I don't understand well is the int main() & int main(void)... What is the purpose of (void)???