473,397 Members | 1,961 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,397 software developers and data experts.

error and warning

mdh
I am not sure if this is directly related to C or the compiler (Xcode)
I am using...but I will ask and see.
I have started creating unique C files ( .c and .h) so that I can
reuse some of the functions when doing the exercises in K&R.
The code below is created in , for example, foo.c. Main.c contains the
directive #included <stdio.hyet unless I include the same directive
in foo.h, the error and warning will not go away. My understanding,
( or rather misunderstanding ) was that once declared in main, one
need not "redeclare" in foo.h. If this is unique to Xcode, I will ask
there.
Thank you.
/*foo.h*/
/* #include <stdio.h */ <<<uncommenting this removes the error
and warning.
char *alloc (int );
void afree(char *p1);

/*foo.c*/
#include "foo.h"
#define BUFFSIZE 120

static char allocbuff[BUFFSIZE];
static char *p = allocbuff; /*assigns ptr to buffer*/
char static *pend = allocbuff + BUFFSIZE;
char *alloc ( int n) {
if ((p + n) < pend){

p +=n;

return p - n;}

else

return NULL; /* error: 'NULL' undeclared (first use in this
function) *****ERROR HERE

}

void afree(char *p1){

if ( p1 >= allocbuff && p1 < pend)

p=p1;

else

printf( "Error: Space requested is not available") ; /* "warning:
incompatible implicit declaration of built-in function 'printf' */
***WARNING HERE

}
Jun 27 '08 #1
18 1367
mdh <md**@comcast.netwrites:
I have started creating unique C files ( .c and .h) so that I can
reuse some of the functions when doing the exercises in K&R.
The code below is created in , for example, foo.c. Main.c contains the
directive #included <stdio.hyet unless I include the same directive
in foo.h, the error and warning will not go away. My understanding,
( or rather misunderstanding ) was that once declared in main, one
need not "redeclare" in foo.h. If this is unique to Xcode, I will ask
there.
No, it is common to all conforming C compilers. Every translation
unit much have declarations for the things it uses. You choose to
include <stdio.hin "foo.h", but this is not the usual way to do it.
It is foo.c the needs some things from stdio.h and it is in foo.c that I
would put the #include <stdio.h>.

