473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

warning: implicit declaration of function 'printf'

I get this warning:

warning: incompatible implicit declaration of built-in function 'printf'

because I use printf in a function that I include in a .h file that is
included by other files...Do I need to remove the printf call?
Mar 30 '06 #1
6 119484
Johs32 wrote:
I get this warning:

warning: incompatible implicit declaration of built-in function 'printf'

because I use printf in a function that I include in a .h file that is
included by other files...Do I need to remove the printf call?


No, you need to make sure <stdio.h> has been included before the point where
you invoke printf(), then fix any errors that result if the call is incorrect.

And if I'm reading you correctly, you're putting code in a header. Don't do
that. Headers should be used for declarations only.

S.
Mar 30 '06 #2
In article <44***********************@news.xs4all.nl>,
Skarmander <in*****@dontmailme.com> wrote:
And if I'm reading you correctly, you're putting code in a header. Don't do
that. Headers should be used for declarations only.


That's a matter of style, not any restriction or recommendation of
the C language itself.

If one had a static function (i.e., file scope) that was used in several
places, I'm not sure that it would make much difference whether
one put the code into a file that was included where needed,
compared to if one put a #define of all of the code into a header
and then explicitly called upon the macro to define the function.
Using the macro version requires using continuation lines all
over the place, and would not allow for the possibility of
conditional compilation in the function definition.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
Mar 30 '06 #3
Walter Roberson wrote:
In article <44***********************@news.xs4all.nl>,
Skarmander <in*****@dontmailme.com> wrote:
And if I'm reading you correctly, you're putting code in a header. Don't do
that. Headers should be used for declarations only.
That's a matter of style, not any restriction or recommendation of
the C language itself.

Of course. The phrase "don't do that" usually signals an opinion on style.
"You can't do that" would be a language restriction.
If one had a static function (i.e., file scope) that was used in several
places, I'm not sure that it would make much difference whether
one put the code into a file that was included where needed,
compared to if one put a #define of all of the code into a header
and then explicitly called upon the macro to define the function.
Using the macro version requires using continuation lines all
over the place, and would not allow for the possibility of
conditional compilation in the function definition.


The first thing that springs to mind is that a function with file scope that
is used in several places is a contradiction in terms, but of course this is
not true; the odd situation is a consequence of C's approach to modularity
being restricted to files. One could have a library where externally visible
functions in different files needed to call a function that should not be
externally visible.

I'd argue that linkers are supposed to solve this problem, even if "linking"
is not part of the C language. One benefit to a solution through a linker is
that there really is only one function, not one function copied many times.
(This discussion is orthogonal to inline functions, which can also be
implemented by putting code in headers, and also have alternate approaches.)

But assuming linker magic isn't a solution, I do agree that #including the
function definition is cleaner than using a macro. I don't think that using
a macro is better just because it's supposed to get around the "don't put
code in a header" recommendation. The point of that recommendation is to
encourage people to have a clean split between interfaces and
implementations. You can do that even when you put code in a header, if you
know what you're doing.

It's comparable to "don't use goto". There will be circumstances where the
most appropriate course of action is to use goto, but if you can recognize
them you don't need the recommendation. All that is intended is to
discourage people from doing it arbitrarily.

S.
Mar 30 '06 #4
On Thu, 30 Mar 2006 13:27:41 +0000 (UTC), in comp.lang.c ,
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
In article <44***********************@news.xs4all.nl>,
Skarmander <in*****@dontmailme.com> wrote:
And if I'm reading you correctly, you're putting code in a header. Don't do
that. Headers should be used for declarations only.
That's a matter of style, not any restriction or recommendation of
the C language itself.


Not entirely. If you put functions in a header, you get multiple
definitions. Plus it makes it even harder to find functions.
If one had a static function (i.e., file scope) that was used in several
places, I'm not sure that it would make much difference whether
one put the code into a file that was included where needed,


