a C program

A

akarl

Baloff said:
Hello
I am learning C++, a book exercise is asking to write a C program for
the purpose of learning compilation output when compile with c or c++
compiler. the program must use puts() without <stdio.h>.

is this a valid C program?

#include <iostream.h>

int main(){
puts("hello");
}

No, but this is:

#include <stdio.h>

int main(void)
{
puts("hello");
return 0;
}


August
 
B

Baloff

Hello
I am learning C++, a book exercise is asking to write a C program for
the purpose of learning compilation output when compile with c or c++
compiler. the program must use puts() without <stdio.h>.

is this a valid C program?

#include <iostream.h>

int main(){
puts("hello");
}


thanks
 
R

Raymond Martineau

Hello
I am learning C++, a book exercise is asking to write a C program for
the purpose of learning compilation output when compile with c or c++
compiler. the program must use puts() without <stdio.h>.

That book exercise doesn't sound like it provides much benefit - at least
not at the stage mhere most beginner programmers are at. It also appears
your reference is slightly out of date, as the header files used are part
of an older standard.

In the current standard, such a program is trivial:

#include <cstdio>

using std::puts;

int main(void)
{
puts("hello");
return 0;
}

But that's another story. I'd suggest to switch books if you become too
confused.
is this a valid C program?

#include <iostream.h>

This would fail if it were a C program. <iostream.h> is a c++ header only.

Most likely, the book intends you to try compiling a "normal" program that
 
A

Alf P. Steinbach

* Baloff:
so it compiled all fine and ran ok, what the point?

If it did then that would be an important point, but it didn't: you didn't
do what the exercise asked, namely to compile as C++.

Checking date...

Huh? It's not september yet?
 
B

Baloff

akarl said:
No, but this is:

#include <stdio.h>

int main(void)
{
puts("hello");
return 0;
}


August

I have this exercise, now I am confused because I don't know what to
make out of its purpose.
In the Standard C library, the function puts( ) prints a char array to
the console (so you can say puts("hello")). Write a C program that
uses puts( ) but does not include <stdio.h> or otherwise declare the
function. Compile this program with your C compiler.
(Some C++ compilers are not distinct from their C compilers; in this
case you may need to discover a command-line flag that forces a C
compilation.) Now compile it with the C++ compiler and note the difference.

*********************main.c********************
//main.c
int main(void){
puts("hello");
return 0;
}
********************makefile********************
cproj: main.o
gcc -o $@ main.o

#did not work
#cproj2: main.o #another way of doing it
#insert <tab> here, g++ -x c -o $@ main.o

cppproj: main.o
g++ -Wall -o $@ main.o

clean:
rm -f *.o cp*

********************output********************
$ make clean
rm -f *.o cp*
$ make cppproj
cc -c -o main.o main.c
g++ -Wall -o cppproj main.o
$ ls
cppproj main.c main.o makefile
$ make cproj
gcc -o cproj main.o
$ ls
cppproj cproj main.c main.o makefile
$ ./cppproj
hello
$ ./cproj
hello

so it compiled all fine and ran ok, what the point?

thank you
 
B

benben

I think the point of the exercise is to hand write the puts() declaration
then manually link it with the C Runtime Library...perhaps...

ben
 

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

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top