469,917 Members | 1,834 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,917 developers. It's quick & easy.

make compilation fail

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

Nov 14 '05 #1
5 1343
st********@my-deja.com wrote:
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
Nov 14 '05 #2
st********@my-deja.com wrote:

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;
}

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #3

<st********@my-deja.com> wrote

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().
Nov 14 '05 #4
Malcolm wrote:
<st********@my-deja.com> wrote

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().


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

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5
CBFalconer wrote:
Malcolm wrote:
...
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.


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.

--
Peter

Nov 14 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Steven T. Hatton | last post: by
5 posts views Thread by ideas2050 | last post: by
8 posts views Thread by Boris | last post: by
20 posts views Thread by Srinivas Mudireddy | last post: by
34 posts views Thread by niranjan.singh | last post: by
1 post views Thread by Roumen Petrov | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.