Syntax and Structure of C Programming Language

Syntax of C, Structure & Basic Terms

The syntax of a programming language defines the rules that dictate how code must be written. In C, the syntax is quite rigid, and a small mistake can lead to compilation errors.

Below are key elements of C syntax:

Example

#include <stdio.h>

int main() 
{
    printf("Hello, World!");
    return 0; 
}
        

Output: Hello, World!

Explanation

Structure of a C Program

A C program typically follows a specific structure, which includes several key components:

stdio:

include:

#:

There are two ways for using the #include directive:

conio:

return:

Function:

int:

void:

Variable:

Data Types:

Example Structure

#include <stdio.h>

// Global variable
int globalVar = 10;

// Function prototype
void greet(); 

int main() 
{
    int localVar = 20;  // Local variable
    greet(); // Function call
    printf("Global Variable: %d\n", globalVar);
    printf("Local Variable: %d\n", localVar);
    return 0; 
}

// Function definition
void greet() 
{
    printf("Hello, from a function!\n");
}
        

Output:
Hello, from a function!
Global Variable: 10
Local Variable: 20

Explanation

Some Rules to Write a C Program

While writing a C program, follow these rules to ensure it compiles and runs successfully:

The C programming language has a well-defined syntax and structure that makes it powerful yet accessible. Understanding these fundamental concepts is crucial for writing efficient and error-free C programs. The language's rigid syntax rules ensure consistency across different implementations, while its structured approach promotes modular and maintainable code.