Syntax in C Programming Language​ with Example

Basic C Language Syntax Rules

The Basic C Language Syntax Rules describe the writing code in the C programming language.

In plain English, these guidelines specify how to build statements in a C language program.

How should the line of code begin? How should it conclude?

When to use double quotes? When to use curly brackets? When to use parenthesis? etc.

  • When it comes to writing code, everything has a specific way of being written and utilized, and that manner is its syntax.

  • C is a language that is case-sensitive, so all C instructions must be written with lowercase letters. main is not synonymous with MAIN.

  • Each and every C statement must end with a semicolon.

  • In C, whitespace is used to add spaces and tabs.

  • You need not concern yourself with code indentation.

  • The body of a function is enclosed in curly brackets, similar to the main() function. This will be covered in depth when we discuss functions.

What is Syntax?

The syntax is any language, whether English, Hindi, or Spanish, that has a grammar that describes the rules for using the language, such as how to create sentences, the meanings of various terms, etc.

In a normal spoken language or a programming language, syntax refers to the arrangement of words, characters, and special characters in order to form a comprehensible statement, expression, etc.

If someone informs you that there is a syntax error in the program, it indicates that you have not written the program correctly; you may have omitted a semicolon or made another error while inputting the program’s code.

A syntax mistake does not indicate that the logic of your code is incorrect; it indicates that you have written it incorrectly.

After the syntax has been validated, only the code is compiled and then executed.

C Basic Syntax

A simple C program’s syntax consists of header files, the main() method, and then program code. Here is the syntax of a basic and minimal C program:

//header files 
return_type main() {
example syntax to program codes
}

Every C program must contain the main() function because program execution begins with main ().

For instance, the following is the simplest and smallest C program that would print “Hello Syntax Compiler in C Programming Language” on the screen:

/* 
    This is my first program.
    I am very excited!
*/

#include <stdio.h>
int main()
{
    // Printing Hello World
    printf("Hello,World");
    // printf("Useless piece of code.");
    return 0;
}

Here is an example of the above C program’s output:

Hellow, World

You can test the above example here!C Online Compiler

Tokens in C

A token in C may be a keyword, a constant, a string constant, a symbol, or an identifier.

The number of tokens in a C program depends on the length of the program.

Consider the following example, in which the statement contains a total of five tokens:

printf("Hello,World");

Every significant character, word, or symbol in the C programming language is a token.

The tokens in this statement are therefore printf(“Hello, World”);. Therefore, C tokens are the fundamental building pieces of a C program.

Semicolons in C

In C programming, a statement is terminated by a semicolon.

In C, the semicolon is also known as a statement terminator, as each statement must end with a statement terminator or semicolon.

This informs the compiler that the statement has been executed.

It indicates the conclusion of one logical object or set of statements.

If there is no semicolon at the conclusion of a statement, the program will not compile and an error message will be generated during compilation.

Consider the following example, which contains two distinct statements each finished by a semicolon:

printf("Hello,World");
 return 0;

Comments in C

In C Programming, comments are similar to help text in a program. The compiler ignores these characters.

Comments are quite useful for code review.

Given that comments are of great use to a coder when reviewing the code.

Programmers can add important information or reminders about a line of code or something else as comments.

In the C programming language, there are two types of comments:

  • Single-line comments – starts from // to the end of line
  • Multi-line comments – starts from /* and ends with */
#include<stdio.h>   
int main()
{
printf("Welcome to basic syntax tutorials!\n"); // This is a single line comment
/* This is a multi-line comment */
};

Here is an example of the above C program’s output:

Welcome to basic syntax tutorials!

On the output screen, only “Welcome to basic syntax tutorials!” will be displayed.

As was previously stated, the compiler ignores all comments. Because only the coder can read the comments.

You can test the above example here!C Online Compiler

Identifiers in C

An identifier is the name used to identify variables, functions, and other user-defined terms in C programming.

An identifier may begin with either letters (A to Z) or underscores (_), followed by zero or more letters, underscores, and digits (0 to 9)

Identifiers in C cannot contain punctuation characters (special characters) such as @, $, and %.

Given that C is a case-sensitive programming language.

Consequently, Total and total are two distinct identifiers in C. The following table provides a list of valid identifiers:

sumtotalavgperc_cal
strc_134juday28_jagfind_sum
c56g8numrescalcalculate_area

Here is a program sample. Just pay attention to this. This program also demonstrates that num and Num, as well as name and Name, are distinct identifiers:

#include <stdio.h>

int main()  
{  
    int a=10;  
    int A=20;  
    printf("Value of a is : %d",a);  
    printf("\nValue of A is :%d",A);  
    return 0;  
}  

Here is a sample execution of the above C program:

Value of a is : 10
Value of A is :20

You can test the above example here!C Online Compiler

Keywords in C

The following is a list of reserved C terms. These reserved words cannot be used as constants, variables, or any other type of identifier.

Keywords are reserved words with a specific significance. The following list presents the C keywords:

autodoubleintstruct
breakelseswitchlong
caseenumregistertypedef
externcharreturnunion
constfloatshortunsigned
continueforsignedvoid
defaultsizeofgotovolatile
doifstaticwhile

Whitespaces in C

Any line having only whitespace, such as a comment, is referred to as a blank line in C programming, and the C compiler completely disregards it.

In C programming, whitespace is the term used to define blanks, tabs, newline characters, and comments.

Whitespace separates one portion of a statement from another one, and it allows the compiler to determine where one element, such as int, stops, and the next element begins.

The following statement:

int age;

For a compiler to distinguish between int (data type) and age (variable), there must be at least one whitespace character (often a space) between them.

If you do not include a whitespace between them, it will be considered as an identifier and become intage.

Another example is the following statement:

fruit = watermelon + mango;   // get the total fruit

 

There are no required whitespace characters between fruit and = or between = and watermelon, although you may include them to improve readability.

 

Frequently Asked Questions (FAQ)

Here are some frequently asked questions regarding the syntax of the C programming language.

  • Question 1: What do you mean by tokens in the C programming language?

    Answer: C Token is the smallest individual unit in the C programming language.

    Tokens are keywords, identifiers, constants, variables, or any other symbol with meaning in the C programming language.

    The C program is also referred to as a collection of distinct tokens.

  • Question 2: What will occur if a semicolon is forgot at the end of a C statement?

    Answer: In the C programming language, omitting the semicolon at the end of a statement will result in a syntactic error.

    In such an instance, the compiler provides an error notice indicating that a semicolon is expected.

  • Question 3: What is a Compilation Error?

    Answer: A compilation error, also known as a compile-time error, is the error produced by the compiler if the C program syntax is incorrect.

    When attempting to compile a program containing a syntax issue, the compiler will generate an error known as a Compile-time error.

  • Question 4: How to add comments in C?

    In a C program, we can add single-line comments by placing // at the beginning of the remark text, and multi-line comments by enclosing the text within /* and */.

  • Question 5: In C, can a program execute without the main() function?

    The main() function defines the starting point of execution in the C programming language.

    If a C program does not have the main() function, no code statement will be performed.

    If the compiler cannot locate the main() function, it often returns an error.

Summary

We trust you comprehend the fundamental grammar of the C programming language and its relationship to program structure.

Tokens, keywords, comments, and other crucial C language components can now be applied to your program with ease.

These C syntax building blocks are crucial for C programming and help you become a good C programmer.

In the next post, I will create a data type in C and attempt to describe its many components in detail.

I hope you appreciate this article on the syntax of the C programming language, in which I attempt to illustrate its basic syntax.


Leave a Comment