Hello World
/*****************************************
** File: hello.c
** Author: Sue Bogar
** Date: Jan 6, 1992
** Section: 101
** 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;
}
Now that we've written hello.c using a text editor (xemacs), we will have to
compile it using our compiler, gcc, before it can be run. This diagram shows
the entire process:
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
** 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 Evans
** Date: Jan 23, 2004
** Section: 101
** E-Mail: bogar@cs.umbc.edu
**
** This variation of hello.c employs South Park
** characters using foul language to illustrate
** how some special characters must be printed
** in C.
*****************************************/
#include
int main()
{
printf("Cartman says : ") ;
printf("Hello, Kyle.\n\n") ;
printf("Kyle says : ") ;
printf("@$#%%\"\\!=@&*\n") ;
return 0;
}
The Output
linux1[76]% gcc -Wall -ansi hello2.c
linux1[77]% a.out
Cartman says : Hello, Kyle.
Kyle says : @$#%"\!=@&*
linux1[78]%
Annotated hello2.c
/*****************************************
** File: hello2.c
** Author: Sue Evans
** Date: Jan 23, 2004
** Section: 101
** E-Mail: bogar@cs.umbc.edu
**
** This variation of hello.c employs South Park
** characters using foul language to illustrate
** how some special characters must be printed
** in C.
*****************************************/
#include
int main()
{
printf("Cartman says : ") ;
printf("Hello, Kyle.\n\n") ;
printf("Kyle says : ") ;
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 F'06
|
lectures
|
news
|
help
last modified on Tuesday, 22-Aug-2006 07:13:52 EDT