What is “-1L” in C?

前端 未结 6 1451
梦谈多话
梦谈多话 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:21

    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.

提交回复
热议问题