Hello World

/* * File: hello.c * ------------- * This program prints the message "Hello, world." * on the screen. The program is taken from the * classic C reference text "The C Programming * Language" by Brian Kernighan & Dennis Ritchie. */ #include <stdio.h> main() { printf("Hello, world.\n"); }

Compiling and running the program

lassie% ls hello.c hello2.c lassie% cc hello.c lassie% ls a.out hello.c hello2.c lassie% a.out Hello, world.

Note


Annotated hello.c

/* * File: hello.c * ------------- * This program prints the message "Hello, world." * on the screen. The program is taken from the * classic C reference text "The C Programming * Language" by Brian Kernighan & Dennis Ritchie. */ #include <stdio.h> main() { printf("Hello, world.\n"); }

Notes


Special Characters

The Program

/* Program: hello2.c Embellishments to hello.c. */ #include <stdio.h> main() { printf("Mork says (in a loud voice): ") ; printf("Hello world.\n\n") ; printf("World says (whispers back): ") ; printf("@$#%%\"\\!=@&*\n") ; }

The Output

lassie% cc hello2.c lassie% a.out Mork says (in a loud voice): Hello world. World says (whispers back): @$#%"\!=@&* lassie%


Annotated hello2.c

/* Program: hello2.c Embellishments to hello.c. */ #include <stdio.h> main() { printf("Mork says (in a loud voice): ") ; printf("Hello world.\n\n") ; printf("World says (whispers back): ") ; printf("@$#%%\"\\!=@&*\n") ; }

NOTE


Tradition

When Kernighan and Richie wrote the first book about the language C, they used a simple program that printed out the string "Hello, World" as the first example program.

Over the years, the traditional starting place for programming discussions has become the "Hello, World" program.

Once you can print out a string, you've mastered the mechanics of getting something to run without delving into the intricacies of getting a real program to work the way you expect it to work.