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:
#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 ??
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.
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
:)
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
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 ?
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
Yes ralph, it stops at 8th char...