March 27, 2018

C Basic Syntax


   Today we will learn about all the basic syntax about C programming language including tokens, keywords, identifiers etc. 



Tokens in C
A token is the smallest element of a program that is meaningful to the compiler. Tokens are either keywords, identifiers, constants, variables or any symbol which has some meaning in C language. For example the following C statement consists of five tokens :
printf("\n Welcome to the world of C");
The individual tokens are:
printf
(
"\n Welcome to the world of C"
)
;


Semicolon ;
In C program, the semicolon is a statement  terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity.

For example, following are two different statements:
printf("\n Welcome to the world of C");
return 0;


Comments
Comments are a way of explaining what a program does. The commented statements are not executed by the compiler. Rather, they are ignored by the compiler as they are simply added in the program to make the code understandable by the programmer as well as other people who read it.

Comments are written inside the delimiters \* and */. There can be single line or multiple line comments. We can write comments anywhere in a program except inside a string constant or a character constant. Some examples of comments are :

(a) Single line comment
/* my first program in C */
(b) Multiple line comment
/* This is a C program
   to calculate simple
   interest. */

The closing delimiter */ can't be used inside a comment, as a result comments can't be nested i.e. we can't write a comment inside another comment.

Identifiers
Identifiers are basically names given to program elements such as variables, arrays, functions, or any other user-defined item.

Rules for naming identifiers :

  • The name should consist of only uppercase and lower case letters ( A to Z & a to z ), digits ( 0 to 9 ) and underscore sign ( _ ).
  • First character should be an alphabet or underscore.
  • The name should not be a keyword.
  • Since C is a case sensitive, the uppercase and lowercase letters are considered different. For example level, Level and LEVEL are three different identifiers.
  • An identifier name may be arbitrarily long. Some implementations of C recognize only the first eight characters, though most implementations recognize 31 characters. ANSI standard compilers recognize 31 characters.

The identifiers are generally given meaningful names. Some examples of valid identifier names :
roll_number, marks, name, _data, basic_pay, HRA, DA, dept_code
Some examples of invalid identifier names are :
  • 23_student  First character should be an alphabet or underscore
  • int         int is a keyword
  • rec#        # is a special character
  • basic pay   blank space is not permitted


Reserved Words / Keywords
There are certain words that are reserved for doing specific tasks. These words are known as keywords and they have standard, predefined meaning in C. They are always written in lowercase. There are only 32 keywords available in C which are given below :
auto  : Storage class declaration. This is optional.
break : Used to exit from a loop block or switch-case block
case  : Refers to a particular choice in a switch-case block
char : Used for character data type representation
const : Implies that the variable cannot be changed
continue : It forces the next iteration of the loop to take place, skipping any code in between
default : If no case satisfies the condition of switch, then default block must be executed
do : It starts do-while loop
double : It is used for storing double precision floating point number
else : It indicates alternative branches in an if statement
enum : Used to declare enumerated data type
extern : Used before an identifier; implies that the identifier can be defined externally
float : It is used for storing single precision floating point number
for : It is used to start a for loop
goto : It provides an unconditional jump from the goto to a labeled statement in the same function
if : It tests a true/false expression and branches accordingly
int : It is used to store integer value
long : It is a size qualifier
register : Used to keep variable in register
return : It sends control and possibly a return value back from a called function
short :  It is a size qualifier
signed : It is a sign qualifier
sizeof : It is used to calculate the sizes of data types, variables or constant
static : Prefix declaration to make local variable static
sturct : It is used to combine data items of different kinds
switch : A statement that executes code based on a test value
typedef : It is used to define a new name for an existing type or user defined type
union : A special data type that can store different data types in the same memory location
unsigned : It is a sign qualifier
void : Represents the absence of type
volatile : It indicates that a variable may change asynchronously
while : It starts a while loop

Whitespace in C
Whitespace is the term used in C to describe space character, newline character, horizontal tab, vertical tab, form feed and carriage return. It separates one part of a statement from another and enables the compiler to identify where one element in a statement ends and the next element begins. Therefore, in the following statement:
  float result;
There must be at least one whitespace character (usually a space) between float and result for the compiler to be able to distinguish them.



         C Data Types »


1 comment: