C - Introduction-Tokens
C - Introduction-Tokens
======================
INTRODUCTION:
----------------------
1.C Programming Language is used to develop programs or software's.
2.C Programming Language was most popular general purpose programming language.
4.Most of the features of C were derived from its previous languages like B and
BCPL.
Brief History of C:
1.C was developed by DENNIS RITCHIE in 1972 at Bell labs of AT & T [American
Telephone and Telegraphs] in USA.
3.C Language was well suited to develop all types of softwares (System softwares &
Application softwares).Hence it was called as general purpose programming language.
Important Points:
---------------------
1.C is a case sensitive programming language. C statements are entered in lower
case letters only.
1. [Documentation Section]
4. Main( ) section
{
[Local declaration section]
Statements
}
Statements
}
Note:Sections in square brackets are optional.
C-Comments:
------------------
C-comments are used to prepare documentation section, and also prepare guide lines
to the beginer.
The commented lines in the program, are skipped by the compiler while execution.
1.Single-line comment:
//....................
2.Multi-line comments:
/*...............
...................
..................
*/
[OR]
/*
This is example C-program to display Hello world
Output:'Hello World'
Author:Cyber World //Documentation Section
Date:20-Oct-2023
*/
#include<stdio.h>
#include<stdlib.h> //Link Section
#define PI 3.14
float pi=3.14;
int max=100; //Global declaration Section
1.Create C-program
Open Dev-C++ editor ; File->New->Sourse file (Ctrl+N)
The printf statement is used to print messages, the values contain in the variable
on the screen.
Example: printf("WELCOME");
scanf():-
----------
It is a pre-defined function, which is used to read data (inputing data) from
standard input device (keyboard) through console window.
Syntax:-
int scanf(�format(s)�,address-1,address-2,....,address-n);
Example:-
int n;
scanf("%d",&n); // Here '&n' is memory Address of n
C - TOKENS
----------------
[software -> set of programs -> set of statements -> set of tokens.]
1. C-Keywords
2. C-Data types
3. Identifiers ---> names of variables,arrays,functions,pointers,structures etc.
4. Constants
5. Character set
6. Escape sequence characters
7. Operators
C-KEYWORDS
-----------------
A c-keyword is a reserved word,which is used for specific purpose in the program
development.
Don't use these keywords for other purpose in the program development.
A c-keyword is a English word/phrase.
All keywords are must be in lower case letters (small alphabet).
Every keyword, that has internal code, hence it is key to code.
C - DATA TYPES
--------------------
DatatType, defines what type of data to be store by a variable in the C-program.
32-bit based
Data type Format specifier Memory size Storage range
------------ --------------- ---------------- -----------------
==================================================================================
[ASCII : American Standard code for Information Interchange]
IDENTIFIERS
------------------
An identifier is a programmer defined/choice of name given to the program
elements,such name is called as Identifier.
The program elements, such as variables,arrays,
pointers ,functions,structures,unions.
But the programmer must follow all Identifier rules while giving own naming to
variables or arrays or pointers or structures ...... etc.
Identifier Rules:
--------------------
1.It must not be a C- keyword.
4.It starts with either underscore or Alphabet character but not digits.
ex;
name, Name, NAME, _name, my_name, var1, Int, AK47 => Valid identifiers
Declaration of a variable: -
-------------------------------
A variable is declared by basic datatype and name of variable.
syntax:
<basic-datatype> identifier/variable-name;
(or)
<basic-datatype> identifier-1, identifier-2,���identifier-n;
Examples:
int n; 4-Bytes //n only stores an integer value.
char ch; 1-Byte //ch only stores one character value.
float f; 4-Bytes // f only stores one float value.
int a,b,c;
n=100;
ch='M'; //variables assignment by values
f=56.7435;
Initialization of variable: -
-------------------------------
At the time of declaring a variable we can store some data into that variable is
known as variable initialization.
syntax:
<datatype> identifier/variable-name = value;
Example: -
int n=100;
1.printf(�%d�,n); output:100
2.printf(�value of n=%d�,n); output: value of n=100
Note:In C language all declarations will be done before the first execution
statment of the program.
CONSTANTS
---------------
Constant in 'C' refers to fixed value that doesn�t change during the execution of a
program.
C supports the following constants.
3)Character constants - - > 'B' , 'A' , 'q' , ..... must be enclosed with
in single quotes.
1.Symbolic Constants: -
------------------------------
It is a pre-processor statement in link section and is used to define symbolic
constants.
Syntax: -
<#define> <identifier/Symbol> <value>
Ex:
#define MAX 100
#define PI 3.14
2.Constant variable:
---------------------------
'const' keyword is also used to constant a variable.
ex;
const float g=9.8; // g is constant for entire program.
g=3.24; //It gives error message, read only variable 'g'
C-CHARACTER SET:
============
C- language supports 256 characters and each character is represented by ASCII
(American Standard Code for Information Interchange) code. It ranges from 0 to 255.
It includes following list of characters;
1.Alphabet Upper case & Lower case [A-Z & a-z]
2.Digits (Numbers: 0 to 9 )
3.Special characters ( ! @ # $ % ^ & * ( ) � � ; : > < ? .
spacecharacter ..��.. )
4.Symbols (sigma,pi,gamma,alpha,delta,.....)
5.Some ASCII codes are reserved for future use.
\n = new line
\t = Horizontal tab (default 8 spaces)
\v = Vertical tab (default 1 line)
\b = Back space
\r = Carriage return
\a = Alert (Beep sound)
Ex;
#include<stdio.h>
void main()
{
printf("hello\nwelcome\tto\ncomputer\tclasses\a");
}
char:
char ch=�h�;
printf(�\n\n%c�,ch); o/p:h
printf(�\n%4c�,ch); o/p:_ _ _h(before 3 spaces)
printf(�\n%-5c�,ch); o/p:h_ _ _ _(after 4 spaces)
float:
float ft=45.1352;
printf(�\n\n%f�,ft); o/p:45.135200
printf(�\n%12f�,ft); o/p:_ _ _45.135200
printf(�\n%-12f�,ft); o/p:45.135200_ _ _
printf(�\n%.2f�,ft); o/p:45.13
printf(�\n%7.2f�,ft); o/p:_ _45.13
String:
char st[10]=�welcome�;
printf(�\n\n%s�,st); o/p:welcome
printf(�\n%9s�,st); o/p:_ _welcome
printf(�\n%.3s�,st); o/p:wel
OPERATORS
==========
"An operator is a symbol or a special character, used to perform an operation"
Unary operator:
This operator requires only one variable to perform an operation.
ex; -- ++ ~ >> << !
Binary operator:
This operator requires two variables to perform an operation.
ex; + - < > == & |
1.Arithmetic operators
2.Relational operators
3.Logical operators
4.Assignment operator (=)
5.Increment operator (Unary ++ )
6.Decrement operator (Unary --)
7.sizeof() operator
8.comma operator (,)
9.conditional operator (Ternary Operator)
10.Bitwise operators
1.Arithmetic operators:
-------------------------------
-> All arithmetic operators are binary type operators, i.e An arithmetic operator
that requires two values or two variable to perform an operation.
An Arithmetic operator applied on numerical values (integers , float values)
Operator Description
----------- --------------
+ Addition
- Subtraction
* Multiplication
-------------------------------------------------------------------------
Ex;
k=a+b/c
k=(a+b)/c
k=a*b+c/d+e
k=a*(b+c)/(d+e)
Example Program:
#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
c=a+b;
printf("\nSUM =%d",c);
printf("\nSUB =%d",(a-b));
printf("\nMUL =%d",(a*b));
printf("\nDIV =%d",(a/b));
printf("\nMOD =%d",(a%b));
}
practice programs:
---------------------
1.Write a C-program to display area and circumference of a circle by reading Radius
of circle.
area=pi*r*r
circumference=2*pi*r;
#include<stdio.h>
#define PI 3.14
const float K=2.0;
void main()
{
float r,a,c;
printf("Enter radius of circle: ");
scanf("%f",&r);
a=PI*r*r;
c=K*PI*r;
printf("\nArea=%f",a);
printf("\nCircumference=%f",c);
}
#include<stdio.h>
void main()
{
float l,b,a,p;
printf("Enter length & breadth of a rectangle\n");
scanf("%f%f",&l,&b);
a=l*b;
p=2.0*(l+b);
printf("\nArea\t=%.2f",a);
printf("\nPerimeter=%.2f",p);
}
3.Write a C-program,to display Employee Gross & Net salary,by reading Basic salary
of an employee;
consider the following criteria;
Assume,
hra=30% of basic
da=27% of basic
ta=15% of basic
pf=12% of basic
esi=5% of basic
gross salary=basic+hra+da+ta
net salary = gross salary-(pf+esi)
#include<stdio.h>
void main()
{
float basic,hra,da,ta,esi,pf,gross,net;
printf("Enter Basic salary of an employee:");
scanf("%f",&basic);
hra=(30.0/100.0)*basic;
da=(27.0/100.0)*basic;
ta=(15.0/100.0)*basic;
pf=(12.0/100.0)*basic;
esi=(5.0/100.0)*basic;
gross=basic+hra+da+ta;
net=gross-(pf+esi);
printf("\n\nBasic salary\t:%.2f",basic);
printf("\nGross salary\t:%.2f",gross);
printf("\nNet salary\t:%.2f",net);
}
4.Write a program to display Simple Interest & Net amount to repayment,by reading
Principal(P) amount,Rate of Interest(R) in % , Time(T) in years.
SI=(P*R*T)/100.0
Net=P+SI
#include<stdio.h>
void main()
{
float SI,P,R,T,NET;
printf("Enter Principal Amount, Rate of Interest in percentage, and Time in years\
n");
scanf("%f%f%f",&P,&R,&T);
SI=(P*R*T)/100.0;
NET=P+SI;
printf("\nPrincipal Amount :%.2f",P);
printf("\nSimple Interest :%.2f for %.2f years",SI,T);
printf("\nNet Amount to Pay:%.2f",NET);
}
% of profit, K=((SP-CP)/CP)*100.0
k=(a+b)/(c*b)
Operator Description
-------- -----------
< Less than
!= Not Equal to
ex;
int a=10, b=45;
(a>b) ---> relational expression , returns false i.e '0'
int a=45,b=10;
(a>b) ---> relational expression , returns true i.e '1'
Example:
-----------
#include<stdio.h>
void main()
{
int a,b;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
printf("a=%d\tb=%d\n",a,b);
printf("\n(a>b)\t:%d",(a>b));
printf("\n(a<b)\t:%d",(a<b));
printf("\n(a>=b)\t:%d",(a>=b));
printf("\n(a<=b)\t:%d",(a<=b));
printf("\n(a==b)\t:%d",(a==b));
printf("\n(a!=b)\t:%d",(a!=b));
}
3.Logical operators:
--------------------------
A logical operator is used to test multiple relational expressions or conditions.
Operator Description
----------- ---------------
|| Logical 'OR'
! Logical 'NOT'
Note:Logical AND(&&), Logical OR(||) are binary type operators, where Logical
Not(!) is unary operator.
Logical operators, work based on Truth Table;
Truth Table:
A B A&&B A||B !A
--- --- ------- ----- -----
0 0 0 0 1
0 1 0 1 1
1 0 0 1 0
1 1 1 1 0
Ex:
int a=10,b=30,c=5;
(b<a) => 0
!(b<a) => 1
0
Example program:
----------------
#include<stdio.h>
void main()
{
int a=20,b=10,c=5;
printf("\n%d",(a>b && c>b));
printf("\n%d",(a>b || c>b));
printf("\n%d",(a<b));
printf("\n%d",!(a<b));
}
4.Assignment operator: [ = ]
------------------------------
It is nothing but equal to (=) symbol.
It is used to assign or copy rightmost value to leftmost variable.
Its associativity always right to left.
ex;
int a=10;
a=12*34;
c=(a+b)*20;
d=a;
Note : Assignment perator also used to initialize variables.
Ex1;
int a=10;
++a; => a=a+1 = 11
Ex2;
int a=10,c;
2)Post-Increment: [variable++]
"Operaor (++) follows the Operand/variable "
Ex1;
int a=10;
a++; =>a=a+1 = 11
Ex2;
int a=10,c;
printf("\na=%d",a); //a=20
++a; //Pre-Increment
printf("\na=%d",a); //a=21
c=10*++a;
printf("\nc=%d a=%d\n",c,a); //c=220 a=22
printf("\nb=%d",b); //b=10
b++; //Post-Increment
printf("\nb=%d",b); //b=11
c=10*b++;
printf("\nc=%d b=%d",c,b); //c=110 b=12
}
1)Pre-Decrement: [--variable]
"Operand/variable follows the operaor --"
Ex1;
int a=10;
--a; =>a=a-1 = 9
Ex2;
int a=10,c;
2)Post-Decrement: [variable--]
"Operaor --, follows the Operand/variable "
Ex1;
int a=10;
a--; =>a=a-1=9
Ex2;
int a=10,c;
void main()
{
int a=20,b=10,c;
printf("\na=%d",a); //a=20
--a; //Pre-Decrement
printf("\na=%d",a); //a=19
c=10*--a;
printf("\nc=%d a=%d\n",c,a); //c=180 a=18
printf("\nb=%d",b); //b=10
b--; //Post-Decrement
printf("\nb=%d",b); //b=9
c=10*b--;
printf("\nc=%d b=%d",c,b); //c=90 b=8
}
7. sizeof( ) operator:
------------------------
It is a compiler operator,and is used to find the allocated memory size of
datatype / variable / array / pointer / structure interms of bytes.
This operator returns a number interms of bytes and It takes one argument, that may
be datatype or variable or array or pointer or structure.
syntax: sizeof(datatype/variable);
Example program:
#include<stdio.h>
void main()
{
char ch;
int n;
float ft;
printf("\nchar size\t:%d byte",sizeof(ch));
printf("\nint size\t:%d bytes",sizeof(n));
printf("\nfloat size\t:%d bytes",sizeof(ft));
printf("\ndouble size\t:%d bytes",sizeof(double));
}
Ex;
int a,b,c,d;
printf("Value of a=%d",a);
scanf("%d%d",&a,&b);
syntax:
Ex1;
int a=10,b=50;
Ex2;
int a=10,b=50,c;
c=(a>b) ? a : b;
printf("\n%d is Greatest",c);
Operator Description
------------ -----------------
& Bitwise 'AND'
| Bitwise 'OR'
^ Bitwise Exclusive OR
~ Bitwise complement
------------------------------------------------------------------------------
4 0 1 0 0
5 0 1 0 1
6 0 1 1 0
7 0 1 1 1
----------------------------------------
Ex;
int a=3,b=6;
a=3 => 0 0 1 1
b=6 => 0 1 1 0
------------------------
a&b => 0 0 1 0 =>2
a=3 => 0 0 1 1
~a => 1 1 0 0 => -ve value
a=3 => 0 0 1 1
a<<1=> 0 1 1 0 =>6
b=6 => 0 1 1 0
b>>1=> 0 0 1 1 =>3
Example program:
-------------------
#include<stdio.h>
void main()
{
int a,b;
printf("Enter two numbers:\n");
scanf("%d%d",&a,&b);
printf("\na=%d\tb=%d\n",a,b);
printf("\na&b\t:%d",(a&b));
printf("\na|b\t:%d",(a|b));
printf("\na^b\t:%d",(a^b));
printf("\n~a\t:%d",(~a));
printf("\na<<1\t:%d",(a<<1));
printf("\nb>>1\t:%d",(b>>1));
}
-----------------------------------------------------------------------------------
----------------------------------
CONTROL STATEMENTS
====================
[Control Structures]
These statements are used to achieve a Non-Sequential flow of execution of a
program.
These are used to implement complexed solutions and used as building blocks.
These are categorized into following three ;
syntax;
.....................
....................
................
if(TestCondition)
{
........
...... //statements to be executed
.......... //'if' block
........
}
..................
..................
.................
Ex5; To display student result by reading each subject marks, each subject ,max.
100, cut-off 35.
#include<stdio.h>
void main()
{
int m,p,c,t;
printf("Enter 3 subject marks\n");
scanf("%d%d%d",&m,&p,&c);
if(m>=35 && p>=35 && c>=35)
{
printf("\nResult: PASS");
t=m+p+c;
printf("\nGain marks:%d out of 300",t);
}
else
{
printf("\nResult:FAIL");
}
}
syntax;
if(testcondition-1)
{
....
....
}
else if(testcondition-2)
{
....
....
}
else if(testcondition-3)09
{
....
....
}
.
.
.
.
else if(testcondition-N)
{
....
....
}
else
{
....
....
}
Ex4; To perform and display specific arithmetic operation using 'else-if' statement
#include<stdio.h>
#include<process.h>
void main()
{
int ch,a,b;
printf("\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Exit");
printf("\n\nEnter your choice: ");
scanf("%d",&ch);
if(ch>=1 && ch<=4)
{
printf("\n\nEnter two numbers\n");
scanf("%d%d",&a,&b);
}
if(ch==1)
{
printf("\nsum=%d",a+b);
}
else if(ch==2)
{
printf("\nsub=%d",a-b);
}
else if(ch==3)
{
printf("\nmul=%d",a*b);
}
else if(ch==4)
{
printf("\ndiv=%d",a/b);
}
else if(ch==5)
{
exit(0);
}
else
{
printf("Invalid choice!");
}
}
if(m<0 || m>100)
{
printf("Invalid marks");
exit(0);
}
else if(m>90)
{
printf("\nA Grade");
}
else if(m>75 && m<=90)
{
printf("\nB Grade");
}
else if(m>60 && m<=75)
{
printf("\nC Grade");
}
else if(m>50 && m<=60)
{
printf("\nD Grade");
}
else if(m>=40 && m<=50)
{
printf("\nE Grade");
}
else
{
printf("\nF Grade");
}
}
#include<stdio.h>
void main()
{
float bill,discount,net;
printf("Enter total bill:");
scanf("%f",&bill);
if(bill<=3000.0)
{
discount=0.0;
}
else if(bill>3000.0 && bill<=10000.0)
{
discount=(10.0/100.0)*bill;
}
else if(bill>10000.0 && bill<=20000.0)
{
discount=(20.0/100.0)*bill;
}
else if(bill>20000.0 && bill<=30000.0)
{
discount=(30.0/100.0)*bill;
}
else if(bill>30000.0 && bill<=40000.0)
{
discount=(40.0/100.0)*bill;
}
else{
discount=(50.0/100.0)*bill;
}
net=bill-discount;
printf("\nBill Amount\t:%.2f",bill);
printf("\nDiscount\t:%.2f",discount);
printf("\nNet Bill to Pay\t:%.2f",net);
}
systax1:
if(Test-condition) //Outer 'if' block
{
if(Test-condition) //Inner 'if' block
{
-----
----
}
syntax2:
if(Test-condition)
{
......................
...................
if(Test-condition)
{
-----
----
}
else
{
----
----
}
..................
................
..............
}
else
{
if(Test-condition)
{
-----
----
}
else
{
----
----
}
}
syntax3:
if(Test-condition)
{
if(Test-condition)
{
if(Test-condition)
{
-----
----
}
}
else if((Test-condition)
{
----
----
}
else
{
else
{
if(Test-condition)
{
-----
----
}
else if((Test-condition)
{
----
----
}
else
{
syntax4:
if(Test-condition)
{
if(Test-condition)
{
if(Test-condition)
{
-----
----
}
}
else if(Test-condition)
{
if(Test-condition)
{
-----
----
}
else
{
----
----
}
}
else
{
----
----
}
else
{
if(Test-condition)
{
-----
----
}
else if(Test-condition)
{
----
----
}
else
{
----
----
}
Ex2;To check the given year is leap year or not a leap year
Centuary Years:
100,200,300, ,........ 1100, .....1900, 2000, 2100,2200,.....3000,......
If a centuary year is divisible by both 4 and 400, then that year is called as Leap
Year.
Non-Centuary Years:
1-99,101-199,201-299,301-399,.....
If a non-centuary year is divisible by 4 only, then that year is called as Leap
Year.
#include<stdio.h>
void main()
{
int y;
printf("Enter a year value: ");
scanf("%d",&y);
if(y%4==0)
{
if(y%100==0)
{
if(y%400==0)
{
printf("\nIt is a Leap Year");
}
else
{
printf("\nIt is not a Leap Year");
}
}
else
{
printf("\nIt is a Leap Year");
}
}
else
{
printf("\nIt is not a Leap Year");
}
}
Each case block contains one or more executable stmts and is terminated by 'break'
keyword.
Which case-value matches with switch argument, that case block only will be
executed.
syntax:
----------
switch(Argument/Expression)
{
case case-value1:
-----
-----
break;
case case-value2:
-----
-----
break;
case case-value3:
-----
-----
break;
.
.
case case-valueN:
-----
-----
break;
default:
------
------
}
default:
printf("Invalid choice");
}
}
case 'G':
case 'g':
printf("GREEN");
break;
case 'B':
case 'b':
printf("BLUE");
break;
default:
printf("Invalid choice");
}
}
Nested 'switch':
-----------------
Writing/Defining a switch block within case block of outer switch block.
It is mostly used to display sub menu of items.
switch(n)
{
case 0:
printf("\nGiven number is zero");
break;
case 1:
printf("\nGiven number is ODD");
break;
default:
rem=n%2;
switch(rem)
{
case 0:
printf("\nGiven number is EVEN");
break;
default:
printf("\nGiven number is ODD");
}
}
}
'for' statement has block represented by pair of curly braces { }, that contains
one or more executable statements.
Working:
First controller of an execution enter into 'initialization', then goto 'test-
condition', if it is true then for block will be executed, then goto 'updation',
then again goto 'test-condition' if it is true again, for block will be executed,
repeats this until test-condition is true, once test-condition is false, then
immediately controller of an execution skip the execution of 'for' block and
continue statement after for block.
Syntax;
.........
...........
........
for(initialization; Test-Condition; Updation)
{
.............
.............
............. //Executable statements (for block)
............
............
............
}
................................
...........................
............................
0 1 1 2 3 5 8 13 21 .....
t1 t2 t3
t1 t2 t3
t1 t2 t3
#include<stdio.h>
void main()
{
int i,n,t1,t2,t3;
printf("Enter Total no.of terms in Fibonacci series: ");
scanf("%d",&n);
t1=0;
t2=1;
printf("\nFibonacci series:\n\n");
printf("%d%c%d%c",t1,32,t2,32);
for(i=3;i<=n;i++)
{
t3=t1+t2;
printf("%d%c",t3,32);
t1=t2;
t2=t3;
}
}
1 2 3 4 5
1 * * * * *
2 * * * * *
i 3 * * * * *
4 * * * * *
5 * * * * *
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
printf("*%c",32);
}
printf("\n");
}
}
Explanation
i=1 =>j=1, j=2, j=3, j=4, j=5
i=2 =>j=1, j=2, j=3, j=4, j=5
i=3 =>j=1, j=2, j=3, j=4, j=5
i=4 =>j=1, j=2, j=3, j=4, j=5
i=5 =>j=1, j=2, j=3, j=4, j=5
1 2 3 4 5
1 *
2 * *
i 3 * * *
4 * * * *
5 * * * * *
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("*%c",32);
}
printf("\n");
}
}
1 2 3 4 5
5 * * * * *
4 * * * *
i 3 * * *
2 * *
1 *
#include<stdio.h>
void main()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
printf("*%c",32);
}
printf("\n");
}
}
1 2 3 4 5
1 *
2 * *
i 3 * * *
4 * * * *
5 * * * * *
#include<stdio.h>
void main()
{
int i,j,n;
printf("\nEnter no.of rows: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf("%c%c",32,32);
}
for(j=1;j<=i;j++)
{
printf("*%c",32);
}
printf("\n");
}
}
1 2 3 4 5
1 * * * * *
2 * * * *
i 3 * * *
4 * *
5 *
#include<stdio.h>
void main()
{
int i,j,n;
printf("\nEnter no.of rows: ");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=1;j<=n-i;j++)
{
printf("%c%c",32,32);
}
for(j=1;j<=i;j++)
{
printf("*%c",32);
}
printf("\n");
}
}
#include<stdio.h>
void main()
{
int n,t1,t2,t3,i;
printf("Enter no.of terms in Fibonacci series: ");
scanf("%d",&n);
t1=0;
t2=1;
printf("\nFibonacci Series:\n\n");
printf("%d%c%d%c",t1,32,t2,32);
i=3;
while(i<=n) //for(i=3;i<=n;i++)
{
t3=t1+t2;
printf("%d%c",t3,32);
t1=t2;
t2=t3;
i++;
}
}
Explanation;
n=123
sum=0
(123>0) => rem=3
sum=0+3=3
n=12
n=12
sum=3
(12>0) => rem=2
sum=3+2=5
n=1
n=1
sum=5
(1>0) => rem=1
sum=5+1=6
n=0
n=0
sum=6
(0>0) => False(0)
#include<stdio.h>
void main()
{
int n,r=0,rev=0;
printf("Enter a number: ");
scanf("%d",&n);
while(n>0)
{
r=n%10;
rev=(rev*10)+r;
n=n/10;
}
printf("\nReverse of given number: %d",rev);
}
Ex1;
123 => (1*1*1)+(2*2*2)+(3*3*3) = 1+8+27 =36
So, 123 != 36 , hence 123 is not an armstrong number
Ex2;
153 =>(1*1*1)+(5*5*5)+(3*3*3) = 1+125+27 =153
So, 153 = 153 , hence 153 is an armstrong number
#include<stdio.h>
void main()
{
int n,t,sum=0,rem=0;
printf("Enter a 3-digit number[100 to 999]: ");
scanf("%d",&n);
t=n;
while(n>0)
{
rem=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}
if(t==sum){
printf("\nGiven number is an Armstrong number");
}
else{
printf("\nGiven number is not an Armstrong number");
}
}
Nested 'while'
-----------
Define/write a 'while' block within another 'while' block.
It is mostly used to represent ROWs and COLUMNs.
syntax;
while(Testcondition) //outer while block[ROWs]
{
1 2 3 4 5
1 * * * * *
2 * * * * *
i 3 * * * * *
4 * * * * *
5 * * * * *
#include<stdio.h>
void main()
{
int i,j;
i=1;
while(i<=5)
{
j=1;
while(j<=5)
{
printf("*%c",32);
j++;
}
printf("\n");
i++;
}
}
1 2 3 4 5
1 $ * * * *
2 * $ * * *
i 3 * * $ * *
4 * * * $ *
5 * * * * $
#include<stdio.h>
void main()
{
int i,j;
i=1;
while(i<=5)
{
j=1;
while(j<=5)
{
if(i==j){
printf("$%c",32);
}
else{
printf("*%c",32);
}
j++;
}
printf("\n");
i++;
}
}
1 2 3 4 5
1 $ * * * $
2 * $ * $ *
i 3 * * $ * *
4 * $ * $ *
5 $ * * * $
#include<stdio.h>
void main()
{
int i,j;
i=1;
while(i<=5)
{
j=1;
while(j<=5)
{
if(i==j || i+j==6){
printf("$%c",32);
}
else{
printf("*%c",32);
}
j++;
}
printf("\n");
i++;
}
}
syntax;
do
{
.....
..... Executable statements
.....
}while(Test-Condition);
Ex1;
/*'do-while' example program
NOTE: 'do-while' guarantees one time execution of 'do' block, even test condition
is FALSE at first iteration/repetition.
*/
#include<stdio.h>
void main()
{
int n=8;
do
{
n++;
printf("\nn=%d",n);
}while(n>10);
}
}while(i<n);
}
default:printf("\nInvalid choice!\n\n");
}
}while(1);
}
1 2 3 4 5
1 * * * * *
2 * * * * *
i 3 * * * * *
4 * * * * *
5 * * * * *
#include <stdio.h>
void main()
{
int i,j;
i=1;
do
{
j=1;
do
{
printf("*%c",32);
j++;
}while(j<=5);
printf("\n");
i++;
}while(i<=5);
}
'break':
----------
It is a keyword.
It is used within switch block or any loop block, to break up/ discontinue
execution of block of statements.
Ex;
#include<stdio.h>
void main()
{
int n=1;
while(n<=10)
{
if(n==5)
{
break;
}
printf("%d\n",n);
n++;
}
printf("\n\nReached END of the program");
}
'continue':
-------------
It is a keyword.
It is used within loop blocks only, to continue next repetition/iteration of loop
block, i.e it skips execution of specific/a range of repetitions of loop block.
It carries the controller of an execution to the begining of the loop.
Ex;
#include<stdio.h>
void main()
{
int n=0;
while(n<10)
{
n++;
if(n==5 || n==7 || n==9)
{
continue;
}
printf("%d\n",n);
}
printf("\n\nReached END of the program");
}
'goto':
------
It is also a keyword, and is used to jumping the controller of an execution from
one point of line to another point of line in the program with help of label-name.
The label-name is identifier i.e programmer choice of name.
General form;
main()
{
.......
.......
goto Mypoint;
................
..............
................
Mypoint:
...............
..................
}
Ex;
#include<stdio.h>
void main()
{
int i=1,s=0;
LOOP:
printf("%d%c",i,32);
s=s+i;
i=i+1;
if(i<=100)
{
goto LOOP;
}
else
{
goto STOP;
}
STOP:
printf("\nsum=%d",s);
}
ARRAYS:
=======
An Array is a collection of data items/elements/values and all elements are belongs
to one datatype.
OR
An Array is a collection of homogeneous data items.
All Array elements shares a common name and each element is associated or
represented by an index value that starts with zero (0).
So we can access or update an array element by corresponding index only.
Types of Arrays:
--------------------
There are two types of Arrays, as follows;
1-D ARRAY:
---------------
In this type,each array element is represented by only one index value.
So we can access/update 1-D array element by corresponding index value. The index
is always start from 0(zero).
'for' statement is a convenient way to implement 1-D array.
<datatype> <array-name>[size];
Ex;
int a[5];
syntax;
<datatype> <array-name>[size]={val1,val2,....valN};
Ex;
0 1 2 3 4 => indices
int a[5]={10,20,30,40,50};
Accessing;
<array-name>[index];
printf("%d",a[0]) =>10
printf("%d",a[1]) =>20
.
.
printf("%d",a[4]) =>50
Update;
a[1]=250;
#include<stdio.h>
void main()
{
int a[5]; //1-D array declaration
int b[]={1,3,5,7,9}; //1-D array initialization
a[0]=10;
a[1]=20; //1-D array assigment
a[2]=30;
a[3]=40;
a[4]=50;
printf("\n%d",a[4]);
a[4]=123; //update/modify array element
printf("\n%d",a[4]);
}
Ex2;Write a program to read & display sum of array elemets using 'for' loop
#include<stdio.h>
void main()
{
int a[5],i,sum=0;
printf("Enter 5 integer values\n");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
printf("\nGiven Array Elments are: ");
for(i=0;i<5;i++)
{
printf("%d%c",a[i],32);
sum=sum+a[i];
}
printf("\nSum of array elements: %d",sum);
}
Ex5:Write a C program to search a given element in the given 1-D array [Linear
Search]
#include<stdio.h>
void main()
{
int a[10]={34,20,56,10,78,23,44,12,47,15};
int i,key,flag=0;
printf("Given Array: ");
for(i=0;i<10;i++)
{
printf("%d%c",a[i],32);
}
printf("\n\nEnter an Element to be Search:");
scanf("%d",&key);
for(i=0;i<10;i++)
{
if(key==a[i])
{
flag=1;
break;
}
}
if(flag==1)
{
printf("\nGiven Element found at %d position\n",(i+1));
}
else{
printf("\nGiven Element not found\n");
}
}
2-D ARRAY:
---------------
In this type,each array element is represented by two index values.One is Row index
and another one is Column index.
Nested 'for' is used to implement 2-D array.
2-D Array is mostly used to represent Matrix and Matrix operations.
Declaration syntax;
<datatype> <array-name>[ROW-size][COL-size];
Ex;
a[0][0]=7;
a[0][1]=9;
a[0][2]=3;
a[0][3]=2;
a[1][0]=17;
a[1][1]=19;
a[1][2]=13;
a[1][3]=12;
a[2][0]=70;
a[2][1]=90;
a[2][2]=30;
a[2][3]=20;
Initialization;
syntax;
<datatype> <array-name>[Row-size][Col-size]={val1,val2,....valN};
Ex;
int a[3][3]={10,20,30,40,50,60,70,80,90};
OR
Representation;
0 1 2
0 10 20 30
a[0][0] a[0][1] a[0][2]
1 40 50 60
2 70 80 90
Matrix-A Transpose of A
0 1 2 0 1
#include<stdio.h>
void main()
{
int a[2][3]={1,2,3,4,5,6};
int t[3][2];
int i,j;
printf("\nGiven Matrix-A\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%d%c",a[i][j],32);
}
printf("\n");
}
Matrix Multiplication:
------------------------------
Rule: Total no.of columns in first matrix must equal to Total no.of rows in second
matrix.
Matri-A Matrix-B
0 1 2 c1 0 1 c2
r1 r2
0 1 2 1 0 1 2
1 2 1 2 1 2 1
2 1 1
1*1+2*2+1*1 1*2+2*1+1*1
2*1+1*2+2*1 2*2+1*1+2*1
Matrix-C
6 5
6 7
#include<stdio.h>
void main()
{
int a[2][3]={1,2,1,2,1,2},b[3][2]={1,2,2,1,1,1},c[2][2],i,j,k,sum;
printf("Given Matrix-A:\n");
for(i=0;i<2;++i)
{
for(j=0;j<3;++j)
{
printf("%d%c",a[i][j],32);
}
printf("\n\n");
}
printf("Given Matrix-B:\n");
for(i=0;i<3;++i)
{
for(j=0;j<2;++j)
{
printf("%d%c",b[i][j],32);
}
printf("\n\n");
}
STRINGS
-----------
A string is a collection of characters.
OR
A string is a group of characters.
OR
A string is a sequence of characters.
OR
A string is an array of characters.
Ex;
"hai"
"hello"
"welcome"
"123"
"Ak47"
"hi"
"12.345"
NOTE:
There is NO direct basic datatype of String in C-language to create strings, So we
create strings in C-language by using 'Character Array'.
Character ARRAY:
------------------------
Syntax;
char <ArrayName>[size];
Ex;
char s1[6]={'H','e','l','l','o','\0'};
Ex;
char s2[ ]="welcome";
Note:
We need not place a NULL (\0) character at end of the string, because Compiler
automatically Appends NULL character.
Ex2;Write a program to display length of a string and also display reversed string
#include<stdio.h>
void main()
{
char str[100];
int i;
printf("Enter a String: ");
gets(str);
for(i=0;str[i]!='\0';i++);
printf("\nThe length of given String:%d characters",i);
printf("\nReversed string :");
for(i=i-1;i>=0;i--)
{
printf("%c",str[i]);
}
}
#include<stdio.h>
void main()
{
char s[100];
int i;
printf("Enter a String:");
gets(s);
printf("\nGiven string:");
for(i=0;s[i]!='\0';i++)
{
printf("%c",s[i]);
if(i==0){
s[i]='M';
}
}
s[i-1]='M';
printf("\n\n%s",s);
}
Ex1;
#include<stdio.h>
#include<string.h>
void main()
{
char str1[]="HeLLo";
char str2[]="WelCome";
char str3[10];
int len;
printf("\nString1: %s",str1);
len=strlen(str1);
printf("\nLength of string1:%d",len);
strcpy(str3,str1);
printf("\nString3: %s",str3);
strrev(str3);
printf("\nReverse of String3: %s",str3);
printf("\nString2 LowerCase: %s",strlwr(str2));
printf("\nString2 UpperCase: %s",strupr(str2));
printf("\nConcatenation of str3 & str2: %s",strcat(str3,str2));
}
Ex2;
#include<stdio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
int i;
printf("\nEnter string1: ");
gets(str1);
printf("\nEnter string2: ");
gets(str2);
i=strcmp(str1,str2); //i=strcmpi(str1,str2); it ignores case sensitivity
if(i==0)
{
printf("\nGiven strings are same");
}
else
{
printf("\nGiven strings are not same");
}
}
Ex4;
/*To check the given string is palindrome or not
*/
#include<string.h>
#include<stdio.h>
void main()
{
char str[100], temp[100];
printf("Enter a string: ");
gets(str);
strcpy(temp,str);
strrev(temp);
printf("\nReversed string: %s\n",temp);
if(strcmpi(str,temp)==0)
{
printf("\nGiven string is a Palindrome");
}
else
{
printf("\nGiven string is not a palindrome");
}
}
C - FUNCTIONS:
==========
A C-function is a self contained block of statements, used to perform a specific
task.
A function my re-use many times by calling it.Hence we can achieve modularity and
code re-usability.
<process.h> exit()
User-defined Functions:
------------------------------
A programmer(user) may develop own functions to accomplish required
operation(service).
There are three steps to create user defined function.
Following is the syntax to create user-defined function;
1).Function Declaration;
syntax;
<return-datatype> <function-name>(parameters list, ..);
ex;
int sum(int,int);
void msg(void);
syntax;
<return-datatype> <function-name>(parameters list, ..)
{
.................
................ //function block
...............
return (value);
}
parameters list :We may declare one or more variables of any datatype, seperated
by comma.
function block :It represents pair of curly braces, that block contains one or
more executable statements.
ex1;
int sum(int a,int b)
{
int c;
c=a+b;
return c;
}
ex2;
void msg( )
{
printf("welcome");
}
3).Function calling
syntax;
<function-name>(Actual arguments);
ex;
int s=sum(10,30);
msg();
Example program:
--------------------
/*The following program describes how a user defined function to be created with
function declaration, function definition and function calling. */
#include<stdio.h>
int sum(int a, int b) //function definition & a,b are formal arguments
{
int c;
c=a+b;
return c;
}
ex;
#include<stdio.h>
void sum(int, int);
void main()
{
int x=10, y=50;
sum(x,y);
}
void main()
{
int z;
z=sum( ); //function calling
printf("\nSUM=%d",z);
}
void main()
{
sum( ); //function calling
sum( );
sum( );
}
#include<stdio.h>
void fact(void);
void fact()
{
int n,f=1;
printf("\nEnter a number: ");
scanf("%d",&n);
while(n>0){
f=f*n--;
}
printf("\nFactorials:%d\n",f);
}
void main()
{
int i;
for(i=1;i<=5;i++)
{
fact();
}
}
practice programs;
1.Write a C-function to display sum of digits of given number
2.Write a C-function to check given number prime or not
3.Write a C-function to display fibonacci series
4.Write a C-function to check given string is palindrome or not
5.Write a C-funtion to sort given array of elements
6.Write C-funtions to implement a seperate user-defined function for each
arithmetic operation
Recurssive Function:
--------------------------
The process of self calling of a function is called as Recurssion.
A function, which has atleast one statement is a self calling statement, then it is
called as Recurssive Function.
syntax;
void main()
{
......
.....
fun( );
.......
.......
}
fun( ) //Recurssive function
{
.........
fun( ); //sefl calling statement
........
}
#include<stdio.h>
void disp(int);
void disp(int n)
{
if(n>1)
{
disp(n-1); //self calling
}
printf("%d%c",n,32);
}
void main()
{
int k;
printf("Enter a number: ");
scanf("%d",&k);
disp(k);
}
----------------------------------------------------------------------
Explanation;
n=5
void disp(5)
{
if(5>1)
{
disp(5-1);
}
printf("%d%c",n,32);
}
void disp(4)
{
if(4>1)
{
disp(4-1);
}
printf("%d%c",n,32);
}
void disp(3)
{
if(3>1)
{
disp(3-1);
}
printf("%d%c",n,32);
}
void disp(2)
{
if(2>1)
{
disp(2-1);
}
printf("%d%c",n,32);
}
void disp(1)
{
if(1>1)
{
//disp();
}
printf("%d%c",n,32);
}
O/p: 1 2 3 4 5
----------------------------------------------------------------
void main()
{
int k;
printf("Enter a number: ");
scanf("%d",&k);
printf("\nFactorials:%d",fact(k));
}
-------------------------------------------------
explanation;
n=5;
int fact(5)
{
if(5==0)
return 1;
else
return 5*fact(4);
}
int fact(4)
{
if(4==0)
return 1;
else
return 4*fact(3);
}
int fact(3)
{
if(3==0)
return 1;
else
return 3*fact(2);
}
int fact(2)
{
if(2==0)
return 1;
else
return 2*fact(1);
}
int fact(1)
{
if(1==0)
return 1;
else
return 1*fact(0);
}
int fact(0)
{
if(0==0)
return 1;
}
Pointers in C Language:
==================
A pointer is a special type of variable.
A pointer is used to store memory address of another variable of same datatype.
A variable memory address is identified by reference operator(&) with variable
name.
ex;
int a=10;
'&a' is representation of memory address of varaible 'a'.
Pointer Declaration:
-------------------------
It is similar to normal variable declaration,but a pointer is declared by prefixing
the pointer-name with star(*) symbol .
syntax;
<datatype> *<pointername>; //<pointername> is programmer choice of name
that follows all identifier rules.
ex;
char *ch; //character pointer, it stores address of another character
variable.
Pointer Initialization:
------------------------
It is good practice,to initialize a pointer with 'NULL' value if No proper value to
initialize a pointer.
ex;
char *ch=NULL;
int a=123;
int *p=&a;
ex1;
//pointers example
#include<stdio.h>
void main()
{
int a=10;
int *p=&a;
printf("%d bytes\n",sizeof(p));
printf("\nAddress of a is %u",&a);
printf("\nAddress of p is %u",&p);
printf("\ncontent of p is %u",p);
printf("\na=%d",*p);
*p=255;
printf("\na=%d",a);
}
Its Initialization;
int n=10;
int *p=&n;
int **dp=&p;
**dp => 10
EX1;
#include<stdio.h>
void main()
{
int n=10;
int *p=NULL;
int **dp=NULL;
printf("\nsize of single-pointer\t:%d bytes",sizeof(p));
printf("\nsize of double-pointer\t:%d bytes",sizeof(dp));
p=&n;
dp=&p;
printf("\n%d",**dp);
**dp=(**dp)*(**dp);
printf("\n%d",n);
}
c=**dpa+**dpb;
printf("\nsum=%d",c);
c=**dpa-**dpb;
printf("\nsub=%d",c);
c=**dpa*(**dpb);
printf("\nmul=%d",c);
c=**dpa/(**dpb);
printf("\ndiv=%d",c);
}
STRUCTURES:
==========
It is a collection of data elements of different data types or same data types.
[OR]
It is a collection of hetrogeneous data elements.
ex;
struct Student
{
int roll; => 4 bytes
char name[20]; => 20 bytes
float per; => 4 bytes
}; ----------------
28 bytes
Note:
Each structure variable has memory is sum of memory is allocated of each member of
structure.
So,
s1 contains 28 bytes => s1.roll, s1.name, s1.per
s2 contains 28 bytes => s2.roll, s2.name, s2.per
s3 contains 28 bytes => s3.roll, s3.name, s3.per
Ex0;
#include<stdio.h>
#include<string.h>
struct Student
{
int roll;
char name[20];
float per;
}s1={21,"mahesh",123.23},s2,s3;
void main()
{
struct Student s7={12,"ramesh",567.5},s8,s9;
s2.roll=27;
strcpy(s2.name,"Donald");
s2.per=345.890;
printf("\n%d\t%s\t%f",s1.roll,s1.name,s1.per);
printf("\n\n%d\t%s\t%f",s2.roll,s2.name,s2.per);
}
Ex1;
#include<stdio.h>
#include<string.h>
struct emp //structure definition
{
int eno;
char ename[20];
float esal; //eno,ename,esal are members of structure 'emp'
}e1={101,"cyber",123.123},e2;
void main()
{
struct emp e3,e4={104,"hello",1212.123};
}
}
void main()
{
struct student s={101,"cyber",45.0,78.0,67.0};
float total;
total=disp(s);
printf("\nTotal marks:%.1f",total);
}
}s={101,"cyber",123.123};
void main()
{
struct emp *p=NULL; //pointer of structure emp
p=&s; //Assigns address of structure to pointer
printf("\nENO\t:%d",p->eno);
printf("\nEName\t:%s",p->ename);
printf("\nESalary\t:%.2f",p->esal);
}
Ex1;
struct Student
{
int roll;
char name[20];
struct Add
{
char street[20];
char city[20];
char state[20];
}a;
}s;
Accessing;
s.roll
s.name
s.a.street
s.a.city
s.a.state
Ex2;
struct Add
{
char street[20];
char city[20];
char state[20];
};
struct Student
{
int roll;
char name[20];
struct Add a;
}s;
Accessing;
s.roll
s.name
s.a.street
s.a.city
s.a.state
Ex;
#include<stdio.h>
#include<string.h>
struct Student //outer structure
{
int roll;
char name[20];
}s={21,"Revanth"};
void main()
{
strcpy(s.a.street,"Prakash Nagar");
strcpy(s.a.city,"Rajahmundry");
strcpy(s.a.state,"Andhra Pradesh");
printf("Roll:%d",s.roll);
printf("\nName:%s",s.name);
printf("\nAddress:\n");
printf("%s\n%s\n%s",s.a.street,s.a.city,s.a.state);
}
UNION:
======
It is similar to Structure, but memory allocation is different.
A 'union' keyword is used to create Union derived datatype.
syntax;
union <union-name>
{
data decaration-1; //union members
data declaration-2;
.
.
data declaration-N;
};
ex;
union emp
{
int eno; => 4 bytes
char ename[20]; =>20 bytes
float esal; => 4 bytes
};
Note2:
Which member of 'union' has highest memory allocation, that memory is allocated to
'union' or union variable.
So a common memory shared by all union members one by one.
There is No dedicated memory for each member of union.
e1.eno, e1.ename, e1.esal, all these are shares 20 bytes one by one.
e1.eno=34;
strcpy(e1.ename,"cyber");
e1.esal=123.12;
Structure Vs Union:
==============
STRUCTURE UNION
---------------- ------------
1.It is used to store records. 1.It is used to store records.
FILES
-------
Definition:-
A FILE is one which enable the user to read , write and store a group of a related
data .
(or)
1) Naming/creating a file
2) Opening a file
3) Reading data from a file
4) Writing data into file
5) Closing a file
FILE *identifier;
syntax:-
FILE *fopen(const char *filename,const char *mode);
file name:- File that the function to be open in main memory at given mode, and
this function returns memory address where file opened in the memory, interms terms
of file object.
MODE:-
String Description
-------- ---------------
"r" : File opens in reading only, if file not exist, it rturns an error.
"w" : File opens in writing only, If a file by that name already exists, it
will be overwritten,
if file not exist a new file is created & write data.
"a" : Append; File opens for writing at end of file, or create for writing
if the file does not exist.
"w+" : Create a new file for update (reading and writing).If a file by that
name already exists,it will be overwritten.
"a+" : Open for append; open for update at the end of the file, or create if
the file does not exist.
Ex;
FILE *fp;
fp=fopen("one.txt","w");
[4].feof():-
It is a Macro that tests if end of file has been reached on a stream.
syn:- int feof(FILE *stream);
ex;
feof(fp);
#include<stdio.h>
void main()
{
FILE *fp=NULL;
char ch;
fp=fopen("one.txt","w");
printf("\nEnter data ...input terminate by * \n");
do{
ch=getchar();
fputc(ch,fp);
}while(ch!='*');
fclose(fp);
}
do{
ch=getche();
fputc(ch,fp);
}while(ch!='*');
fclose(fp);
}
if(fp==NULL)
{
printf("\nFile Not Found! Not exist");
exit(0);
}
do
{
ch=fgetc(fp);
printf("%c",ch);
fclose(fp);
}
3.Static memory is allocated for Arrays. 3.Dynamic memory is allocated for Linked
List.
1.Malloc():-
---------
It is a function which is used to allocating memory at run time.
Syntax:- void *malloc(size_t size);
size_t:- unsigned integer. this is used for memory object sizes.
Example:-
int *p;
p=(int *)malloc(sizeof(int)); //for 1 location
p=(int *)malloc(n*sizeof(int)); //for n locations
2.Calloc():-
------------
This is also used for allocating memory at run time.
Syntax: void *calloc(size_t nitems, size_t size);
3.Realloc():-
--------------------
It reallocates Main memory.
Syntax:- void *realloc(void *block, size_t size);
4.free():-
------------
It deallocates the allocated memory.
Syntax:- void free(void *block);
Example program:
-----------------------
#include<stdio.h>
#include<stdlib.h>
void main()
{
int *p,n,i,s=0;
printf("Enter no.of elements to be store:");
scanf("%d",&n);
p=(int *)malloc(n*sizeof(int));
printf("\nEnter %d integer elements:\n",n);
for(i=0;i<n;i++)
{
scanf("%d",(p+i));
}