Thread: Apologies for the VERY basic newbie question...

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    2

    Apologies for the VERY basic newbie question...

    I'm trying to write some code that will allow me to enter text of an arbitrary length. I think I need to use an array for this. Here's my code...

    Code:
    #include <stdio.h>
    
    main()
                   
            {
            char InputText[100];         /* Character Array setup  */        
            printf("Hello, enter some text...\n this program uses \"inputText[100] to hold the text\" \n\n\t");
            scanf("%s", &InputText);
            }
    It works, but not as I expected. I thought the number in square brackets dictated the number of characters that could be entered. However, the resultant program just seems to allow me to enter as much text as I like, no matter what number I place within the brackets!

    Would some kind soul please explain (preferably in simple terms)

  2. #2
    Registered User
    Join Date
    Aug 2017
    Posts
    17
    C does not have an array bound checking so even if you create an array of size 1, it will show no error in accepting hundreds of characters/data. But you might not be able to access them since they're not allocated.

  3. #3
    Registered User
    Join Date
    Aug 2017
    Posts
    2
    Quote Originally Posted by Iiz View Post
    C does not have an array bound checking so even if you create an array of size 1, it will show no error in accepting hundreds of characters/data. But you might not be able to access them since they're not allocated.
    From what liz is saying, there seems no point in the numerical values! (In other words, I don't understand)

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    However, the resultant program just seems to allow me to enter as much text as I like, no matter what number I place within the brackets!
    Yes the program you supplied will allow you to enter any number of characters into your string. However you are not using the scan() properly to avoid this serious problem. You should never use a function that retrieves a string that doesn't limit the number of characters it will try to retrieve. To limit the number of characters scanf() will try to retrieve into a string you need to use the proper width specifier. Something more like:

    Code:
    char buffer[10];
    printf("Please enter a string: ");
    scanf("%9s", buffer); // See the note below.
    printf("%s", buffer);
    Note: Notice the difference between the size of the array (10) and the width specifer (9), you must reserve room for the end of string character that is automatically added to the string by scanf().

    By the way you need to also remember that scanf() will also stop processing the string when it encounters a white space character, if you want to retrieve a string that has white space you should use fgets() instead.

    Jim

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by TonyB View Post
    From what liz is saying, there seems no point in the numerical values! (In other words, I don't understand)
    liz doesn't know what it's talking about.

    The array size guarantees that the array can hold that many elements.
    But if you go beyond that, you will be writing into space that may actually be used by other variables.
    This is a common and difficult bug since one variable's value is being changed by writing to another variable.

    Strangely, you can even write to locations before the array with negative indices (although the compiler can catch obvious cases of this).

    BTW, this
    I'm trying to write some code that will allow me to enter text of an arbitrary length.
    is not a newbie question. It requires dynamic memory allocation / reallocation, i.e., malloc and realloc.
    Last edited by algorism; 08-07-2017 at 09:52 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Apologies
    By cboard_member in forum A Brief History of Cprogramming.com
    Replies: 55
    Last Post: 08-04-2005, 06:04 PM
  2. Really Basic Newbie Help? Please? ;)
    By RavelArangelus in forum C Programming
    Replies: 4
    Last Post: 04-25-2003, 10:28 AM
  3. Apologies for outage
    By webmaster in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-26-2002, 09:29 PM
  4. apologies
    By webmaster in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-30-2002, 06:18 PM
  5. Apologies All Around
    By Sayeh in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 08-09-2002, 03:02 PM

Tags for this Thread