Tuesday, October 8, 2019

Tokens and keywords




C Keywords and Identifier

In this tutorial, you will learn about keywords; reserved words in C programming that are part of the syntax. Also, you will learn about identifiers and how to name them.

Character set

A character set is a set of alphabets, letters and some special characters that are valid in C language.

Alphabets

Uppercase: A B C ................................... X Y Z
Lowercase: a b c ...................................... x y z
C accepts both lowercase and uppercase alphabets as variables and functions.

Digits

0 1 2 3 4 5 6 7 8 9

Special Characters

Special Characters in C Programming
,<>._
();$:
%[]#?
'&{}"
^!*/|
-\~+ 
White space Characters

Blank space, newline, horizontal tab, carriage, return and form feed.
-----------------

C tokens, Identifiers and Keywords are the basics in a C program. All are explained in this page with definition and simple example programs.

1. C TOKENS:

  • C tokens are the basic buildings blocks in C language which are constructed together to write a C program.
  • Each and every smallest individual units in a C program are known as C tokens.
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: +, /,-,*)

C TOKENS EXAMPLE PROGRAM:

where,
  • main – identifier
  • {,}, (,) – delimiter
  • int – keyword
  • x, y, total – identifier
  • main, {, }, (, ), int, x, y, total – tokens
Do you know how to use C token in real time application programs? We have given simple real time application programs where C token is used. You can refer the below C programs to know how to use C token in real time program.

2. IDENTIFIERS IN C LANGUAGE:

C Identifiers

Identifier refers to name given to entities such as variables, functions, structures etc.
Identifiers must be unique. They are created to give a unique name to an entity to identify it during the execution of the program. For example:
  1. int money;
  2. double accountBalance;
Here, money and accountBalance are identifiers.
Also remember, identifier names must be different from keywords. You cannot use int as an identifier because int is a keyword.

Rules for naming identifiers

  1. A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores.
  2. The first letter of an identifier should be either a letter or an underscore.
  3. You cannot use keywords as identifiers.
  4. There is no rule on how long an identifier can be. However, you may run into problems in some compilers if the identifier is longer than 31 characters.
You can choose any name as an identifier if you follow the above rule, however, give meaningful names to identifiers that make sense. 
  • Each program elements in a C program are given a name called identifiers.
  • Names given to identify Variables, functions and arrays are examples for identifiers. eg. x is a name given to integer variable in above program.

RULES FOR CONSTRUCTING IDENTIFIER NAME IN C:

  1. First character should be an alphabet or underscore.
  2. Succeeding characters might be digits or letter.
  3. Punctuation and special characters aren’t allowed except underscore.
  4. Identifiers should not be keywords.

3. KEYWORDS IN C LANGUAGE:

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:
  1. 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.
All these keywords, their syntax, and application will be discussed in their respective topics. However, if you want a brief overview of these keywords without going further, visit List of all keywords in C programming.
  • Keywords are pre-defined words in a C compiler.
  • Each keyword is meant to perform a specific function in a C program.
  • Since keywords are referred names for compiler, they can’t be used as variable name.
C language supports 32 keywords which are given below. Click on each keywords below for detail description and example programs.

List of all Keywords in C Language

a brief information on all 32 keywords in C programming.

autodouble
intstruct
constfloat
shortunsigned
breakelse
longswitch
continue
for
signed void
caseenum
registertypedef
defaultgoto
sizeofvolatile
charextern
returnunion
do
if
static while

Description of all Keywords in C


auto

The auto keyword declares automatic variables. For example:
auto int var1;
This statement suggests that var1 is a variable of storage class auto and type int.
Variables declared within function bodies are automatic by default. They are recreated each time a function is executed.
Since, automatic variables are local to a function, they are also called local variables. 

break and continue

