-2
#include<stdio.h>
#include<conio.h>
int main()
{
char test [7];
for(int i=0;i<10;i++)
scanf("%c",&test[i]);
puts(test);
getch();
return 0;
}

I am using DevC++ (University rules) and I know that gets() has no bounds check so I have intentionally used for() loop to enter a string. When I am entering a string greater than the size of the array, puts is printing an extra character. Why so ??

Sample Input: helloworld Output: hellowos

Sample Input: Hellopeople Output: Hellopep

Varun Singh
  • 15
  • 1
  • 1
  • There is no `gets`, please forget this function has ever existed. OTOH `fgets` is alive and well and you should be using it instead of your hand-written loop, which could in principle work had you not firgotten to terminate your string with the NUL character. Also what's with the intentional overflowing the 7 character long buffer with up to 10 characters? – n. m. could be an AI Dec 14 '14 at 10:08
  • 1
    UB multi-dupe. Don't overrun array bounds, and note that SO is searchable. – Martin James Dec 14 '14 at 10:10
  • @n.m. Intentional overflow was just to check behavior of puts in case of overflow.Can you explain me why puts() is printing more 8 characters when it should have printed only 7.And why the character printed is immediate next. – Varun Singh Dec 14 '14 at 10:36
  • 1
    Checking how overflow works makes very little sense unless you know how things work without overflowing. (1) Remove the overflow and return to the normal non-overflowing behaviour. (2) Google what "null-terminated string" means. You need to be familiar with the concept before you ever touch a string in C. – n. m. could be an AI Dec 14 '14 at 11:07

4 Answers4

2

It's because you're overflowing memory. Your array only has enough for seven characters and you try to populate it with ten:

char test [7];             // Array indexes 0-6 allowed.
for(int i=0;i<10;i++)      // Array indexes 0-9 used.
    scanf("%c",&test[i]);

You can fix it (including allowing for a string terminator) with something like:

char test [11];            // Array indexes 0-10 allowed.
for(int i=0;i<10;i++)      // Array indexes 0-9 used.
    scanf("%c",&test[i]);
test[10] = '\0';           // And add string terminator before puts().

If you want a hardened user input function with buffer overflow protection, something built from fgets() is generally the best way in standard C. Something like this, for example.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

You have a buffer overflow as you try to store 10 characters into a buffer that can at-most store 7 elements

char test [7];
for(int i=0;i<10;i++)
  scanf("%c",&test[i]);

You can fix this by making the buffer 10 elements(So that it can store 9 characters plus one for the \0 at the end) and using this:

char test [10]; //10 elements long
scanf("%9s",test); //Get at-most 9 chars

or you can use fgets too.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
0

your test array is only 7 chars, while you read 10 chars into it via the for loop. This could crash.

Also puts will expect your string to be 0-terminated. You don't explicitly put a 0 after your chars, so the output will contain whatever garbage is after your chars, up until the first 0-byte.

geert3
  • 7,086
  • 1
  • 33
  • 49
0

Your strings does not end with '\0'.

Cronovirus
  • 187
  • 1
  • 9