First C “Hello World!” Program
Everyone knows “Hello World” program is the first step towards learning any programming language and also one of the simplest program you will learn in C Programming Language.
Let start write the first C hello world program,
#include <stdio.h> main() { printf("Hello World!\n"); system("pause"); }
If you haven’t botched anything, such as omitting a character or misspelling something, the compilation will proceed silently, and make an executable file called a. out. If you run a. out by typing the command
a.out
Output looks below,
Hello World
The very first step for running this program is to make a text file containing above code. Be sure that the file is a pure text file. You must save the text file with .c extension.
Then you must compile the source code. The result of compile step is an executable file that you can run it. If you are running your example on Unix/Linux type operating systems, you can remove the line which is commented to be necessary just under Windows.
If you look into the directory which you saved your project, you will find 5 different files:
- c (main C program file)
- o (intermediate compile file called “object file”)
- win (Make file is used by Dev-CPP compile settings of your project)
- dev (Project File contains Dev-CPP settings of your project)
- exe (Executable file which can be run independent from Dev-CPP or C Compiler)
The final product of your C program is the windows executable (.exe) file. You will normally distribute the executables of your software to users and keep the source code (*.c) for your own.
Details of Test C hello world program
#include <stdio.h> Tells C compiler to include the file “stdio.h” in this point of your C program before starting compile step. This “include file” contains several definitions, declarations etc. This code tells the compiler to include information about the standard input/output library; this line appears at the beginning of many C source files.
#main() C program consist of one or more functions. Functions are building blocks of C programs. main() function is different from other functions by that it is the start point of program execution. Our program contains only function while complicated programs may contain thousands.
#{ Opening brace marks the start of a block. Closing brace will mark its end. This one marks main () function start
# printf (“Hello world!”); This line of code prints the statement between quotation marks on your output screen. \n tells program to start a new line in output screen.
Each command line in C ends with “;” character. Control statements are exceptions. You will soon be able to determine when you must use ; to end a line of code.
# system(“pause”); The output window will close in Windows™, immediately after program execution has been finished. In this way you will not be able to see results of the execution (as it happens very fast). We have put this command to pause the window and wait for a keystroke before closing the window.
You can remove this line from our examples if you do not use Windows operating system. This command actually sends the “pause” command to windows operating system and windows runs the its “pause” command at this point. We will learn more about this command in later lessons.
# } closes main() function.
This program contains only one function while complicated programs may contain several functions.
Please write your comments if you find anything incorrect OR need to explain in detail, or you want to share more information about the topic discussed above.