system call to return current line number 
Author Message
 system call to return current line number


Quote:

>Can you tell me how I can get the current line
>number in the source code ?

>For example, consider the following piece of code:

>main()
>{
> printf("line number = %i\n",linenum());
> printf("line number = %i\n",linenum());
>}

>The first linenum() should return 3 while second 4,assuming main() is starting
>from the first line.

You can use the predefined `constant' __LINE__ for this.
Other such values are __DATE__, __TIME__ and __FILE__.
They may not be available on a non-ANSI compiler.

B.t.w, the first __LINE__ should give you at least 4 (because printf()
requires #include <stdio.h>).

--
Miguel Carrasquer         ____________________  ~~~
Amsterdam                [                  ||]~  



Thu, 20 Feb 1997 06:00:03 GMT  
 system call to return current line number

Quote:
> Can you tell me how I can get the current line
> number in the source code ?

> For example, consider the following piece of code:

> main()
> {
>  printf("line number = %i\n",linenum());
>  printf("line number = %i\n",linenum());
> }

> The first linenum() should return 3 while second 4, assuming main() is
> starting from the first line.

Does linenum() have to be a function or is it just there to demonstrate
how you desire your code to work? Anyway doing:

main()
{
  printf ("line number = %i\n", __LINE__);
  printf ("line number = %i\n", __LINE__);

Quote:
}

works on my platform...I don't believe you need to include anything for
__LINE__ to work although you should include <stdio.h> for printf() to
work since it is a variable number of arguments function. When I compile
with 'gcc -Wall' I get:

line.c:2: warning: return-type defaults to `int'
line.c: In function `main':
line.c:3: warning: implicit declaration of function `printf'
line.c:5: warning: control reaches end of non-void function

Probably best to code it:

#include <stdio.h>

int main (void)
{
  printf ("line number = %i\n", __LINE__);
  printf ("line number = %i\n", __LINE__);

  return (0);

Quote:
}

Or at least that shuts up the compiler ;-).
--





Thu, 20 Feb 1997 06:43:19 GMT  
 system call to return current line number

Quote:


>>Can you tell me how I can get the current line
>>number in the source code ?
>>For example, consider the following piece of code:
>>main()
>>{
>> printf("line number = %i\n",linenum());
>> printf("line number = %i\n",linenum());
>>}
>>The first linenum() should return 3 while second 4,assuming main() is starting
>>from the first line.
>B.t.w, the first __LINE__ should give you at least 4 (because printf()
>requires #include <stdio.h>).

In ANSI C, the alternative is to provide the prototype yourself:

int printf(const char *, ...); main()
{
 printf("line number = %i\n", __LINE__);
 printf("line number = %i\n", __LINE__);
 return 0;

Quote:
}

Ugly and confusing, but legal ANSI C.

--
HansM



Sat, 01 Mar 1997 20:17:32 GMT  
 system call to return current line number

Quote:
> For example, consider the following piece of code:

> main()
> {
>  printf("line number = %i\n",linenum());
>  printf("line number = %i\n",linenum());
> }

> The first linenum() should return 3 while second 4,assuming main() is
> starting from the first line.

The best way I can think of that will work the way you are using it and
produce the results you desire would be:

#include <stdio.h>

#define linenum() ((int) __LINE__)
#line 1
int main (void)
{
  printf ("line number = %i\n", linenum ());
  printf ("line number = %i\n", linenum ());

  return (0);

Quote:
}

It won't count the include file or the define statement as lines in your
program, hence it will produce the output of 3 and 4 for the 2 printf's
yet still be ANSI C.
--





Sun, 02 Mar 1997 01:44:17 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. system call to return current line number

2. system call to return current line number

3. How to get current line number??

4. Bug: Textbox.Lines.Length returns an incorrect number

5. Calling Fortran-functions returning complex numbers from C

6. printing line number of calling function ?

7. call system() in linux return code = 2560

8. Q: Return-values of system("...") calls

9. Help: C system call that returns list of all processes in Unix

10. API call to return a specific machines system clock

11. LINE -- Line Number Pipe for MS-DOS

12. LINE -- Line Number Pipe

 

 
Powered by phpBB® Forum Software