We are starting our C question answers by writing a simaple Hello world program. Our objective is to write a small program and save it in a C source file and then compiling it and at last running it and get the output in console.

#include <stdio.h>
int main (int argc, char *argv[])
{
    printf ("Hello World!");
    return (0);
}

C program can be written with a file having .c file extension like hello_world.c and there should be at least one function called main() in it. The prototype of main is-

int main (int argc, char *argv[]);

argc = number of arguments passed to this program
argv = list / vector of arguments as string passed to this function.

Main should return 0, to say a success to the shell/OS and non-zero to say that it has failed.

We generally place multiple input output statements inside main() or alternatively we can define additional functions with I/O statements and call these functions from main(). A better design is to keep the main simple and small and divide the entire workflow into smaller functions. However our main has only one I/O statement i.e. printf which gives a hello message to the console. There are some input statements like scanf, getch etc to take user inputs from console. Prototypes of printf, scanf etc are declared in stdio.h header file(standard input output) thus this file has been included. There are however numerous other header files available in C for doing different types of work. We need to include these as we do these works in our program. For example doing string related work we need to include string.h etc.

All these above information is related to work function of the C file. Now comes documentation part. We often include a file header comment for putting description. Similarly each function can have function header comment to describe the functionality and the input/output arguments and return value etc. We may optionally define sections to define global/static variable and static and global functions etc. Now considering all these we are re-writing our hello_world.c source code -

/* Hello World demo file */
/* Section for include files*/
#include <stdio.h>
/* Sections for Global Variables */
/* Section for Static variables */
10  /* Section for Static/local function */
11 
12  /* Section for global functions */
13  /* Entry point function Main () */
14  int main (int argc, char *argv[])
15  {
16    printf ("Hello World!");
17    return (0);
18  }

Now compiling and running the program. There are the steps.

Linux:
$ gcc hello_world.c -o hello_world
$ chmod +x hello_world
$ ./hello_world
Hello World!
$
Windows:
>cl hello_world.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

hello_world.c
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:hello_world.exe
hello_world.obj

>hello_world.exe
Hello World!
>

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 1

Similar topics related to this section

flow chart, pseudo codes, ares of development, compilers, Turbo/Borland C, GCC, VC++, Watcom C, DJGPP, Xcode, first C program, header files, library, compilation, pre-processing, compilation, optimization, linking format, debugging with GDB, structured programming language,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan