make compilation fail

S

star_night

Hi,

I am starting to learn C laguange. I am using gcc 3.3.2 on
Solaris10(SPARC).

test1.c
-------
main()
{
int i =3;

printf("\n Address of i = %u", &i);
printf("\n Value of i =%d", i);
printf("\n Value of i =%d", *(&i));

}

gcc test1.c succeeds. How gcc knows where to find printf? I
deliberately set GCC_EXEC_PREFIX to /tmp in my shell to make it fail,
but this does not work. I guess, the question is how to make gcc forget
default search locations for libraries.

Sometime in the past I read that to get printf working, you should
include stdio.h. Is that not a requirement anymore?(again standard
includes kicking in?)

Thanks
SN
 
R

Richard Bos

main()
{
int i =3;

printf("\n Address of i = %u", &i);
printf("\n Value of i =%d", i);
printf("\n Value of i =%d", *(&i));

}

gcc test1.c succeeds. How gcc knows where to find printf?

Who knows? As far as C is concerned, by magic, or in its left back
pocket. As long as it _does_ find it, the Standard is satisfied.
deliberately set GCC_EXEC_PREFIX to /tmp in my shell to make it fail,
but this does not work. I guess, the question is how to make gcc forget
default search locations for libraries.

That question is gcc-specific, and should therefore be asked in a gcc
newsgroup, not here.
Sometime in the past I read that to get printf working, you should
include stdio.h. Is that not a requirement anymore?

Well, yes and no. First of all, C99 does require that you have a
declaration in scope for any function, but you're probably not calling
gcc as a C99 compiler. Second, even under C89, calling a variadic
function (such as printf()) without the right declaration has always
been undefined behaviour. This means that the implementation need not
make your program work correctly, but it may: _you_ are required to
#include <stdio.h> (or equivalent); if the implementation feels lenient,
it is allowed to make your program work even if you do not; but it would
be a very bad idea to rely on this.

Richard
 
C

CBFalconer

I am starting to learn C laguange. I am using gcc 3.3.2 on
Solaris10(SPARC).

If the compiler and system matter the question is off-topic here.
However, they don't.
test1.c
-------
main()
{
int i =3;

printf("\n Address of i = %u", &i);
printf("\n Value of i =%d", i);
printf("\n Value of i =%d", *(&i));
}

gcc test1.c succeeds. How gcc knows where to find printf? I
deliberately set GCC_EXEC_PREFIX to /tmp in my shell to make it
fail, but this does not work. I guess, the question is how to
make gcc forget default search locations for libraries.

Sometime in the past I read that to get printf working, you
should include stdio.h. Is that not a requirement anymore?(again
standard includes kicking in?)

<OT> You should use at least -W -Wall -ansi -pedantic with gcc to
have a standards compliant compiler. Contrast your program above
with the following, which has various failings fixed. Look
closely.

#include <stdio.h>
int main(void)
{
int i = 3;

printf(" Address of i = %p\n", (void *)&i);
printf(" Value of i =%d\n", i);
printf(" Value of i =%d\n", *(&i));
return 0;
}
 
M

Malcolm

Sometime in the past I read that to get printf working, you should
include stdio.h. Is that not a requirement anymore?(again standard
includes kicking in?)
It's the other way round. In the olden days, it was sometimes the done thing
to call functions without a prototype. It is still possible to do this in
some cases for reasons of backwards compatibility, but things have tightened
up. Current thinking is that prototypes are desireable and compilers should
complain if not given them. Certainly you should always include stdio.h if
you wnat to use printf().
 
C

CBFalconer

Malcolm said:
It's the other way round. In the olden days, it was sometimes the
done thing to call functions without a prototype. It is still
possible to do this in some cases for reasons of backwards
compatibility, but things have tightened up. Current thinking is
that prototypes are desireable and compilers should complain if
not given them. Certainly you should always include stdio.h if you
wnat to use printf().

Not only should, but must. Using any variadic function without a
prototype in scope results in undefined behaviour.
 
P

Peter Nilsson

CBFalconer said:
Not only should, but must. Using any variadic function without a
prototype in scope results in undefined behaviour.

You don't _have_ to include <stdio.h> to get a prototype for
printf...

int printf(const char *, ...);

int main(void)
{
printf("%s %s\n", "Hello", "World");
return 0;
}

This is valid in C99 too, even though the first parameter of
printf is normally restrict qualified.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top