0% found this document useful (0 votes)
12 views

C program1

The document outlines the fundamental components of the C programming language, including tokens such as keywords, identifiers, constants, variables, and data types. It explains the rules for declaring variables, the use of the typedef keyword for creating user-defined data types, and various operators including arithmetic, relational, and logical operators. Additionally, it covers the structure of statements and expressions in C, emphasizing their role in instructing the computer to perform tasks.

Uploaded by

happy9865527792
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

C program1

The document outlines the fundamental components of the C programming language, including tokens such as keywords, identifiers, constants, variables, and data types. It explains the rules for declaring variables, the use of the typedef keyword for creating user-defined data types, and various operators including arithmetic, relational, and logical operators. Additionally, it covers the structure of statements and expressions in C, emphasizing their role in instructing the computer to perform tasks.

Uploaded by

happy9865527792
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C program

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:

Data Type Data sub_type Bytes Format Range


character Signed character 1 %c -128 to 127
Unsigned character 1 %c 0 to 255
Integer Short signed int 2 %d -32768 to 32767
Short unsigned int 2 %d 0 to 65535

Long signed int 4 %ld -2147483648 to


2147483647
Long unsigned int 4 %ld 0 to 4294967295
Float 4 %f 3.4E-38 to 3.4E+38

Double 8 %lf 1.7E-308 to 1.7E


+308
Long double 10 %lf 3.4E-4932 to 3.4E
+4932

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();
}

Operators and Expressions


C supports a rich set of operators. An operator is a symbol that tells the computer to perform
certain mathematical or logical manipulations. Operators are used in programs to manipulate
data and variables. They usually form a part of the mathematical or logical expressions.
C operators can be classified into a numbers of categories. They include:-
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Increment and decrement operators
6. Conditional operators
7. Bitwise operators
8. Special operators

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

Increment and Decrement operators


C allows two very useful operators known as increment and decrement operators:
++ and --
The operator ++ adds 1 to the operand ,while – subtract 1. Both are unary operators and takes
the following form:
++n; or n++;
--n or n--;
++n is equivalent to n=n+1
--n is equivalent to n=n-1
Increment and decrement operators are unary operators and they require variable as
their operands.
When postfix ++ or – is used with a variable in an expression ,the expression is evaluated
first using the original value of the variable and then the variable is incremented or
decremented by one.
When prefix ++ or – is used in an expression ,the variable is incremented or
decremented first and then the expression is evaluated using the new value of the
variable.

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

You might also like