0

I run the below program. I expected it'll give error. But it run perfectly and gave output.

Program:

#include <stdio.h> 
#include <unistd.h>
#include <fcntl.h>
#include <string.h> 
#include <stdlib.h>

int main()
{
    int fd, retval;
    char wBuf[20] = "Be my friend", rBuf[20] = {0};

    fd = open("test.txt", O_RDWR | O_CREAT, 0666);
    write(fd, wBuf, strlen(wBuf));

    retval = lseek(fd, -3L, SEEK_END); //Observe 2nd argument
    if(retval < 0) {
            perror("lseek");
            exit(1);
    }

    read(fd, rBuf, 5);
    printf("%s\n", rBuf);
}

lseek also works for

lseek(fd, -3I, SEEK_END); //but didn't print anything

For other letters it's giving error like

error: invalid suffix "S" on integer constant

What is the meaning of L and I on lseek?

gangadhars
  • 2,584
  • 7
  • 41
  • 68

2 Answers2

2

L simply means that the constant will be treated as a long type rather than the default integer type.

I is not a standard C suffix so, unless your compiler has some sort of extension (a), it shouldn't be valid. It may be that you're mistaken a lower-case l (which means the same as L) for the upper-case I, although I suspect in that case it would still seek, read and print, something you seem to indicate it doesn't.

In fact, the only way I can think of where you could use I in standard C and have it print nothing would be with something like:

#define I * 0

which would effectively turn the lseek argument into zero.


(a) Such as gcc with its complex data types, as may be the case. The effect of passing one of those to lseek is likely to be dysfunctional at best.

Perhaps this is an attempt to seek to a specific character position within the file and then also seek at right angles to that position :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • i know L for Long and U for Unsigned. I tried them. but Only L and I are working. S is for Short. but gave me error. I don't think they are not number suffixes – gangadhars Feb 19 '14 at 11:54
  • @SGG, there is no s suffix in standard C. And if u doesn't work, the implementation is broken. – paxdiablo Feb 19 '14 at 12:46
0

The L suffix on a literal number simply means the type of number is long instead of the default of int

123 //this is an int
123L //this is a long

The I suffix is a gcc extension for complex/imaginary numbers.

123; //this is an int
123I; //this is a _Complex

In C, there are the following suffixes for integer constants (they are case insensitive)

  • l for long,
  • ll for long long.
  • u for unsigned

And for floating point constants:

  • f for float.
  • l for long double

(and any extensions a particular compiler might support.)

nos
  • 223,662
  • 58
  • 417
  • 506