What is C tokens

C tokens

The tokens of a language are the basic building blocks which can be put together to construct programs. A token can be a reserved word (such as int or while), an identifier (such as b or sum), a constant (such as 25 or "Alice in Wonderland"), a delimiter (such as { or ;) or an operator (such as + or =).
For example, consider the following portion of the program we met in this article:
main() 
{
int a, b, sum;
a = 14;b = 25;
sum = a + b;
printf("%d + %d = %d\n", a, b, sum);
}
Starting from the beginning, we can list the tokens (in bold) in order:
main - identifier
( - left bracket, delimiter
) - right bracket, delimiter
{ - left brace, delimiter
int - reserved word
 
a - identifier
, - comma, delimiter
b - identifier
, - comma, delimiter
sum - identifier
; - semicolon, delimiter
a - identifier
= - equals sign, operator
14 - constant
; - semicolon, delimiter
and so on. Thus we can think of a program as a ‘stream of tokens’, which is precisely how the compiler views it. So that, as far as the compiler is concerned, the above could have been written:
main() { int a, b, sum;
a = 14; b = 25; sum = a + b;
printf("%d + %d = %d\n", a, b, sum); }
The order of the tokens is exactly the same; to the compiler, it is the same program. To the computer, only the order of the tokens is important. However, layout and spacing are important to make the program more readable to human beings.

Comments in C

All programming languages let you include comments in your programs. Comments can be used to remind yourself (and others) of what processing is taking place or what a particular variable is being used for. They can be used to explain or clarify any aspect of a program which may be difficult to understand by just reading the programming statements.

This is very important since the easier it is to understand a program, the more confidence you will have that it is correct. It is worth adding anything which makes a program easier to understand.

Remember that a comment (or lack of it) has absolutely no effect on how the program runs. If you remove all the comments from a program, it will run exactly the same way as with the comments.

Each language has its own way of specifying how a comment must be written. In C, we write a comment by enclosing it within /* and */, for example:

/* This program prints a greeting */

A comment extends from /* to the next */ and may span one or more lines. The following is a valid comment:

/* This program reads characters one at a time
and counts the number of letters found */

C also lets you use // to write one-line comments. The comment extends from // to the end of the line, for example:

a = s * s; //calculate area; store in a

In this series, we will use mainly one-line comments.

4 comments:

  1. #include
    main()
    {
    int i,j;
    float R;
    char ch;
    scanf("%d %c %d",&i,&ch,&j);
    switch(ch)
    {
    case'+':
    R=(i+j);
    break;
    case'-':
    R=(i-j);
    break;
    case'*':
    R=(i*j);
    break;
    case'/':
    R=(float)i/(float)j;
    break;
    case'%':
    R=(i%j);
    break;
    }
    printf("%f",R);
    return 0;

    ReplyDelete
  2. Question: Write a program which can print all odd numbers which is divisible by 5 from a given range.

    #include

    main(){

    int a,b,d,i;
    printf("Enter the beginer number: \n");
    scanf("%d",&a);
    printf("Enter the last number: \n");
    scanf("%d",&b);
    d=5;
    for(i=a;i<=b;i++){
    if(i%5==0){
    printf("%d\n",i);
    }
    }
    }

    ReplyDelete
  3. //write a program whice can print all odd numbers which is divisable by 5 from an given range
    #include
    main()
    {
    int i,first,end;
    printf("Enter the first range:\n");
    scanf("%d",&first);
    printf("Enter the end range:\n");
    scanf("%d",&end);
    for(i=first;i<=end;i++)
    if(i%5==0)
    printf(" %d",i);
    }

    ReplyDelete

  4. The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

    1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

    Let us list the factors of the first seven triangle numbers:

    1: 1
    3: 1,3
    6: 1,2,3,6
    10: 1,2,5,10
    15: 1,3,5,15
    21: 1,3,7,21
    28: 1,2,4,7,14,28

    We can see that 28 is the first triangle number to have over five divisors.

    What is the value of the first triangle number to have over five hundred divisors?

    ReplyDelete