What is "-1L" in C?
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.
Related videos on Youtube

bogatyrjov
Updated on May 08, 2022Comments
-
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 over 11 yearsOk, i got it. Then how do I catch the situation where ftell returns -1L ? --- if(ftell(fp) == -1L) {} ? or if(ftell(fp) == -1) {} ?
-
bogatyrjov over 11 yearsSo, if L represents long, which characters represent other types? Is there a related reference on the net?
-
Matthew Flaschen over 11 years
-
Mark Elliot over 11 years
-
sepp2k over 11 years@Mat: It's also partly incorrect, isn't it? In C the type of
'c'
isint
, notchar
. -
R.. GitHub STOP HELPING ICE over 11 years
NULL
is not a valid return value forftell
.NULL
is only valid in pointer contexts andftell
does not return a pointer. -
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 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 almost 9 yearsNo,
NULL
is not a normal result forftell
. Theftell
function returns along
, not a pointer. -
Keith Thompson almost 9 years
long
is an integer type. I think you mean that the default type for integer constants isint
-- but even that's an oversimplification. -
Keith Thompson almost 9 yearsConverting a null pointer to an integer doesn't necessarily yield
0
. -
kfsone almost 9 yearsNULL 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 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 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 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 tovoid*
. Both0
and((void*)0)
are valid definitions forNULL
in a C implementation (so is('-'-'-')
). Note that the outer parentheses are required; definingNULL
as just(void*)0
would cause problems with operator precedence. -
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 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 almost 9 yearsYou'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.)