"v4vijayaku mar" <v4vijayakumar@ yahoo.com> writes:[color=blue]
> why the following statement dumps the core(Segmentati on fault)?
>
> printf("%s\n", __FILE__);[/color]
Perhaps because it's not part of a complete C program. (If it were,
presumably you would have shown it to us.)
Or maybe the previous line defines printf as a macro.
Or maybe you closed stdout.
If you want actual answers rather than guesses, show us a complete
program. If this is a quiz rather than an actual problem you're
having, please say so.
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote:[color=blue]
> "v4vijayaku mar" <v4vijayakumar@ yahoo.com> writes:[color=green]
> > why the following statement dumps the core(Segmentati on fault)?
> >
> > printf("%s\n", __FILE__);[/color]
>
> Perhaps because it's not part of a complete C program. (If it were,
> presumably you would have shown it to us.)
>
> Or maybe the previous line defines printf as a macro.
>
> Or maybe you closed stdout.
>
> If you want actual answers rather than guesses, show us a complete
> program. If this is a quiz rather than an actual problem you're
> having, please say so.
>
> --
> Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
> We must do something. This is something. Therefore, we must do this.[/color]
sorry. Here is the complete program and sample run.
#cat test.c
#include <stdio.h>
int main()
{
printf("%s\n", __FILE__);
printf("%s\n", __LINE__);
printf("%s\n", __func__);
Keith Thompson <kst-u@mib.org> wrote:[color=blue]
> "v4vijayaku mar" <v4vijayakumar@ yahoo.com> writes:[color=green]
> > why the following statement dumps the core(Segmentati on fault)?
> >
> > printf("%s\n", __FILE__);[/color][/color]
[color=blue]
> Perhaps because it's not part of a complete C program. (If it were,
> presumably you would have shown it to us.)[/color]
v4vijayakumar wrote:[color=blue]
> Keith Thompson wrote:[color=green]
> > "v4vijayaku mar" <v4vijayakumar@ yahoo.com> writes:[color=darkred]
> > > why the following statement dumps the core(Segmentati on fault)?
> > >
> > > printf("%s\n", __FILE__);[/color]
> >
> > Perhaps because it's not part of a complete C program. (If it were,
> > presumably you would have shown it to us.)
> >
> > Or maybe the previous line defines printf as a macro.
> >
> > Or maybe you closed stdout.
> >
> > If you want actual answers rather than guesses, show us a complete
> > program. If this is a quiz rather than an actual problem you're
> > having, please say so.
> >
> > --
> > Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
> > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
> > We must do something. This is something. Therefore, we must do this.[/color]
>
> sorry. Here is the complete program and sample run.
>
> #cat test.c
> #include <stdio.h>
>
> int main()[/color]
Use int main(void)
[color=blue]
> {
> printf("%s\n", __FILE__);
> printf("%s\n", __LINE__);[/color]
__LINE__ expands to a decimal integer constant. But you've supplied a
conversion character that expects a pointer to a char. Use:
"v4vijayaku mar" <v4vijayakumar@ yahoo.com> wrote in message
news:1148884536 .665256.218760@ j55g2000cwa.goo glegroups.com.. .[color=blue]
>
> sorry. Here is the complete program and sample run.
>
> #cat test.c
> #include <stdio.h>
>
> int main()
> {
> printf("%s\n", __FILE__);
> printf("%s\n", __LINE__);
> printf("%s\n", __func__);
>
> return 0;
> }
>
> #cc test.c
> #./a.out
> test.c
> Segmentation fault (core dumped)
> #[/color]
My system doesn't define __func__, so I took that line out. Running that, I
get a segfault just like you. However, a couple seconds of looking tells me
the problem:
[color=blue]
> gcc -E test.c |tail[/color]
int main()
{
printf("%s\n", "test.c");
printf("%s\n", 6);
return 0;
}
__LINE__ is replaced with 6, not "6", so %s is not the correct format
specifier. Using %d works just fine.
I'm sure someone will comment on whether this is required or up to the
implementation.
S
--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin
v4vijayakumar wrote:[color=blue]
> Keith Thompson wrote:[color=green]
> > "v4vijayaku mar" <v4vijayakumar@ yahoo.com> writes:[color=darkred]
> > > why the following statement dumps the core(Segmentati on fault)?
> > >
> > > printf("%s\n", __FILE__);[/color]
> >
> > Perhaps because it's not part of a complete C program. (If it were,
> > presumably you would have shown it to us.)
> >
> > Or maybe the previous line defines printf as a macro.
> >
> > Or maybe you closed stdout.
> >
> > If you want actual answers rather than guesses, show us a complete
> > program. If this is a quiz rather than an actual problem you're
> > having, please say so.
> >
> > --
> > Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
> > San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
> > We must do something. This is something. Therefore, we must do this.[/color]
>
> sorry. Here is the complete program and sample run.
>
> #cat test.c
> #include <stdio.h>
>
> int main()
> {
> printf("%s\n", __FILE__);
> printf("%s\n", __LINE__);
> printf("%s\n", __func__);
>
> return 0;
> }[/color]
perhaps you wanted this
int main()
{
printf("%s\n", __FILE__);
printf("%d\n", __LINE__); <<<< note the change
printf("%s\n", __func__);
return 0;
}
Stephen Sprunk wrote:[color=blue]
> "v4vijayaku mar" <v4vijayakumar@ yahoo.com> wrote in message
> news:1148884536 .665256.218760@ j55g2000cwa.goo glegroups.com.. .[color=green]
> >
> > sorry. Here is the complete program and sample run.
> >
> > #cat test.c
> > #include <stdio.h>
> >
> > int main()
> > {
> > printf("%s\n", __FILE__);
> > printf("%s\n", __LINE__);
> > printf("%s\n", __func__);
> >
> > return 0;
> > }
> >
> > #cc test.c
> > #./a.out
> > test.c
> > Segmentation fault (core dumped)
> > #[/color]
>
> My system doesn't define __func__, so I took that line out. Running that, I
> get a segfault just like you. However, a couple seconds of looking tells me
> the problem:
>[color=green]
> > gcc -E test.c |tail[/color]
>
>
> int main()
> {
> printf("%s\n", "test.c");
> printf("%s\n", 6);
>
> return 0;
> }
>
> __LINE__ is replaced with 6, not "6", so %s is not the correct format
> specifier. Using %d works just fine.
>
> I'm sure someone will comment on whether this is required or up to the
> implementation.[/color]
Keith will provide the last word but as far as can tell from my library
refernce, __LINE__ expands to a decimal integer constant. So yes, %d is
needed and %s is the wrong conversion specifier to use.
Additionally, the OP implementation may not be fully C99 compliant in
which case the last of his printf's may also be the cause of the
segmentation fault.
"v4vijayaku mar" <v4vijayakumar@ yahoo.com> wrote:
[color=blue]
> why the following statement dumps the core(Segmentati on fault)?
>
> printf("%s\n", __FILE__);[/color]
Because you have made a mistake earlier in the code, probably involving
writing through an invalid pointer. That line in itself is fine.
"santosh" <santosh.k83@gm ail.com> writes:[color=blue]
> Stephen Sprunk wrote:[color=green]
>> "v4vijayaku mar" <v4vijayakumar@ yahoo.com> wrote in message
>> news:1148884536 .665256.218760@ j55g2000cwa.goo glegroups.com.. .[color=darkred]
>> >
>> > sorry. Here is the complete program and sample run.
>> >
>> > #cat test.c
>> > #include <stdio.h>
>> >
>> > int main()
>> > {
>> > printf("%s\n", __FILE__);
>> > printf("%s\n", __LINE__);
>> > printf("%s\n", __func__);
>> >
>> > return 0;
>> > }[/color][/color][/color]
[snip][color=blue][color=green]
>> __LINE__ is replaced with 6, not "6", so %s is not the correct format
>> specifier. Using %d works just fine.
>>
>> I'm sure someone will comment on whether this is required or up to the
>> implementation.[/color]
>
> Keith will provide the last word but as far as can tell from my library
> refernce, __LINE__ expands to a decimal integer constant. So yes, %d is
> needed and %s is the wrong conversion specifier to use.
>
> Additionally, the OP implementation may not be fully C99 compliant in
> which case the last of his printf's may also be the cause of the
> segmentation fault.[/color]
C99 6.10.8:
__FILE__ The presumed name of the current source file (a
character string literal).
__LINE__ The presumed line number (within the current source
file) of the current source line (an integer
constant).
with a footnote:
The presumed source file name and line number can be changed by
the #line directive.
The wording in C90 is a bit different:
__LINE__ The line number of the current source line (a decimal
constant).
__FILE__ The presumed name of the source file (a character string
literal).
A couple of odd points: C99 dropped the requirement that __LINE__ has
to be a *decimal* constant. And neither standard requires the
expansion of __LINE__ to be of type int. Possibly that's meant to
cater to implementations with 16-bit int that allow source files
longer than 32767 lines.
It looks like, strictly speaking, this:
printf("%d\n", __LINE__);
could invoke undefined behavior; to avoid the problem, use this:
printf("%d\n", (int)__LINE__);
or this:
printf("%ld\n", (long)__LINE__) ;
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Keith Thompson wrote:[color=blue]
> "santosh" <santosh.k83@gm ail.com> writes:[color=green]
> > Keith will provide the last word but as far as can tell from my library
> > refernce, __LINE__ expands to a decimal integer constant. So yes, %d is
> > needed and %s is the wrong conversion specifier to use.[/color][/color]
.... snip ...
[color=blue]
> A couple of odd points: C99 dropped the requirement that __LINE__ has
> to be a *decimal* constant. And neither standard requires the
> expansion of __LINE__ to be of type int. Possibly that's meant to
> cater to implementations with 16-bit int that allow source files
> longer than 32767 lines.
>
> It looks like, strictly speaking, this:
> printf("%d\n", __LINE__);
> could invoke undefined behavior; to avoid the problem, use this:
> printf("%d\n", (int)__LINE__);
> or this:
> printf("%ld\n", (long)__LINE__) ;[/color]
2006-05-29 <1148884536.665 256.218760@j55g 2000cwa.googleg roups.com>, v4vijayakumar wrote:[color=blue]
>
> Keith Thompson wrote:[color=green]
>> "v4vijayaku mar" <v4vijayakumar@ yahoo.com> writes:[color=darkred]
>> > why the following statement dumps the core(Segmentati on fault)?
>> >
>> > printf("%s\n", __FILE__);[/color]
>>
>> Perhaps because it's not part of a complete C program. (If it were,
>> presumably you would have shown it to us.)
>>
>> Or maybe the previous line defines printf as a macro.
>>
>> Or maybe you closed stdout.
>>
>> If you want actual answers rather than guesses, show us a complete
>> program. If this is a quiz rather than an actual problem you're
>> having, please say so.
>>
>> --
>> Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
>> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
>> We must do something. This is something. Therefore, we must do this.[/color]
>
> sorry. Here is the complete program and sample run.
>
> #cat test.c
> #include <stdio.h>
>
> int main()
> {
> printf("%s\n", __FILE__);
> printf("%s\n", __LINE__);
> printf("%s\n", __func__);
>
> return 0;
>}
>
> #cc test.c
> #./a.out
> test.c
> Segmentation fault (core dumped)
> #[/color]
printf("%s\n",_ _LINE__) is causing the segfault. if it were the __FILE__
line causing the segfault, you likely wouldn't see its output.
2006-05-29 <1148897823.069 006.190830@y43g 2000cwc.googleg roups.com>, santosh wrote:[color=blue]
> Keith Thompson wrote:[color=green]
>> "santosh" <santosh.k83@gm ail.com> writes:[color=darkred]
>> > Keith will provide the last word but as far as can tell from my library
>> > refernce, __LINE__ expands to a decimal integer constant. So yes, %d is
>> > needed and %s is the wrong conversion specifier to use.[/color][/color]
> ... snip ...
>[color=green]
>> A couple of odd points: C99 dropped the requirement that __LINE__ has
>> to be a *decimal* constant. And neither standard requires the
>> expansion of __LINE__ to be of type int. Possibly that's meant to
>> cater to implementations with 16-bit int that allow source files
>> longer than 32767 lines.
>>
>> It looks like, strictly speaking, this:
>> printf("%d\n", __LINE__);
>> could invoke undefined behavior; to avoid the problem, use this:
>> printf("%d\n", (int)__LINE__);
>> or this:
>> printf("%ld\n", (long)__LINE__) ;[/color]
>
> Or:
> printf("%lu\n", (unsigned long) __LINE__);[/color]
"santosh" <santosh.k83@gm ail.com> writes:[color=blue]
> Keith Thompson wrote:[color=green]
>> "santosh" <santosh.k83@gm ail.com> writes:[color=darkred]
>> > Keith will provide the last word but as far as can tell from my library
>> > refernce, __LINE__ expands to a decimal integer constant. So yes, %d is
>> > needed and %s is the wrong conversion specifier to use.[/color][/color]
> ... snip ...
>[color=green]
>> A couple of odd points: C99 dropped the requirement that __LINE__ has
>> to be a *decimal* constant. And neither standard requires the
>> expansion of __LINE__ to be of type int. Possibly that's meant to
>> cater to implementations with 16-bit int that allow source files
>> longer than 32767 lines.
>>
>> It looks like, strictly speaking, this:
>> printf("%d\n", __LINE__);
>> could invoke undefined behavior; to avoid the problem, use this:
>> printf("%d\n", (int)__LINE__);
>> or this:
>> printf("%ld\n", (long)__LINE__) ;[/color]
>
> Or:
> printf("%lu\n", (unsigned long) __LINE__);[/color]
Yes, that's an important improvement if the printf statement appears
on line 2147483648 or later of the source file. 8-)}
--
Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Comment