Hello World
/*****************************************
** File: hello.c
** Author: Sue Bogar
** Date: Jan 6, 1992
** Section: 101
** SSN: 123-45-6789
** E-Mail: bogar@cs.umbc.edu
**
** 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
int main()
{
printf("Hello, world.\n");
return 0;
}
Compiling and running the program
linux1[72]% ls
hello.c hello2.c
linux1[73]% gcc -Wall -ansi hello.c
linux1[74]% ls
a.out hello.c hello2.c
linux1[75]% a.out
Hello, world.
linux1[76]%
Note
- We compiled the program with the gcc compiler using the
-Wall and -ansi options, which will show us all warnings
and make sure our code is ANSI C compliant.
- The compiler created the file a.out , an executable file.
- Type a.out to run your program
Annotated hello.c
/*****************************************
** File: hello.c
** Author: Sue Bogar
** Date: Jan 6, 1992
** Section: 101
** SSN: 123-45-6789
** E-Mail: bogar@cs.umbc.edu
**
** 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
int main()
{
printf("Hello, world.\n");
return 0;
}
Notes
- the ".c" denotes a C language source code file
- Comments lie between /* and */
- Library inclusions
- The #include preprocessor directives cause the header files
of libraries to be taken in by the C compiler.
- #include < xyzzy.h > -- for a standard system library
- The printf function
- Main program
Special Characters
The Program
/*****************************************
** File: hello2.c
** Author: Sue Bogar
** Date: Jan 6, 1992
** Section: 101
** SSN: 123-45-6789
** E-Mail: bogar@cs.umbc.edu
**
** Embellishments to hello.c.
*****************************************/
#include
int main()
{
printf("Mork says (in a loud voice): ") ;
printf("Hello world.\n\n") ;
printf("World says (whispers back): ") ;
printf("@$#%%\"\\!=@&*\n") ;
return 0;
}
The Output
linux1[76]% gcc -Wall -ansi hello2.c
linux1[77]% a.out
Mork says (in a loud voice): Hello world.
World says (whispers back): @$#%"\!=@&*
linux1[78]%
Annotated hello2.c
/*****************************************
** File: hello2.c
** Author: Sue Bogar
** Date: Jan 6, 1992
** Section: 101
** SSN: 123-45-6789
** E-Mail: bogar@cs.umbc.edu
**
** Embellishments to hello.c.
*****************************************/
#include
int main()
{
printf("Mork says (in a loud voice): ") ;
printf("Hello world.\n\n") ;
printf("World says (whispers back): ") ;
printf("@$#%%\"\\!=@&*\n") ;
return 0;
}
NOTE
- \n for newline
- %% for %
- \" for "
- \\ for \
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.
CSEE
|
201
|
201 S'02
|
lectures
|
news
|
help