tell me the function of scanf();

This is a discussion on tell me the function of scanf(); within the C Programming forums, part of the General Programming Boards category; what is scanf function can someone help me to explain it?...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    4

    Question tell me the function of scanf();

    what is scanf function
    can someone help me to explain it?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    60
    scanf() is made to take input from the user. To give a simple example:

    Code:
    int a = 0;
    printf("Enter an integer: ");
    scanf("%d", &a);
    This short example declares an integer a, asks the user to enter an integer and waits until an integer is entered.

    Let's dissect the scanf(): it has two parts, separated by a comma:

    "%d": means that the input has to be interpreted as an integer. This is called a conversion character. You can also take "%c" which interpretes the input as a character ('a' or '#', or whatever). With "%s" you can input a whole string.

    &a: means that the value that was entered should be stored in the variable a. The & means "the address of". In this example we put the value in the address where a is pointing to. If this sounds like magic, read first about pointers and addresses.

    To summarize:

    scanf("%d", &a); takes an integer and puts it in the address of a.
    I hope you're a bit wiser now...
    Last edited by hilarius; 11-27-2009 at 08:59 AM. Reason: typo

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    4
    Quote Originally Posted by hilarius View Post
    scanf() is made to take input from the user. To give a simple example:

    Code:
    int a = 0;
    printf("Enter an integer: ");
    scanf("%d", &a);
    This short example declares an integer a, asks the user to enter an integer and waits until an integer is entered.

    Let's dissect the scanf(): it has two parts, separated by a comma:

    "%d": means that the input has to be interpreted as an integer. This is called a conversion character. You can also take "%c" which interpretes the input as a character ('a' or '#', or whatever). With "%s" you can input a whole string.

    &a: means that the value that was entered should be stored in the variable a. The & means "the address of". In this example we put the value in the address where a is pointing to. If this sounds like magic, read first about pointers and addresses.

    To summarize:

    scanf("%d", &a); takes an integer and puts it in the address of a.
    I hope you're a bit wiser now...
    thx bro

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    103
    Get familiar with unix/linux (especially man pages) or type in man scanf and see what you get.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 5
    Last Post: 02-08-2003, 06:42 PM
  4. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21