Basic Syntax of C Language

Basic Syntax of C Language

In this article, I will discuss the Basic Syntax of the C Language in detail. Please read our previous article, where we discussed how to create a project using CodeBlocks IDE and compile, debug, and run projects in CodeBlocks IDE. You will understand the Syntax of the C Language at the end of this article.

Basic Syntax of the C Language

A basic C program follows a structured format, typically including these components:

Preprocessor Commands: These are lines included at the beginning of a C program. They start with # and direct the C compiler to include specific files or macros. The most common one is #include <stdio.h>, which includes the standard input-output library for functions like printf() and scanf().

Function Definitions: A C program contains one or more functions. One of these is main(), which is the program’s starting point. Other functions can be written to perform specific tasks and are called from main() or other functions.

Variables Declarations: Before using variables, they are declared, specifying their type, such as int, float, char, etc. Variable declarations can be done at the start of a function.

Main Function: Every C program must have a main() function. The execution of the program begins from here. It can call other functions defined in the program.

Statements and Expressions: The body of functions contains statements and expressions that implement the program logic. For example, assignments, loops, conditions, function calls, etc.

Comments: Comments are used to describe parts of the code and are ignored by the compiler. Single-line comments start with //, and multi-line comments are enclosed between /* and */.

Here is an example of a simple C program
#include <stdio.h>

