Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Feb 2003
    Location
    East Side/West Side
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What is scanf()?

    Hello,

    I have had trouble understanding the scanf() function in C. Basically, what is it? What does it do? When is it helpful/when do you use it? If someone can help me out, it'd be coo

  • #2
    New to the CF scene
    Join Date
    Feb 2003
    Location
    Lahore, PK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi... I remember when I started to code in C... I ran across the same thing !

    ok.. so without any further delays... scanf() is a function that is used to take input from the stdin. it takes in a format string of the type "%d" (similar to printf() format string) and the reference to variables that will hold the value e.g. &iVal

    so if I were to scan an integer .. I would do:

    int iVal;

    printf("Enter The Value Please: ");

    scanf("%d", &iVal);

    Hope that helps ...

    Regards...
    Maverik

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Location
    East Side/West Side
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Couldn't you do:

    int i;
    printf("Enter an integer please:\n");
    i = getc(stdin);

    and get the same result?

  • #4
    New to the CF scene
    Join Date
    Feb 2003
    Location
    Lahore, PK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nopes... that would return a character from the standard input only which will be implicitly type casted to int resulting an ASCII value only... which is probably not what you would want in most cases

    Using scanf() with format strings allow you to take any kind of input provided u give in the right format string...

    Regards...
    Maverik

  • #5
    Regular Coder
    Join Date
    Feb 2003
    Location
    East Side/West Side
    Posts
    118
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Makes sense

    Thanks

  • #6
    New to the CF scene
    Join Date
    Feb 2003
    Location
    Lahore, PK
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Glad to be of any assitance

    Regards...
    Maverik


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •