#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    1

    Arguments for scanf and printf for strings


    For the other data types I have learned so far, the arguments for scanf and printf were different.
    For example,
    Code:
    int i;
    printf("enter an integer> ");
    scanf("%d", &i);
    printf("you entered %d", i);
    but with strings, you use the array name for the arguments in both i/o functions:
    Code:
    char msg[100];
    printf("enter a message> ");
    scanf("%s", msg);
    printf("you entered %s", msg);
    I understand that you don't need an address-of operator in scanf to store data in an array, because an array name is the address of the first element in the array, and so is a pointer type.
    Then again, I also understand that using *msg in printf would dereference the first character in msg, which is a character, not a string. So I "intuitively" get that msg is the right argument for printf.

    Still I can't get my head around why you can use the same arguments for scanf and printf.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    scanf needs addresses to know where to store the values that it creates by converting the input string. printf needs values to print out. That is why for most variables, like ints, you need to use the address operator to get the variable's address, but you don't use the address operator for printf. In the case of a string, printf expects a char pointer which just happens to be what scanf expects. So the situation that is puzzling you is kind of a special case that just happens often.

    An important difference between scanf and printf is that you have to be very careful with the format string for scanf. All scanf knows is the location to store the value, but it doesn't know the data type. It has to get that information from the format string, which must include the size. If it's a double, then you have to tell it that so that it doesn't try to store a float instead. If it's a short int, then you have to tell it that; just storing an int would overflow.

    We had a question on that here a couple years ago. The function had a couple short ints declared but his scanf was storing int values in them. A 32-bit int does not fit in a 16-bit short int space, so he was overwriting the other short int which gave him "weird results".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    1
    In the case of a string, printf expects a char pointer which just happens to be what scanf expects.
    I see.

    An important difference between scanf and printf is that you have to be very careful with the format string for scanf.
    Another example I had for this is scanf stored a string of length 5 in an array which had only size 3.
    In this case, the program was only to read a single string and echo the input, so I didn't get "weird results", but I suppose I will if I try to store multiple strings.

IMN logo majestic logo threadwatch logo seochat tools logo