PINCU1_TOKENS

 C TOKENS

Tokens are the basic building blocks in C language.  A token is a smallest individual unit in a C program. This means that a program is constructed using a combination of these tokens. 

There are six main types of tokens in C.

  • Keywords

  • Variables

  • Constants

  • Strings

  • Special characters

  • Operators


CHARACTER SET IN C

In C. a character means any letter from English alphabet, digit or special symbol used to represent information. These characters when combined together form tokens that act as basic building blocks of a C program. 

The character set of C can therefore be given as:

a. English alphabet: Include both lower case (a - z) as well as upper case (A-Z) letters

b. Digits: Include numerical digits from 0 to 9

d. White space characters: These characters are used to print a blank space on the screen. 

e. Escape sequence: Escape sequences include \\, \', \", \n, \a, \0, \?.








KEYWORDS

C has a set of reserved words often known as keywords that cannot be used as an identifier. All keywords are basically a sequence of characters that have a fixed meaning. By convention all keywords must be written in lowercase (small) letters. Table  shows the list of keywords in C.



Identifiers

Identifiers are basically the names given to program elements such as variables, arrays, and functions. Identifiers may consist of sequence of letters, numerals, or underscores.

Rules for Forming Identifier Names

Some rules have to be followed while forming identifier names. They are as follows:

  • Identifiers cannot include any special characters or punctuation marks (like #, $, ^,?,, etc.) except the underscore

  • There cannot be two successive underscores.

  • Keywords cannot be used as identifiers.

  • C is a case sensitive language. For example, 'FIRST' is different from 'first' and 'First'.

  • Identifiers must begin with a letter or an underscore.

  • Identifiers can be of any reasonable length. They should not contain more than 31 characters.  

Examples of valid identifiers include:

roll number, marks, name, emp number, basic pay, HRA, DA, dept code, Dept Code, RollNo, EMP NO

Examples of invalid identifiers include:

23_student, Smarks, name, #emp_number, basic.pay, HRA, (DA), &dept_code, auto


BASIC DATA TYPES IN C


A data type is a characteristic that defines what type of data a variable can hold and the set of operations that can be applied.








In C the data types are broadly classified into the following types:

1.Primary Data types:

The standard and basic data types of C are known as the primary data types. C has five basic data types:


1. Char: A char type represents character data type. It occupies 1 byte of memory. It uses the keyword “char”. Character constants and literals can be represented within single quotes.

2.Int: Integers are the numbers without any decimal point (period/exponent). An int type can be classified into three types:

  • short: A short type can represent smallest integers. It occupies 2 bytes of memory.

  • int: An int data type occupies 2 bytes / 4 bytes of storage. Its range is -32,768 to 32767 or or -2,147,483,648 to 2,147,483,647.

  • long: A long type can represent long integers. It occupies 4 bytes of storage.

3. Float: The numbers with decimal point (exponent) are known as floating point numbers or real numbers. They can use the keyword “float”. Float types are classified into three types:

  • float: A float type can represent 6 digits of precision. It occupies 4 bytes of storage. Its range is -3.4e38 to +3.4e38.

  • double: A double type can represent double precision floating point numbers. It occupies 8 bytes of storage. Its range is -1.7e308 to +1.7e308.

  • long double: A double type can represent large range of values. It occupies 10 bytes of storage.

4. Void type: A void type can represent “valueless”. It specifies an empty set of values.


VARIABLES  A variable is defined as a meaningful name given to a data storage location in computer memory. When using a variable, we actually refer to address of the memory where the data is stored. C language supports two basie kinds of variables numeric and character.

Numeric Variables

Numeric variables can be used to store either integer values or floating point values. While an integer value is a whole number without a fraction part or decimal point, a floating point value can have a decimal point.

Character Variables

Character variables are just single characters enclosed within single quotes. These characters could be any character from the ASCII character set letters ('a', 'A'), numerals ('2'), or special characters ('&'). In C, a number that is given in single quotes is not the same as a number

without them. This is because 2 is treated as an integer value but '2' is a considered character not an integer.

Declaring Variables

Each variable to be used in the program must be declared. To declare a variable, specify the data type of the variable followed by its name. The data type indicates the kind of values that the variable will store. 

In C, variable declaration always ends with a semicolon, for example:

  • int emp_num; 
  • float salary;
  • char grade; 
  • double balance amount; 
  • int acc no;


  1. In C variables are declared at three basic places as follows:
  2. When a variable is declared inside a function it is known as a local variable.
  3. When a variable is declared in the definition of function parameters it is known as formal parameter  
  4. When the variable is declared outside all functions, it is known as a global variable.

Initializing Variables

While declaring the variables, we can also initialize them with some value.

For example,

  • int emp_num= 7; 
  • float salary = 2156.35;
  • char grade=’A’
  • double balance_amount=100000000;


When variables are declared but not initialized they usually contain garbage values 


CONSTANTS

Constants are identifiers whose values do not change. While values of variables can be changed at any time, values of constants can never be changed. Constants are used to define fixed values like pi or the charge on an electron so that their value does not get changed in the program even by mistake.

A constant is an explicit data value specified by the programmer. The value of the constant is known to the compiler at the compile time. C allows the programmer to specify constants of 

  • Integer type

  • Floating point type

  • Character type

  • String type

1.Integer Constants

A constant of integer type consists of a sequence of digits. For example, 1, 34, 567, 8907 are valid integer constants. A literal integer like 1234 is of type int by default. For a long integer constant the literal is succeeded with either 'L' or 'l' (like 1234567L). Similarly, an unsigned int literal is written with a 'U' or 'u' suffix (ex, 12U). Therefore, 1234L, 12341, 1234U, 1234u, 1234LU, 1234ul are all valid integer constants.

Integer literals can be expressed in decimal, octal or hexadecimal notation. 

a)Decimal integers

