0% found this document useful (0 votes)
10 views109 pages

C Programming Concepts and Techniques

The document is a comprehensive guide to the C programming language, covering topics such as header files, variables, data types, control statements, functions, arrays, pointers, strings, structures, and file handling. It includes explanations, examples, and code snippets to illustrate key concepts. The content is organized into chapters and sections for easy navigation and understanding.

Uploaded by

chithra.jothi84
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)
10 views109 pages

C Programming Concepts and Techniques

The document is a comprehensive guide to the C programming language, covering topics such as header files, variables, data types, control statements, functions, arrays, pointers, strings, structures, and file handling. It includes explanations, examples, and code snippets to illustrate key concepts. The content is organized into chapters and sections for easy navigation and understanding.

Uploaded by

chithra.jothi84
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

Table of Contents

Chapter : 1 ......................................................................................................... 1
1.1 Introduction: ................................................................................................ 1
1.2 Header Files: ................................................................................................ 2
1.3 Variables in C: ............................................................................................. 3
[Link] Variable........................................................................................ 5
[Link] Variable ...................................................................................... 6
[Link] Variable........................................................................................ 7
[Link] Variable ................................................................................ 9
[Link] Variable ................................................................................. 10
1.4. DataTypes : ............................................................................................... 11
[Link] Data Types: .......................................................................... 11
[Link].Integer Data types: ......................................................................... 13
[Link].Character Data Type: ..................................................................... 17
[Link].Float Data Types: ........................................................................... 19
1.5 KEYWORDS:............................................................................................ 21
1.6 IDENTIFIER : ........................................................................................... 21
1.7 Comments in C .......................................................................................... 22
1.7.1 Single Line Comments: ........................................................................ 22
1.7.2. Multi line Comments: ......................................................................... 23
1.8 Format Specifier: ....................................................................................... 24
1.8.1 Character Format Specifier: (%c) ..................................................... 24
1.8.2 Signed Integer Format Specifier : (%d, %i) ...................................... 25
1.8.3 Unsigned Integer Format Specifier:(%u) .......................................... 25
1.8.4 Floating point Format Specifier:(%f, %e) ......................................... 26
1.8.5 Unsigned Octal Number For Integer Format Spectifier: (%o) ........... 27
1.8.6 Unsigned hexadecimal for Integer Format Specifier :(%x) ............... 27
1.8.7 String Printing:(%s) .......................................................................... 28
1.8.8 Double Floating point Number(%lf) ................................................. 28
1.9 Escape Squences: ....................................................................................... 29
1.10 Operators: ................................................................................................ 29
Types of Operators: ...................................................................................... 29
1.10.1 Arithmetic Operator: ....................................................................... 30
1.10.2 Assignment Operator :(=)................................................................ 31
1.10.3 Relational Operator: ........................................................................ 32
1.10.4 Logical Operator: ............................................................................ 33
1.10.5 Bitwise Operator: ............................................................................ 34
1.10.6 Conditional operator: ...................................................................... 38
1.10.7 Modify Operator: ............................................................................ 39
1.10.8 Special Operator: ............................................................................ 41
1.11 Constants: ................................................................................................ 42
1.12 C Tokens? ................................................................................................ 44
CHAPTER :2 ................................................................................................... 46
2.1Control Statements: ..................................................................................... 46
2.1.1 Sequential Statement: ........................................................................... 46
2.1.2 Selection statement: ............................................................................. 46
[Link] If Statement:................................................................................... 46
[Link] IF-Else Statement: .......................................................................... 48
[Link] Else- if Ladder statement ............................................................... 49
[Link] Nested If Statement: ....................................................................... 50
[Link] Switch Case Statement: .................................................................. 52
2.1.3 Iteration / Looping: ............................................................................. 55
[Link] While Loop : .................................................................................. 55
[Link] Do-while Loop: .............................................................................. 56
[Link] For Loop: ....................................................................................... 58
2.2 Nested Loop: .............................................................................................. 59
2.3 Break Statement: ........................................................................................ 60
2.4 Continue Statement: ................................................................................... 61
2.5 Goto Statement: ......................................................................................... 62
Chapter : 3 ....................................................................................................... 63
3.1 Functions: .................................................................................................. 63
3.1.1 No Arguments with No Return Values ................................................. 65
[Link] Arguments with No Return values ............................................... 66
[Link] Arguments with Return values: ................................................... 67
[Link] Arguments with Return Values: ..................................................... 68
3.2 Call by Value : ........................................................................................... 69
3.3 Call by Reference:...................................................................................... 70
3.4 Recursion: .................................................................................................. 71
CHAPTER : 4 .................................................................................................. 72
4.1 Arrays: ....................................................................................................... 72
[Link] Dimensional Array: ...................................................................... 74
[Link] Dimensional array: ....................................................................... 77
CHAPTER : 5 .................................................................................................. 80
[Link]: ...................................................................................................... 80
[Link] and Pointer: ................................................................................ 82
[Link] of Pointers: ................................................................................. 83
[Link] to Function: .............................................................................. 85
CHAPTER : 6 .................................................................................................. 85
[Link]:....................................................................................................... 85
[Link] the String................................................................................ 87
[Link].h ...................................................................................................... 88
[Link] String functions ............................................................................. 88
CHAPTER : 7 .................................................................................................. 93
[Link]: ................................................................................................... 93
[Link] Structure: ..................................................................................... 94
[Link] of Structures:.................................................................................... 95
[Link]: ........................................................................................................ 97
7.5.Type_def: ................................................................................................... 98
[Link]:............................................................................................ 101
CHAPTER : 8 ................................................................................................ 101
[Link] Handling : ......................................................................................... 101
C Language
Chapter : 1

1.1Introduction:
The C Programming Language was designed by Dennis Ritchie at Bell
Laborators in the early 1970s
Standardized in 1989 by ANSI (American National Standards Institute)
known as ANSI C
A C Development environment includes:
1) System Libraries and headers:
A set of standard libraries and their header files
2) Application Source:
Application Source and header files
3) Compiler:
Converts source to object code for a specific platform.
4) Linker:
Resolves external references and produces the executables module.
Program 1:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Hello");
getch();
}

1
Output:
Hello

1.2Header Files:
 Usually, all C programs begin with include statements starting with a
#(hash / pound)
 The symbol # is a directive for the preprocessor
 That means, these statements are processed before the compilation
process begins
 Header file contains the definition of predefined functions.
Example:
1. #include<stdio.h> - standard Input and Output - scanf, printf
2. #include<conio.h> - console input and output - clrscr(), getch()
3. #include<math.h> - mathematical operations - cos(), tan(), pow()
4. #include<string.h>- character - strlen(), strcmp(), strcpy()
Program 2:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("enter the Number\n");
scanf("%d",&a);
printf(" value of A : %d",a);
getch();
}
Output:
Enter the number:
5
Value of A: 5

2
Program 3:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x;
clrscr();
x=pow(2,8);
printf("%d",x);
getch();
}

Output:
256

1.3Variables in C:
 A variable is a name of the memory location. It is used to store data.
Its value can be changed, and it can be reused many times.
 It is a way to represent memory location through symbol so that it
can be easily identified.

Let's see the syntax to declare a variable:

1. type variable_list;

The example of declaring the variable is given below:


