What is “-1L” in C?

前端 未结 6 1438
梦谈多话
梦谈多话 2020-12-10 11:16

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

For example, in ftell reference, it says

... If an error occurs, -1L is returned ...

6条回答
  •  执念已碎
    2020-12-10 11:25

    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
    }
    

提交回复
热议问题