C - Basic Syntax: Essential Rules for Writing Code

In C programming, "syntax" refers to the set of rules that dictate how code must be written. This section outlines the key elements and rules necessary for crafting a valid C program, helping you understand the foundational structure required for successful coding.



C - Basic Syntax

In C programming, "syntax" is the set of rules for writing code. Below are the key elements and rules for writing a C program.

Syntax

/* Hello World program */ // Comments

#include <stdio.h> // Header File
int a = 10; // Global declarations

// The main function
int main() {
    char message[] = "Hello World"; // Local variable
    printf("%s", message);
    return 0;
}
        

Tokens in C

A C program is made up of tokens: keywords, identifiers, constants, string literals, or symbols. For example:

Syntax

printf("Hello, World!\n");
        

The tokens here are printf, (, "Hello, World! \n", ), and ;.

Identifiers in C

Identifiers are names for variables, functions, etc. They start with a letter (A-Z, a-z) or an underscore (_), followed by letters, underscores, or digits (0-9). Examples:

  • mohd
  • zara
  • abc
  • move_name
  • a_123
  • myname50
  • _temp
  • j
  • a23b9
  • retVal

Identifiers cannot contain punctuation or be keywords. C is case-sensitive, so Manpower and manpower are different.

Keywords in C

Keywords are reserved words with predefined meanings. They must be used correctly and are all lowercase. Examples include:

Syntax

auto    else    long    switch
break   enum    register    typedef
case    extern  return  union
char    float   short   unsigned
const   for     signed   void
continue goto    sizeof  volatile
default if      static  while
do      int     struct  _Packed
double
        

Semicolons in C

Semicolons (;) end statements. Multiple statements can be on one line or one statement can span multiple lines:

Syntax

int a = 10; if (a >= 50) printf("pass"); else printf("fail");
        
Output

pass
        

Or a statement can be split over multiple lines:

Syntax

if (a >= 50)
    printf("pass");
else
    printf("fail");
        

Comments in C

Comments start with /* and end with */. They are ignored by the compiler:

Syntax

/* my first program in C */
        

Source Code

C source code files have a .c extension. The compiler only processes files with this extension.

The main() Function

Every C program must have one main() function, where execution starts. Functions can be defined anywhere in the file, and functions called before their definitions need a forward declaration.

Header Files

Header files with predefined functions are included using #include at the top of the source code:

Syntax

#include <stdio.h>
        

Variable Declaration

C is statically typed, meaning variables must be declared before use. They can be global or local and must store values of their declared type.

Statements in a C Program

Statements are executed in top-to-bottom order by default, controlled by conditionals or loops. Each statement ends with a semicolon (;).

Whitespaces in a C Program

Whitespaces (spaces, tabs, newlines) are ignored by the compiler but improve readability. They separate statement components:

Syntax

int age;
fruit = apples + oranges; // get the total fruit
        

Compound Statements in C

Compound statements group multiple statements using curly brackets. Example:

Syntax

if (marks < 50) {
    printf("Result: Fail\n");
    printf("Better Luck next time");
} else {
    printf("Result: Pass\n");
    printf("Congratulations");
}
        
Output

Result: Fail
Better Luck next time
        

Curly brackets are also used in function definitions, structs, and arrays:

Syntax

float area_of_square(float side) {
    float area = pow(side, 2);
    return area;
}

struct student {
    char name[20];
    int marks, age;
};

int marks[] = {50, 56, 76, 67, 43};