1. int a;
2. float b;
3. char c;

Here, a, b, c are variables. The int, float, char are the data types.

3
We can also provide values while declaring the variables as given below

1. int a=10, b=20;//declaring 2 variable of integer type


2. float f=20.8;
3. char c='A';

Rules for defining variables:


 A variable can have alphabets, digits, and underscore.
 A variable name can start with the alphabet, and underscore only. It can't
start with a digit.
 No whitespace is allowed within the variable name.
 A variable name must not be any reserved word or keyword, e.g. int, float,
etc.

Valid variable names:

1. int a;
2. int _ab;
3. int a30;

Invalid variable names:

1. int 2;
2. int a b;
3. int long;

Types of Variables in C :

There are many types of variables in c:

1. local variable
2. global variable
3. static variable
4. automatic variable
5. external variable

4
[Link] Variable

A variable that is declared inside the function or block is called a local


[Link] must be declared at the start of the block.

1. void function1(){
2. int x=10;//local variable
3. }

Program 4:
#include<stdio.h>
#include<conio.h>
int fun();
void main()
{
int a,b;
clrscr();
a=3;
b=4;
printf("%d%d\n",a,b);
fun();
getch();
}
int fun()
{
int c;
c=5;
printf("%d",c);
return 0;
}

5
Output:
34
5

[Link] Variable

A variable that is declared outside the function or block is called a global


variable. Any function can change the value of the global variable. It is available
to all the [Link] must be declared at the start of the block.

1. int value=20;//global variable


2. void function1(){
3. int x=10;//local variable
4. }

Program 5:
#include<stdio.h>
#include<conio.h>
int a,b=10;
void fun1();
void fun2();
void main()
{
clrscr();
printf("global a=%d\n",a);
printf("global b=%d\n\n",b);
fun1();
fun2();
getch();
}
void fun1()

6
{
printf("Fun1 Global a=%d\n",a);
printf("Fun1 Global b=%d\n\n",b);
}
void fun2()
{
int a=5;
printf("fun2 a=%d\n",a);
}
Output:
Global a=0
Global b=10
Fun1 global a=0
Fun1 global b=10
Fun2 a=5

[Link] Variable

A variable that is declared with the static keyword is called static [Link]
retains its value between multiple function calls.

1. void function1(){
2. int x=10;//local variable
3. static int y=10;//static variable
4. x=x+1;
5. y=y+1;
6. printf("%d,%d",x,y);
7. }

If you call this function many times, the local variable will print the same
value for each function call, e.g, 11,11,11 and so on. But the static variable will
print the incremented value in each function call, e.g. 11, 12, 13 and so on.

7
Program 6:
#include <stdio.h>
#include<conio.h>
void fun1();
void main()
{
clrscr();
fun1();
fun1();
fun1();
getch();
}
void fun1()
{
int a=1;
static int b=100;
printf("a=%d\n",a);
printf("b=%d\n",b);
a++;
b++;
}
Output:
a=1
b=100
a=1
b=101
a=1
b=102

8
[Link] Variable

All variables in C that are declared inside the block, are automatic variables by
default. We can explicitly declare an automatic variable using auto keyword.

1. void main(){
2. int x=10;//local variable (also automatic)
3. auto int y=20;//automatic variable
4. }
Program 7:
#include<stdio.h>
#include<conio.h>
void main()
{
auto int i=1;
clrscr();
{
auto int i=2;
{
auto int i=3;
printf("%d\n",i);
}
printf("%d\n",i);
}
printf("%d\n",i);
getch();
}
Output:
3
2
1
9
[Link] Variable

We can share a variable in multiple C source files by using an external variable.


To declare an external variable, you need to use extern keyword.

1. extern int x=10;//external variable (also global)

Program 8:
#include<stdio.h>
#include<conio.h>
extern int i=1;
int fun();
void main()
{
int i=3;
clrscr();
printf("%d\n",i);
fun();
getch();
}
int fun()
{
printf("%d",i);
return 0;
}
Output:
3
1

10
[Link] :
What is Data Types?
 It is a representation of data. Int, float, char, .. etc.,
 It is mandatory to use datatype for variable.
Data type represent two things about a variable.
1. What type of data is allowed to store
2. How much memory is required to store a data
Classification:Data type classified into three types.
1. Primitive  char, int, float, void
2. Derived  Array, String. Pointer
3. User Defined  Structure, Union, Type_def, Enum.
[Link] Data Types:
1. Char It Represent a single character.
2. Int  It Represent an integer value.
3. Float  It Represent an decimal point value.
4. Void  It Represent Nothing.
Char 1 Byte
Int  2 Byte 1 Byte = 8 bits
Float  4 Byte
Program 9:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
float b=10.5;
char c='m';
clrscr();
printf("The Integer Value : %d \n",a);
printf("The Float Value : %f \n",b);
printf("The Character Value : %c \n",c);
11
getch();
}
Output:
The Integer Value : 10
The Float Value : 10.500000
The Character Value : m

Primitive Data Types: Signed


2 Bytes
Short
Unsigned

Signed
2- or 4-
Int bytes Bytes
Unsigned
Integer

Signed

Long 4 Bytes
Unsigned

Signed
char 1 Byte

Primitive Unsigned
Data
Types Float 4 Bytes

Double 8 Bytes
Float

Long 10 Bytes
Double

void

12
[Link].Integer Data types:
Signed
2 Bytes
Short
Unsigned

Signed
2- or 4-
Int bytes Bytes
Integer Unsigned

Signed

Long 4 Bytes
Unsigned

Signed Short:
 It allow signed and unsigned value. (Both +ve and -ve value)
 It occupy 2 Bytes in the memory location of a variable.
 Don’t need to define Explicitly.

Unsigned short:
 It allow only unsigned value(only +ve values)
 It occupy 2 Bytes in the memory location of a variable.
 You have to define Explicitly.

13
Data Measurements:
Data Measurement Size
Bit Single Binary Bit (0 and 1)
Byte 8 bits
Kilobyte (kb) 1024 Bytes
Megabyte (mb) 1024 kb
Gigabyte (gb) 1024 mb
Terabyte (tb) 1024 gb

0 Bit

0 1 1 1 1 0 0 0 Byte

8 bits =1 Byte 28 256 Range

0 0 0 0 0 0 0 0 0

16 bits =2 Byte
1 1 1 1 1 1 1 1 256
216
65536 Range

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 65536

Unsigned Int:(65536)
Value Range 0 to 65535
SingnedInt :
14
65536 / 2 = -32768 to +32767 (…+3,+2,+1,0,1,2,3….)

Declaration of Signed Short Integer:


short a;
short Int a;
signed short a;
signed short int a;
Declaration of UnSigned Short Integer:
unsigned short a;
unsigned short int a;

Program 10:
#include<stdio.h>
#include<conio.h>
void main()
{
short a=32767;
unsigned short b=65535;
clrscr();
printf("The Signed short is :%d\n",a);
printf("The unsigned short is :%u",b);
getch();
}
Output:
The signed short is : 32767
The unsigned short is : 65535
Short Integer:

15
Short Integer:
Signed short 2 Bytes -32768 to 32767
Unsigned short 2 Bytes 0 to 65535