If you have a function you need in several files, its by definition
not a static, and shouldn't be declared as such. You're just bloating
your code. Put a declation in the header and a body in a source file.
Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 30 '06 #5
"Mark McIntyre" <ma**********@spamcop.net> wrote in message
news:sv********************************@4ax.com...
On Thu, 30 Mar 2006 13:27:41 +0000 (UTC), in comp.lang.c ,
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
In article <44***********************@news.xs4all.nl>,
Skarmander <in*****@dontmailme.com> wrote:
And if I'm reading you correctly, you're putting code in a header. Don't
do
that. Headers should be used for declarations only.


That's a matter of style, not any restriction or recommendation of
the C language itself.


Not entirely. If you put functions in a header, you get multiple
definitions. Plus it makes it even harder to find functions.


Harder to find? When I want to know where a function is, I grep *.h to find
it; it's usually clear from that output if it's defined or declared in the
header. If it's merely declared in the headers, then (depending on the
coding conventions) I may need to look through dozens of .c files to find
the definition.

Still, I would agree with the general "should not" as long as people
recognize that does not equate to "must not", similar to the near-ban on
gotos.
If one had a static function (i.e., file scope) that was used in several
places, I'm not sure that it would make much difference whether
one put the code into a file that was included where needed,


If you have a function you need in several files, its by definition
not a static, and shouldn't be declared as such. You're just bloating
your code. Put a declation in the header and a body in a source file.


Unless the function is declared inline, in which case it's duplicated
everywhere it's used anyway (possibly optimized differently in each case).
The compiler may be inlining your functions anyways without being told to,
depending on the optimization level.

I frequently use static inline functions in headers to replace macros to
avoid multiple-evaluation and type bugs (and I just don't like macros in
general). The gcc-ism of extern inline functions is even better in some
cases, if you have that extension available.

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

*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
Mar 31 '06 #6
On Thu, 30 Mar 2006 21:24:31 -0600, in comp.lang.c , "Stephen Sprunk"
<st*****@sprunk.org> wrote:
Harder to find? When I want to know where a function is, I grep *.h to find
Great. Please feel to apply that idea to a many MLOC project split
into dozens of subdirectories, on a non-Unix platform... .
:-)
Still, I would agree with the general "should not" as long as people

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Mar 31 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jason | last post by:
Hi, Im running windows xp pro and compiling using dev c++ 4. I have the following situation: #include <iostream> #include <string> using namespace std; int main() {
2
by: nick | last post by:
the following is my programming code and compile message why the warning message arise, have i done somethings wrong? #include<stdio.h> typedef struct card{ int abc; }card;
7
by: Paminu | last post by:
On a gentoo linux system i don't get any warnings when I comlpile this code: #include <stdio.h> typedef struct test { void *content; struct test_ *bob; } test_;
5
by: Christian Christmann | last post by:
Hi, in one file I've a function that is used by another file containing the main function: file1.c: .... void test( int a ) { ... } ....
5
by: TalonStriker | last post by:
Hi guys, When I compile this code, I'm getting the following error: warning: implicit declaration of function 'strdup' I've included <string.h> and <wchar.h> so I shouldn't be getting the...
1
by: Amaresh Chandra Das | last post by:
hi i got this type of warning. Can any body will guide me. Thanks in advance Amaresh
5
by: DanielJohnson | last post by:
I call a function which is named as func_name in file /source/folderA/ fileA.c. The actual function definition is in /source/folderB/fileB.c. And I get this error. warning: implicit...
3
by: samdomville | last post by:
hello...upon compilation, i get a warning: "warning: implicit declaration of funciton funciton_name" what, techicallly, is an implicit declaration? how/where would i look to track down the error?...
1
by: samdomville | last post by:
Hello - I have a .c file that gives me a warning "implicit declaration of function function_name". However, I have #included the header file that #defines that funciton, so how is it possible to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.