NO

Author Topic: gets_s() reads 8 characters although the supplied array size is 10  (Read 3890 times)

mynameisbahaa

  • Guest
Hello all
my name is bahaa and i am a new member here
i am using pelles c for months now- only the compiler not the IDE - with sublime text editor: writing small c programs in sublime and compiling them from a command prompt using cc.exe

here the problematic code:

Code: [Select]
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>

int main(void){

char temp[10] ;
if (gets_s(temp,10) == NULL){
printf("Error reading input\n");
}
printf("%s",temp);
return 0;
}

the problem is that the compiled program accepts only 8 characters not 9. even though i read that "The gets_s function reads at most one less than the number of characters specified by n"
and
"The discarded new-line character does not count towards number of characters read."
from the  C11 draft :ISO/IEC 9899:201x

Am i doing something wrong or what ??

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2097
Re: gets_s() reads 8 characters although the supplied array size is 10
« Reply #1 on: January 23, 2015, 11:58:29 AM »
IMHO this is a bug, or better, a misinterpretation of the standard.
Strictly following the runtime constraints definition
Quote
dst shall not be a null pointer. max shall neither be equal to zero nor be greater than RSIZE_MAX. A new-line character, end-of-file, or read error shall occur within reading n-1 characters from stdin. If there is a runtime-constraint violation, dst[0] is set to the null character, and characters are read and discarded from stdin until a new-line character is read, or end-of-file or a read error occurs.
the behavior is consistent: if you type 9 chars then the new line the latter happens on nth char, not the nth-1 as stated.
But this makes no sense.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

mynameisbahaa

  • Guest
Re: gets_s() reads 8 characters although the supplied array size is 10
« Reply #2 on: January 23, 2015, 01:10:55 PM »
Thanks frankie

I found this in the c11 draft and what i understood is that gets_s() shouldn't count the newline character as quoted below :

Quote
The gets_s function reads at most one less than the number of characters specified by n
from the stream pointed to by stdin, into the array pointed to by s. No additional
characters are read after a new-line character (which is discarded) or after end-of-file.
The discarded new-line character does not count towards number of characters read. A
null character is written immediately after the last character read into the array.

I sent a message to the author of the compiler with a link to this post to see what he will say
 :)


Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: gets_s() reads 8 characters although the supplied array size is 10
« Reply #3 on: January 24, 2015, 12:03:56 AM »
The "newline"character doesn't get counted towards the length of the string but you still have to account for the /0 character that terminates the string.
So char temp [10] will be able to hold 9 entered characters plus the /0 terminator, hence your get_s() length parameter can't exceed 9...

Ralf

mynameisbahaa

  • Guest
Re: gets_s() reads 8 characters although the supplied array size is 10
« Reply #4 on: January 24, 2015, 05:07:21 AM »
Hello Ralf

I know about '\0' string terminator and i know that gets_s() reserves a place for it to be appended at the end of the input string given that  (input string) + ('\0') <= size specified to gets_s() as the second parameter.
what i understood is that gets_s() should allow me to store 9 characters at my "temp[10]" variable without taking the newline into consideration and the 10th character will be the '\0' terminator
It doesn't do that, it only allow my to enter 8 characters
so 8 character input by me and '\0' by gets_s() , what it is the use of the missing character ?

and if gets_s() do count newline as an input character why it discards it ?
« Last Edit: January 24, 2015, 05:13:01 AM by mynameisbahaa »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: gets_s() reads 8 characters although the supplied array size is 10
« Reply #5 on: January 24, 2015, 06:32:31 AM »
I have to play with the code when I have some time tomorrow, something doesn't smell right, something simple, but after the long workweek that I had, I just don't see what else it could be right now...

Ralf

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2097
Re: gets_s() reads 8 characters although the supplied array size is 10
« Reply #6 on: January 24, 2015, 04:37:08 PM »
Yes ralph, it stops at 8th char...
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide