C Language Basic Syntax Rules

Syntax basically refers to protocol or rules to be followed while writing a C program.

The following are the basic building blocks of a C program:

Hello world Program:

#include <stdio.h>

int main()
{
   // This is the Hello World Program 
   printf("\n Hello world\n");
   return 0;
}

Header file:
C has a series of predefined functions placed in the header files in the C library. They can be used directly in the source code.

Standard header files are provided with each compiler and cover a wide range of areas like string handling, mathematical functions, data conversion, printing, and reading of variables.

stdio stands for standard input and output and it has function definition for scanf() and printf() to take the input and display the output respectively.

Main Function:
Every C program must have this function and it is the starting point of any programs.

In the above section int written before the main is the return type of the function So here we wrote a function which would return some value whose type must be integer. The curly braces { } just after the main function encloses the body of the main() function.

Tokens:
Tokens are the smallest element of a program. The compiler breaks a program into the smallest possible units called tokens and goes through various stages of the compilation A C program consisting of a series of tokens. They can be:

  • Keywords:
    Keywords are a set of words that have a special purpose in the program and have a special function associated with it. Keywords should not be used for assigning variable names. There are 32 keywords in C. Few of the common keywords used are: for, while, int, break, switch, goto, if, else, const, etc.
    All keywords are lower case in nature.
  • Identifiers/variables:
    Identifiers are names given to variables, arrays, and functions. The user may use a combination of different characters available but there are certain rules:
    • First character: It can either be an alphabet or an underscore but cannot be a digit.’
    • No special characters: No special characters like commas or punctuation marks can be used.’
    • No Keywords: Keywords cannot be used, they are reserved.
    • No white space: white spaces including blank spaces, new lines, a horizontal tab cannot be used.
    • Word limit: The length of the identifiers should not exceed 31 characters.
    • Case sensitive: Uppercase and lowercase are treated differently.
  • Constant:
    As the name suggests, constant values cannot be changed, that is values cannot be changed during the program once they are defined. Example: const int x = 10. const keywords used.
  • Punctuators:
    Some characters in C are used as punctuators which have their own syntactic and semantic significance. These are not operators or identifiers. A few of the common punctuators used are:
    • [ ] = Array delimiters,
    • ( ) = Function parameters,
    • * = Pointer declaration,
    • ; = Statement end,
    • ” ” = string literals or header name
    • < > = header name
  • Operators:
    They are used to perform operations on data. Operations are performed on operands. Again, this can be classified as:
    • Unary Operator: One operand eg: !
    • Binary operators: Arithmetic, logical, bitwise, assignment, conditional operators
    • Ternary operators: Can be used in place of if-else conditions as ternary operators.

Comments in C:
It’s good practice to have comments in our program which specify the purpose of the program or logic. Comments can be a single-line comment or a multi-line comment. These comments are ignored by the compiler and not executed.

To add a single line comment, start by adding two forward slashes // followed by a comment, and to have a multiline comment, enclose it between /* ….. and */, as in the above program.

Whitespaces:
The C programming language uses whitespaces to describe blanks, newline characters, and comments. Whitespace separates one statement from another statement for the compiler to identify.

int data;
If we don’t provide proper whitespaces within the statement where it is needed, the compiler will complain and will not generate the machine code.

Semicolon:
In C it is mandatory for every statement to end with a semicolon. This is needed to terminate the statement and indicate the start and end of the statement execution. If the semicolon is not placed at the end of the statement, the compiler would throw errors.

Rules for a Program in C

  • C is a case-sensitive language. Most of the program statements are lower case.
  • All statements must necessarily be terminated with a semicolon.
  • White spaces should only be provided between variables and keywords.

Relevant Posts:



Categories: C Language

1 reply

Trackbacks

  1. Features of C programming language - Tech Access

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: