However, it is obvious that the value of '0' is an int that is representable as an unsigned char.Originally Posted by c.user
However, it is obvious that the value of '0' is an int that is representable as an unsigned char.Originally Posted by c.user
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
but it should be casted while the pass to isdigit (prototype rule) and then C99 7.4.1 undefined behavior
it has greater than zero code in ASCII, so any symbol of 7-bit ASCII should not casted hence
Last edited by c.user; 05-24-2009 at 01:05 AM.
What do you mean? Since the value is known to be within the range specified by the pre-condition, there is no need to cast to unsigned char and back to int. cas' point was that since the contents of the dollars array indirectly comes from user input, it cannot be guaranteed that the value of a given char from that array will satisfy the pre-condition, hence a cast to unsigned char is necessary to force satisfaction of the pre-condition and thus avoid undefined behaviour.Originally Posted by c.user
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
you think function can show a digit on a non-digit character ? if this range sticked (maybe it can change for the codes, but they will be greater than zero) isdigit will know only the digits, and for the one character like dollars[0] it will find only one match, how it will be undefined for this situation ? how it can ?Originally Posted by laserlight
It will be undefined because the Standard says so, even if it returns the value you expect for every standard library implementation in existence, simply because I can write a standard library implementation that does what you do not expect and yet is still standard conforming.Originally Posted by c.user
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
How can you write it for the one char from dollar[0] ?
It must return that non-digit is a digit (error like)
For example:Originally Posted by c.user
So, assuming ASCII and that EOF is -1, and fgets() manages to read -2 converted to a char (where char is signed), and then that char happens to be placed into dollar[0], then isdigit(dollar[0]) will return 1, even though the character with value of -2 is not a digit. Some of the other character input functions are immune to this since they read characters as unsigned char converted to int, but as far as I can tell fgets() is not required to do that.Code:int isdigit(int c) { return (c >= '0' && c <= '9') || (c < 0 && c != EOF); }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I got one segmentation fault when I use int type for the array where fgets save
but when I changed it to signed char, it's ok
you know why it doesn't print a true ? because every value saved in every char by fgets, then the check checks for only one char; digits (their codes) are sticked obviously, when he takes the dollar[0], he takes only one chararacter (8 bits in this case), also digits places only once in the table (every digit has it's own place and only one) and he can't take -207 from one char (it can include numbers from -127 to up only)Code:#include <stdio.h> #include <ctype.h> main() { signed char c[3]; printf("%c\n", -2); fgets(c, 3, stdin); printf("%c\n", *c); printf("%d\n", isdigit(*c)); return 0; }
How did you get the true for non-digit ?
Last edited by c.user; 05-24-2009 at 09:05 PM.
Stop doing stupid stuff, and compile with warnings on:Didn't you read the part about "stop trying to user YOUR setup to prove if something's standard"?cuser1.c:5: warning: return type defaults to `int'
cuser1.c: In function `main':
cuser1.c:9: warning: pointer targets in passing arg 1 of `fgets' differ in signedness
Quzah.
Hope is the first step on the road to disappointment.
Code:#include <stdio.h> main() { unsigned char line1[100]; signed char line2[100]; char line3[100]; fgets(line1, sizeof line1, stdin); fgets(line2, sizeof line2, stdin); fgets(line3, sizeof line3, stdin); return 0; }Code:[guest@station src]$ cc -Wall *.c -o test main.c:5: предупреждение: по умолчанию возвращаемый тип функции - ‘int’ main.c: In function ‘main’: main.c:10: предупреждение: pointer targets in passing argument 1 of ‘fgets’ differ in signedness main.c:11: предупреждение: pointer targets in passing argument 1 of ‘fgets’ differ in signedness [guest@station src]$ ./test one two three [guest@station src]$it's from C99 standard but in C89 there is no restrict only, so, quzah, it doesn't matter what you seeCode:7.19.7.2 The fgets function Synopsis [#1] #include <stdio.h> char *fgets(char * restrict s, int n, FILE * restrict stream); Description [#2] The fgets function reads at most one less than the number of characters specified by n from the stream pointed to by stream into the array pointed to by s. No additional ____________________ 232An end-of-file and a read error can be distinguished by use of the feof and ferror functions.