Help. My first .c program (hello world)

Help. My first .c program (hello world)

Post by Matt Sla » Thu, 11 Mar 1999 04:00:00



I made this simple little program, but when I run it nothing happens.

============================================
#include <stdio.h>

main( )
{
 printf("Hello world.");

Quote:}

===============================================

then I compiled it, and that works, because I get an executable file:

  cc -o hello hello.c

But when I run it, it does NOTHING, and I am back at the command prompt.

Surely something simple is worng.

 
 
 

Help. My first .c program (hello world)

Post by pe.. » Thu, 11 Mar 1999 04:00:00



> I made this simple little program, but when I run it nothing happens.
> ============================================
> #include <stdio.h>
> main( )
> {
>  printf("Hello world.");
> }
> ===============================================
> then I compiled it, and that works, because I get an executable file:
>   cc -o hello hello.c
> But when I run it, it does NOTHING, and I am back at the command prompt.
> Surely something simple is worng.

Your systems stdlib seems a little flaky.

You have forgot the trailing '\n' whicg causes your program to
write a new-line. The newline itself might be used by the stdlib
to actually transport the characters to your display.

Normally a program that terminates with character still in the output buffer
would get them displayed. Your system won't.

Keep up with the programming !

--
--
Peter H?kanson            Phone +46 0708 39 23 04
Network Management AB     Fax   +46 031 779 7844
Email : use peter (at) gbg (dot) netman (dot) se  No copy to sanford wallace!

 
 
 

Help. My first .c program (hello world)

Post by Robert J. Clar » Thu, 11 Mar 1999 04:00:00


Hello Matt,

        The reason that you are not seeing any output is that printf()
buffers its output until either a new line "\n" is output or you call
fflush(stdout)

        If you change your print line to either of:

                printf("Hello World.\n");

or

                printf("Hello World.");
                fflush(stdout);

they you should see "Hello World." printed on the the console.

- Rob

--


> I made this simple little program, but when I run it nothing happens.

> ============================================
> #include <stdio.h>

> main( )
> {
>  printf("Hello world.");
> }

> ===============================================

> then I compiled it, and that works, because I get an executable file:

>   cc -o hello hello.c

> But when I run it, it does NOTHING, and I am back at the command prompt.

> Surely something simple is worng.

 
 
 

Help. My first .c program (hello world)

Post by Dr. Michael Alber » Thu, 11 Mar 1999 04:00:00


Quote:>    The reason that you are not seeing any output is that printf()
> buffers its output until either a new line "\n" is output or you call
> fflush(stdout)

>    If you change your print line to either of:

>            printf("Hello World.\n");

> or

>            printf("Hello World.");
>            fflush(stdout);

This is true, but actually the standard requires that when a
program exit()'s, it should flush the stdio buffers.  "Falling off"
the end of main constitutes an implicit call to exit() which
should result in "Hello World." being printed without a new
line.  The original poster's system really is a little buggy,
as another poster pointed out.  One might see what happens
if one puts in an explicit call to exit(0) or return(0) at
the end of the program.

Later, you get to learn the difference between exit() and _exit().
On a UNIX platform, you can use _exit() if you *don't* want
the stdio buffers flushed.  This is sometimes useful with
fork().  Well, lots of fun toy to play with! :-)

Best wishes,
  Mike

 
 
 

Help. My first .c program (hello world)

Post by Alois Steind » Fri, 12 Mar 1999 04:00:00


Hallo,
just try to use a different name for your program. 'test' is a shell
intrinsic. And of course, append a newline.
Alois


> I made this simple little program, but when I run it nothing happens.

> ============================================
> #include <stdio.h>

> main( )
> {
>  printf("Hello world.");
> }

> ===============================================

> then I compiled it, and that works, because I get an executable file:

>   cc -o hello hello.c

> But when I run it, it does NOTHING, and I am back at the command prompt.

> Surely something simple is worng.

--

___________________________________________________________________________

Alois Steindl,                  Tel.: +43 (1) 58801 / 32558
Inst. for Mechanics II,         Fax.: +43 (1) 58801 32598
Vienna University of Technology,

___________________________________________________________________________

 
 
 

Help. My first .c program (hello world)

Post by ray » Tue, 16 Mar 1999 04:00:00


try
# ./hello
in stead of
# hello

> Hello Matt,

>         The reason that you are not seeing any output is that printf()
> buffers its output until either a new line "\n" is output or you call
> fflush(stdout)

>         If you change your print line to either of:

>                 printf("Hello World.\n");

> or

>                 printf("Hello World.");
>                 fflush(stdout);

> they you should see "Hello World." printed on the the console.

> - Rob

> --


> > I made this simple little program, but when I run it nothing happens.

> > ============================================
> > #include <stdio.h>

> > main( )
> > {
> >  printf("Hello world.");
> > }

> > ===============================================

> > then I compiled it, and that works, because I get an executable file:

> >   cc -o hello hello.c

> > But when I run it, it does NOTHING, and I am back at the command prompt.

> > Surely something simple is worng.

 
 
 

1. erroneous "hello" from forked "hello world" process!


fork() and wait() are not part of the C language, so this belongs in
comp.unix.programmer.  I have redirected followups.  (Old-timers may
note that I try to be impartial about all misdirected stuff, not just
MS-DOS stuff).

His TA expects

to produce

but instead it produces one of `Hello\nHello\nWorld' or `Hello\nWorld\nHello'.

Your TA should; he or she should certainly know about the boundaries
between `things in the operating system' and `things not in the
operating system' in order to work with an OS class in the first place.
printf() is part of the C runtime system, *not* the Unix OS; fork()
and wait() are part of the Unix OS, not the C runtime system.  Thus
there is no guarantee about the way the two interact.

In particular, when printing to a `buffered' file the runtime system
buffers (hence the name) the text until it has a `suitable amount' to
write at once.  It then sends it all out with one or more write() system
calls.  Since fork() simply duplicates all the data in the program, it
duplicates any buffered text.  Thus the program above becomes one
process with `Hello\n' buffered, and one with `Hello\nWorld\n' buffered.

The real oddity is not that the word `Hello' is ever duplicated, but
rather that it is sometimes *not* duplicated.  This happens whenever
the string `Hello\n' is written out before the program forks, which
includes whenever the standard output is line buffered or unbuffered.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)

2. sendmail problem

3. g++ errors on hello world program!

4. MS Sound card w/ no manual, no DOS, no nothing

5. Hello World program...

6. grep filter (?)

7. How do i make a graphic "hello World" Program under X?

8. machine freeze and xdm

9. audio "hello world" programs

10. weird problem with simple "hello, world" program

11. Even the simple "Hello world" program can't be compiled.

12. Help me with Perl "Hello world"

13. Help: atomic_t error when compile an simple kernel module "hello world"