Program 11:
#include<stdio.h>
#include<conio.h>
void main()
{
short a=32770;
unsigned short b=65540;
clrscr();
printf("The signed value is : %d \n",a);
printf("The unsigned value is : %u",b);
getch();
}
Output:
The signed value is : -32766
The unsigned value is : 4

16
Short Integer:
Signed short 2 Bytes -32768 to 32767
Unsigned short 2 Bytes 0 to 65535

Integer:
Signed int 2 Bytes -32768 to 32767
Unsigned int 2 Bytes 0 to 65535

Long Integer:
Signed long 4 Bytes -2,147,483,648 to
2,147,483,647
Unsigned long 4 Bytes 0 to 4,294,967,295

[Link].Character Data Type:


 It holds a single character(letter, Symbol, Numbers… etc).

Signed
char 1 Byte
Unsigned
Unsigned Char:
1Byte 256 0 to 255 Range of value

Signed Char :
1 Byte 256/2 -128 to +127

Character System:
Using Character System we can represent all the character in an
constant integer format.
ASCII – American Standard Code for Information Interchange.

17
Program 12:
#include<stdio.h>
#include<conio.h>
void main()
{

18
char t='s';
clrscr();
printf("The Character is : %c\n",t);
printf("The ASCII Value is : %d",t);
getch();
}
Output:
The character is : s
The ASCII Value is : 115

[Link]. Float Data Types:


It represent a real number.
Example: 56.07,2.05,70.6……
Float data type is sub classified in to 3 types
1. Float
2. Double
3. Long Double
Ex: 21.25
2 21 0.25 * 2 = 0.50 0 =01
2 10 1 0.50 * 2 = 1.00 1
2 5 0
2 2 1 Binary Value for 21.25
1 0 =10101 is =10101.01

Type Size Range Format Specifier

1.2E-38 TO
Float 4 bytes %f
3.4E+38

19
2.3E-308 TO
Double 8 Bytes %lf
1.7E+308

3.4E-4932 TO
Long double 10 Bytes %Lf
1.1E+4932

Program 13:
#include<stdio.h>
#include<conio.h>
void main()
{
float a=57.89;
double b=1234.56;
long double c;
clrscr();
c=a+b;
printf("The First value is : %f\n",a);
printf("The second value is : %lf\n",b);
printf("The sum of two values are : %Lf",c);
getch();
}
Output:
The First value is : 57.889999
The second value is : 1234.560000
The sum of two values are : 1292.449999

20
1.5 KEYWORDS:
 Keywords serves as basic building blocks for program statements. All
keywords have fixed meanings and cannot be changed.
 Keywords cannot be used as normal identifier.
 Keywords are written in lowercase letters.
 For examples of keywords used in c.

1.6 IDENTIFIER :
 C identifier refers to name used to identify variable, function, structures
or any other user defined item or entity.
 Identifier must be unique.
 Identifier names must be different from keywords.
 For example : You cannot use double as an identifier because double is a
keyword
Rules For an Identifier:
 An identifier can only have alphanumeric charaters(a-z,A-Z,0-
9)&Underscore( _ )
 The First character of an identifier can only contain alphabet (a-z, A-Z) or
Underscore ( _ ).
For Example : int roll_num
Identifier are also case sensitive in C.
For Example :
Name and Name are two different identifiers in C

21
Program 14:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c; // int- keyword
a=10; // a,b,c- Identifier
b=25;
c=a+b;
printf("Addition = %d",c);
getch();
}

Output:
Addition = 35

1.7 Comments in C
Comments in C language are used to provide information about lines
of code. It is widely
Used for documenting code. There are 2 types of comments in the C language.
1. Single Line Comments
2. Multi-Line Comments

1.7.1 Single Line Comments:


Single Line comments are represented by double slash \\. Let’s see an
example of a single line comment in C.

22
Program 15:
#include<stdio.h>
#include<conio.h>
void main()
{
//printing information
clrscr();
printf("HELLO");
getch();
}
Output:
HELLO

[Link] line Comments:


Multi line are represented by slash asterisk /*…..*/. It can occupy
many lines of code, but it can’t be nested.
Syntax:
/*
Code
To be commented
*/

Program 16:
#include<stdio.h>
#include<conio.h>
void main()
{
/* printing
information */
clrscr();
printf("HELLO");
getch();
}
Output:
Hello
23
1.8 Format Specifier:
What is Format specifier?
 The Format Specifier is used during Input and Output.
 It is a way to tell the compiler what type of data is in a variable
during taking input using scanf() or Printing using Printf()
 Some Examples are %c, %d, %f, Etc.

Basic Types of Format Specifiers:


 Character Format Specifier
 For Signed Integer Format Specifier
 Unsigned Integer Format Specifier
 Floating -point Format Specifier
Some Additional Format Specifiers:
 Unsigned octal number for integer
 Unsigned Hexadecimal for integer
 String printing
 Double floating-point number

1.8.1 Character Format Specifier: (%c)


Program 17:
#include<stdio.h>
#include<conio.h>
void main()

24
{
char ch='B';
clrscr();
printf("%c\n",ch);
getch();
}
Output:
B

1.8.2 Signed Integer Format Specifier : (%d, %i)


Program 18:
#include<stdio.h>
#include<conio.h>
void main()
{
int x=45;
clrscr();
printf("%d\n",x);
printf("%i\n",x);
getch();
}
Output:
45
45

1.8.3 Unsigned Integer Format Specifier:(%u)


Program 19:
#include<stdio.h>
#include<conio.h>
void main()
{

25
unsigned int a=-10, b=10;
clrscr();
printf("%u\n",a);
printf("%u\n",b);
getch();
}

Output:
65526
10

1.8.4 Floating point Format Specifier:(%f, %e)


Program 20:
#include<stdio.h>
#include<conio.h>
void main()
{
float a=12.67;
clrscr();
printf("%f\n",a);
printf("%e\n",a);
getch();
}
Output:
12.670000
1.267000e+01

26
1.8.5 Unsigned Octal Number For Integer Format Spectifier: (%o)
Program 21:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=67;
clrscr();
printf("%o\n",a);
getch();
}

Output:
103

1.8.6 Unsigned hexadecimal for Integer Format Specifier :(%x)

Program 22:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=15;
clrscr();
printf("%x\n",a);
getch();
}

27
Output:
f

1.8.7 String Printing:(%s)


Program 23:
#include<stdio.h>
#include<conio.h>
void main()
{
char a[]="welcome";
clrscr();
printf("%s\n",a);
getch();
}

Output:
Welcome

1.8.8 Double Floating point Number(%lf)


Program 24:
#include<stdio.h>
#include<conio.h>
void main()
{
double a=45.65;
clrscr();
printf("%lf\n",a);
getch();
}

Output:
45.650000

28
1.9 Escape Squences:

1.10 Operators:
 We define operator as a symbols that helps us to perform specific
mathematical and logical computations on operand.
For example,
C=a+b;
 Here ‘+’ is the operator known as Additional operator.
 ‘a’ and ‘b’ are the operands.
 The addition operator tells the compiler to add both of the
Operand.

Types of Operators:
 Arithmetic Operator. (Binary operator)
 Assignment Operator
 Relational Operator
 Logical Operator
 Bit wise Operator
 Conditional operator. (Ternary Operator)
 Increment / decrement Operator. (Unary Operator)
 Special Operator.

