www.delorie.com/djgpp/doc/ug/intro/your-first.html   search  
Guide: Your First Program

Time to actually do something with djgpp!

The traditional ``first program'' for people learning C or a new development environment is the classic ``hello, world'' program. It looks like this:

#include <stdio.h>

int main(void)
{
  printf("Hello, world!\n");
  return 0;
}

If you don't know C, don't expect this Guide to teach you. You can still try out djgpp with the above program, and learn C from a book written for that purpose.

If you have a text editor you're familiar with, go ahead and type in the above program. Note that you cannot use a word processor for this - you must use something that saves in plain text, like notepad or edit. You could also install and learn to use rhide or emacs. If you don't want to figure any of that out yet, you can still create the file from the command line, like this (the examples assume your current directory is C:\STUFF):

C:\STUFF > copy con hello.c
#include <stdio.h>

int main(void)
{
  printf("Hello, world!\n");
  return 0;
}
<Ctrl-Z>

That last line means press the Z key while holding the Control key. It's DOS's way of saying ``all done''.

The program that does all the work is called gcc, which stands for GNU C Compiler. It's a front end program, which means that it runs other programs to perform the various tasks you ask of it. The command line you want to type in in this case is thus:

C:\STUFF > gcc hello.c -o hello.exe

This asks gcc to compile your C source and produce a DOS executable. If it works, it won't print anything, and you'll be able to run your program:

C:\STUFF > hello
Hello, World!

If you're curious about how gcc gets its job done, or if you're having problems and you need more information about what gcc is trying to do, add -v to the command line:

C:\STUFF > gcc -v hello.c -o hello.exe

This will make gcc print out all the programs it runs, its version number, and where it is looking for things.


  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 1997   by DJ Delorie     Updated Jan 1997