By default an integer is expressed in decimal notation. Decimal integers consist of a set of digits, 0 through 9, preceded by an optional or + sign. 

Examples of decimal integer constants include: 123, -123, +123, and 0.

b)Octal integer

An integer constant preceded by a zero (0) is an octal number. Octal integers consist of a set of digits, 0 through 7. 

Examples of octal integers include

012 0 01234

c)Hexadecimal integer

Similarly, an integer constant is expressed in hexadecimal notation if it is preceded with 0x or 0X. Hexadecimal numbers contain digits from 0-9 and letters A through F, which represent numbers 10 through 15. For example, decimal 72 is equivalent to 0110 in octal notation and 0x48 in hexadecimal notation.

Examples of hexadecimal integers are 0X12, 0x7F, 0xABCD, 0X1A3B.



2.Floating Point Constants

Integer numbers are inadequate to express numbers that have a fractional part. A floating point constant therefore consists of an integer part, a decimal point, a fractional part, and an exponent field containing an e or E (e means exponent) followed by an integer where the fraction part and integer part are a sequence of digits. However, it is not necessary that every floating point constant must contain all these parts. Some floating point numbers may have certain parts missing.

Some valid examples of floating point numbers are: 0.02, 0.23, 123.456, +0.34 123, 0.9, -0.7, +0.8 etc.


3.Character Constants

A character constant consists of a single character enclosed in single quotes. For example, 'a' and ' are character constants. In computers, characters are stored using machine's character set using ASCII codes. All escape sequence characters are constants.

4.String Constants

A string constant is a sequence of characters enclosed in double quotes. So "a" is not the same as 'a'. The characters comprising the string constant are stored in successive memory locations. When a string constant is encountered in a C program, the compiler records the address of the first character and appends a null character ('\0') to the string to mark the end of the string. Thus, length of a string constant is equal to number of characters in the string plus 1 (for the null character). Therefore, the length of string literal "hello" is 6.


Declaring Constants

To declare a constant, precede the normal variable declaration with const keyword and assign it a value. For example,

const float pi 3.14;

The const keyword specifies that the value of pi cannot change.

However, another way to designate a constant is to use the pre-processor command define. #define pi 3.14159.

#define service tax 0.12


No comments:

Post a Comment