Learn C Basic Syntax Rules with Examples

In the previous tutorial, you’ve already seen the structure of the C program on how to create a simple program. So it’s simple to understand the other tutorials in the C programming language.

As we move further, we’ll be focusing on C syntax and program structure. In this tutorial, we won’t explain any advanced program code.

However, this discussion might be able to help you to begin a basic C program simply. By the end of this topic, you might be able to explain every line of code.

For now, let us begin the discussion of C basic Syntax.

What is C Basic Syntax?

Syntax refers to an agreement that needs to be maintained when writing a program. It needed to follow the appropriate syntax when coding in order to obtain the required set of output.

The basic syntax in C language is divided into five which are header, main() function, variable declaration, body, and return type of the program.

You may also read: C Environment Setup in Linux, Mac OS, and Windows

The Structure of C Program

Structure of C ProgramDescription
Header#include <stdio.h>
Main()int main()
{
Variable Declarationint x = 1000
Bodyprint(“%d”, x)
Returnreturn 0;
}
  • Header – a program that starts in the first line with an extension “.h” and consists of macro definitions and C functions.
  • Main – A C program that consists a main() function, because the main() function is the starting of execution in C programming language.
  • Variable Declaration – A variable declaration must be completed inside the main() function and we can declare the variables which are called the global variables.
  • Body – For a body, we conduct operations needed inside the function. For example search, sort, addition, average and print.
  • Return Function – a return function specifies for returning values of a program. When the return type is null there is no return statement.

We will look at the example below.

// This is the example of Basic Syntax of C Program
#include <stdio.h>

// This is the main function
int main()
{

	//This is the body function
	printf(" Hello! Programmers. This is the Basic Syntax in C program ");
	
	// This is the return statement
	return 0;
}

Output:

Hello! Programmers. This is the Basic Syntax in C program

Try to run the example code here: C Online Compiler

Tokens in C Program

The C programming language contains different tokens and the token will be a keyword, identifier, constant, string literal, or maybe a symbol.

Also read or visit: Overview of C Programming Language

Let’s have a look at the first example of tokens in C program.

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

The second example below is for the individual tokens.

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

Semicolons

A semicolon in C language is a presentation of a terminator, which means each one of the statements must end with a semicolon.

Let’s have a look at the example below, which consists of two different functions.

// A C program to present the used of Semicolon
#include <stdio.h>
  
int main() {
  
    printf("This is my first program to know!");
    return 0;
}

If we run the code example above the output will be like this:

This is my first program to know!

Comments

In a C programming language, a comment function is valuable information that is ignored by the compiler.

Obviously, because comments are used to make the program easy to read and understand the functions.

There are two types of comments which are single-line comments and the other one is multiline comments.

Type of CommentsDescription
1. single-line commentsThe single line comments start with the symbol double forward slash (//). It is used only for a single line and it will end at the of the next line.
2. multiline comments.The multiline comments start with the symbol single slash forward with asterisk/*” and it will ended like this “*/“. It can use either single line or multiline.

Example of Single-line and Multiline comments.

// A C Program to test the comments
#include <stdio.h>

int main()
{

	// This is the example single line comment in C Programming language

	/*
		This is the example Multiline comment in C Programming language
	*/

	printf("This is my first program to run in C language!");

	return 0;
}

Identifiers

In a C Language, the identifiers are used to identify user-defined functions and variables.

The identifiers begin in a capital letter A to Z, small letter a to z, underscore(_), and digits (0-9).

In addition, characters like %, @ and $ are not allowed to be used in C language. Here are examples of identifiers in C programming.

eliver glenn jude caren_lovero
johnpaul26 _elijah samer_10

Keywords

In a C program, there are so-called reserved keywords that have a specific meaning. Reserved keywords cannot be used as a variable or constant or the other identifier name.

The table below shows the keywords in C language.

do_Packedstructint
ifwhilestaticdefault
typedefregisterenumbreak
gotovolatilecontinuesizeof
switchlongelseauto
charunsignedfloatshort
signedconstvoidfor

Whitespace in C

In a C program, a line that consists of whitespace, comments, and blank lines is ignored by a compiler.

Furthermore, whitespace is a name used in C to specify the comments, tabs, blanks, and newline characters. A whitespace can divide each part of a function from another and enables the compiler to specify the factor of a statement.

For example:

int name;

However, Age and int must be separated by at least one whitespace character, typically a space, for the compiler to be able to recognize them.

vegetables = eggplant + tomatoes;   // get the total of vegetables

There are no whitespace characters needed between vegetables and = or in between = and eggplant. You are free to add if you want to raise the readability.

Conclusion

To conclude, I hope you understand the C basic Syntax with a program structure.

You may now apply the identifiers, tokens, comments keywords, semicolons, and other important elements of C language in your program structure.

Leave a Comment