int main() {
    // Declare an integer variable
    int number;

    // Ask for user input
    printf("Enter an integer: ");  
    // Read the input from the user
    scanf("%d", &number);

    // Display the input value
    printf("You entered: %d", number);

    return 0; // Indicate that the program finished successfully

This program includes a preprocessor command, a main() function, variable declarations, statements, and comments. The printf() and scanf() functions are used for output and input, respectively. The program’s execution starts in main(), which controls its flow.

Whitespace in C Program

In C programming, whitespace is a term that refers to characters that are used to format the code to make it more readable. It includes spaces, tabs, and newline characters. Whitespace in C plays a crucial role in separating tokens but doesn’t affect the program’s execution. Here’s a closer look at the role of whitespace in C programs:

  • Separating Tokens: A whitespace is used to separate tokens (the smallest individual units in a program, like keywords, operators, identifiers, etc.) in the source code. For example, in the statement int a = 5;, spaces separate the keyword int, the identifier a, and the integer 5.
  • Readability and Organization: Proper use of whitespace (like spaces and indentation) makes the code more readable and helps in organizing the code logically, which is essential for understanding the program’s flow, especially in complex programs.
  • Indentation: Indentation, which is often a combination of spaces and tabs, is used to visually represent the structure of the code, particularly in blocks of code like those in if statements, loops, or function bodies. Consistent indentation is a key aspect of writing clean, maintainable code.
  • Newlines: Newline characters are used to separate lines of code. They are important in defining the end of one statement and the start of another, especially when the semicolon (;) is used to terminate a statement.
  • Ignored by the Compiler: Except for separating tokens, whitespace characters are generally ignored by the C compiler. This means that adding extra spaces, tabs, or newlines does not affect how the program runs.
  • Preprocessor Directives: In preprocessor directives (like #include and #define), whitespace is used after the # symbol and before the actual directive. However, it’s not allowed between the # and the directive name (e.g., #include).
  • Within Strings and Characters: Whitespace characters can also be part of string literals (e.g., “Hello, World!”) and character literals (e.g., ‘ ‘ for a space). In these cases, they are treated as regular characters and are significant.

Example: int age;

There must be at least one whitespace character (usually space) between int and age for the compiler to be able to distinguish them.

Semicolon in C Language

In C programming, the semicolon (;) plays a crucial role as a statement terminator. It marks the end of an individual statement, which is an executable instruction. Here’s a more detailed look at the usage and importance of semicolons in C:

  • End of a Statement: The most common use of the semicolon in C is to signify the end of a statement. This allows the C compiler to clearly distinguish where one statement ends and the next begins. For instance, int a = 5; is a complete statement, and the semicolon indicates its termination.
  • Multiple Statements on a Single Line: Semicolons enable writing multiple statements on a single line. Without semicolons, the compiler couldn’t distinguish between different statements. For example: int x = 1; int y = 2;
  • Empty Statements: A standalone semicolon can be used to create an empty statement, which does nothing. This might be useful in certain situations, like in a for loop where you might not need one of the components. For example, for (; ; ) { /* infinite loop */ }.
  • Syntax Errors: Omitting a semicolon where it is expected can lead to syntax errors. The compiler typically reports an error pointing to the line where it expected a semicolon, helping in debugging.
  • Loop and Control Statements: In loops and control statements, the semicolon can change the behavior of the code. For example, if statements or loops like while, for, do…while, an accidental semicolon immediately after the condition can lead to a logic error, causing the loop or condition to execute differently than intended.
  • Compound Statements: In compound statements (a block of code enclosed within {}), semicolons terminate individual statements within the block but not after the closing brace.
  • Macro Definitions: In macro definitions using #define, a semicolon is not typically used at the end of the macro definition. However, the macro in the code should be terminated with a semicolon as it expands into a statement.

Here’s an example to illustrate the use of semicolons in a C program:

#include <stdio.h>

int main() {
    int a = 5;  // Semicolon terminating the statement
    int b = 10; // Another statement

    if (a < b) { // Start of if statement
        printf("a is less than b\n"); // Semicolon terminating the printf statement
    } // No semicolon needed after the closing brace

    return 0; // Semicolon at the end of the return statement

In this example, semicolons are used to terminate variable declarations, the printf statement, and the return statement. The absence of a semicolon in any of these places would result in a syntax error.

Tokens in C Langauge

In the C programming language, a token is the smallest element that can be meaningful in a program. Tokens are the basic building blocks of a C program and are categorized into several types:

  • Keywords: These are reserved words that have special meaning in C. They are used to perform various actions or to define data types. Examples include int, return, if, while, etc. Keywords cannot be used as identifiers (names of variables, functions, etc.).
  • Identifiers: Identifiers are names given to various program elements, such as variables, functions, arrays, etc. They must start with a letter (either uppercase or lowercase) or an underscore (_), followed by letters, digits, or underscores.
  • Constants: Constants are fixed values that do not change during the execution of a program. They can be an integer, float, character, or string constants, like 100, 3.14, ‘a’, or “Hello”.
  • String Literals: A string literal is a sequence of characters enclosed in double quotes, such as “Hello, World!”.
  • Operators: Operators are symbols that perform operations on operands. They include arithmetic operators (+, -, *, /), relational operators (<, >, ==), logical operators (&&, ||, !), etc.
  • Punctuators: Punctuators are symbols that are used in the syntax of the language to organize program structures. This includes braces ({ }), brackets ([ ]), parentheses (( )), semicolons (;), commas (,), and others.
  • Special Symbols: These include symbols like the hash (#) used in preprocessor directives.

Each token serves a specific purpose in a C program and follows the language’s syntax rules. Understanding and correctly using these tokens is fundamental to writing C code.

Identifiers in C Language:

In the C programming language, identifiers are names given to various programming elements, such as variables, functions, arrays, and types. They serve as a way to uniquely identify these elements within the code. Here are some key points about identifiers in C:

Purpose: Identifiers are used for naming variables, functions, arrays, structures, unions, and other user-defined data types.

Rules for Naming:
  • An identifier can be a combination of letters (both uppercase and lowercase), digits, and underscores (_).
  • The first character of an identifier must be a letter (a-z or A-Z) or an underscore.
  • Identifiers are case-sensitive (myVar, MyVar, and myvar are three different identifiers).
  • Identifiers cannot start with a digit.
  • Identifiers cannot be keywords. For example, int, if, while, etc., cannot be used as identifiers.
  • There is no limit on the length of an identifier, but the first 31 characters are significant in most C compilers.
Types of Identifiers:
  • Local Identifiers: Declared within a block or function and are only accessible within that block.
  • Global Identifiers: Declared outside any block or function and are accessible throughout the program.
  • Formal Parameters: Used in function headers and are local to the function.
  • Valid identifiers: count, myVar123, _value, MAX_SIZE.
  • Invalid identifiers: 123abc (starts with a digit), int (keyword), my-var (contains a hyphen).

Conventions: While not enforced by the language, there are common naming conventions, such as using camelCase for variables and functions and UPPER_CASE for constants or macros.

  • The scope of an identifier is the region of the program where the identifier is accessible.
  • Local identifiers have block scope, while global identifiers have file scope.
  • The lifetime of an identifier is the time during which it is stored in the memory.
  • Local variables typically have an automatic lifetime (they are created when the block is entered and destroyed when it is exited), while global variables have a static lifetime (they are created when the program starts and destroyed when it ends).

Understanding and adhering to these rules and conventions for identifiers is important for writing clear, maintainable, and error-free C programs.

Keywords in C Language

In C programming language, keywords are reserved words with a special meaning to the compiler. Your program cannot use these keywords as identifiers (like variable names, function names, etc.). Here’s a list of keywords in C:

  1. auto
  2. break
  3. case
  4. char
  5. const
  6. continue
  7. default
  8. do
  9. double
  10. else
  11. enum
  12. extern
  13. float
  14. for
  15. goto
  16. if
  17. int
  18. long
  19. register
  20. return
  21. short
  22. signed
  23. sizeof
  24. static
  25. struct
  26. switch
  27. typedef
  28. union
  29. unsigned
  30. void
  31. volatile
  32. while

Each of these keywords serves a specific purpose in the language, such as defining data types, controlling program flow, or specifying storage class. For example:

  • if, else, switch, case, default, and while are used in control flow statements.
  • int, char, float, double, void are data types.
  • struct, enum, union, typedef are used for creating new data types.
  • const, volatile are type qualifiers.
  • return is used in functions for returning a value.
  • static, extern, auto, register are storage class specifiers.

Understanding these keywords and their usage is crucial for programming effectively in C.

In the next article, I am going to create a Sample C Program and then try to explain the different parts of a C program in detail. In this article, I try to explain the Basic Syntax of C Programming Language, and I hope you enjoy this Syntax of C Language article.

Leave a Reply

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