Basic Structures And Syntax Of C Program | Hello World First Program In C Language

Writing Your First C Program – Syntax and Structure

Now that you have an understanding of what C is and why it is important, it’s time to write and run your first C program. In this chapter, we will cover the basic syntax and structure of a C program, understand how it works, and explore essential concepts such as header files, main function, statements, and comments.

Writing Your First C Program

Before we dive into the details, let’s look at a simple C program that prints “Hello, World!” on the screen. This is traditionally the first program written in any language.

Example: “Hello, World!” in C

#include <stdio.h>  // Header file for standard input-output functions

int main() {  // Main function – entry point of a C program

    printf(“Hello, World!\n”);  // Prints text to the console

    return 0;  // Indicates that the program executed successfully

}

📌 Output:

Hello, World!

Understanding the Structure of a C Program

Every C program follows a specific structure. Let’s break it down:

Basic Structure of a C Program

#include <header_file>  // Preprocessor directive

int main() {  // Main function

    // Statements inside the function

    return 0;  // Return statement

}

Key Components of a C Program

ComponentDescription
Preprocessor DirectivesUsed to include header files. Example: #include <stdio.h>
Main Function (main())Every C program must have a main() function as the starting point.
Statements & ExpressionsCode inside {} that performs operations. Example: printf(“Hello, World!”);
Return Statement (return 0;)Indicates successful execution of the program.

Header Files and Preprocessor Directives

In C, we use header files to access built-in functions. The #include directive tells the compiler to include the contents of a file before compilation.

Common Header Files in C

Header FilePurpose
<stdio.h>Standard Input/Output functions (e.g., printf, scanf)
<stdlib.h>General utilities (e.g., malloc, free, exit)
<math.h>Math functions (e.g., sqrt, pow)
<string.h>String handling (e.g., strlen, strcpy)

Example:

#include <stdio.h>  // Includes standard input-output functions

The main() Function

The main() function is where program execution begins. Every C program must have a main() function.

Example:

int main() { 

    printf(“Welcome to C Programming!\n”);

    return 0; 

}

🔹 int before main indicates that the function returns an integer (0 means successful execution).

Printing Output in C (printf)

To display text on the screen, we use the printf() function from <stdio.h>.

Example: Using printf()

#include <stdio.h>

int main() {

    printf(“C programming is fun!\n”);

    printf(“I am learning C.\n”);

    return 0;

}

📌 Output:

C programming is fun!

I am learning C. 🔹 \n (newline character) moves text to the next line

Leave a Comment

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