Thread: Primary Questions

  1. #1
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57

    Primary Questions

    Hey all, used this forum a couple weeks ago and it was great...have a couple of questions about arrays right now...

    I have this code:
    Code:
    #include <stdio.h>
    #include<stdlib.h>
    int main()
    {
    char astring[10];
    
    scanf( "%s", astring );
    printf("%s", astring);
    return 0;
    }
    the array astring suppose to get only 10 elements right?!
    but if I put aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ( which is more than 10 elements), it will print it out all of the As!!! why is it like that?!

    and why if I press "space" in between my characters when I'm inputting them, it won't print out the characters after space?!

    what can I do to assign an empty element(like space) in one of my slots in the array?!
    (I mean I wanted it to print a FIRSTNAME LASTNAME thing, like John Terry, with space in between, but it will only print out John...)

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    scanf("%s") just keeps reading characters until it encounters whitespace. That is actually the answer to both your questions.

    But to expand ....

    On being able to enter more than ten characters, you're just getting lucky (or unlucky, depending on perspective). It performs no check to ensure the supplied buffer (astring in your code) is long enough to check that input. If the number of characters entered exceeds the length of the buffer, scanf() doesn't care. It will keep writing past the end of the buffer. What happens as a result is undefined. It may appear to do nothing untoward. It can trash other variables. The operating system may detect your program writing to memory it shouldn't and terminate your program. Even worse, the behaviour may change if you build your code with a different compiler (or, even with different compiler settings that change how the program is laid out in memory).

    If you enter any whitespace (including the space character) you cause scanf("%s") to stop reading characters. That is what the "%s" format is documented to mean. You need to make additional scanf() calls if you expect more input after the whitespace.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    HUH! hard to grasp :/

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    126
    It's hard to grasp what you are trying to do lol, you could also go the route of using sscanf and negating the white space
    Last edited by Sorinx; 11-09-2012 at 09:30 PM.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    in other words, do something like this:

    Code:
    char first_name[10];
    char last_name[10];
    
    printf("Please enter your first and last name");
    scanf("%s%s", first_name, last_name); //no space required between the two %s
    printf("%s %s", first_name, last_name);  //a space is required between the two %s for a space between the two names

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("%s%s", first_name, last_name); //no space required between the two %s
    Did you try this, to see what happened?
    Preferably with two names shorter than 10 characters.

    The presence (or absence) of a space in the format string has nothing to do with the buffer overflow issue in post #1.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    The original poster said he was interested in scanning in two names. That is what I was addressing.

  8. #8
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    tnx for the answers...have another one:
    why do we have to use fgets, when using strings?!
    I mean I am using scanf here and it works totally fine!!!!
    Code:
    #include <stdio.h>
    
    int main()
    {
    char name[10];
    printf("enter your name\n");
    scanf("%s", name);
    printf("your name is: %s", name);
    return 0;
    }
    and why if I don't use & or use it in scanf function for strings, it doesn't really matter?!! both ways it seems to work!

  9. #9
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    I guess one reason we use fgets is scanf doesn't read the words after "space"!!!!!!

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You don't have to use fgets(). scanf() may suit your needs better. But you definitely should not be using gets(), because it is vulnerable to buffer overflows, and fgets gives you enough control to protect yourself. More details here: FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com.

    In your example, you're using the name of an array as the parameter to scanf, and regardless of whether or not you use the & operator, it's going to pass in a pointer (which is what scanf needs). With other data types you need to use the & operator to make sure you're passing in a pointer.

  11. #11
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    Quote Originally Posted by sean View Post
    In your example, you're using the name of an array as the parameter to scanf, and regardless of whether or not you use the & operator, it's going to pass in a pointer (which is what scanf needs). With other data types you need to use the & operator to make sure you're passing in a pointer.
    ok so arrays work like pointers, right?! there is no need for & in scanf?!

  12. #12
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    another thing...
    is there any difference between an array of characters, and string (and c-style string)?! I mean char i[10] is what we call a string, meanwhile it's an array of characters, right?!

    and, in this code:
    Code:
    char k;
    char i[5];
    i[6]= 'k';
    printf("\n%c",i[6]);
    the program won't crash!!!!! however my string has only 5 slots!!! and I assigned 'k' to the sixth slot, which doesn't even exist!!! how come it works?!
    Last edited by Ashl7; 11-17-2012 at 02:02 PM.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > the program won't crash!!!!! however my string has only 5 slots!!! and I assigned 'k' to the sixth slot, which doesn't even exist!!! how come it works?!
    Pure dumb luck.

    C doesn't check array bounds at all, so there are all manner of dumb things you can do which "seem to work" at some point, but which will bite you in the ass at some future time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User Ashl7's Avatar
    Join Date
    Oct 2012
    Posts
    57
    questions pouring up...
    confirm if this is true:
    char f[3];
    first character goes to slot 0, second goes to slot 1, third to slot 2, forth is a null character which goes to slot 3.(is it true?)
    and null character is what occurs when we're using arrays of characters only(or strings), right?

  15. #15
    Registered User
    Join Date
    May 2012
    Posts
    1,066

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. primary-expression error
    By sopa in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2012, 10:20 AM
  2. primary expression before else
    By Rareit in forum C++ Programming
    Replies: 4
    Last Post: 12-03-2010, 11:12 PM
  3. Epected Primary Errors
    By veronicak5678 in forum C++ Programming
    Replies: 7
    Last Post: 09-29-2007, 02:12 PM
  4. expected primary expression
    By mju4t in forum C Programming
    Replies: 2
    Last Post: 03-27-2007, 06:59 PM
  5. Primary colors question
    By Robin Hood in forum C++ Programming
    Replies: 8
    Last Post: 07-25-2002, 04:30 AM