Before starting with the basics of C programming, let us have a look on C programming structure so that we can easily get interacted with the upcoming chapters.

Example

My First C program

// Write a program to print “Hello World”.

                   #include<stdio.h>

                   int main(void)

                   {

                             /*My first C program*/

                             printf(“Hello World”);

                             return 0;

                   }

A C program basically contains following parts:

1. Preprocessor Commands
2. Functions
3. Variables
4. Statements and Expression
5. Comments

Let us discuss the above “Hello World” program briefly:

  • The first line of above code includes the stdio file (#include<stdio.h>), is a preprocessor which includes standard input output to your program before compilation.
  • The next respective line int main(void) is the main function from where the program execution starts.
  • The line /*….*/ will be ignored by the compiler and it has been put to add additional comments in the program.
  • printf(“….”) is another function available in C in stdio library that is standard input output library, which causes the message “Hello World” to be displayed on the screen of the user.
  • The return 0; terminates the main function and returns 0.

Compile and Execute C program:

After writing your program, the programmer needs to type it and instruct the machine to execute it. To type the program a programmer need to use software that is Editor. Once program has been typed it needs to be converted to machine level instructions before the machine execute it. To carry out the conversion of high level language to machine level language, we need another program called Compiler. Compiler vendors provide an Integrated Development Environment which consists of an Editor and Compiler as well.

There are several IDEs available in the market targeted towards different operating system and microprocessors. There is TurboC++, DevC++ etc. compilers are present in the market.

There is another way to Compile and Execute C program, which is also known as traditional way when IDEs were no developed.

To execute the written program two basic steps must be followed:

  1. Compile
  2. Execute

After compiling the written text file, we have source file on our hand which produce output or result after complete execution.

Here are some simple steps to compile and execute the program:

  • Open any text editor and write program as mentioned.
  • Save that file with the extension FileName.c (FileName can be user-defined name).
  • Now, open the command prompt and go to the directory where you have saved your FileName.c file.
  • Write command gcc FileName.c and press enter; it will compile your code.
  • If no any errors in your written code, the command prompt will take you to the next line and generate out executable file.
  • Now, the user need to type a.out to execute the program.
  • The output will be displayed on the screen.
  1. $ gcc FileName.c
  2. $ ./a.out
  3. Hello World

    Keep in mind that the gcc compiler is in your path and that you are running it in the directory containing the source file FileName.c.