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.
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 -
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! >
About our authors: Team EQA
You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.
Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions
Compilers & Editors
Download Visual Studio Download XCode Download Visual Studio Code Android studio install sdk Eclipse installer Best C compilers IDEs
Development system setup
Windows media creation tool MSDN subscription Ubuntu virtualbox
New updated posts
Why learn C? Draw on printer Memory leaks