C FAQs section 20.27

K

Kenneth Brody

arnuld said:
This is the question that I need some explanation to understand it:

Some features of C which keep it from being a strict subset of C++ (that
is, which keep C programs from necessarily being acceptable to C++
compilers) are that main may be called recursively, character constants
are of type int, prototypes are not required, and void * implicitly
converts to other pointer types.


anyone has an example ?

#include <stdio.h>

int main(int argc,char **argv)
{
printf("%d '%s'\n",argc,(*argv)?(*argv):"<null>");
if ( argc > 0 )
return main(argc-1,argv+1);
return 0;
}

However, my C++ compiler also compiles the above, without any warnings,
even on highest warning level.
I learned C++ 1st. This fact was strange for me.
Okay.


you mean decelerations (e.g of variables and arrays) ? They are also not
required in C++.

No, it says "prototypes".

This is valid C:

int foo()
{
return bar(7,8);
}
int bar(int a,int b)
{
return a+b;
}

My C++ compiler gives an error on the call to bar().
This was the question already ran in a separate thread started by me and I
got nice answers from the regular posters :)


--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Keith Thompson

arnuld said:
This is the question that I need some explanation to understand it:

Some features of C which keep it from being a strict subset of C++ (that
is, which keep C programs from necessarily being acceptable to C++
compilers) are that main may be called recursively, character constants
are of type int, prototypes are not required, and void * implicitly
converts to other pointer types.



anyone has an example ?

Already posted in this thread. Note that calling main recursively is
rarely, if ever, a good idea, and the prohibition in C++ is not a real
burden; you can always write another recursive function and call it
from main.

[...]
you mean decelerations (e.g of variables and arrays) ? They are also not
required in C++.

No, a prototype is simply a function declaration that declares the
types of its parameters. For example, this:

int foo(int x, int y);

is a prototype, and this:

int foo (int x, int y)
{
/* code that uses x and y */
}

is a function definition that includes a prototype. This:

int foo(x, y)
int x;
int y;
{
/* code that uses x and y */
}

is an old-style non-prototype function definition; it's still allowed
in C (even C99), but not in C++. (It's considered poor style even in
C, since it doesn't allow type checking on calls.)

[...]
 
A

arnuld

This is the question that I need some explanation to understand it:



Some features of C which keep it from being a strict subset of C++ (that
is, which keep C programs from necessarily being acceptable to C++
compilers) are that main may be called recursively, character constants
are of type int, prototypes are not required, and void * implicitly
converts to other pointer types.

main may be called recursively

anyone has an example ?


character constants are of type int

I learned C++ 1st. This fact was strange for me.

prototypes are not required


you mean decelerations (e.g of variables and arrays) ? They are also not
required in C++.

void * implicitly converts to other pointer types.

This was the question already ran in a separate thread started by me and I
got nice answers from the regular posters :)
 
K

Keith Thompson

arnuld said:
In my other thread titled "K&R2, exercise 7.6". There is one function
named "compare_files" if I remove the prototype then program does not
compile, which directly means C requires a prototype but the example given
by "Kenneth Brody" in the other post compiles fine without any prototype.
here is the code for both programs:

http://dpaste.com/46209/
http://dpaste.com/46210/

No, the fact that removing a prototype causes the program to fail to
compile doesn't imply that C requires a prototype. In fact, when I
remove the prototype of "compare_files" in your first example, it
compiles with just a warning.

[...]
 
A

arnuld

#include <stdio.h>

int main(int argc,char **argv)
{
printf("%d '%s'\n",argc,(*argv)?(*argv):"<null>");
if ( argc > 0 )
return main(argc-1,argv+1);
return 0;
}
However, my C++ compiler also compiles the above, without any warnings,
even on highest warning level.


mine does:

[arnuld@raj C]$ g++ -ansi -pedantic -Wall -Wextra test.c
test.c: In function `int main(int, char**)':
test.c:18: error: ISO C++ forbids taking address of function `::main'
[arnuld@raj C]$



int foo()
{
return bar(7,8);
}
int bar(int a,int b)
{
return a+b;
}

My C++ compiler gives an error on the call to bar().


mine does not
 
A

arnuld

No, a prototype is simply a function declaration that declares the
types of its parameters. For example, this:

int foo(int x, int y);

is a prototype, and this:

int foo (int x, int y)
{
/* code that uses x and y */
}

is a function definition that includes a prototype.


In my other thread titled "K&R2, exercise 7.6". There is one function
named "compare_files" if I remove the prototype then program does not
compile, which directly means C requires a prototype but the example given
by "Kenneth Brody" in the other post compiles fine without any prototype.
here is the code for both programs:

http://dpaste.com/46209/
http://dpaste.com/46210/

int foo(x, y)
int x;
int y;
{
/* code that uses x and y */
}

is an old-style non-prototype function definition; it's still allowed
in C (even C99), but not in C++. (It's considered poor style even in
C, since it doesn't allow type checking on calls.)

yes, I saw it for 1st time in K&R2 :)
 
S

santosh

arnuld said:
On Mon, 21 Apr 2008 09:05:01 -0700, Keith Thompson wrote:

No, a prototype is simply a function declaration that declares the
types of its parameters. For example, this:

int foo(int x, int y);

is a prototype, and this:

int foo (int x, int y)
{
/* code that uses x and y */
}

is a function definition that includes a prototype.


In my other thread titled "K&R2, exercise 7.6". There is one function
named "compare_files" if I remove the prototype then program does not
compile, which directly means C requires a prototype [...]

No. C does not require a prototype and the code should compile under all
versions of Standard C, though diagnostics (again not absolutely
necessary) may be produced by your compiler. However implicit int
return type has been outlawed by the C99 standard.

<snip>
 
H

Harald van Dijk

In my other thread titled "K&R2, exercise 7.6". There is one function
named "compare_files" if I remove the prototype then program does not
compile, [...]

http://dpaste.com/46209/

Others have already replied stating that C does not require prototypes,
but I'd like to ask how you "removed the prototype". If you tried to
compile

int main( int argc, char* argv[] )
{
FILE *pf1, *pf2;
/* ... */
compare_files( pf1,pf2 );
/* ... */
}

void compare_files( FILE* pf1, FILE* pf2 )
{
/* ... */
}

you may get an error, as this implicitly declares compare_files as
returning int (since no declaration of compare_files is visible yet when
it is called), and then defines it as returning void. You don't need a
prototype, but you do need a declaration:

void compare_files();

int main(argc, argv)
int argc;
char *argv[];
{
FILE *pf1, *pf2;
/* ... */
compare_files( pf1,pf2 );
/* ... */
}

void compare_files(pf1, pf2)
FILE *pf1;
FILE *pf2;
{
/* ... */
}
 
A

arnuld

No, the fact that removing a prototype causes the program to fail to
compile doesn't imply that C requires a prototype. In fact, when I
remove the prototype of "compare_files" in your first example, it
compiles with just a warning.


I thought by using "-ansi" flag with GCC I am using ANSI C
 

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
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top