29
1.10.1 Arithmetic Operator:
Addition +
Subtraction -
Multiplication *
Division / (Quotient)
Modulus % (Reminder)

Program 25:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
printf("Addition : %d \n",a+b);
printf("Subtraction : %d \n",a-b);
printf("Multiplication : %d \n",a*b);
printf("Division : %d \n",a/b);
printf("Modulus : %d",a%b);
getch();
}
Output:
Enter Two Numbers:
5
2
Addition : 7

30
Subtraction : 3
Multiplication : 10
Division : 2
Modulus : 1

1.10.2 Assignment Operator :(=)


 Assignment Operator are used to assign value to a variable.
 The left side operand of the assignment operator is a variable and
right side operand of the assignment operator is a value.
For Example :
Int a =10;
Int b=20;
Char c=’s’;

a=a+b; a+=b;
a=a-b; a-=b;
a=a*b; a*=b;
a=a/b; a/=b;
a=a%b; a%=b;
Program 26:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
a=10;
b=20;
printf("A=%d\n",a);
printf("B=%d\n",b);
a=a+b; //a+=b;
printf("A=%d\n",a);
getch();
}
Output:
A=10

31
B=20
A=30

1.10.3 Relational Operator:


 Relational Operators are used for comparsion of two values of two
operands.
 For example checking if one operand is equal to another operator
or not, an operand is greater than the other operand or [Link]….
 The relational operator are:
(==,<,>,<=,>=,!=)
Program 27:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the two values");
scanf("%d%d",&a,&b);
if(a>b) //< , >=, <=, !=, ==
printf("A is Greater");
else
printf("B is Greater");
getch();
}

Output:
Enter Two values:
5
4

32
A is Greater

1.10.4 Logical Operator:


 Logical Operator are used to combine two or more
conditions.
 Logical Operators are(&&, ||, !)
Logical AND (&&):
It return true when both the conditions in consideration are
satisfied otherwise it return false.

Logical OR(||):
It return true when one or both of the condition in
consideration is satisfied. Otherwise it return false.
Logical Not(!):
It return true the condition in consideration is not satisfied.
Otherwise it return false. For example !a return true if a=0
Program 28:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=40,b=20,c=30,d=40;
clrscr();
//Logical AND
if(a>b&&c<d) //||
printf("A is greater than B AND D is greater than c");
else
printf("Logicaal AND function does not satisfied");
getch();
}

33
Output:
A is greater than B AND D is greater than c

Logical NOT:
Program 29:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=4; //a=0;
clrscr();
//Logical NOt
if(!a)
printf("A is Zero");
else
printf("A is non zero");
getch();
}
Output:
A is non zero

1.10.5 Bitwise Operator:


 It is used to perform bit-level operation on the operands.
 The value is first converted in to bit-level and then
calculation is performed on the operands.
 The Bitwise operator are:(&, |, ~, <<, >>, ^)
Bitwise AND(&):
 It takes two numbers as operand and does AND on every bit
of two numbers.
 The result of AND is 1 only if both bits are 1.
34
 For Example: A=5, B=4(Binary Value for 5=101 and for
4=100)
A B A&B
1 1 1
0 0 0
1 0 0
Bitwise OR (|):
 It takes two numbers as operand and does OR on every bit of
two numbers.
 The result of OR is 1 only if any of the two bits is 1.
 For Example: A=5, B=4(binary value for 5=101 and for
4=100)

A B A|B
1 1 1
0 0 0
1 0 1

Bitwise XOR(^):
 It Takes two numbers as operand and does XOR on every
bit of two numbers.
 The result of XOR is 1 only if both bits are different.
 For Example: A=5, B=4(binary value for 5=101 and for
4=100)

A B A^B
1 1 0
0 0 0

1 0 1

35
Left Shift(<<):
 It takes two numbers, left shifts the bits of the first operand,
the second operand decides the number of places to shift.
 For Example: A=5, B=2(binary value for 5=101 Result = 20)

A 0 0 0 0 0 1 0 1

A<<B 0 0 0 1 0 1 0 0

Right Shift(>>):
 It takes two numbers, Right shifts the bits of the first
operand, the second operand decides the number of places to
shift.
 For Example: A=5, B=2(binary value for 5=101 Result = 1)

A 0 0 0 0 0 1 0 1

A<<B 0 0 0 0 0 0 0 1

Bitwise NOT (~): (Unary)


 It takes one number and inverts all bits of it.
 For Example A=5(Binary for 5 is 101).

A ~A
0 1
0 1
0 1
0 1
0 1
1 0
0 1
1 0

36
Program 30:
Bitwise AND,OR,XOR
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=4;
clrscr();
printf("A & B is : %d\n",a&b);
printf("A | B is : %d\n",a|b);
printf("A ^ B is : %d\n",a^b);
getch();
}
Output:
A & B is : 4
A | B is : 5
A ^ B is : 1

Bitwise Left Shift & Right Shift


Program 31:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=2;
clrscr();
printf("A << B is : %d\n",a<<b);
printf("A >> B is : %d\n",a>>b);
37
getch();
}
Output:
A << B is : 20
A >> B is : 1

Bitwise Not
Program 32:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5;
clrscr();
printf("A is : %d\n",~a);
getch();
}
Output:
A is : -6

1.10.6 Conditional operator:


Syntax:
(Expression1? Expression2: Expression3)
 Here, Expression1 is the condition to be evaluated.
 If the condition is true Expression2 will Execute.
 Otherwise Expression3 will Execute.
 We may replace the use of if…. else statement by conditional operator.
Program 33:
#include<stdio.h>
38
#include<conio.h>
void main()
{
int a=5,b=6;
clrscr();
printf("%d",(a<b?a:b));
getch();
}

Output:
5

1.10.7 Modify Operator:

Modify Operator(unary)

Increment(++) Decrement(--)

Pre Increment Post Increment Pre Decrement Post Decrement


Eg: ++(variable) eg: (variable)++ eg: --(variable) eg: (variable)--
++a a++ --a a--
Follow Steps:
1. Pre increment / decrement
2. Substitution
3. Evaluation
4. Assignment
5. Post increment / decrement

39
Program 34:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10, b=2;
clrscr();
printf("a++ = %d\n",a++);
printf("a = %d\n",a);
printf("++a = %d\n",++a/b);
printf("a = %d\n",a);
printf("a-- = %d\n",a--);
printf("a = %d\n",a);
printf("--a = %d\n",--a);
getch();
}
Output:
a++ = 10
a = 11
++a = 6
a = 12
a-- = 12
a = 11
--a = 10

40
1.10.8 Special Operator:

The Special Operators are, & * sizeof()


, Comma used as a separator.
& used to get the address of the variable
* used as pointer to a variable
Sizeof() gives the size of the variable

Sizeof():
 It return the size of:
1. Variable
2. Data type
3. Expression
4. pointer
Program 35:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,*b;
float c,*d;
clrscr();
printf("Address of A is : %u",&a);
b=&a;
printf("\n%u",b);
printf("\n sizeof c : %d",sizeof(c));
printf("\n sizeof :%d",sizeof(char));
printf("\n sizeof : %d",sizeof(++a));
printf("\n sizeof : %d",sizeof(*d));

