Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "-1L" in C?

Tags:

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 ?

like image 414
bogatyrjov Avatar asked Nov 06 '10 22:11

bogatyrjov


People also ask

What does 1L size mean?

A litre is a cubic decimetre, which is the volume of a cube 10 centimetres × 10 centimetres × 10 centimetres (1 L ≡ 1 dm3 ≡ 1000 cm3). Hence 1 L ≡ 0.001 m3 ≡ 1000 cm3; and 1 m3 (i.e. a cubic metre, which is the SI unit for volume) is exactly 1000 L.

What is the number 1L?

It's an integer constant that has a long int type instead of int . long int n = 2147483648: if int is 32 bits (or less), there is an overflow.

What is L after number in C?

The C Programming Language says: An integer constant like 1234 is an int . A long constant is written with a terminal l (ell) or L , as in 123456789L ; an integer constant too big to fit into an int will also be taken as a long .

What does it mean 1L in R?

In R integers are specified by the suffix L (e.g. 1L ), whereas all other numbers are of class numeric independent of their value. The function is. integer does not test whether a given variable has an integer value, but whether it belongs to the class integer .


2 Answers

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 } 
like image 184
Mark Elliot Avatar answered Sep 28 '22 17:09

Mark Elliot


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

like image 27
Clifford Avatar answered Sep 28 '22 18:09

Clifford