开发者

What is "-1L" / "1L" in C?

What do "-1L", "1L" etc. mean in C ?

For example, in ftell reference, it says

... 开发者_C百科If an error occurs, -1L is returned ...

What does this mean ? What is the type of "1L" ?

Why not return NULL, if error occurs ?


The L specifies that the number is a long type, so -1L is a long set to negative one, and 1L is a long set to positive one.

As for why ftell doesn't just return NULL, it's because NULL is used for pointers, and here a long is returned. Note that 0 isn't used because 0 is a valid value for ftell to return.

Catching this situation involves checking for a non-negative value:

long size;
FILE *pFile;

...

size = ftell(pFile);
if(size > -1L){
    // size is a valid value
}else{
    // error occurred
}


ftell() returns type long int, the L suffix applied to a literal forces its type to long rather than plain int.

NULL would be wholly incorrect because it is a macro representing a pointer not an integer. Its value, when interpreted and an integer may represent a valid file position, while -1 (or any negative value) cannot.

For all intents and purposes you can generally simply regard the error return as -1, the L suffix is not critical to correct operation in most cases due to implicit casting rules


It means to return the value as a long, not an int.


That means -1 as a long (rather than the default type for numbers, which is an integer)


-1 formated in long int is a -1L. Why not simple NULL? Because NULL in this function is a normal result and can't sygnalize error too. Why NULL in this function is a normal result? Because NULL == 0 and ftell returns position in a stream, when you are on start of stream function returns 0 and this is a normal result not error, then if you compare this function to NULL to check error, you will be get error when you will be on start position in stream.


Editing today implies more details are still wanted.

Mark has it right. The "L" suffix is long. -1L is thus a long -1.

My favored way to test is different from Marks and is a matter of preference not goodness.

if ( err >= 0L )
    success
else
    error

By general habit I do not like looking for explicit -1. If a -2 ever pops up in the future my code will likely not break.

Ever since I started using C, way back in the beginning of C, I noticed most library routines returning int values return 0 for success and -1 on error. Most.

NULL is not normally returned by integer functions as NULL is a pointer value. Besides the clash of types a huge reason for not returning NULL depends on a bit of history.

Things were not clean back when C was being invented, and maybe not even on small systems today. The original K&R C did not guarantee NULL would be zero as is usually the case on CPUs with virtual memory. On small "real memory" systems zero may be a valid address making it necessary for "invalid" addresses to be moved to some other OS dependent location. Such would really be accepted by the CPU, just not generated in the normal scheme of things. Perhaps a very high memory address. I can even see a hidden array called extern const long NULL[1]; allowing NULL to become the address of this otherwise unused array.

Back then you saw a lot of if ( ptr != NULL ) statements rather than if ( ptr ) for people serious about writing portable code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