Syntax Rules in C

Syntax Rules in C Programming

The C programming language has a defined set of rules and syntax that determine how code must be written and structured. These rules are essential for ensuring that the program is syntactically correct and can be compiled and executed without errors. Below are the key syntax rules in C programming, with explanations and examples.

1. Case Sensitivity

C is a case-sensitive language, which means that uppercase and lowercase letters are treated differently. For example, the variable age and Age are considered two different variables in C.

Example:

int Age = 25;  // This is a different variable from `age`

int age = 30;  // Both `Age` and `age` exist independently

2. Every Statement Ends with a Semicolon (;)

In C, each statement must end with a semicolon (;). This tells the compiler that the statement is complete and separates it from other statements.

Example:

int x = 10;      // Correct

x = x + 5;       // Correct

int y = 20       // Error: missing semicolon

3. Comments in C

Comments in C are used to annotate the code and are ignored by the compiler. C supports two types of comments:

  • Single-line comments: Start with //.
  • Multi-line comments: Enclosed between /* and */.

Example:

// This is a single-line comment

/*

   This is a multi-line comment

   It can span across multiple lines

*/

4. The main() Function

Every C program must have a main() function, as it is the entry point where the program’s execution begins. The main()function typically returns an integer (int) and may take arguments, although these are optional.

Example:

int main() {     // Program execution starts here

    // Code goes here

    return 0;    // Return 0 indicates successful execution

}

5. Curly Braces Define the Scope

Curly braces {} are used to define a block of code or the scope of functions, loops, and control structures. Anything within the braces is part of the block, and it’s important that they are balanced (each { must have a corresponding }).

Example:

int main() {

    int x = 10;

    if (x > 5) {    // Start of if block

        printf(“x is greater than 5\n”);

    }               // End of if block

    return 0;

}

6. Variables Must Be Declared Before Use

In C, you must declare variables before using them. Declaring a variable tells the compiler its name and type, and optionally, its initial value.

Example:

int age;          // Declaring an integer variable named ‘age’

age = 25;         // Initializing ‘age’ with a value of 25

int weight = 60;  // Declaration and initialization in a single statement

7. Data Types

C is a statically typed language, meaning that each variable must have a declared type, such as int, char, float, etc. The data type defines what kind of data the variable can store.

Example:

int age = 25;       // Stores integer values

float height = 5.9; // Stores decimal values

char initial = ‘A’; // Stores a single character

8. Function Declarations

All functions in C must be declared before they are called. This declaration (also called a function prototype) specifies the return type, function name, and parameters.

Example:

int add(int a, int b);  // Function declaration

int main() {

    int result = add(5, 10);  // Function call

    printf(“Sum: %d\n”, result);

    return 0;

}

int add(int a, int b) {  // Function definition

    return a + b;

}

9. Use of the return Statement

The return statement is used to exit a function and optionally pass a value back to the calling function. In the main()function, return 0; usually indicates successful program execution.

Example:

int main() {

    return 0;  // Returning 0 indicates successful execution

}

10. White Space is Ignored

C ignores white space (spaces, tabs, newlines) except where it is used to separate tokens (keywords, operators, etc.). However, for readability, you should use proper indentation and spacing.

Example:

int main() {

    int x = 5;  // This is the correct way to write code

    int  y = 10; // C allows extra spaces, but it’s not good practice

    return 0;

}

11. Case Sensitivity in Keywords and Identifiers

C keywords, such as int, return, if, and else, must be written in lowercase. Identifiers (variable names, function names) are also case-sensitive, meaning myVariable and MyVariable would be treated as two different names.

Example:

int x = 10;  // Correct

INT y = 20;  // Error: `INT` is not a valid keyword (C is case-sensitive)

12. Use of Control Structures

C supports several control structures like if, else, for, while, and switch. These control the flow of execution in the program.

Example:

int main() {

    int x = 10;

    // If-else control structure

    if (x > 5) {

        printf(“x is greater than 5\n”);

    } else {

        printf(“x is less than or equal to 5\n”);

    }

    return 0;

}

13. String Handling and Use of Double Quotes

Strings in C are arrays of characters terminated by a null character (\0). Strings must be enclosed in double quotes, while single characters are enclosed in single quotes.

Example:

char name[] = “John”;  // Correct: Strings in double quotes

char initial = ‘J’;    // Correct: Character in single quotes

char wrong = “J”;      // Error: Single character in double quotes is incorrect

14. Arrays and Pointers

In C, arrays and pointers are important features. Arrays store multiple values of the same type, and pointers are used to store the memory address of variables.

Array Example:

int numbers[] = {1, 2, 3, 4, 5};  // Declaring and initializing an array

Pointer Example:

int a = 10;

int *ptr = &a;  // Pointer stores the address of the variable ‘a’

15. Escape Sequences

Escape sequences allow you to print special characters like newlines (\n), tabs (\t), and backslashes (\\) in strings.

Example:

printf(“Hello, World!\n”);  // Prints “Hello, World!” followed by a new line

printf(“Name:\tJohn\n”);    // Prints “Name:    John” (with a tab space)

16. Naming Conventions

  • Variable and function names should start with a letter or underscore and can be followed by letters, numbers, or underscores. Names cannot start with a digit.
  • C keywords (like int, if, return) cannot be used as variable names.

Example:

int myVariable = 10;  // Valid

int 2num = 20;        // Invalid: Cannot start with a digit

int float = 30;       // Invalid: ‘float’ is a keyword

error: Content is protected !!
Open chat
1
Hi,how Can We Help You ?