Thread: Why i can print string beyond the range?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    18

    Why i can print string beyond the range?

    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    void main()
    {
        char arr[7];
        scanf("%s", arr);
        printf("String:%s",arr);
    }
    If I used scanf, why am I able to print string beyond the range?
    But it does not happen when i use fgets.
    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    void main()
    {
        char arr[7];
        fgets(arr, 7, stdin);
        printf("String:%s",arr);
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Just lucky, or unlucky, depending on your perspective. scanf() will happily attempt to write as much data to a string as it can depending on what is input. The problem is that, if your array is of length 7, and scanf() writes 25 characters to it, then the last 18 characters are overwriting some random area of memory. The C standard calls the results of that "undefined". Practical results can include a program crash, reformatting your hard drive and never executing the printf() statement, or (possibly) no observable unwanted effect.

    Regardless of what effect you get (or don't) your code using scanf() is invalid, unless the user only enters 6 characters.

    fgets() deliberately stops when it reaches the supplied length (7).

    Also, main() returns int, not void.
    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
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by wonderwall View Post
    If I used scanf, why am I able to print string beyond the range?
    Because C has absolutely no runtime error checking and it's up to you to write code that doesn't screw up.

    But it does not happen when i use fgets.
    Because fgets() has a limit to the number of characters you can enter.

    You can do more or less the same thing with scanf by specifying a field size...
    Code:
    scanf(" %32s",string);
    will only take the first 32 characters you type.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Add a code to pdfp.exe to print range
    By jat421 in forum C Programming
    Replies: 1
    Last Post: 08-12-2011, 10:46 AM
  2. Print numbers within a certain range of an Array
    By omegasli in forum C Programming
    Replies: 4
    Last Post: 04-10-2011, 01:08 AM
  3. how to print only N numbers of string
    By umen242 in forum C Programming
    Replies: 1
    Last Post: 06-26-2008, 12:55 AM
  4. New programmer: how to print a string
    By 9988776655 in forum C Programming
    Replies: 1
    Last Post: 12-27-2007, 07:32 PM
  5. how to print a string backwards
    By Unregistered in forum C# Programming
    Replies: 2
    Last Post: 01-27-2002, 01:04 PM