03. C – Basic Syntax

By | October 24, 2020

You have seen the basic structure of a C program, so it will be easy to understand other basic building blocks of the C programming language.

Tokens in C

Tokens are the smallest elements of a program, which are meaningful to the compiler. The following are the types of tokens: Keywords, Identifiers, Constant, Strings, Operators, etc.

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

The individual tokens are −

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

Semicolons

In a C program, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon.

Given below are two different statements −

printf("Hello, World! \n");
return 0;

Comments

comment is an explanation or description of the source code of the program. It helps a developer explain logic of the code and improves program readability. At run-time, a comment is ignored by the compiler.

There are two types of comments in C:

1) Multiline comments: /* …….. */.

2) Single-line Comments: It uses a double slash // dedicated to commenting single lines

Here is an example of comments type

Example of Single Line comment 
#include <stdio.h> 
int main(void) 
{ 
  
	// This is a single line comment 
	printf("Guru99"); 
	return 0;  // return zero
}
Example of Multiline Comment
/*Line 1
Line 2
….
…
*/

Identifiers

A C identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z, a to z, or an underscore ‘_’ followed by zero or more letters, underscores, and digits (0 to 9).

C does not allow punctuation characters such as @, $, and % within identifiers. C is a case-sensitive programming language. Thus, Manpower and manpower are two different identifiers in C. Here are some examples of acceptable identifiers −

mohd       zara    abc   move_name  a_123
myname50   _temp   j     a23b9      retVal

Keywords

Keywords are predefined, reserved words used in programming that have special meanings to the compiler. Keywords are part of the syntax and they cannot be used as an identifier. For example:

int money;

Here, int is a keyword that indicates money is a variable of type int (integer).

As C is a case sensitive language, all keywords must be written in lowercase. Here is a list of all keywords allowed in ANSI C.

autoelselongswitch
breakenumregistertypedef
caseexternreturnunion
charfloatshortunsigned
constforsignedvoid
continuegotosizeofvolatile
defaultifstaticwhile
dointstruct_Packed
doublewhile
Keywords in ‘C’

Whitespace

In C++, whitespace refers to spaces, tabs and newlines. In some places in our code, whitespace is necessary whereas, in other places, it is just given to improve readability.

For example, while writing ‘int main()‘, it is necessary to give a space between int and main() (as they are two different words).

int main()

On the contrary, there is no need to give any space in the following statement with the output operator.

std::cout<<“Hello World”;

Leave a Reply

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