V
v4vijayakumar
why the following statement dumps the core(Segmentation fault)?
printf("%s\n", __FILE__);
printf("%s\n", __FILE__);
v4vijayakumar said:why the following statement dumps the core(Segmentation fault)?
printf("%s\n", __FILE__);
Keith said: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.
Perhaps because it's not part of a complete C program. (If it were,
presumably you would have shown it to us.)
v4vijayakumar said: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)
#
gcc -E test.c |tail
v4vijayakumar said: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__);
v4vijayakumar said: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;
}
Stephen said: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:
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.
v4vijayakumar said:why the following statement dumps the core(Segmentation fault)?
printf("%s\n", __FILE__);
santosh said:Stephen said:[snip]v4vijayakumar said: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;
}
__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.
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.
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__);
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)
#
... snip ...
Or:
printf("%lu\n", (unsigned long) __LINE__);
Jordan said:or
#define s(x) #x
printf("%s\n", s(__LINE__));
or even
printf(""s(__LINE__)"\n");
santosh said:... snip ...
Or:
printf("%lu\n", (unsigned long) __LINE__);
Keith said: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__);
I tought that parameters passed to
printf that matched a %d conversion in a printf call were implicitly
converted to int, isn't it?
I mean, if you have the following snippet (take all needed headers as
included):
unsigned short int a = 5;
printf("%d\n", a);
printf function doesn't receive an 'int' argument (by making an implicit
conversion to 'a')? Or I'm totally wrong?
Alberto said:I don't understand that last point. I tought that parameters passed to
printf that matched a %d conversion in a printf call were implicitly
converted to int, isn't it?
I mean, if you have the following snippet (take all needed headers as
included):
unsigned short int a = 5;
printf("%d\n", a);
printf function doesn't receive an 'int' argument (by making an implicit
conversion to 'a')? Or I'm totally wrong?
I don't understand that last point. I tought that parameters passed to
printf that matched a %d conversion in a printf call were implicitly
converted to int, isn't it?
I mean, if you have the following snippet (take all needed headers as
included):
unsigned short int a = 5;
printf("%d\n", a);
printf function doesn't receive an 'int' argument (by making an implicit
conversion to 'a')? Or I'm totally wrong?
But they are promoted by standard rules; actually you must ensure thatNo. Parameter values *never* have their type converted based upon
the format string: instead it is your responsibility to ensure that
the format string matches the parameter value's type.
The unsigned short will undergo "the usual conversions" to
unsigned int just because it appears in a function call in a position
prototyped as ... (three periods). %d is the wrong format string
to print out an unsigned int. You just didn't happen to notice it
because the value wasn't greater than the maximum signed positive
integer.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.