Thread: Input & Output Explain?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    36

    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.

  2. #2
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115
    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.
    I live in a giant bucket.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Can someone then explain why scanf() cannot be used to take an input that has a "space" while fgets() can?

  4. #4
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115
    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.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char myarray[100];
    	char myarray2[100];
    	char myarray3[100];
    
    	printf("%s\n%s\n%s\n%d\n", myarray, myarray2, myarray3, scanf("%s%s%s", myarray, myarray2, myarray3));
    	return 0;
    }
    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
    Code:
    abcdefg
    123456
    ABCDEFG
    3
    as the output.

    Code:
    scanf("%s%s%s", myarray, myarray2, myarray3);
    is the same as

    Code:
    scanf("%s", myarray);
    scanf("%s", myarray2);
    scanf("%s", myarray3);
    so a more readable version of the program would be this:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i = 0;
    	char myarray[100];
    	char myarray2[100];
    	char myarray3[100];
    
    	i+= scanf("%s", myarray);
    	i+= scanf("%s", myarray2);
    	i+= scanf("%s", myarray3);
    
    	printf("%s\n", myarray);
    	printf("%s\n", myarray2);
    	printf("%s\n", myarray3);
    	printf("%d\n", i);
    	return 0;
    }
    (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...)
    Last edited by Aerie; 04-24-2005 at 08:51 AM.
    I live in a giant bucket.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Another thing I don't understand well is the int main() & int main(void)... What is the purpose of (void)???

  6. #6
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

    The exact question you asked is in the FAQ.
    I live in a giant bucket.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Bad output -- possible input buffer problems
    By jake123 in forum C Programming
    Replies: 8
    Last Post: 02-18-2008, 03:36 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Using SSCANF input string output two variables
    By kevndale79 in forum C Programming
    Replies: 9
    Last Post: 10-02-2006, 02:57 PM
  5. output as input
    By DDC in forum Linux Programming
    Replies: 3
    Last Post: 02-23-2003, 07:59 AM