Tokens in C
In a C source program, the basic elements recognized by the compiler is the "Tokens". A token is source program text that the compiler does not break down into component elements.The Keywords, Identifiers, Constants, String literals and Operators are example of tokens. Punctuation characters such as Brackets([ ]), Braces({ }), Parentheses(( )) and Commas(,) are also tokens.
The C keywords: -
C keywords are reserved words by the compiler. All the C keywords have been assigned fixed meaning. The keywords cannot used as variable name because they have been assigned fixed job. For utilizing the keywords in a program, no header file is to be included. For example... auto, break, int, float..... etc.
Identifiers:-
A C identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore _ followed by zero or more letters, underscores, and digits (0 to 9).
C does not allow punctuation characters such as @, $, and % within identifiers. C is a case sensitiveprogramming language.
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.
Comments:-
Comments are like helping text in your C program and they are ignored by the compiler. They start with /* and terminates with the characters */ as shown below:
/* my first program in C */
You can use single line Comments like // this is single line comments....
Constants:-
The Constants in C are applicable to the values, which do not change during the execution of a program. There are several types of constants in C. They are classified as :
- Integer Constants
- Real Constants
- Character Constants
Variables:-
A variables is a data name used for storing a data value may be changed during the program execution.
Data Types in C
Data types refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.
S.N. |
Types and Description
|
---|---|
1 | Basic Types: They are arithmetic types and consists of the two types: (a) integer types and (b) floating-point types. |
2 | Enumerated types: They are again arithmetic types and they are used to define variables that can only be assigned certain discrete integer values throughout the program. |
3 | The type void: The type specifier void indicates that no value is available. |
4 | Derived types: They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types. |
Integer Types
Following table gives you details about standard integer types with its storage sizes and value ranges:
Type | Storage size | Value range |
---|---|---|
char | 1 byte | -128 to 127 or 0 to 255 |
unsigned char | 1 byte | 0 to 255 |
signed char | 1 byte | -128 to 127 |
int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
unsigned int | 2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
short | 2 bytes | -32,768 to 32,767 |
unsigned short | 2 bytes | 0 to 65,535 |
long | 4 bytes | -2,147,483,648 to 2,147,483,647 |
unsigned long | 4 bytes | 0 to 4,294,967,295 |
Floating-Point Types
Following table gives you details about standard floating-point types with storage sizes and value ranges and their precision:
Type | Storage size | Value range | Precision |
---|---|---|---|
float | 4 byte | 1.2E-38 to 3.4E+38 | 6 decimal places |
double | 8 byte | 2.3E-308 to 1.7E+308 | 15 decimal places |
long double | 10 byte | 3.4E-4932 to 1.1E+4932 | 19 decimal places |
The void Type
The void type specifies that no value is available. It is used in three kinds of situations:
S.N. | Types and Description |
---|---|
1 | Function returns as void There are various functions in C which do not return value or you can say they return void. A function with no return value has the return type as void. For example void exit (int status); |
2 | Function arguments as void There are various functions in C which do not accept any parameter. A function with no parameter can accept as a void. For example, int rand(void); |
3 | Pointers to void A pointer of type void * represents the address of an object, but not its type. For example a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be casted to any data type. |
The void type may not be understood to you at this point, so let us proceed and we will cover these concepts in the upcoming chapters.
Lets see an example...
#include <stdio.h> int main() { /* variable definition: */ int a, b; int c; /* actual initialization */ a = 10; b = 20; c = a + b; printf("value of c : %d \n", c); printf("Storage size for int : %d \n", sizeof(int)); // geting size of a data type.. return 0; } output: value of c : 30 Storage size for int : 4
Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C language is rich in built-in operators and provides the following types of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Misc Operators
Arithmetic Operators
Following table shows all the arithmetic operators supported by C language. Assume variable A holds 10 and variable B holds 20 then:
Operator | Description | Example |
---|---|---|
+ | Adds two operands | A + B will give 30 |
- | Subtracts second operand from the first | A - B will give -10 |
* | Multiplies both operands | A * B will give 200 |
/ | Divides numerator by de-numerator | B / A will give 2 |
% | Modulus Operator and remainder of after an integer division | B % A will give 0 |
++ | Increments operator increases integer value by one | A++ will give 11 |
-- | Decrements operator decreases integer value by one | A-- will give 9 |
Relational Operators
Following table shows all the relational operators supported by C language. Assume variable A holds 10 and variable B holds 20, then:
Operator | Description | Example |
---|---|---|
== | Checks if the values of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. |
!= | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A <= B) is true. |
Logical Operators
Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then:
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. If both the operands are non-zero, then condition becomes true. | (A && B) is false. |
|| | Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. | (A || B) is true. |
! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A && B) is true. |
Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ are as follows:
p | q | p & q | p | q | p ^ q |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Assume if A = 60; and B = 13; now in binary format they will be as follows:
A = 0011 1100
B = 0000 1101
No comments:
Post a Comment