41
getch();
}
Output:
Address of A is : 65524
65524
Sizeofc : 4
Sizeof : 1
Sizeof : 2
Sizeof : 4

1.11 Constants:

 Constants are affixed values that do not change during the execution of a
program.
 C supports several types of constants.
o Numeric Constants
o Character Constants

Constants

Numeric Character
Constants Constants

Integer Real Single String


Constants Constants Character Constants
Constants

42
Integer Constant :
 Integer constant refers to sequence of digits.
 There are three types of integers, namely

Integer Constant

Decimal Octal Hexa Decimal


Integer Integer Integer Constants
Constants Constants

Decimal Integer:
Consist of a set of digits, 0 through 9 proceded by on optional – or+ sign
For example:
234 -456 0 3465677 +23
Embedded spaces, commas and non digit characters are not permitted
between two digits.
For Example :
15, 345 $45678
Octal Integer:
Any combination of digitd from the set 0 through 7 with a leading 0.
For Example:
089 0 07654 023
Hexadecimal Integer:
Asequence of digits proceded by 0x or 0X considered by hexadecimal
integer
Alphabets A through F or a through f.
The letters A through F represent the numbers 10 through 15.
For example:
0x2 0x4F 0Xbcd 0x

43
Real Constants:
 Integer number are inadequate to represent quantities that vary
continuously, such as distances, heights, temperatures, prices and
so on.
 For Example:-
17.548 such numbers are called real or floating point
constants.

 A real number may also be expressed in exponential (or


Scientific)notation.
 For Example:-
The value 215.65 may be written as 2.1565e2 in
exponential notation.
Single Character Constants:
 A single character constant (or simply character constant) contains
a single character enclosed within a pair of single quotes marks.
 For Example:-
‘5’ ‘X’ ‘;’
 Character constants have integer values known as ASCII values.
 For example:-Printf(“%d”,’a’);
Would print the number 97
String Constants:
 A string constant is a sequence of character enclosed in double
quotes.
 The character may be letters, numbers, special characters & blank
space
 For Example:-
“hello” “1987” “WELLDONE”

1.12 C Tokens?
C Tokens are the basic building blocks in C language which are
constructed together to write a C program.
Every Smallest individual units in a C program are known as C tokens.

44
Types of C Tokens:
 C has six types of tokens.
1. Keywords : Ex - float, int, double, for, while
2. Identifiers : Ex - Main, amount
3. Constants : Ex - 12.3, 3456
4. Strings : Ex - “COMPUTER”, “SCIENCE”
5. Special Symbols : Ex - [, ], {, }, (, )
6. Operators : Ex - +, -, *, /

Program 36:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c; // int- keyword
a=10; // a,b,c- Identifier
b=25; //{,},(,) – Special Symbols
c=a+b;
printf("Addition = %d",c);
getch();
}

Output:
Addition = 35

45
CHAPTER :2
2.1Control Statements:
 In a program, a control structure determines he order in which
statement are executed.
 The following are the basic control structures in the programming
language:
1. Sequential
2. Selection
3. Looping / Iteration

2.1.1 Sequential Statement:


 In Sequential execution each statement in the source code
will be executed one by one in a sequential order.
 This is the default mode of execution.
For example:
Void Main()
{
int a=10,b=20,c;
C=a+b;
Printf(“The Result is : %d”,c);
getch();
}
Output : 30

2.1.2 Selection statement:


 The selection control statement is used for making decision
and branching statement.
 The following are the basic selection statement.
1. If
2. If-else
3. Else- if Ladder
4. Nested If
5. Switch case
[Link] If Statement:
 The if statement checks for the condition and if the condition
is satisfied it executes the statement.
 Otherwise it exits the IF block.

46
Syntax:
If(condition)
{
Statements;
}

Program 37:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the Two Numbers");
scanf("%d%d",&a,&b);
// if statement
if(a>b)
{
printf("A is Greater");
}
getch();
}

Output:
Enter the Two Numbers:
20
10
A is Greater

47
[Link] IF-Else Statement:
 The if-else statement checks for the condition and if the
condition is satisfied it executes the statement of the IF block
 Otherwise it executes the Else block.
Syntax:
If(condition)
{
Statements;
}
else
{
Statements;
}

Program 38:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the Two Numbers");
scanf("%d%d",&a,&b);
// if – Else statement
if(a>b)
{
printf("A is Greater");
}
else
{
printf("B is Greater");
}
getch();
}
Output:
20
10
A is Greater

48
10
20
B is Greater

[Link] Else- if Ladder statement

Syntax:
If(condition)
{
Statements;
}
else if
{
Statements;
}
else
{
Statements;
}

Program 39:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the Two Numbers");
scanf("%d%d",&a,&b);
// Else- if Ladder statement
if(a>b)
{
printf("A is Greater");
}
else if(a<b)
{
printf("B is Greater");

49
}
else
{
printf("A and B are Equal");
}
getch();
}
Output:
20
10
A is Greater

10
20
B is Greater

10
10
A and B are Equal

[Link] Nested If Statement:


 Nested If means an if statement inside another if statement.
Syntax:
If(condition)
{
If(condition)
{
Statements;
}
Else

50
{
Statements;
}
}
else
{
Statements;
}

Program 40:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the Two Numbers");
scanf("%d%d",&a,&b);
// Nested if statement
if(a>b)
{
printf("A is Greater");
if(a>100)
{
printf(" and A is Greater than 100");
}
else
{

51
printf(" but A is lesser than 100");
}
}
else
{
printf("B is Greater");
}
getch();
}
Output:
120
90
A is Greater and A is Greater than 100

90
80
A is Greater but A is lesser than 100

80
90
B is Greater

[Link] Switch Case Statement:


Switch case statement is used when there is a multiple cases to
choose from.

52
Syntax:
Switch(Expression)
{
case 1: statements;
break;
case 2: statements;
break;
case 3: statements;
break;
default :
statements;
}

Program 41:
#include<stdio.h>
#include<conio.h>
void main()
{
int num, a=10, b=5;
clrscr();
printf("[Link]\n [Link]\n [Link]\n [Link]");
printf("\n\n choose your option:");
scanf("%d",&num);
switch(num)
{
case 1:
printf("Addition is : %d",a+b);
break;

53
case 2:
printf("Subtraction is :%d",a-b);
break;
case 3:
printf("Multiplication is :%d",a*b);
break;
case 4:
printf("Division is : %d",a/b);
break;
default:
printf("Choose the correct options...");
}
getch();
}
Output:
1. Addition
2. Subtraction
3. Multiplication
4. Division

Choose Your Option : 1


Addition : 15
Choose Your Option: 2
Subtraction : 5

Choose Your Option: 3


Multiplication : 50

54
Choose Your Option: 4
Division : 2

Choose Your Option: 5


Choose the correct option….

2.1.3 Iteration / Looping:


 The Iteration control structure are used for repetitively
executing a block of code multiple times, until the condition
if false.
 The Following are the basic iteration statement:
1. While
2. Do-while
3. For

[Link] While Loop :


 A while loop executes statement repeatedly until the
condition is false.
Syntax:
While(condition)
{
Statement;
}

Program 42:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
55
while(i<=10)
{
printf("%d\n",i);
i++;
}
getch();
}
Output :
1
2
3
4
5
6
7
8
9
10

