C program1
C program1
Tokens
Token is the smallest individual units of C language. The different types of tokens are:
1. Keyword
2. Identifier
3. Constant
4. Variables
5. Data Types
6. Operators
Keywords
The reserved words that have standard predefined meaning in C is called Keywords, The keywords can
not be used as variable name , array name or any others name. They are used by the compiler as an aid
to compiling the program. They are always written in lowercase. Some of them are:
break, case , char , int , void , do , if ,while etc.
Identifier:
An identifier is used for any variable , function ,array , data definition etc. Identifier consists letter and
digits in any order but the rule is that the first character must be letter . It can be written in both
uppercase and lowercase letters are not having the same meaning . For example, Name ,NAME and
name refer to different variables.
Constants:
Constant is the fixed values that do not change during the execution of a program. C supports four types
of constants. They are character, string , integer and floating –point constants. As the name suggest
integer and floating point constants represent numbers.
The numeric –type constants must follows the following rules:
1. Commas and blank spaces cannot be included within the constant.
2. The value of a constant cannot exceed specified minimum and maximum bounds.
Types of constants are:
1. Character constant:- It is a single character which is enclosed with single quotation marks. Some
of the valid character constants are ‘a’ ,’A’, ‘y’ etc.
2. String constant:- It consist of any number of characters enclosed in double quotation marks
whose maximum length is 255 characters. Some examples are “computer”, “red” etc.
3. Integer constant : An integer constant refers to a sequence of digits that will be either positive
or negative .For example 123, 678 ,1 etc.
4. Floating point constant:- A floating point constant contains real number with decimal point. For
example 123.56 , 12.89 etc.
Variables
A variable is a named data storage location in your computer’s memory. By using a variable’s name in
program ,referring to data stored there. For many compilers, a variable name can be up to 31 characters
long.
In C , variables names must follow the following rules:
1. The names can contain letters, digits and underscore characters(_).
2. The first character of the variable name must be a letter.
3. Case matters(that is ,uppercase and lowercase)
4. C keywords cannot be used as variable names.
Declaring variables:
A variable declaration tells the compiler the name and type of a variable and optionally initializes the
variable to a specific value. A variable declaration has the following form:
Data_type variable_name ;
For example ,
int a;
char c; etc
Data types
A program contains different types of data types like integer, float , character etc) and need to
store the values being used in the program. C language is rich of data types.
Data types are used to declare the variable name in C language. C supports different types of data,
each of which may be represented differently within the computer’s memory. Data types can be
classified as –
1. Primary data type
2. Derived data type
3. User-defined data type
1. Primary data type : All C compilers accept the following fundamental data types:
2. Derived data type: Functions ,Arrays and pointers are derived data type
3. User defined type : The user defined data types are: Structure , Union and Enumeration.
The typedef keyword :
The typedef keyword is used to create a new name for an existing data type. C language allows user to
define their own data type which are based on existing data types.
The general format of declaring data type is:
typedef existing_data_type user_defined_data_type;
for example :
typedef int integer;
program :
#include<stdio.h>
# include<conio.h>
Void main()
{
typedef int integer;
integer a,b,c;
printf(“Enter your numbers :”);
scanf(“%d %d”,&a,&b);
c=a+b;
printf(“sum is %d”,c);
getch();
}
Arithmetic operators:
C provides all the basic arithmetic operators. It is used to perform mathematical operations.
Operator symbol Example
addition + x+y
Subtractions - x-y
Multiplication * x*y
Division / x/y
Modulus % x%y
Assignment operators
The assignment operator is used to assign a value to a variable. The form is as follows:
Variable=expression;
When executed, expression is evaluated and resulting value is assigned to variable.
Some assignment operators are : += , -= , *= , %= etc
The symbol = is used as assign operator.
Examples:
X=y;
A+=5;
Relational operators
We often compare two quantities and depending on their relation ,take certain decisions .
These comparisons can be done with the help of relational operators. An expression such as
a<b or 5>3 containing a relational operator is termed as a relational expression. The value of
relational expression is either one(true) or zero(false).
operator symbol Example
equal == x==y
Greater than > x>y
Less than < x<y
Greater than or equal to >= x>=y
Less than or equal to <= x<=y
Not equal != x!=y
Logical operators
The logical operators are used to combine two or more relational expression into a single
expression that evaluates to either true or false.
C has three logical operators:
operator symbol example
AND && exp1 && exp2
OR || exp1 || exp2
NOT ! ! exp1
Ternary Operator
The ternary operator is used to check certain relational expression and execute the true
statement if the condition is true and display the false statement if the condition is false.
Syntax
[Condition] ? [true statement]:[false statement];
e.g.
a>b ? printf(“A is greaterno”) : printf(“B is greater no”);
Comma operator
The comma operators use in different ways. They are
I. Comma operators can be used in variable declarations.
Examples int a, b, c;
II. Comma operators can be used in for loop.
Example:
void main()
{
int i, j;
printf(“Enter nos “);
scanf(“%d %d”,&a , &b);
printf(“The nos are %d and %d”,a,b);
}
Bitwise Operators
The bitwise operators are used for having bit level computations of different values.
Operators Meaning
& bitwise AND
| bitwise OR
^ bitwise exclusive OR
<< shift left
>> shift right
Statements
A statement is a complete direction instructing the computer to carry out some task. In C ,
statements are usually written one per line and end with a semicolon.
A. Null statement: If you place a semicolon by itself on a line, you create a null statement –
a statement that doesn’t perform any action.
B. Expression statement : An expression statement is a constant, variable or combination
of constant and variable . This can also include a function call. An expression statement
consists of any valid C expression and followed by semicolon.
Example
A=b;
C=a+b;
c. Control statement : Control statements are used to create special program features,
such as logical tests, loops and branches.
Examples if(control)
else
Expressions:
An expression represents a single data