First C Program

In this section, we will write and understand our first C program. Writing your first program is an essential step in learning the C programming language.
Let's start with the "Hello World" program, which is commonly used as the first example in any programming language tutorial.

Steps to Write Your First C Program

  1. Install a C compiler such as GCC or use an IDE that supports C programming.
  2. Create a file with a .c extension (e.g., first_program.c).
  3. Write the code inside the file.
  4. Compile the program using the C compiler.
  5. Run the compiled program to see the output.

Example: Hello World Program

                            
#include <stdio.h>  
int main() 
{  
  printf("Hello, World!\n");  
  return 0;  
}
                            
                        

Output

Hello, World!

Explanation of the Program

Here is a detailed explanation of the code:

  • #include <stdio.h>: This is a preprocessor directive that includes the Standard Input Output library, which is necessary for functions like printf().
  • int main(): The main() function is the entry point of every C program. It marks where the execution starts.
  • printf("Hello, World!n"); This function prints the text "Hello, World!" followed by a newline.
  • return 0; This statement ends the program and returns 0 to indicate successful execution.

Compiling and Running the Program

Follow these steps to compile and run your program:

  1. Open your terminal or IDE.
  2. Navigate to the directory where your .c file is saved.
  3. Compile the file using the command: gcc first_program.c -o first_program
  4. Run the compiled program using the command: ./first_program
  5. You should see the output: Hello, World!

Congratulations! You have successfully written and executed your first C program.