[Link] Do-while Loop:


 Do-while Loop execute the statement first after that it
check’s the condition.
Syntax:
Do
{
Statements;
}
While(condition)

56
Program 43:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
do
{
printf("%d\n",i);
i++;
}
while(i<=10);
getch();
}
Output:
1
2
3
4
5
6
7
8
9
10

57
[Link] For Loop:
The for Loop like this.
Syntax:
For(initialization; condition; increment / decrement)
{
Statement;
}

Program 44:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d\n",i);
}
getch();
}
Output:
1
2
3
4
5
6

58
7
8
9
10

2.2 Nested Loop:


Syntax:
For(initialization; condition; increment / decrement)
{
For(initialization; condition; increment / decrement)
{
Inner Loop Statement;
}
Outer Loop Statement
}
Program 45:
#include<stdio.h>
#include<conio.h>

void main()
{
int i,j,n;
clrscr();
printf("enter the n value\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=10;j++)

59
{
printf("%d\t",(i*j));
}
printf("\n");
}
getch();
}

Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30

2.3 Break Statement:


Program 46:
#include<stdio.h>
#include<conio.h>

void main()
{
int i;
clrscr();
for(i=0;i<=10;i++)
{
if(i==3)
break;
printf("\n%d",i);
60
}
printf("\nEnded");
getch();
}

Output:
0
1
2
Ended

2.4 Continue Statement:


Program 47:
#include<stdio.h>
#include<conio.h>

void main()
{
int i;
clrscr();
for(i=0;i<=10;i++)
{
if(i==3)
continue;
printf("\n%d",i);
}
printf("\nEnded");
getch();
61
}
Output:
0
1
2
4
5
6
7
8
9
10

2.5 Goto Statement:


Program 48:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,i=0;
clrscr();
again:
printf("Enter the two numbers\n");
scanf("%d%d",&a,&b);
printf("\n Addition : %d\n",a+b);
i++;
if(i<3)
goto again;
62
getch();
}
Output:
Enter two numbers
3
4
Addition : 7
Enter two numbers:
5
6
Addition : 11
Enter two numbers :
7
8
Addition : 15

Chapter : 3
3.1 Functions:

 A function is a group of statements that together perform a task.


 Every C Program has at least one function which is main()
 You can add N number of function in a single program each program a
specific task.
 A function definition provides the actual body of the function.
 C provides numerous built in function you can call in your
program([Link](), strcpy(), clrscr(), getch(),…etc.,)
 To define a function you should give the function name, and return type,
parameters.

63
Syntax:
Return_typefunction_name(parameter list)
{
Body of the function(function definition)
}
Program 49:
#include<stdio.h>
#include<conio.h>
int add(int,int);
void main()
{
clrscr();
add(7,8);
getch();
}
int add(int a, int b)
{
int c=a+b;
printf("The Addition value of: %d",c);
return(c);
}
Output:
The Addition value of : 15
Types of Functions:
 No Arguments with No Return values
 With Arguments with No Return values
 With Arguments with Return values
 No Arguments with Return values

64
3.1.1 No Arguments with No Return Values
Syntax:
Void function_name(void)
{
---------
---------
}
Program 50:
#include<stdio.h>
#include<conio.h>
void cel();
void main()
{
clrscr();
cel();
getch();
}
void cel()
{
int a,b,c;
printf("enter two numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("add=%d",c);
}
Output:
Enter two numbers:
5
65
5
Add=10

[Link] Arguments with No Return values


Syntax:
Void function_name(int a)
{
---------
---------
}
Program 51:
#include<stdio.h>
#include<conio.h>
void cel(int,int);
void main()
{
clrscr();
cel(4,5);
getch();
}
void cel(int a,int b)
{
int c=a-b;
printf("Difference=%d",c);
}
Output:
Difference=-1

66
[Link] Arguments with Return values:
Syntax:
int function_name(char b)
{
--------
--------
return a;
}
Program 52:
#include<stdio.h>
#include<conio.h>
int cel(char);
void main()
{
clrscr();
printf("ASCII=%d",cel('A'));
getch();
}
int cel(char a)
{
return a;
}
Output:
ASCII : 65

67
[Link] Arguments with Return Values:
Syntax:
Float function_name(void)
{
------
------
return b;
}

Program 53:
#include<stdio.h>
#include<conio.h>
float cel();
void main()
{
clrscr();
printf("Division=%f",cel());
getch();
}
float cel()
{
float a,b,c;
printf("enter two value");
scanf("%f%f",&a,&b);
c=a/b;
return c;
}

68
Output:
Enter Two values :
10.6
2
Division : 5.300000

3.2 Call by Value :


Program 54 :
#include<stdio.h>
#include<conio.h>

void c_value(int x)
{
x=x+10;
printf("value of x: %d\n",x);
}

void main()
{
// Call by value
int a=10;
clrscr();
c_value(a); //calling function
printf("value of A After Calling : %d ",a);
getch();
}

69
Output:
Value of x: 20
Value of A After Calling : 10

3.3 Call by Reference:


Program 55:
#include<stdio.h>
#include<conio.h>

void c_value(int *x)


{
*x=*x+10;
printf("value of x: %d\n",x);
}

void main()
{
// Call by reference
int a=10;
clrscr();
c_value(&a); //calling function
printf("value of A After Calling : %d ",a);
getch();
}

Output :
Value of x : -12
Value of A After calling : 20
70
3.4 Recursion:
Program 56:
#include<stdio.h>
#include<conio.h>
void rev(int);
void main()
{
clrscr();
rev(10);
getch();
}
void rev(int n)
{
if(n<=0)
printf("Ended");
else
{
printf("%d\n",n);
rev(n-1);
}
}
Output:
10
9
8
7
6
5

71
4
3
2
1
Ended

CHAPTER : 4
4.1 Arrays:
What is an Array?
Array stores a collection of related values of a same data type.

What is the use of an Array? Memory Memory


Location Location
For example: n n
Int n=5;
5 10
n=10
6550
6550

 So, Using primitive variable we can store only one value at a time. We
can’t store multiple value in that variable.
 If use primitive variable to store 100 number of students register number
we have to use 100 variables.
 For that only we are using Array.
 By using array we can store N number of value in a single variable of
same data type.
 For example int stud_no[20]; This hold 20 student Number.

Syntax:
Datatype identifier [size of array];

For example:
int ABC[3];
72
Memory Allocation for this Array:

0 1 2
10 20 30

6550 6552 6554


Program 57:
#include<stdio.h>
#include<conio.h>
void main()
{
int array[2]={10,20};
clrscr();
printf("%d\n",array[0]);
printf("%d",array[1]);
getch();
}

Output:
10
20

Program 58:
#include<stdio.h>
#include<conio.h>
void main()
{ // 0 1 2 3 4

73
int i,array[5]={10,20,30,40,50};
clrscr();
for( i=0;i<=4;i++)
{
printf("%d\n",array[i]);
}
getch();
}

Output:
10
20
30
40
50

Types of Array:
Arrays are classified in to two Types:
1. One Dimensional Array
2. Two Dimensional Array

[Link] Dimensional Array:


One Dimensional Array is used to store data in a linear form.
Eg: int a[5]={1,2,3,4,5};

1 2 3 4 5

74
Array having one subscript value is called one Dimensional Array.
Eg: int a[5] ={1,2,3,4,5};
It is also used as Linear Array.
Initialization of one Dimensional Array:
1. Compile time Initialization
2. Run Time Initialization

// Compile time Initialization :


Program 59:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={1,2,3,4,5}; //compile time Initialization
int i;
clrscr();
printf("The Values are \n");
for(i=0;i<5;i++)
{
printf("%d\n",arr[i]);
}
getch();
}
Output:
The Values are:
1
2
3