The break statement makes program jump out of the innermost enclosing loop (while, do, for or switch statements) explicitly.
The continue statement skips the certain statements inside the loop.
for (i=1;i<=10;++i)
{
   if (i==3)
   continue;
   if (i==7)
   break;
   printf("%d ",i);
} 
1 2 4 5 6
When i is equal to 3, continue statement comes into effect and skips 3. When i is equal to 7, break statement comes into effect and terminates the for loop. 

switch, case and default

The switch and case statement is used when a block of statements has to be executed among many blocks. For example:
switch(expression)
{
    case '1':
    //some statements to execute when 1
    break;
    case '5':
    //some statements to execute when 5
    break;
    default:
    //some statements to execute when default;
}


char

The char keyword declares a character variable. For example:
char alphabet;
Here, alphabet is a character type variable.


const

An identifier can be declared constant by using const keyword.
const int a = 5;


do...while

int i;
do 
{
   print("%d ",i);
   i++;
}
while (i<10)


double and float

Keywords double and float are used for declaring floating type variables. For example:
float number;
double longNumber;
Here, number is single precision floating type variable whereas, longNumber is a double precision floating type variable.


if and else

In C programming, if and else are used to make decisions.
if (i == 1)
   printf("i is 1.")
else
   prinf ("i is not 1.")
If value of i is other than 1, output will be :
i is not 1


enum

Enumeration types are declared in C programming using keyword enum. For example:
enum suit
{
    hearts;
    spades;
    clubs;
    diamonds;
};
Here, a enumerated variable suit is created having tags: heartsspadesclubs and diamonds.

extern

The extern keyword declares that a variable or a function has external linkage outside of the file it is declared.

for

There are three types of loops in C programming. The for loop is written in C programming using keyword for. For example:
for (i=0; i< 9;++i)
{
  printf("%d ",i);
}
Output
0 1 2 3 4 5 6 7 8

goto

The goto keyword is used for unconditional jump to a labeled statement inside a function. For example:
for(i=1; i<5; ++i)
{
    if (i==10)
    goto error;
}
printf("i is not 10");
error:
    printf("Error, count cannot be 10.");
Output
Error, count cannot be 10.

int

The int keyword declares integer type variable. For example:
int count;
Here, count is a integer variable.

short, long, signed and unsigned

The short, long, signed and unsigned keywodrs are type modifiers that alters the meaning of a base data type to yield a new type.
short int smallInteger;
long int bigInteger;
signed int normalInteger;
unsigned int positiveInteger;
Range of int type data types
Data typesRange
short int-32768 to 32767
long int-2147483648 to 214743648
signed int-32768 to 32767
unsigned int0 to 65535

return

The return keyword terminates the function and returns the value.
int func()
{
    int b = 5;
    return b;
}
This function func() returns 5 to the calling function. 

sizeof

The sizeof keyword evaluates the size of data (a variable or a constant).
#include <stdio.h>
int main()
{
    printf("%u bytes.",sizeof(char));
}
Output
1 bytes.

register

The register keyword creates register variables which are much faster than normal variables.
register int var1;

static

The static keyword creates static variable. The value of the static variables persists until the end of the program. For example:
static int var;

struct

The struct keyword is used for declaring a structure. A structure can hold variables of different types under a single name.
struct student{
    char name[80];
     float marks;
     int age;
}s1, s2;

typedef

The typedef keyword is used to explicitly associate a type with an identifier.
typedef float kg;
kg bear, tiger;

union

A Union is used for grouping different types of variable under a single name.
union student 
{
    char name[80];
    float marks;
    int age;
}

void

The void keyword indicates that a function doesn't return any value.
void testFunction(int a)
{
  .....
}
Here, function testFunction( ) cannot return a value because the return type is void.

volatile

The volatile keyword is used for creating volatile objects. A volatile object can be modified in an unspecified way by the hardware.
const volatile number
Here, number is a volatile object.
Since, number is a constant variable, the program cannot change it. However, hardware can change it since it is a volatile object.

No comments:

Post a Comment