a C program

Discussion in 'C++' started by akarl, Aug 15, 2005.

  1. akarl

    akarl Guest

    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
     
    akarl, Aug 15, 2005
    #1
    1. Advertising

  2. akarl

    Baloff Guest

    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
     
    Baloff, Aug 15, 2005
    #2
    1. Advertising

  3. On 15 Aug 2005 11:27:09 +1000, 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>.


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

    >
    >int main(){
    > puts("hello");
    >}
    >
     
    Raymond Martineau, Aug 15, 2005
    #3
  4. * 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?
     
    Alf P. Steinbach, Aug 15, 2005
    #4
  5. akarl

    Baloff Guest

    akarl <> 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
     
    Baloff, Aug 15, 2005
    #5
  6. akarl

    benben Guest

    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
     
    benben, Aug 15, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pipi
    Replies:
    1
    Views:
    639
    Joseph Millar
    Jul 21, 2003
  2. Rey
    Replies:
    4
    Views:
    11,068
    Roedy Green
    Dec 12, 2003
  3. ola
    Replies:
    3
    Views:
    727
    Marco Schmidt
    Feb 16, 2004
  4. Parvsandhu
    Replies:
    2
    Views:
    428
    Thomas Weidenfeller
    Jul 11, 2005
  5. Replies:
    1
    Views:
    544
    Andrew Thompson
    Oct 31, 2005
Loading...

Share This Page