Structure of a C program

coderz.py

Keep Coding Keep Cheering!

C Program – Structure

Matrix multiplication

Before we begin with our basic concepts of the C programming language let’s take a look at the C Program -Structure of a simple code for better understanding.

A C program consists of the following components−

  • standard input-output library functions
  • variables
  • comments
  • statements
  • operators
  • operands
  • functions
Hello World Program
#include <stdio.h>    
    1. int main()
    2.  {    
    3. /* first program in C */
    4. printf("Hell, World!");    
    5. return 0;   
    6. }  

Description of the above code:

  • here the first line of the code #include <stdio.h> is the standard input-output library. printf() function is defined in the stdio.h library file.
  • int main() is the main function where the execution of the code begins.
  • The opening curly brace { symbolizes the beginning of the block.
  • The statement inside /*…*/ is the comment which is ignored by the compiler.
  • The print() function prints the message “Hello World” on the output window/screen.
  • The return 0 statement is the ending of the code, as well as returns the execution state of the OS.
  • The closing curly brace } symbolizes the end of the block.
Compiling and Executing a C Program

Let us take a look at how to compile and run a program:

  • If you are using a C code editor, you may directly click on the compile and run button resulting in the compilation of the program and an output will be generated on the screen.
  • Otherwise, if you are using the command prompt then follow the below-given methods.
  • open an editor and type your code in it
  • save the file with the “.c” extension.
  • open the command prompt go to the directory where you saved your c file
  • type gcc filename.c -o filename and press enter.
Output
$ gcc filename.c -o filename
$ ./filename
Hello, World!

Make sure the gcc compiler is in your path and that you are running it in the directory containing the source file filename.c.

Now you might have a good understanding of the C Program -Structure.

Note: Also read C Programming Language Overview C Programming Language

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement