What is "-1L" in C?

c
20,973

Solution 1

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
}

Solution 2

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

Solution 3

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

Solution 4

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

Solution 5

-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.

Share:
20,973

Related videos on Youtube

bogatyrjov
Author by

bogatyrjov

Updated on May 08, 2022

Comments

  • bogatyrjov
    bogatyrjov 11 days

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

    For example, in ftell reference, it says

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

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

    Why not return NULL, if error occurs ?

  • bogatyrjov
    bogatyrjov over 11 years
    Ok, i got it. Then how do I catch the situation where ftell returns -1L ? --- if(ftell(fp) == -1L) {} ? or if(ftell(fp) == -1) {} ?
  • bogatyrjov
    bogatyrjov over 11 years
    So, if L represents long, which characters represent other types? Is there a related reference on the net?
  • Matthew Flaschen
    Matthew Flaschen over 11 years
    @Jevgeni, see this list. It's not complete, though. It's missing at least L for a wchar_t string.
  • Mark Elliot
    Mark Elliot over 11 years
    @Jevgeni, some standard ones are f for float and ul for unsigned long, here's a reference
  • sepp2k
    sepp2k over 11 years
    @Mat: It's also partly incorrect, isn't it? In C the type of 'c' is int, not char.
  • R.. GitHub STOP HELPING ICE
    R.. GitHub STOP HELPING ICE over 11 years
    NULL is not a valid return value for ftell. NULL is only valid in pointer contexts and ftell does not return a pointer.
  • R.. GitHub STOP HELPING ICE
    R.. GitHub STOP HELPING ICE over 11 years
    +1 for being the only person who seems to know the difference between pointers and integers.
  • Mark Elliot
    Mark Elliot over 11 years
    @R..: I never said NULL was a valid return value (in fact, I believe I said the opposite, admittedly the rationale was wrong), edited anyway, though.
  • Keith Thompson
    Keith Thompson almost 9 years
    No, NULL is not a normal result for ftell. The ftell function returns a long, not a pointer.
  • Keith Thompson
    Keith Thompson almost 9 years
    long is an integer type. I think you mean that the default type for integer constants is int -- but even that's an oversimplification.
  • Keith Thompson
    Keith Thompson almost 9 years
    Converting a null pointer to an integer doesn't necessarily yield 0.
  • kfsone
    kfsone almost 9 years
    NULL does NOT represent a pointer, it is defined as 0, 0L or 0ULL, etc. That's why C++11 introduced nullptr. Consider: int foo(const char*); int foo(unsigned long f); ... foo(NULL);. That isn't going to call the one you expect if you think "NULL" is a pointer type.
  • Clifford
    Clifford almost 9 years
    @ksone: In C (as the question is tagged) NULL is defined as (void*)0. C++ does indeed define it as 0. In C++ the use of NULL is discouraged (by Bjarne Stroustrup) in favour of literal zero exactly because it is a macro and may be incompatibly redefined. C++ guarantees that an integer zero casts to an invalid pointer. In either C or C++ however, NULL is defined with the intent that it is used as an invalid pointer value rather than an integer.
  • Clifford
    Clifford almost 9 years
    @KeithThompson: You think? Either way, whatever it casts to might be a valid ftell() position which is more the point.
  • Keith Thompson
    Keith Thompson almost 9 years
    @Clifford: In C, NULL expands to an implementation-defined null pointer constant, which is either an integer constant expression with the value zero, or such an expression cast to void*. Both 0 and ((void*)0) are valid definitions for NULL in a C implementation (so is ('-'-'-')). Note that the outer parentheses are required; defining NULL as just (void*)0 would cause problems with operator precedence.
  • Keith Thompson
    Keith Thompson almost 9 years
    @Clifford: Then you should update your question to say that. It currently claims that casting a null pointer to an integer will have value zero, which is misleading.
  • Clifford
    Clifford almost 9 years
    @KeithThompson: Its not my question to update - you mean my answer. The truth is I don't know that that is not the case - I was rather hoping that you would enlignten us. The point about the definition of NULL in C is accepted - nonetheless the point stands that it is not intended to be used as an integer. I don't think the precise language semantics add much to the general point that NULL should not be used to mean zero - it still represents a pointer even if it is not defined with one. Besides this has stood for three years without comment - I shall consider changes in that context.
  • Keith Thompson
    Keith Thompson almost 9 years
    You're right, I meant "answer". The main point, that NULL should not be used as an integer, is of course correct. But (a) the standard says nothing about the result of converting a null pointer to an integer type (except that it's implementation-defined and "intended to be consistent with the addressing structure of the execution environment", and (b) doesn't really matter in this case. And the question didn't say anything about casting the result. I suggest that your answer would be improved by deleting the second sentence of the second paragraph. (It's never too late.)