75
4
5

// Run Time Initialization:


Program 60:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]; //Run time Initialization
int i,j;
clrscr();
printf("Enter Values are \n");
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
printf("The values are : \n");
for(j=0;j<5;j++)
{
printf("%d\n",arr[j]);
}
getch();
}
Output:
Enter the Values:
1
2
76
3
4
5
The values are:
1
2
3
4
5

[Link] Dimensional array:


 Array having more than one subscript variable is called multidimensional
array.
Syntax:
Data_typearray_name[row_size][column_size];
Example:
int arr[4][3];
 It is also called as Matrix
Memory Allocation:
Int arr[4][3];

Col 1 Col 2 Col 3

Row 1 arr[0][0] arr[0][1] arr[0][2]

Row 2 arr[1] [0] arr[1][1] arr[1][2]

Row 3 arr[2][0] arr[2][1] arr[2][2]

Row 4 arr[3][0] arr[3][1] arr[3][2]

Total values stored in the array are 4*3=12


77
Initialization of Two Dimensional Array:
1. Compile time Initialization
2. Run Time Initialization
Program 61:
#include<stdio.h>
#include<conio.h>
void main()
{
// compile time Initialization
int arr[4][3]={{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
int i,j;
clrscr();
printf("The values are:\n");
for(i=0;i<4;i++) //for row
{
for(j=0;j<3;j++) //for column
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
getch();
}
Output:
1 2 3
4 5 6
7 8 9
10 11 12

78
Program 62:
#include<stdio.h>
#include<conio.h>
void main()
{
// Run time Initialization
int arr[4][3];
int i,j;
clrscr();
printf("Enter values are:\n");
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&arr[i][j]);
}
printf("\n");
}
printf("The values are:\n");
for(i=0;i<4;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
getch();

79
}
Output:
Enter values are :
1
2
3
4
5
6
7
8
9
10
11
12
The values are:
1 2 3
4 5 6
7 8 9
10 11 12

CHAPTER : 5
[Link]:
 A Pointer is a variable that contains the address of the another variable.
 In other Words, it points to the location of a variable and can indirectly
access the variable.
 Pointers are declared using the * symbol.
* Asterisk

Eg : int *p;

80
For Example: a b
void main() 5 5
{
6550 6557
int a=5, b;
int *p; p
p=&a; 6550
printf(“%u”,p);
b=*p; *p=5 6559

printf(“%d”,b);
}

Program 63:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10;
int *p;
clrscr();
p=&a;
printf("Addres of a:%u\n",p);
printf("value of a : %d\n",a);
printf("value of a:%d\n",++*p);
printf("value of a: %d",a);
getch();
}

81
Output:
Address of a:65524
Value of a:10
Value of a:11
Value of a:11

[Link] and Pointer: 0 1 2


Sample Program: 10 20 30
void main() 6550 6552 6554
{ ar
r
int arr[3]={10,20,30}; 6550

int *P; P

P=arr; or P=&arr[0]; 6550

printf(“%u”,P); 6780
printf(“%d”,*P);
}

Program 64:
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={2,4,6,8,10};
int *p,i;
clrscr();
p=&arr[0];
for(i=0;i<5;i++)
printf("%u\n",*(p+i)+10);

82
getch();
}

Output:
12
14
16
18
20

[Link] of Pointers:
Array:
Array is a collection value of same type.
Eg: int arr[5];
Array of Pointers:
Array of Pointer is a collection of address of same type.
Eg: int *p[5];

Sample Program:
0 1 2
void main()
10 20 30
{
6550 6552 6554
int arr[3]={10,20,30};
ar
int *p[3]; r
6550
0 1 2
int i;
for(i=0;i<3;i++) 6550 6552 6554
P
{ 6540 6542 6544
6540
P[i]=&arr[i];
Printf(“%d”,*p[i]);

83
}
}

Program 65:
#include<stdio.h>
#include<conio.h>
void main()
{
//Array of Pointers

int arr[5]={10,20,30,40,50};
int *p[5],i;
clrscr();

for(i=0;i<5;i++)
{
p[i]=&arr[i];
printf("%d\n",*p[i]);
}
getch();
}

Output:
10
20
30
40
50
84
[Link] to Function:
Program 66:
#include<stdio.h>
#include<conio.h>
// pointer to function
int add(int,int);
void main()
{
int (*p)(int,int);
clrscr();
p=&add;
printf("%d",p(10,20));
getch();
}
int add(int x, int y)
{
int z=x+y;
return z;
}
Output:
30

CHAPTER : 6
[Link]:
String is a array of character or a collection of character.
CHARACTER:
It represent a single letter within the single codes.

85
Eg: ‘a’, ‘e’, ‘i’, ‘o’, ‘u’
STRING:
It represent a collection of character within double codes.
Eg.”hello”
Declaration syntax:
Char string_name[size of the string];
Initialization:
Using single character constant.
Eg: char STR[10]=”hello”;

0 1 2 3 4 5 6 7 8 9

H e l l o \0

6550 6551 52 53 54 55 56 57 58 59

Memory Allocation:
Char str[10]={‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};
Char str[10]=”Hello”;
Format Specifier :
The format Specifier “%s” used for accessing the string till it encounted ‘\0’.
Example:
Char str[10] = “Hello”;
Printf(“%s”,str);

Program 67:
#include<stdio.h>
#include<conio.h>
void main()
86
{
char str1[10]={'H', 'e', 'l', 'l', 'o', '\0'};
char str2[10]="Hello";
clrscr();
printf("%c\n",str1[0]);
printf("%s",str2);
getch();
}
Output:
H
Hello

[Link] the String


Scanf():
For Reading the string values we don’t need to use the ‘&’ operator.
Example:
Char str[10];
Scanf(“%s”,str);
0 1 2 3 4 5 6 7 8 9

H e l l o \0
6551 52 53 54 55 56 57 58 59 60

6551
str
Program 68:
#include<stdio.h>
#include<conio.h>
void main()

87
{
char str[20];
clrscr();
printf("Enter your name:");
gets(str);
printf("%s",str);
getch();
}
Output:
Enter your name : Abi karthika
Abi karthika

[Link].h
 C provides a set of built in function to manipulate the strings.
 That means we can perform some operation on string such as joining a
string, comparing…etc.
 All the string handling function are stored in the header file called
string.h

[Link] functions are:


 Strlen()
 Strrev()
 Strlwr()
 Strupr()
 Strcat()
 Strncat()
 Strcmp()
 Strncmp()
 Strcpy()
 Strncpy()
 Strstr()

88
 Strlen(): used to find the length

 Strrev(): used to reverse the string

 Strupr(): used to convert lower case to upper case.

 Strlwr(): used to convert upper case to lower case.

 Strcpy(): used for copying the source string into Destination string

 Strcmp(): used for comparing the 2 strings.

 Strcat(): used for combining 2 strings(Concatenating)

