Saturday 17 May 2014

Some Terminology

Token In C 

In a C source program, the basic element recognized by the compiler is the "token." A token is source-program text that the compiler does not break down into component elements.
A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.
So, C tokens are of six types. They are,
        1. Keywords               (eg: int, while),
        2. Identifiers               (eg: main, total),
        3. Constants              (eg: 10, 20),
        4. Strings                    (eg: “total”, “hello”),
        5. Special symbols  (eg: (), {}),
        6. Operators              (eg: +, /,-,*)

 For example, the following C statement consists of five tokens:

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


The individual tokens are:

printf                 //first token
(                       //second
"Hello, World! \n"  //third
)                       //fourth     
;                       //fifth 

Semicolons:

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 or ended of the execution of that statement. It is very powerful token,in future we will see about this.

For example, following are two different statements:

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

Comments

Comments are like helping text in your C program and they are ignored by the compiler. In a software company, this comment will help to understand of your code to the other. There are two types of showing the comment :

1. Multiple line comment

                       /* my first program

                           in C*/ 

2. Single line comment
                      //my first program  in C


Declarations and Initialization:

All variables must be declared before use, although certain declarations can be made
implicitly by content. A declaration specifies a type, and contains a list of one or more
variables of that type, as in:

int  lower, upper, step;
char c, line[1000];
A variable may also be initialized in its declaration. If the name is followed by an equals sign and an expression, the expression serves as an initializer, as in:
int   i = 0;
int   limit = MAXLINE+1;
float eps = 3.14;
The initialization is done once only, conceptionally before the program starts executing, and the initializer must be a constant expression.

 Lvalues and Rvalues in C:
There are two kinds of expressions in C:     
lvalue : An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.     
rvalue : An expression that is an rvalue may appear on the right- but not left-hand side of an assignment. Variables are lvalues and so may appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned and can not appear on the left-hand side. Following is a valid statement:
int g = 20;
But following is not a valid statement and would generate compile-time error:
10 = 20;

0 comments :

Post a Comment