Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > a C program

Reply
Thread Tools

a C program

 
 
akarl
Guest
Posts: n/a
 
      08-15-2005
Baloff wrote:
> 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
 
Reply With Quote
 
 
 
 
Baloff
Guest
Posts: n/a
 
      08-15-2005
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
 
Reply With Quote
 
 
 
 
Raymond Martineau
Guest
Posts: n/a
 
      08-15-2005
On 15 Aug 2005 11:27:09 +1000, Baloff <(E-Mail Removed)> wrote:

>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:uts;

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
had it's <stdio.h> header removed. A pointless exercise, since omitting
such headers can result in undefined behaviour.

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


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-15-2005
* 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?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Baloff
Guest
Posts: n/a
 
      08-15-2005
akarl <(E-Mail Removed)> writes:

> Baloff wrote:
>
> > 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


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
 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      08-15-2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to javac a java program w/ another java program which is w/o a main method cjeffwang@yahoo.com Java 1 10-31-2005 04:25 AM
System program/ Application program ?? Parvsandhu Java 2 07-11-2005 09:08 AM
how to convert a java program to an exe program ola Java 3 02-16-2004 09:42 AM
Calling Java program in another Java program Rey Java 4 12-12-2003 10:18 PM
passing data between Java program and C program--help pipi Java 1 07-21-2003 05:02 AM



Advertisments