Program 69:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20];
clrscr();

printf("enter the name");


gets(str);
printf("The Length of the String is : %d", strlen(str));
printf("\n The Reverse of the string : %s", strrev(str));
printf("\n Upper case : %s", strupr(str));
printf("\n Lower case : %s", strlwr(str));
getch();
}
89
Output:
Enter the name tamil
The Length of the string : 5
The Reverse of the string :limat
Upper case : LIMAT
Lower case :limat

Strcpy:
Program 70:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20]="welcome";
char str2[20];
clrscr();
strcpy(str2,str1);
printf("%s", str2);
getch();
}
Output:
Welcome

strcat:
program 71:
#include<stdio.h>

90
#include<conio.h>
#include<string.h>
void main()
{
char str1[20]="Hi Guys";
char str2[20]="welcome";
clrscr();
strcat(str1,str2);
printf("%s", str1);
getch();
}
Output:
Hi guyswelcome

Strcmp:
Program 72:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20]="Mall";
char str2[20]="Mall";
int n;
clrscr();
n=strcmp(str1,str2);
if(n==0)
printf("Both strings are equal");
else if(n>0)
91
printf("string1 is greater");
else
printf("string2 is greater");
getch();
}
Output:
Both strings are equal
Strstr:
Program 73:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20];
char b[20];
clrscr();
printf("enter the string");
gets(a);
printf("enter the word to be find");
gets(b);
printf("%s",strstr(a,b));
getch();
}
Output:
Enter string Ram is a good boy
Enter the word to be find a
a good boy
92
CHAPTER : 7

[Link]: Main Memory


Structure memory

Sample program:
Num Name avg

2 Bytes 10 Bytes 4 Bytes


Struct student
{ 6550 6552 6562

int num;
char name[10]; 6550
Float avg; s
};
Struct student s;
Program 74:
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
int rollno;
char name[10];
float average;
}a;
void main()
{
clrscr();
strcpy([Link],"Arun");
[Link]=1201;
93
[Link]=98.7;
printf("student roll no %d\n",[Link]);
printf("student name %s\n",[Link]);
printf("student average %f\n",[Link]);
getch();
}
Output:
Student rollno:1201
Student name : Arun
Student Average : 98.699997

[Link] Structure:
Program 75:
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno;
char name[20];
};
struct student s1,s2;
void main()
{
clrscr();
printf("enter the 1st student no");
scanf("%d",&[Link]);
printf("enter the 1st student name");
scanf("%s",[Link]);
94
printf("enter the 2nd student no");
scanf("%d",&[Link]);
printf("enter the2nd student name");
scanf("%s",[Link]);
printf("\nRollno\t\tName\n");
printf("%d\t\t",[Link]);
printf("%s",[Link]);
printf("\n%d\t\t",[Link]);
printf("%s",[Link]);
getch();
}
Output:
Enter the 1st student no: 1201
Enter the 1st student name: Raji
Enter the 2nd student no: 1202
Enter the 2nd student name: Raj
Rollno Name
1201 Raji
1202 Raj

[Link] of Structures:
Program76:
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno;
char name[20];
95
};
struct student s[3];
void main()
{
int i;
clrscr();
for (i=0;i<3;i++)
{
printf("Enter the no:");
scanf("%d",&s[i].rollno);
printf("Enter the name : ");
scanf("%s",s[i].name);
}
printf("\nRollno\t\tName");
for(i=0;i<3;i++)
{
printf("\n%d\t\t",s[i].rollno);
printf("%s",s[i].name);
}
getch();
}
Output:
Enter the no: 1
Enter the name :rajesh
Enter the no: 2
Enter the name :rajee
Enter the no: 3
Enter the name :raj

96
Rollno Name
1 rajesh
2 rajee
3 raj

[Link]:
sample program:
Union un 20.2

{
int a;
char b;
float c;
o
};
Union un o;
Void main()
{
o.a=10;
o.b=’A’;
o.c=20.2;
}

Program77:
#include<stdio.h>
#include<conio.h>
union un
{
int a;
char b;

97
float c;
};
union un o;
void main()
{
clrscr();
printf("%d\n",sizeof(o));
o.a=10;
o.b='b';
o.c=20.5;
printf("%f",o.c);
printf("\n%c",o.b);
printf("\n%d",o.a);
getch();
}
Output:
4
20.500000
0

7.5.Type_def:
Program78:
#include<stdio.h>
#include<conio.h>
void main()
{
typedef int num;
num a=10;
clrscr();
98
printf("%d",a);
getch();
}
Output :
10

Program79:
#include<stdio.h>
#include<conio.h>
void main()
{
typedef int a;
a num[5]={1,2,3,4,5};
clrscr();
printf("%d",num[2]);
getch();
}
Output:
3
Program80:
#include<stdio.h>
#include<conio.h>
void main()
{
typedef int* num;
num a,b;
int a1;
a=&a1;
99
clrscr();
printf("%u",a);
getch();
}
Output:
65522
Program81:
#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct stud
{
int a;
char b[10];
}st;
void main()
{
st s1;
clrscr();
s1.a=1201;
strcpy(s1.b,"Arun");
printf("%d\n",s1.a);
printf("%s",s1.b);
getch();
}
Output:
1201
Arun
100
[Link]:
Program82:
#include<stdio.h>
#include<conio.h>
void main()
{
enum days { sun=10,mon=20,tue=30,wed=40,thur=50,fri=60, sat=70};
enum days d;
clrscr();
d=fri;
printf("%d",d);
getch();
}
Output:
60

CHAPTER : 8
[Link] Handling :
Program 83:
#include<stdio.h>
#include<conio.h>
void main()
{
//file handling
FILE *fp;
fp=fopen("[Link]","w");
if(fp==NULL)
printf("Does not Exit");
101
else
{
printf("File created");
}
fprintf(fp,"THis is my first program in file handling");
fclose(fp);
getch();
}
Output:
File created

Program84:
#include<stdio.h>
#include<conio.h>
void main()
{
//file handling
FILE *fp;
char str[50];
clrscr();
fp=fopen("[Link]","r"); //name,mode
if(fp==NULL)
printf("Does not Exit");
else
{
printf("File created\n");
}
while(fscanf(fp,"%s",str)!=EOF)
102
{ //End Of File
printf("%s ", str);
}
fclose(fp);
getch();
}
Output:
File created
This is my first program in file handling

Program85:
#include<stdio.h>
#include<conio.h>
void main()
{
//file handling
FILE *fp;
int a=10;
clrscr();
fp=fopen("[Link]","w"); //name,mode
if(fp==NULL)
printf("Does not Exit");
else
{
printf("File created\n");
}
fprintf(fp,"Hi Guys %d",a);
fclose(fp);
103
getch();
}
Output :
File created

Program 86:
#include<stdio.h>
#include<conio.h>
void main()
{
//file handling
FILE *fp;
int rollno;
char name[20];
clrscr();
printf("Enter the rollno& name");
scanf("%d",&rollno);
scanf("%s",&name);
fp=fopen("[Link]","w"); //name,mode
fprintf(fp,"ROLLNO :%d \n NAME : %s",rollno,name);
fclose(fp);
getch();
}
Output:
Enter the rollno& name
121
raji

104
105

You might also like