<snip>
char *alloc ( int n) {
if ((p + n) < pend){

p +=n;

return p - n;}

else

return NULL; /* error: 'NULL' undeclared (first use in this
function) *****ERROR HERE

}
Is your code really formatted out like this? If so, you will annoy
most people who have to read it!

--
Ben.
Jun 27 '08 #2
mdh said:
I am not sure if this is directly related to C or the compiler (Xcode)
I am using...but I will ask and see.
I have started creating unique C files ( .c and .h) so that I can
reuse some of the functions when doing the exercises in K&R.
The code below is created in , for example, foo.c. Main.c contains the
directive #included <stdio.hyet unless I include the same directive
in foo.h, the error and warning will not go away.
It's not entirely clear what you're doing, but I *guess* that you are
(rightly) compiling foo.c and Main.c (or possibly main.c) separately and
linking them at the end - which is fine.

But remember that they *are* compiled separately. The compiler can't (or at
least, is not required to) see what you've done in main.c when it's
compiling foo.c, and vice versa. So if you need to call printf (or
whatever) in both, you need <stdio.hto be included in both.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #3
mdh wrote:
I am not sure if this is directly related to C or the compiler (Xcode)
I am using...but I will ask and see.
I have started creating unique C files ( .c and .h) so that I can
reuse some of the functions when doing the exercises in K&R.
The code below is created in , for example, foo.c. Main.c contains the
directive #included <stdio.hyet unless I include the same directive
in foo.h, the error and warning will not go away. My understanding,
( or rather misunderstanding ) was that once declared in main, one
need not "redeclare" in foo.h. If this is unique to Xcode, I will ask
there.
Actually every translation unit (or "source file") must include the
appropriate headers to compile. Hence if foo.c uses functions from
stdio.h, then you must place a include directive for that header at the
top of foo.c or in a private header that foo.c includes. You could also
place the include directive in foo.h, but that is not really a good
design, since foo.h is mainly meant for code that calls services in
foo.c.

<snip>

Jun 27 '08 #4
mdh
>
Is your code really formatted out like this? *If so, you will annoy
most people who have to read it!

--
Ben.
The **last** thing I would try and do is annoy people in this
group!!! :-)
So, being a relative novice to C, I am open to suggestions.

As to the question, thanks for the answer.
Jun 27 '08 #5
mdh
On Apr 24, 7:29*am, Richard Heathfield
But remember that they *are* compiled separately. The compiler can't (or at
least, is not required to) see what you've done in main.c when it's
compiling foo.c, and vice versa. So if you need to call printf (or
whatever) in both, you need <stdio.hto be included in both.

<snip>

Ok... thank you Richard and Santosh for your answers.
Jun 27 '08 #6
mdh <md**@comcast.netwrites:
>>
Is your code really formatted out like this? Â*If so, you will annoy
most people who have to read it!

--
Ben.

The **last** thing I would try and do is annoy people in this
group!!! :-)
Then best not to quote sigs, too. :-)
So, being a relative novice to C, I am open to suggestions.
I learned C from K&R and this is, pretty much, how I lay out my code,
but there are lots of other perfectly dreadfu^H^H^H^H^H^H^Hacceptable
ways to do it.

The two keys points are to be (a) consistent and (b) reasonably
generous with space. I hate this form:

if( x < 42 )
{
y += x;
return( y * y );
}
else
{
return( -x );
}

but I would not comment on it since it is reasonably clear and
consistent. I *would* comment on:

if(x<43){
y+=x; return y*y;}
else {
return (-x);
}

because it seems to be neither. I'd write:

if (x < 42) {
y += x;
return y * y;
}
else
return -x;

--
Ben.
Jun 27 '08 #7
mdh
. *I'd write:
>
* if (x < 42) {
* * * y += x;
* * * return y * y;
* }
* else
* * * return -x;

--
Ben.
Points taken...or should I say "pointers" taken. :-)

thank you Ben.
Jun 27 '08 #8
mdh said:
>
>>
Is your code really formatted out like this? If so, you will annoy
most people who have to read it!

--
Ben.

The **last** thing I would try and do is annoy people in this
group!!! :-)
So, being a relative novice to C, I am open to suggestions.
Everything should be either useful or ornamental. If it's both, that's a
bonus. Code layout is *useful* for indicating structure.

Here are two versions of the same code (which is based pretty solidly on a
K&R code sample), with everything replaced by whitespace except the
structure words and the braces. The first uses K&R bracing style, and the
second Allman style. Pick the style you think indicates structure more
clearly, and consider using it from now on.

K&R style:

main()
{
while() {
while() {
if() {
} else if() {
} else if() {
} else {
}
}
}
}

Allman style:

main()
{
while()
{
while()
{
if()
{
}
else if()
{
}
else if()
{
}
else
{
}
}
}
}
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #9
mdh
On Apr 24, 5:54*pm, Richard Heathfield
Everything should be either useful or ornamental. If it's both, that's a
bonus. Code layout is *useful* for indicating structure.

Here are two versions of the same code (which is based pretty solidly on a
K&R code sample), with everything replaced by whitespace except the
structure words and the braces. The first uses K&R bracing style, and the
second Allman style. Pick the style you think indicates structure more
clearly, and consider using it from now on.

Thank you Richard.
Jun 27 '08 #10
Richard Heathfield wrote, On 25/04/08 01:54:

<snip>
second Allman style. Pick the style you think indicates structure more
clearly, and consider using it from now on.

K&R style:

main()
<snip>

To the OP, it is better not to rely on implicit int but rather to be
specific about the return type. It is also better to be specific if a
function does not take parameters. So

int main(void)

rather than
main()

One reason for this is that the latest standard (not commonly fully
implemented) no longer has implicit int, so the "main ()" form does not
work in it.

Richard, would it not be better to give a good example when showing a
newbie styles and telling them to pick one?
--
Flash Gordon
Jun 27 '08 #11
Flash Gordon said:

<snip>
int main(void)

rather than
main()

One reason for this is that the latest standard (not commonly fully
implemented) no longer has implicit int, so the "main ()" form does not
work in it.
You forgot to complain about the missing definitions, contents of while()
and if(), lack of return code, etc etc.
Richard, would it not be better to give a good example when showing a
newbie styles and telling them to pick one?
I showed him K&R (1TBS) and Allman, with huge swathes of stuff chopped out
so that the brace placement stood out in sharp relief.

If you don't think that K&R and Allman are good examples of indenting
style, perhaps you'd show us something that you think /is/ a good example.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #12
Flash Gordon wrote:
Richard Heathfield wrote, On 25/04/08 01:54:

<snip>
>second Allman style. Pick the style you think indicates structure
more clearly, and consider using it from now on.

K&R style:

main()

<snip>

To the OP, it is better not to rely on implicit int but rather to be
specific about the return type. It is also better to be specific if a
function does not take parameters. So

int main(void)

rather than
main()

One reason for this is that the latest standard (not commonly fully
implemented) no longer has implicit int, so the "main ()" form does
not work in it.

Richard, would it not be better to give a good example when showing a
newbie styles and telling them to pick one?
I believe 'mdh' has been following this group long enough to have read
about both implicit int and the recommended form for main more times
than he could enumerate. :-)

Jun 27 '08 #13
Richard Heathfield wrote, On 25/04/08 09:15:
Flash Gordon said:

<snip>
>int main(void)

rather than
main()

One reason for this is that the latest standard (not commonly fully
implemented) no longer has implicit int, so the "main ()" form does not
work in it.

You forgot to complain about the missing definitions, contents of while()
and if(), lack of return code, etc etc.
The missing contents for while()/if() are far more obvious since the
compiler will produce a diagnostic (almost certainly an error) for them.
Remember that some people would use "main()" and not have the compiler
produce any warning about it (by default the version of gcc *you* have
install won't warn about it, and I said gcc not your alias for it which
adds loads of options). If I had thought more I probably would have
complained about you not returning a value from main as well if you
didn't indicate that.
>Richard, would it not be better to give a good example when showing a
newbie styles and telling them to pick one?

I showed him K&R (1TBS) and Allman, with huge swathes of stuff chopped out
so that the brace placement stood out in sharp relief.

If you don't think that K&R and Allman are good examples of indenting
style, perhaps you'd show us something that you think /is/ a good example.
I was not complaining about the style of indenting. I was complaining
about you posting something that could be construed as encouraging using
"main()" and I stand by that complaint. I would say that those styles
without the modification of specifying the return type of main are
definitely sub-standard.

If you want what I would consider a good example of style just take your
K&R example (or probably Allman, although I can't remember what that
looked like) and make the return type explicit, use a prototype
definition of main at the start then return an appropriate value at the
end of main.
--
Flash Gordon
Jun 27 '08 #14
Flash Gordon said:

<snip>
I was not complaining about the style of indenting. I was complaining
about you posting something that could be construed as encouraging using
"main()" and I stand by that complaint.
In the same vein, I would just like to point out that the above could be
construed as an explanation that you were complaining about my using a
string literal as the entry point for a C program. But thus to construe it
would be daft, right?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #15
Richard Heathfield wrote, On 26/04/08 03:47:
Flash Gordon said:

<snip>
>I was not complaining about the style of indenting. I was complaining
about you posting something that could be construed as encouraging using
"main()" and I stand by that complaint.

In the same vein, I would just like to point out that the above could be
construed as an explanation that you were complaining about my using a
string literal as the entry point for a C program. But thus to construe it
would be daft, right?
It could not be so construed in context. Your post in context could
easily be construed as I described.
--
Flash Gordon
Jun 27 '08 #16
Flash Gordon said:
Richard Heathfield wrote, On 26/04/08 03:47:
>Flash Gordon said:

<snip>
>>I was not complaining about the style of indenting. I was complaining
about you posting something that could be construed as encouraging
using "main()" and I stand by that complaint.

In the same vein, I would just like to point out that the above could be
construed as an explanation that you were complaining about my using a
string literal as the entry point for a C program. But thus to construe
it would be daft, right?

It could not be so construed in context. Your post in context could
easily be construed as I described.
Sorry, but I disagree. It would be daft to do that, and I don't accept that
either you or the OP is daft.
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #17
Richard Heathfield wrote, On 26/04/08 12:28:
Flash Gordon said:
>Richard Heathfield wrote, On 26/04/08 03:47:
>>Flash Gordon said:

<snip>

I was not complaining about the style of indenting. I was complaining
about you posting something that could be construed as encouraging
using "main()" and I stand by that complaint.
In the same vein, I would just like to point out that the above could be
construed as an explanation that you were complaining about my using a
string literal as the entry point for a C program. But thus to construe
it would be daft, right?
It could not be so construed in context. Your post in context could
easily be construed as I described.

Sorry, but I disagree. It would be daft to do that, and I don't accept that
either you or the OP is daft.
I know that you do not recommend that form. However since we get people
coming he here using it some people would not realise that, and the
audience is not only me and the OP. However, as we are not going to
agree we might as well drop it.
--
Flash Gordon
Jun 27 '08 #18
Ben Bacarisse wrote:
>
.... snip ...
>
but I would not comment on it since it is reasonably clear and
consistent. I *would* comment on:

if(x<43){
y+=x; return y*y;}
else {
return (-x);
}

because it seems to be neither. I'd write:

if (x < 42) {
y += x;
return y * y;
}
else
return -x;
I'll go along so far, but I would write :-)

if (x >= 42) return -x;
else {
y += x;
return y * y;
}

and I _might_ use '42 <= x' in the conditional. If y is automatic
and local scope I would also seriously consider:

if (42 <= x) return -x;
else {
y += x; return y * y;
}

Note the implementation of 'long term last'.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #19

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

Similar topics

0
by: Cary | last post by:
Trying to install on SuSE 8.2 from source. ../configure --with-apxs=/usr/local/apache/bin/apxs --with-mysql --with-unixODBC=/usr/lib getting this error: /root/php-4.3.2/ext/odbc/php_odbc.c -o...
14
by: kosuke | last post by:
I keep getting the following error/warning message when using the python based program getmail4: /usr/lib/python2.3/optparse.py:668: FutureWarning: %u/%o/%x/%X of negative int will return a...
1
by: Azzedine | last post by:
Dear All, I compiled a project under Unix, I have got the following errors: ------------------------------------------ CC -I. -DUNIX -c XPCFileStat.C -g -o XPCFileStat.o "XPCFileStat.C",...
1
by: yanwan | last post by:
Hello I met some problems in linking a project, and hope someone can give me some advice. -----------Configuration: lighting - Win32 Release-------------------- Linking... LINK : warning...
0
by: mmarkzon | last post by:
I have been struggling compiling linkchecker from http://linkchecker.sourceforge.net/. The last thing I get is "error: command 'gcc' failed with exit status 1" which is not very helpful. This is...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
7
by: i | last post by:
#include<stdio.h> #include<conio.h> #include<process.h> #include<string.h> char ch; int w; int n,m; //void main(); char check(int n,int m,char ch); void cash(int n,int m,char ch);
0
by: bill gates | last post by:
I was trying to use msbee to build an old project. I don't want to upgrade to dotnet2.0. Is there any way to get this to compile using DevStudio 2005 using dotnet1.1. ? Build FAILED....
39
by: Tsb | last post by:
Now I use FreeBSD 7.0 Current with Gnome. And I use Anjuta IDE to write my C program, and then just do like this. #gcc MYFILE.NAME -o MYFILE.NAME #MYFILE.NAME then it works well. but second...
1
by: dewi | last post by:
Dear All, I am trying to compile a C code using Visual C++. Can anyone explain how to solve it? Thank You. #include <math.h> #include <string.h> #include "RV2AJFRONT_NEW.h" #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.