Silly hack:
#include <stdio.h>
int main(void)
{
struct yyy{ char buff[__LINE__ ]; } ;
#define LINE1 ((int)sizeof(struct yyy))
int a[] = {
1,
2,
3,
4
};
struct zzz{ char buff[__LINE__ ]; } ;
#define LINE2 ((int)sizeof(struct zzz))
printf("[%d,%d]There are %d rows\n", LINE1 , LINE2 , LINE2 - LINE1 - 4);
return 0;
}
And it works at file scope,too:
#include <stdio.h>
struct yyy{ char buff[__LINE__ ]; } ;
#define LINE1 ((int)sizeof(struct yyy))
int a[] = {
1,
2,
4
};
struct zzz{ char buff[__LINE__ ]; } ;
#define LINE2 ((int)sizeof(struct zzz))
int main(void)
{
printf("[%d,%d]There are %d rows\n", LINE1 , LINE2 , LINE2 - LINE1 - 4);
return 0;
}
NOTE:this does not create variables, only two (unused) structure types.
Yet another ugly trick: use enums (which are not objects, but constants_in_disguise):
#include <stdio.h>
enum { OMG=__LINE__ , } ;
int a[] = {
#define INDEX(ll) ( (ll)- ((OMG)+3))
INDEX(__LINE__),
INDEX(__LINE__),
INDEX(__LINE__),
INDEX(__LINE__),
INDEX(__LINE__),
INDEX(__LINE__)
};
enum { WTF=__LINE__ , } ;
int main(void)
{
printf("[%d,%d]There are %d rows\n", OMG , WTF , WTF - OMG - 4);
return 0;
}