Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an implementation that has sizeof (int) == 1 "fully conform"? [duplicate]

Tags:

c

c99

c11

According to the C standard, any characters returned by fgetc are returned in the form of unsigned char values, "converted to an int" (that quote comes from the C standard, stating that there is indeed a conversion).

When sizeof (int) == 1, many unsigned char values are outside of range. It is thus possible that some of those unsigned char values might end up being converted to an int value (the result of the conversion being "implementation-defined or an implementation-defined signal is raised") of EOF, which would be returned despite the file not actually being in an erroneous or end-of-file state.

I was surprised to find that such an implementation actually exists. The TMS320C55x CCS manual documents UCHAR_MAX having a corresponding value of 65535, INT_MAX having 32767, fputs and fopen supporting binary mode... What's even more surprising is that it seems to describe the environment as a fully conforming, complete implementation (minus signals).

The C55x C/C++ compiler fully conforms to the ISO C standard as defined by the ISO specification ...

The compiler tools come with a complete runtime library. All library functions conform to the ISO C library standard. ...

Is such an implementation that can return a value indicating errors where there are none, really fully conforming? Could this justify using feof and ferror in the condition section of a loop (as hideous as that seems)? For example, while ((c = fgetc(stdin)) != EOF || !(feof(stdin) || ferror(stdin))) { ... }

like image 759
autistic Avatar asked Jun 15 '15 01:06

autistic


People also ask

How do you implement sizeof in C?

Implement Your Own sizeof Now come to implementation of the sizeof operator. The sizeof in C is an operator, and all operators have been implemented at compiler level; therefore, you cannot implement sizeof operator in standard C as a macro or function. You can do a trick to get the size of a variable by pointer arithmetic.

Why sizeof is not allowed in if statements?

A sizeof cannot be used in a #if directive, because the preprocessor does not parse type names. But sizeof in the #define is not evaluated by the preprocessor, so the code here is legal. Now come to implementation of the sizeof operator.

Is sizeof in c evaluated by the preprocessor?

But sizeof in the #define is not evaluated by the preprocessor, so the code here is legal. Now come to implementation of the sizeof operator. The sizeof in C is an operator, and all operators have been implemented at compiler level; therefore, you cannot implement sizeof operator in standard C as a macro or function.

What is sizeof operator in C++?

It is a compile time unary operator which can be used to compute the size of its operand. The result of sizeof is of unsigned integral type which is usually denoted by size_t. sizeof can be applied to any data-type, including primitive types such as integer and floating-point types, pointer types, or compound datatypes such as Structure, union etc.


1 Answers

The function fgetc() returns an int value in the range of unsigned char only when a proper character is read, otherwise it returns EOF which is a negative value of type int.

My original answer (I changed it) assumed that there was an integer conversion to int, but this is not the case, since actually the function fgetc() is already returning a value of type int.

I think that, to be conforming, the implementation have to make fgetc() to return nonnegative values in the range of int, unless EOF is returned.

In this way, the range of values from 32768 to 65535 will be never associated to character codes in the TMS320C55x implementation.

like image 101
pablo1977 Avatar answered Sep 22 '22 18:09

pablo1977