0% found this document useful (0 votes)
47 views158 pages

MSC MCA BCA-C Language-Lokesh Rathore

Uploaded by

ss8987120
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)
47 views158 pages

MSC MCA BCA-C Language-Lokesh Rathore

Uploaded by

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

C-Language Notes

Part1
Basics of programming: Program, Code, Computer Language, Problem, Programming, C
compiler, Software, Hardware, Keyboard, Monitor, RAM, Hard disk, CPU, Peripherals,
Console, Standard I/O, Flow of data, Algorithm, Flowchart.
Part2
Introduction of C language, Characteristics of C language, Sample program of C
language, Typing, Compiling and running C program, Structure of C program, C-
Tokens: C character set, Constants,Variable, Keywords, Operators, Data Types, Variable
Declaration.
Part3
Console Input output statement: Formatted & unformatted I/O, scanf():printf(), getch,
getche, getchar, putch, putchar. Operators & Expressions: Arithmetic Operators,
Relational Operators, Logical operators, Assignment operators, Increment(++) &
Decrement(--) operator, Shorthand operators, Conditional (Ternary) operator, Bitwise
operators. Rules for operation, Type Conversion: Implicit type conversion, Type casting.
Operator precedence / priority & Associativity.
Part4
Control Statement: Conditional Control Statement- if, if-else, else if ladder, nesting of if-
else, switch-case-default. Looping Control Statement- while, do-while, for, while v/s do-
while, Nesting loop. Jumping Control Statements: break, continue, goto, avoid goto.
Part5
Array: One, two, Multi dimensional, String: accessing string using %s and gets() and
puts(), String manipulation function. Structure, array of Structure, Structure within
structure, Union, Enumeration.
Part6
Function: need of function, Types of function, Library function, User defines function,
Working with user defined function: Function Definition, A return statement, Function
Declaration, Function calling, Function terminology: Calling function v/s Called
function, actual argument v/s Formal arguments. Function calling technique: Call by
value or pass by value, Call by reference or pass by reference. Recursion, Command line
argument, Local scope v/s Global scope, Lifetime of a variable, Static variable, Storage
class, Identifier, Modifier, Qualifier, sizeof, typedef.
Part7
Pointer: Pointer operator, Advantages of pointer, DMA: malloc(), calloc(), realloc(),
free(). Pointer to pointer, Pointer Arithmetic, Pointer of array, Array of Pointer, Pointer
of string, Passing pointer of data, Passing array to function, Returning pointer from
function, Pointer of function, Pointer of structure, Self Referencial Structure, Passing
structure to function, returning structure from function, copy all members of structure
into another.
Part8
File Handling: Properties of file, Operation on file, Types of file, File Pointer and mode,
C libray function for file handling: fopen() and fclose(), fputc() & fgetc(), putw() &
getw(), fputs() & fgets(), fprintf() & fscanf(), fwrite() & fread(). Sequential v/s Random
access of file: feof(), ftell(), rewind(), fseek().
Part9
Preprocessor: Preprocessor statements, File include, Macro Directives, passing argument
to macro, Conditional compilation: #ifdef, #ifndef, #if, #elif, #else, #endif.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e |1
C-Language Notes

PART-1
Basics of programming:
Program
Code
Computer Language
Problem, Programming
C compiler
Software
Hardware
Keyboard
Monitor
RAM
Hard disk
CPU
Peripherals
Console
Standard I/O
Flow of data
Algorithm
Flowchart

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e |2
C-Language Notes
Basics of programming
Program: A set of instructions / commands / statements called program.
नद श/कमांड/ टे टमट के समह
ू को ोाम कहते ह।

Code: Instructions which are written in particular language like C called


code.
ऐसे नद श िजहे कसी !वशेष भाषा जैसे सी मे &लखा जाता है उहे कोड कहा जाता है ।

Computer Language: A language in which we write code and perform that


task called computer language. Example: Binary, assembly, C, C++, Java,
C#, php, Visual Basic etc.
एक ऐसी भाषा िजसम हम कोड को &लखकर उस काय- को क./यट
ु र 1वारा संपा3दत करवाते ह

ु र भाषा कहते ह। जैसे बाइन6र, एस.7ल8, सी,सी++, जावा,सी#, पीएचपी, !वजअ
उसे क./यट ु ल
बे&सक आ3द।

Problem: Given task that has to be performed by computer called problem.


3दया गया काय- िजसे क./यट
ु र 1वारा संपा3दत करवाना हो उसे ो7लेम कहते ह।

Programming: The art of writing codes in a computer language to solve


problem and solution is ready in the form of program then it is called
programming.
ो7लेम को हल करने के &लए जब कसी क./यट
ु र भाषा मे कोड को &लखा जाता है और उसका
हल ोाम के ?प मे तैयार हो जाए तो इसे ोा&मंग कहते ह।

C-compiler: A programs that checks code of C program and convert whole


C code into machine code. Example: Turbo C, gcc, ANSI C etc.
एक ऐसा ोाम जो सी ोाम के कोड को चेक कर उसे मशीन कोड मे प6रवत-त कर दे उसे

सी कंपाइलर कहते ह। जैसे टब@ सी, gcc, ANSI सी आ3द।

Software: Program to solve problem called software / application.


कसी ो7लेम को हल करने वाले ोाम को ह8 सॉBटवेर / एि/लकेशन कहते ह।

Hardware: All physical components of computer machine called hardware.


For example-keyboard, monitor, RAM, hard disk, CPU.
ु र मशीन के सभी भौतक उपकरण को हाड-वेर कहते ह। जैसे कEबोड-, मॉनटर, रे म, हाड-
क./यट
Fड क, सीपीयू आ3द।

Keyboard: Input device used to type program and input data.


यह एक इनपट
ु उपकरण होता है िजसका उपयोग ोाम को टाइप करने और डाटा को इनपट

करने के &लए करते ह।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e |3
C-Language Notes
Monitor: Output device used to show program and output results.
यह एक आउटपट
ु उपकरण होता है िजसका उपयोग ोाम और प6रणाम को दे खने के &लए
करते ह।

RAM (Random access memory) – Primary Memory device used to load


program and store data temporary.
यह एक मेमोर8 Fडवाइस होता है िजसका उपयोग ोाम को लोड करने और डाटा को अ थाई
?प से टोर करने के &लए करते ह।

Hard disk: Secondary storage device used to store progam and result in the
form of file parmanently.
यह एक सेकंडर8 टोरे ज उपकरण है िजसका उपयोग ोाम और प6रणाम को फ़ाइल के ?प मे
थायी ?प से टोर करने के &लए करते ह।

CPU (Central Processing Unit): Processing device used to control


program and executes all operators of program using ALU (Arithmetic
Logic Unit).
यह एक ोसे&संग उपकरण है िजसका उपयोग ोाम को नयंJKत करने और सभी ऑपरे टर को
ALU(अ6रथमे3टक लॉिजक यू नट) 1वारा Nयािवत करने के &लए करते ह।

Peripherals: All Input / Output devices commonly called peripherals.


सभी इनपट
ु और आउटपट
ु उपकरण को संयO
ु त ?प से पे6रफेरQस कहा जाता है।

Console: Combination of keyboard and monitor called console (terminal).


कEबोड- और मॉनटर के कॉि.बनेशन को कसोल(टरमीनल) कहते ह।

Standard I/O: Keyboard, monitor and hardisk.


Flow of data in computer using I/O library functions of C:

CPU
ALU+CU+Registers

Operators: +- Resulting Value


*/%<=>&&++
Keyboard Console Input RAM Console Output Monitor
(Type program) (Program+Data) (Show Results)
fprintf, fputc, File
fscanf, fgetc, printf, putch,
scanf, getch,
putw, fputs, Input / getw, fgets, putchar, putsgets
getche, getchar, Output
fwrite fread
gets

Hard Disk
(Data Files)

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e |4
C-Language Notes
Algorithm
When any problem is breaking into number of steps so that it can be solved
then collection of steps are called algorithm.
जब कसी सम या को कई चरण मे &लखकर हल कया जाता है तब इन चरण के
समह
ू  को एQगॉ6रSम कहा जाता है ।
Characteristic:
1) Always written in English.
2) Problem is break into number of steps.
3) Meaning of each step must be clear.
4) Next step is depending on completion of previous step.
5) Steps must be finite.
6) Up to last step problem has to be solved.
!वशेषताएँ:
१. एQगॉ6रSम को हमेशा इंिXलश मे ह8 &लखा जाना चा3हए।
२. सम या को छोटे छोटे कई चरण मे &लख लेते है ।
३. \येक चरण कर अथ- प ट होना चा3हए।
४. एक चरण के पूण- होने के बाद ह8 अगला चरण शु? होता है ।
५. चरण कE सं_या नि`चत होना चा3हए।
६. अंतम चरण होने तक सम या का हल ा/त ह जाना चा3हए।
Example:
Algorithm: Given number is prime or not.
Step1: Input number n.
Step2: set I=2
Step3: Check I<n. If false goto step6.
Step4: Check n MOD 2 is 0. If true goto step7.
Step5: i=i+1 then goto step3.
Step6: print-Prime number. goto step8.
Step7: print-Not Prime number.
Step8: Exit
Advantage:
- Good approach before coding in any computer language.
- Any person can also understand problem.
Limitation:
- It is not user friendly.
- Flow of steps are not clear.
लाभ:
- कसी भी क./युटर भाषा मे कोड &लखने से पहले यह एक अbछा !वकQप है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e |5
C-Language Notes
- आलोगो6रथ मे &लखी गई सम या के हल को कोई भी सरलता से समझ सकता
है ।
कमयाँ:
यह यूजर dडल8 नह8ं है ।
चरण का वाह समझना मुि`कल होता है ।

Flowchart
Pictorial representation of any problem is called flowchart.
कसी भी सम या का eचKा\मक वण-न को Bलोचाट- कहा जाता है ।

Characteristics:
1. Each task is defined in symbol.
2. Specific symbols are used for different task.
3. Symbols are linked via flow line.
!वशेषताएँ:

1. \येक काय- eचहो मे प6रभा!षत होते है ।


2. \येक काय- का एक नि`चत eचह होता है ।
3. सभी eचह Bलो लाइन के 1वारा जड़
ु े होते है ।

Symbol used in flowchart:

SNo Symbol Name Uses


1 Oval Shape Start / End of flowchart

2 Parallelogram Input /Output operation

3 Rectangle Internal Processing

4 Diamond Decision making

5 Arrow Connect Symbols

6 Circle Connector one part of flowchart


on next page.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e |6
C-Language Notes
Example: Given number is prime or not.
Start

Input-n

i=2

Is i<n ? F
Print-is Prime

T End

Is T
n Mod i =0 Print-not Prime
?

F
i=i+1

Advantages:
- Better clarity as compared to algorithms.

- Program can be easily coded by following flowchart.

- Flow of program can easily express by flow line.


Limitation:
- Needs large space.

- Not suited for a large problem.


लाभ:
एQगॉ6रSम कE तल
ु ना मे यह अeधक प ट है ।
Bलोचाट- का अनस
ु रण करते हुये ोाम बनाना सरल हो जाता है ।
Bलो लाइन के 1वारा ोाम के Bलो को सरलता से jयOत कर सकते है ।
कमयाँ:
Bलोचाट- बनाने के &लए अeधक जगह कE आव`यकता होती है ।
बड़ी ो7लेम के &लए उपयO
ु त नह8ं है ।
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e |7
C-Language Notes
Algorithm and flowchart for factorial of given number
Algorithm: Factorial of given number.
Step1: Input number: n
Step2: Initialize: i = n, fact=1.
Step3: Check while i>0 If false go to step:
Step4: fact=fact*i.
Step5: i=i-1 and go to step3.
Step6: Print- fact
Step7: Exit

Flowchart for factorial


Start

Input-n

i=n, Fact=1

Is F
Print-Fact
i>0 ?

T
Fact=Fact*i End

i=i-1

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e |8
C-Language Notes

PART 2
Introduction of C language
Characteristics of C language
Sample program of C language
Typing, Compiling and running C program
Structure of C program
C-Tokens:
C character set
Constants
Variable
Keywords
Operators
Data Types
Variable Declaration

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e |9
C-Language Notes
Introduction of C language
Before 1960 programming languages were developed on the basis of
working areas, like COBOL language for business field, FORTRAN
language for science and engineering field and so on. Thus programmer had
to learn all programming languages for different fields. To use one language
for all fields, International committee was developed a new language called
“ALGOL60”. It were a lot of limitations to programming for all fields
therefore a number of new languages were developed one after one in 10
years like CPL, BCPL, B language etc. All these languages were
unsuccessful to program for all fields.
Finally in 1972, C-language was introduced by Mr. Dennis Ritchie at
invented at AT & T’s Bell Laboratory, USA.
सन ् 1960 के पहले काय- lेK के आधार पर क./युटर भाषाओं का नमा-ण कया जाता था। जैसे
jयापार से संबिधत काय- के &लए कोबोल भाषा, !वnान और अ&भयांJKकE से संबिधत काय- के
&लए फ़ोरoान भाषा इ\या3द। इस&लए ोामर को सभी कार कE फEQड के &लए एक से अeधक
ोा&मंग भाषाओं का nान होना ज?र8 होता था। सभी फEQड के &लए केवल एक ह8 भाषा का
उपयोग कया जा सके इसके &लए 1960 मे इंटरनेशनल कमेट8 1वारा "ALGOL60" नाम कE
भाषा का नमा-ण कया गया। इसमे कुछ क&मयाँ थी िजनको दरू करने के यास मे एक के बाद
एक 10 साल मे CPL, BCPL, B भाषाओं का नमा-ण कया गया, कतु सभी पूण- ?प से अलग
अलग फEQड कE ोा&मंग के &लए सफल नह8ं हो सकE।

अंतत: सबसे अंत मे 1972 मे C-भाषा का नमा-ण &म टर डेनस 6रची के 1वारा AT&T's बैल
लैबोरे टर8 USA मे कया गया।

Characteristics of C language
1. One for all: using C language, we can write program for all field like
business or science field. We do not need to learn different language
for every field.
2. Small & simple language: C language is smaller than other
languages. It has only 32 keywords and due to English like language,
C is easy to learn and simple.
3. Easy to debug: Code errors can be easily traced and removed using
debug tool of c language.
4. Powerful: Using C language, we can developed any type of software
like operating system (Unix), hardware’s driver etc.
5. Middle level: Writing code in C language is easy as compared to low
language(Machine and assembly) and execution speed of C program
is faster as compared to high level language. Therefore C language is
also called middle level language.
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 10
C-Language Notes
6. Portable: Program that write using C language can be running on any
computer machine and operating system.
7. Mother language: When we have to learn first C language then
learning all advance languages becomes easy like C++, Java, C# etc.
8. POP(Procedure Oriented Programming) language: All codes of
program can be divided in to a number of blocks(procedures) and
these can be used on same program or different programs.
9. Compile nature: First, all lines (statements) of program are checked
for any errors by C compiler program (gcc / tc), after then error free
program is converted into binary / machine code. Later that code run
by operating system.
10.Reliable and robustness: Program developed using C language can
work on any system therefore we can believe on C language for the
programming of any fields.
11.Free form language: Every C instruction ends with semi column(;)
and a set of instructions (compound statement) are enclosed in curly
braces {}. In this way we can write more than two instructions in a
single line.
सी-भाषा क वशेषताएँ:
१. सभी फ!ड क एक भाषा: सी भाषा के 1वारा कसी भी फEQड कE ोा&मंग कE जा सकती
है । अथा-त \येक फEQड के &लए अलग अलग भाषाओं को नह8ं सीखना पड़ता है ।
२. छोट& और सरल भाषा: सभी भाषाओं कE तल
ु ना मे सबसे छोट8 भाषा है , केवल ३२ कवp-स
है और इंिXलश लाइक होने के कारण सीखना बहुत सरल है ।
३. आसान debugging: सी भाषा मे बने ोाम कE K3ु टय(error) को आसानी से दरू कया
जा सकता है ।
४. पावरफुल: सी भाषा के 1वारा ऑपरे 3टंग &स टम(यूनOस) और हाड-वेर के qाइवर जैसे
&स टम सॉBटवेर का नमा-ण कया जा सकता है ।
५. म-य .तर&य: न.न तर8य भाषा कE तुलना मे ोाम का कोड सी भाषा म &लखना
सरल है तथा अय उbच तर8य भाषा कE तुलना मे सी भाषा मे &लखा हुआ ोाम
अeधक तीr गत से काय- कर सकता है । इस&लए सी भाषा को मsय तर8य भाषा भी कहा
जाता है ।
६. पोट/ बल: सी भाषा मे बने हुये ोाम को कसी भी क./युटर मशीन और ऑपरे 3टंग
मशीन पर रन कया जा सकता है ।
७. मातभ
ृ ाषा: पहले सी भाषा सीखने से एडवांस भाषाएँ जैसे सी++, जावा, सी# आ3द को
सीखना सरल हो जाता है ।
८. POP(2ोसीजर ओ5रएंटेड़ 2ो8ाम) भाषा: ोाम के कोड को छोटे छोटे 7लॉक मे !वभOत
कर सकते है िजहे आव`यकतानुसार उसी ोाम या अय ोाम मे उपयोग कर सकते
है ।
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 11
C-Language Notes
९. कंपाइल 2:;त: ोाम कE सभी लाइन को सबसे पहले सी-कंपाइलर ोाम(gcc, tc ) के
1वारा जांचा जाता है , K3ु टर3हत होने पर ह8 कंपाइलर सी ोाम को बाइनर8 कोड (मशीन
कोड) मे प6रवत-त कर दे ता है , िजसे ऑपरे 3टंग &स टम 1वारा रन कर दे ता है ।
१०. भरोसेमंद और अ>डग: सी मे बना ोाम कसी भी &स टम पर काय- करने कE lमता
रखता है इस&लए कसी भी फEQड कE ोा&मंग के &लए सी-पर भरोसा कया जा सकता
है ।
११. @ फॉमB भाषा: \येक नद श का अंत अQप !वराम(;) से होता है और नद श के
समह
ू (कंपाउं ड नद श) को मझला कोyठक {} मे &लखते है । इस कार से एक से
अeधक नद श को एक ह8 लाइन मे भी &लखा जा सकता है ।

Sample program of C language


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("\nInput two numbers:");
scanf("%d%d", &a,&b);
c=a+b;
printf("\nSum=%d", c);
getch();
}
Output:
Input two numbers: 10 20 <Enter>
Sum=30

Typing, Compiling and running C program


1. Install Turbo C software in DOS supported operating system.
टब@ सी सॉBटवेर को DOS आधा6रत &स टम मे इ टाल करते ह।
2. Open TC.exe file from location c:\TC\BIN\.
TC.exe फ़ाइल को c:\ TC\BIN\ लोकेशन से ओपेन करते ह।
3. Type C program and save(F2).(ex: prog1.c)
सी ोाम को टाइप कर सेव करते है ।
4. Compile using Alt+F9.
Alt+F9 दबाकर ोाम को कंपाइल करते है ।
5. If program is error free then run using Ctrl+F9.
य3द ोाम K3ु ट र3हत है तब ctrl+F9 दबाकर ोाम रन कर दे ते है ।
6. Check output screen using Alt+F5.
आउटपुट NEन को दे खने के &लए alt+F5 दबाते ह।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 12
C-Language Notes
Structure of C program
A complete structure of C program is divided into following 5 sections.

Document Section
Link File section
Global Declaration Section
Main function section
void main( )
{
local declaration;
code section;
}
User defined Function
Function-1
Function-2
:
:
Function-n

1) Document Section: It is used to write information about program like


title, developer, date etc. To ignore such information at compile time, we
write in C comment(/*------*/).
इस सेOशन का उपयोग ोाम से संबिधत जानका6रय को &लखने के &लए करते है
जैसे ोाम का टाइटल, डेवलपर, डेट आ3द। ऐसी जानका6रय को कंपाइल टाइम पर
इXनोर करने के &लए सी कम ट (/*------*/) का उपयोग करते ह।

Ex: /* Title: Addition of two number


Author: www.LRsir.net -9425034034
Date: 14/11/1978
*/
2) Link File Section: It is used to include those header files that contain
declaration of library functions which are using in current program. For
example, printf() and scanf() are library functions which are declared in
stdio.h header file.
इसका उपयोग ऐसी है डर फ़ाइल को ोाम मे शा&मल करने के &लए करते ह िजसमे
उपयोग मे लाये जा रहे लाइेर8 फंOशन का Fड़Oलेरेशन होता है । जैसे, printf() और
scanf() लाइेर8 फंOशन stdio.h है डर फ़ाइल मे FडOलैर ह।

Ex:#include<stdio.h>
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 13
C-Language Notes
#include<conio.h>
3) Global Declaration: Anything that can be shared by all functions of
program is declared in this section like declarations of variable and user
define function.
सी ोाम मे वे सभी कुछ िजहे सभी फंOशन के 1वारा शेयर कया जा सके उन
सभी को इस सेOशन मे FडOलैर कया जाता है । जैसे वे6रयबल और फंOशन का
FडOलैरेशन।

Ex: int x;
int add(int,int);
4) Main function Section: This is compulsory section for every C
program. In this section first we declare local variables then write code.
Main function is also called driver function because program run from its
first line and ends to its last line.
\येक सी ोाम मे यह सेOशन अनवाय- है। इसमे सबसे पहले वे6रयबल को
FडOलैर करते है उसके बाद कोड को। इस फनशन को qाईवर फंOशन भी कहा जाता
है OयूंकE इसी कE पहल8 लाइन से ोाम रन होना शु? होता है और अंतम लाइन
पर ख\म होता है ।

Ex: void main()


{
int a=5,b=4,c;
c=add(a,b);
printf(“%d”,c);
}
5) User defined Function Section: In this section we write code
separately that needs more than one times by other function.
इस सेOशन म हम ऐसे कोड को सेपरे ट फनशन मे &लखते है िजनकE ज?रत अय
फंOशन 1वारा बार बार होती है ।

Ex: int add(int r1,int r2)


{
x=r1+r2;
return(x);
}

Open TC.exeType programsave to file prog1.c in to “c:\tc\bin\”


/*Title: Addition of two number
Author: www.LRsir.net -9425034034
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 14
C-Language Notes
Date: 14/11/1978
*/
#include<stdio.h>
#include<conio.h>
int x;
int add(int,int);
void main()
{
int a=5,b=4,c;
c=add(a,b);
printf(“%d”,c);
}
int add(int r1,int r2)
{
x=r1+r2;
return(x);
}
Perform following steps:
1. Compile (Alt+F9): To check error & convert into machine code
2. Run (Ctrl+F9): To execute program
3. Output(Alt+F5): To see output screen.
Ex: 9

C-Tokens
Smallest unit of C program through which we define data and write
programming code, called C-tokens. It consists following things.
1) C character Set
2) Constants
3) Variables
4) Keywords
5) Operators
C character set:
It includes all ASCII characters of 1 Byte such as all lower and upper
alphabets (a to z, A to Z), digits (0-9), special symbols (+ - = . # etc) and
back slash characters (\n, \t, \a, \\, \', \", \0 etc).
इसम उन सभी ASCII कैरOटर को शा&मल कया गया है िजनकE साइज़ 1 बाइट होती
है । जैसे अंेजी के सभी छोटे और बड़े अlर (a-z, A-Z), अंक(0-9), !वशेष eचह (+ -
= . # आ3द) एवं बैक लेश कैरOटर(\n, \t, \\, \', \", \0 आ3द)।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 15
C-Language Notes
Constants:
A quantity that remains unchanged during the program execution. It
includes integer constant (ex. 122, 345), float constant (ex. 23.34, 2e7, .2E3)
and character constant ('a', '0', '+', '\0').
एक ऐसी रा&श जो ोाम के Nयावयन के समय अप6रवत-त रहती है, उसे क ट ट
कहते ह। इसमे शा&मल है - इंट8जर क ट ट (122, 345), Bलोट क ट ट(23.34, 2e7,
.2E3) एवं कैरOटर क ट ट('a', '0', '+', '\0').

Variable:
A quantity that may be changed during the program execution. It is an
identifier used to access data. (ex. p, r, t and si are variable to hold data for
simple interest).
एक ऐसी रा&श जो ोाम के Nयावयन के समय प6रवत-त होती रहती है उसे
वे6रएबल कहते ह। यह एक आइड3टफ़ायर होता है िजसके 1वारा डाटा को एOसैस कर
सकते है । ( उदा. p, r, t और si का उपयोग साधारण 7याज के डाटा को होQड करने के
&लए कर सकते है )

Keywords:
Reserved word of C language that can be used to define an instruction. Only
32 keywords are available in C language. All keywords are written only in
small letter.
सी भाषा के सभी आरˆlत श7द िजनके 1वारा नद श को प6रभा!षत करते है उहे
कEवड- कहते ह। सी भाषा म माK 32 कEवड- है । सभी कEवड- को अंेजी कE छोट8 वण-
मे &लखा जाता है ।

(ex. void, int, float, char, signed, unsigned, short, long,


double, return, if, else, switch, case, default, do, while,
for, break, continue, goto, struct, union, enum, auto,
register, static, extern, typedef, const, near and far).
Operators:
Special symbols used to operate operands called operators.
!वशेष eचह िजनके 1वारा डाटा को ओपरे ट करते है उहे ऑपरे टर कहते ह।

ex: Arithmetic (+, -, *, /, %), Relational (<,<=, >, >=, ==,!=), Logical (&&,
||, !), Assignment (=, +=, -=, *=, /=, %=), Increment (++), decrement (--),
Conditional (? :), Bitwise (&, |, ~, ^, >>, <<)

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 16
C-Language Notes
Data Types
A quantity that can be used define data at the time of variable declaration
and function creation called data types. All data types are classified into
following categories.
एक ऐसी रा&श िजसका उपयोग वे6रयबल को FडOलैर करते समय तथा फंOशन को बनाते
समय कया जाता है उसे डाटा टाइप कहते ह। सभी डाटा टाइप को न.न भाग मे
वग‰Šत कर सकते ह।

1. Primitive Data type (Primary /Basic / fundamental / predefine / inbuilt


data type)
1.1 Numeric data type: It is used to define integer and float data types.
इसका उपयोग इंट8जर और Bलोट डाटा टाइ/स को प6रभा!षत करने के &लए
करते ह।
C keywords for integer data type:
int (Space 2 Byte)
long( Space 4 Byte)
C keywords for float data type:
float (Space 4 Byte)
double( Space 8 Byte)
1.2 Non numeric data type: It is used to define character data type.
इसका उपयोग कैरOटर डाटा टाइप के &लए करते ह।
C keywords for character data type:
char (Space 1 Byte)
2. Non primitive data types (Secondary data type)
2.1 Derived Data type: It uses existing one data type for multiple data.
इसका उपयोग उपल7ध डाटा टाइप के 1वारा कई सारे डाटा के &लए करते ह।
Example: Array, Pointer
2.2 User define data type: It also uses existing different data types for
multiple data.
इसका उपयोग भी उपल7ध डाटा टाइप के 1वारा कई सारे अलग अलग कार
के डाटा टाइप के &लए करते ह।
Example: Structure, Union.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 17
C-Language Notes
Variable Declaration
Place holder of data is called variable. It is declared using following syntax.
डाटा को िजसमे होQड कया जाता है उसे वे6रयबल कहते ह। इसे न.न &संटेOस के
1वारा FडOलैर करते ह।
Syntax: data-type var1, var2, …., varn;
Example: int a, b,c;
float p,q,r;
char ch;
Initialization of variable: Default value of variable is garbage. Assign any
initial value of variable called initialization.
वे6रयबल कE ारि.भक वैQयू को इन&शलाइजशन कहते है । वे6रयबल कE Fडफ़ाQट वैQयू
garbage होती है ।
Ex: int a=10;
float pi=3.14;
Variables can be declared inside function (local) or outside the function
(global).
वे6रयबल को फंOशन के अंदर(लोकल) या बाहर(Xलोबल) FडOलैर कर सकते ह।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 18
C-Language Notes

PART3
Console Input output statement:
Formatted & unformatted I/O
scanf()
printf()
getch()
getche()
getchar()
putch()
putchar()
Operators & Expressions:
Arithmetic Operators
Relational Operators
Logical operators
Assignment operators
Increment(++) & Decrement(--) operator
Shorthand operators
Conditional (Ternary) operator
Bitwise operators
Rules for operation
Type Conversion:
Implicit type conversion
Type casting
Operator precedence / priority & Associativity.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 19
C-Language Notes
Console Input output statement
A statement that can be used to read data from keyboard called input
statement and that used to show data on monitor called output statement.
All I/O statements are classified into following two fields.
एक ऐसा टे टम ट जो कEबोड- से इनपुट कए गए डाटा को र8ड करे उसे इनपुट टे टम ट
और जो डाटा को मॉनटर पर 3दखा सके उसे आउटपुट टे टम ट कहते ह। सभी I/O
टे टम ट को न.न दो भाग मे वग‰कृत कया गया है ।
1. formatted I/O statement 2. Unformatted I/O statement

1) Formatted I/O: They are used to access one or more data.


इनका उपयोग एक या अeधक डाटा को एOसैस करने के &लए करते ह।
a) scanf(): It is a library function of stdio.h header file, that can read one
or more data from keyboard and assign to the specified variables.
यह stdio.h है डर फ़ाइल का एक फंOशन है जो कEबोड- से एक या अeधक डाटा
को र8ड कर 3दये गए वे6रया7Qस मे असाइन कर दे ता है ।
Syntax:
scanf("formatted codes", &var1, &var2, ….,&varn);
Formatted codes are:
%d for integer
%f for float
%c for character
At runtime, scanf will stop execution. We input sufficient data using
keyboard. When we press <Enter> key after very data then data
assigning to specified variable.
रन टाइम पर, scanf Nयावयन को रोक दे ता है । जब हम कEबोड- से पया-/त
डाटा को इनपुट करते ह। जब \येक डाटा को इनपुट कर <enter> कE ैस
करते ह तब 3दये गए वे6रयबल मे डाटा असाइन हो जाता है ।

b) printf(): It is also library function of stdio.h header file, that can show
data of one or more variables, constant and message on monitor.
यह भी stdio.h है डर फ़ाइल का एक लाइर8 फंOशन है जो एक या अeधक
वे6रया7Qस, क टं ट और मैसेज को मॉनटर 3दखा सकता है ।
Syntax:
printf("formattedCodes/Message/backSlashCharacter",
var1, var2, ….., varn);
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 20
C-Language Notes
Formatted code:
%d for integer
%f for float
%c for character
Backslash character:
\n move curser to new line
\t move curser to 8 space
\\ to use \
\" to use "
\' to use '
Remark: Formatted code will be skip when we do not shows values of
variable.
फोरमेटेड कोड को छोड़ दे ते ह जब हमे वे6रया7Qस कE वैQयू को नह8ं 3दखाना हो।
Example of printf() and scanf():
#include<stdio.h>
void main()
{
char a;
int b;
float c;
printf("\nInput character, integer then float value:");
scanf("%c%d%f", &a, &b, &c);
printf("\ncharacter=%c\tInteger=%d\tfloat=%f", a,b,c);
}
output:
Input character, integer then float value:
+ <enter>
23<enter>
2.5<enter>
Character=+ Integer=23 float=2.500000

2) Unformatted I/O statement: (getch, getche, getchar, putch, putchar):


They are used access only one character data in different ways.
इनका उपयोग केवल एक हे कैरOटर को अलग अलग तर8के से एOसैस करने के
&लए करते है ।

Unformatted input statement:


a) getch(): Read one character before <enter> but not shows and alter. It
is declare in conio.h header file.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 21
C-Language Notes
यह एक कैरOटर को <enter> कE के ैस होने से पहले ह8 र8ड कर लेता है
कतु 3दखाता नह8ं है और ना ह8 उसे बदल सकते ह। यह conio.h हैडर फ़ाइल
मे FडOलैर है ।
Syntax:
char-var=getch();
b) getche(): Read one character before <enter> and shows but never
alter. It is also declare in conio.h header file.
यह एक कैरOटर को <enter> कE के ैस होने से पहले ह8 र8ड कर लेता है और
3दखाता भी है कतु उसे बदल नह8ं सकते ह। यह conio.h है डर फ़ाइल मे
FडOलैर है ।
Syntax:
char-var=getche();
c) getchar(): Read one character after <enter> and shows as well as can
be alter. It is declare in stdio.h header file.
यह एक कैरOटर को <enter> कE के ैस होने के बाद ह8 र8ड करता है और
3दखाता भी है , साथ ह8 उसे बदल भी सकते ह। यह stdio.h है डर फ़ाइल मे
FडOलैर है ।
Syntax:
char-var=getchar();
Unformatted output statement:
a) putch(): Shows one character that in any form like constant, variable
or ASCII value. It is declare in conio.h header file.
यह एक कैरOटर जैसे क टं ट, वे6रयबल या ASCII वैQयू के ?प मे हो, उसे
3दखा सकता है । यह conio.h है डर फ़ाइल मे FडOलैर होता है।
Syntax:
putch(character);
b) putchar(): Shows also one character that in any form like constant,
variable or ASCII value. It is declare in stdio.h header file.
यह एक कैरOटर जैसे क टं ट, वे6रयबल या ASCII वैQयू के ?प मे हो, उसे
3दखा सकता है । यह stdio.h है डर फ़ाइल मे FडOलैर होता है ।
Syntax:
putchar(character);
Example of unformatted I/O statement:
#include<stdio.h>
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 22
C-Language Notes
#include<conio.h>
void main()
{
char ch;
ch=getch();
putch(ch);
ch=getche();
ch=getchar();
putchar(ch);
}

Operators & Expressions


To operate data for different operation, c provides a list of symbols called
operator and a statement that made up of data (operand) and operators called
expressions.
!व&भन कार के ऑपरे शन के &लए जब डाटा को ऑपरे ट करना हो तब सी भाषा मे
!व&भन साइन कE एक &ल ट होती है िजनहे ऑपरे टर कहते ह और एक ऐसा टे टम ट
जो डाटा(ओपेरड) तथा ऑपरे टर से तैयार होता है उसे एO े`न कहते ह।

Example: 2+3 is arithmetic expression. + is operator used to add operand 2


and 3.
2+3 एक अ6रथमे3टक एO े`न है । + का यूस ओपेरंड 2 और 3 को जोड़ने के &लए
करते ह।

C supports following operators.


सी भाषा मे न.न ऑपरे टर सहायक होते ह।
1. Arithmetic Operators
2. Relational Operators
3. Logical operators
4. Assignment operators
5. Increment(++) & Decrement(--) operator
6. Shorthand operators
7. Conditional (Ternary) operator
8. Bitwise operators
1) Arithmetic Operators: Usable for mathematical calculations.
ये गŒणतीय गणना करने के &लए उपयोगी है ।
Operator Name uses
+ Add sum of two data
- Subtract subtract two data
* Multiply product of two data
/ Division gives quotient of two data
% Modulo gives remainder of two data
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 23
C-Language Notes
Example:
#include<stdio.h>
void main()
{
int a,b;
printf("\nInput two integer data:");
scanf("%d%d", &a, &b);
printf("\nsum=%d", a+b);
printf("\nsubtract=%d", a-b);
printf("\nproduct=%d", a*b);
printf("\ndivision=%d", a/b);
printf("\nremainder=%d", a%b);
}
Output:
Input two integer data:
14
8
sum=22
subtract=6
product=112
division=1
remainder=6
Remark: % (modulo) operates on only integer data.
%(modulo) ऑपरे टर केवल इंट8जर डाटा को ह8 ऑपरे ट करता है ।
2) Relational Operators: Used to compare data numerical data.
इनका उपयोग सं_या\मक डाटा कE तुलना करने के &लए करते ह।
Operator Name
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
== Equal to
!= Not Equal to

Example and uses

< <= > >= == !=


4<5(true) 4<=5(true) 4>5(false) 4>=5(false) 4==5(false) 4!=5(true)
5<4(false) 5<=4(false) 5>4(true) 5>=4(true) 5==4(false) 5!=4(true)
5<5(false) 5<=5(true) 5>5(false) 5>=5(true) 5==5(true) 5!=5(false)

Relational operators are mostly applying on conditional and looping


statement (if, while and for).
6रलेशनल ऑपरे टर का उपयोग कंFडशन और लूप टे टम ट मे ह8 सबसे अeधक होता है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 24
C-Language Notes
3) Logical operator: Used to compare Boolean data (true / false).
इनका उपयोग बू&लयन डाटा(true/false) कE तुलना करने के &लए करते ह।
Operator Name as
&& Logical AND
|| Logical OR
! Unary Logical NOT
Example and uses
&& || !
(both true mean true) (one true mean true) (invert)
True && True (True) True || True (True) !True (False)
True && False (False) True || False (True) !False (True)
False && True (False ) False || True (True )
False && False (True) False || False (False)

True / False values are generated by combining relational operators.


True/false वैQयूस को 6रलेशनल ऑपरे टर के 1वारा उ\पन कया जाता है ।
Ex: (4<5)&&(4==5) similar to True && False (False)

4) Assignment operators (=): Used to assign right side value to left


variable. Only one = sign is used.
इसका उपयोग राइट साइड कE वैQयू को लेBट वे6रयबल मे असाइन करने के &लए
करते ह। = को एक ह8 बार &लखते ह।
Example:
#include<stdio.h>
void main()
{
int a, b;
a=10;
b=a;
printf("\na=%d\tb=%d",a,b);
}
Output:
a=10 b=10

5) Increment(++) & Decrement(--) operator:


++ is used to increase variable’s value by 1.
-- is used to decrease variable’s value by 1.
++ का उपयोग वे6रयबल कE वैQयू को 1 से बड़ाने के &लए करते है ।
-- का उपयोग वे6रयबल कE वैQयू को 1 से घटाने के &लए करते है ।
These operators can be uses in following form –
इन ऑपरे टर को न.न दो ?प से उपयोग मे लाते है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 25
C-Language Notes
Prefix(++a and --a): First variable’s value increment / decrement then uses
in current statement.
इस ?प मे सबसे पहले वे6रयबल कE वैQयू बड़ती/घटती ह उसके बाद कर ट टे टम ट मे
उपयोग होती है ।
Postfix(a++ and a--): First variable’s value uses in current statement then
increment / decrement.
इस ?प मे सबसे पहले वे6रयबल कE वैQयू कर ट टे टम ट मे उपयोग होती है उसके बाद
बड़ती/घटती ह है ।
Example:
#include<stdio.h>
void main()
{
int a,b;
a=10;
b=10;
printf("\n In Current Statement: a=%d\tb=%d", ++a, b++);
printf("\n After Increment=%d\tb=%d", a,b);
}
Output:
In current statement: a=11 b=10
After Increment: a=11 b=11

6) Shorthand operators: Used to assign right side value to left variable


before applying arithmetic operation.
इनका उपयोग राइट साइड वैQयू को लेBट वे6रयबल मे 3दये गए अ6रथमे3टक
ऑपरे शन के साथ असाइन करने के &लए होता है ।
Operator example similar to
+= a+=2 a=a+2
-= a-=2 a=a-2
*= a*=2 a=a*2
/= a/=2 a=a/2
%= a%=2 a=a%2

7) Conditional (Ternary) operator: It has three parts.


इसके तीन भाग होते ह।
Syntax: Expression1? Expression2: Expression3
Expression1 gives true / false, Expression 2 evaluates for true value whereas
expression3 evaluates for false value.
एO े`न1 true/false वैQयू 6रटन- करता है । एO े`न2 true वैQयू के &लए और
एO े`न3 false वैQयू के &लए सNय होता है ।
Example: find bigger value
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 26
C-Language Notes
#include<stdio.h>
void main()
{
int a,b,c;
a=10;
b=15;
c=(a>b)?a:b;
printf("\bigger value=%d",c);
}
Output:
Bigger value=15
Since expression1 (a>b) is false therefore value of expression3 assign to
variable c.
चूंक एO े`न1(a>b) फाQस है इस&लए एO े`न3 कE वैQयू वे6रयबल c मे असाइन होगी।

8) Bitwise operator: Used to operate data at binary level.


इनका उपयोग डाटा को बाइन6र तर पर ऑपरे ट करने के &लए करते ह।
Operator Name Meaning
& Bitwise AND both1 means 1
| Bitwise OR any 1 means 1
~ Bitwise NOT 1 means 0
^ Bitwise XOR both 1 or 0 means 0
<< Left Shift(n<<2) shift 2 bit of n towards left
>> Right Shift(n>>2) shift 2 bit of n towards right
Internally integer value convert into binary form then operator apply on bits
one by one.
आंत6रक ?प से पहले इंट8जर वैQयू उसके बाइन6र ?प मे प6रवत-त होगी फर उनकE
Jबस को एक के बाद एक ऑपरे ट होगी।
Ex:
Result of 5&6 is 4 due to following internal operation
1 0 1 binary value of 5
& & & bitwise & operator
1 1 0 binary value of 6
1 0 0 binary value of 4

Type of Expression:
1) Arithmetic expression: 2+3 or a+2 or a/b
2) Relational expression: 2<5 or a==2 or a>b
3) Mixed expression : a<b && a<3

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 27
C-Language Notes
Rules for operation:
1) Data must be similar type before operation.
ऑपरे शन के पहले डाटा एक समान टाइप के होने चा3हए।
2) Resulting data type is similar to operand type.
ओपेरpस का जो टाइप होता है प6रणाम भी उसी टाइप का ा/त होता है ।

Example: data1 operator data2 gives resulting data


Data1 Data2 Result data
Integer Float Invalid
Integer Integer Integer
Float Float Float
Character Character Character

Type Conversion
Before applying operators, type of data should be similar. e.g. 2 + 3.4 is
invalid expression because 2 is integer and 3.4 is float. Thus we have to
convert 2 into float(2.0) or 3.4 into integer(3) so that both data becomes
same type, then it is called type conversion.
ऑपरे टर को अ/लाई करने से पहले, डाटा का टाइप एक समान होना चा3हए। जैसे
2+3.4 एक तरह से अमाय होता है OयूंकE 2 इंट8जर और 3.4 Bलोट है। अत: हमे 2
को Bलोट टाइप (2.0) या 3.4 को इंट8जर टाइप (3) मे प6रवत-त करना होगा िजससे
क दोन डाटा एक समान टाइप के बन जाएँ। इसे ह8 टाइप कवश-न कहते ह।

In C language, type conversion performs in following two ways.


सी भाषा मे टाइप कवश-न न.न दो कार से हो सकता है ।

1) Implicit type conversion(Auto conversion):


When input data types are not same types then C compiler, automatically
convert small size of data into bigger one, so that actual value persist. It
is called implicit type conversion.
जब इनपुट डाटा के टाइ/स एक समान नह8ं ह तब सी कंपाइलर वत: ह8 ऊनमे से
छोटे वाले डाटा को बड़े वाले डाटा के टाइप मे प6रवत-त कर दे ता है िजससे क
वा त!वक वैQयू बनी रहती है । इसे ह8 एं/ल8&सट कवश-न कहते ह।
2+3.4 becomes 2.0+3.4
2) Explicit type conversion(Type casting):
If we want to force type conversion of any data into another called type
casting. Actual value may be loss.
य3द हम एक टाइप के डाटा को कसी अय टाइप मे बलपूवक
- प6रवत-त करते है
तब इसे टाइप काि टं ग कहते ह। वा त!वक वैQयू लॉस हो सकती है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 28
C-Language Notes
Syntax:
(cast-type) variable
Cast-type = int / float / char etc
Example: Division of integer number.
#include<stdio.h>
void main()
{
int a=10,b=4;
float c;
c= (float) a / b;
printf("division=%f", c);
}
Output:
division=2.500000
When value of a is casted to float so value of b automatically converts into
float, thus result will be float, therefore we choose float for variable c.
जब वे6रयबल a कE वैQयू को float मे का ट करते ह तब b कE वैQयू वत: float मे
प6रवत-त हो जाती है, अत: प6रणाम भी float मे ा/त होगा इस&लए हम इसके &लए
वे6रयबल c का टाइप float का चयन करते ह।

Operator precedence (priority) & Associativity


Operator precedence: An expression in which many operators then order
of operator is called priority or precedence of that operator. It means first
higher rank of operator will be performed.
एक ऐसा एO े`न िजसमे एक से अeधक ऑपरे टर हो तब उनके हल होने के Nम को
ह8 ऑपरे टर कE वर8यता या ी&सडस कहते ह। इसका अथ- यह8 हुआ क सबसे पहले
वह8 ऑपरे टर अपना काय- करे गा िजसकE वर8यता अय सभी से अeधक हो।

Ex: 5+7*2 5+14


19 (rank of * is higher than +)
Meaning of Associativity: An expression on which many operators have
same rank then evaluations will perform from left to right or right to left
called associativity.
एक ऐसा एO े`न िजसमे एक से अeधक ऑपरे टर एक जैसी रक के हो तब वे लेBट से
राइट या राइ‘त से लेBट कE ओर हल हगे उसे ऑपरे टर कE एसो&सए3ट!वट8 कहते ह।
6*2
Ex: 18/3*2 12 (associativity of / &* is left to right)
Priority and Associativity Chart
C language supports number operators so each has different priority and
associativity.
सी भाषा मे कई सारे ऑपरे टर होते ह इस&लए \येक कE अपनी एक नि`चत वर8यता
और एसो&सए3ट!वट8 होती है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 29
C-Language Notes
Operator Type Rank
Associtivity
(Priority)
()
1 Left to Right
Postfix ++ --
Prefix ++ --
Unary + -
2 Right to Left
! (Logical NOT)
~ (Bitwise NOT)
* (Arithmetic Multiply)
/ (Arithmetic Division) 3 Left to Right
% (Arithmetic Modulo)
+ (Arithmetic Add)
4 Left to Right
- (Arithmetic subtract)
<< (Bitwise Left Shift)
5 Left to Right
>> (Bitwise Right Shift)
< (Relational Less than)
<= (Relational Less than or equal)
6 Left to Right
> (Relational Greater than)
>= (Relational Greater than or equal)
== (Relational Equal to)
7 Left to Right
!= (Relational Not Equal to)
& (BitwiseAND) 8 Left to Right
^ (Bitwise XOR) 9 Left to Right
| (Bitwise OR) 10 Left to Right
&&(Logical AND) 11 Left to Right
|| (Logical OR) 12 Left to Right
?: (Conditional) 13 Right to Left
= (Assignment)
14 Right to Left
+= -= *= /= %= (Shorthand)

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 30
C-Language Notes

PART4
Control Statement:
Conditional Control Statement-
if
if-else
else- if ladder
nesting of if-else
switch-case-default
Looping Control Statement
While
do-while
for
while v/s do-while
Nesting loop
Jumping Control Statements
break
continue
goto
avoid goto.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 31
C-Language Notes
Control Statement
At runtime, normally a statement run step by step but depends on logic we
want to skips or repeat the execution of a set of steps. To handle such
situations C language has following two types of control statements.
रन टाइम पर, सामायत: टे टम ट एक के बाद एक रन होते जाते ह कतु लॉिजक के
आधार पर हम टे टम ट के कसी समह
ू को रन होने से रोकना चाहते है या फर से रन
करना चाहते है । ऐसी प6रि थत को ह डल करने के &लए सी भाषा म न.न सभी कंoोल
टे टम ट को न.न दो भाग मे बांटा गया है ।

1) Conditional (?:, if-else, switch-case)


2) Looping (while, do-while and for)
3) Jumping (break, continue and goto)

Conditional Control Statement:


When we have one or more sets of statement and we want to execute any
one of them then such sets are written in following conditional statements.

जब हमारे पास एक से अeधक टे मट के समूह है और उनमे से कसी एक को ह8


Nयािवत करवाना हो तब ऐसे टे टम ट के समह
ू  को न.न कंडीशनल टे टम ट मे &लख
सकते है ।

True Condition1 False

Statement1 Statement2

Statement-x

• Using Conditional (Ternary) Operator (? : )


• Using only if keyword
• Using if-else keywords
• Using ladder form of if-else keywords
• Using nesting of if-else
• Using switch-case-default keywords

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 32
C-Language Notes
Using only if keyword:
Syntax:
if(condition1) Flowchart:
{
Statement1 True Condition1 False
}
Statement-x Statement1

Statement-x

If condition1 is true then Statement1 execute otherwise not.


य3द condition1 स\य है तब statement1 Nयािवत होगा अयथा नह8ं।

Example: To convert negative to positive


#include<stdio.h>
void main()
{
int a=-10;
if(a<0)
{
a=-a;
}
printf("+ve vale is %d",a);
}
Output: +ve value is 10

Using if-else keyword:


Syntax:
if(condition1) Flowchart:
{
Statement1
True Condition1 False
}
else Statement1 Statement2
{
Statement-x
Statement2
}
Statement-x

If condition1 is true then Statement1 execute otherwise Statement2.


य3द condition1 स\य है तब statement1 Nयािवत होगा अयथा statement2
होगा।

Example: To find larger of two numbers:


#include<stdio.h>
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 33
C-Language Notes
void main()
{
int a=10, b=15,c;
if(a>b)
{
c=a;
}
else
{
c=b;
}
printf("Large is %d",c);
}
Output:Large is 15

Using ladder form of if-else keyword:


Syntax:
if(condition1) Flowchart:
{
Statement1
True Condition1
} Statement1
else if(condition2) False
{
True
Statement2 Statement2 Condition1

}
:::::: False

else Statement-n
{
Statement-n
} Statement-x
Statement-x

If condition1 is true then Statement1 execute otherwise if condition2 is


true then Statement2 executes and this will continued. Statement-n
executes when all above conditions are false.

य3द condition1 स\य है तब statement1 Nयािवत होगा अयथा condition2 के

स\य होने पर statement2 Nयािवत होगा और यह Nम नरं तर हो सकता है । जब सभी

condition अस\य हो जाती है तब statement-n Nयािवत होगा।

Example: To print grade of percentage.


#include<stdio.h>
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 34
C-Language Notes
void main()
{
int p=55;
char grade;
if(p>=60)
{
grade='A';
}
else if(p>=50)
{
grade='B';
}
else if(p>=40)
{
grade='C';
}
else
{
grade='D';
}
printf("Grade: %c", grade);
}
Output: Grade:B

Using nesting form of if-else


Syntax:
if(condition1) Flowchart:
{
if(condition2)
{ True
False
Statement1 a>b
}
else
a>c b>c
{
Statement2 m=a m=c m=b m=c
}
}
Print m
else
{
if(condition3)
{
Statement3
}
else
{
Statement4
}
}
Statement-x

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 35
C-Language Notes
If Condition1 is true then Condition2 checks otherwise Condition3
checks.
य3द condition1 स\य है तब condition2 चेक होगी अयथा condition3 चेक होगी।

Example: To find maximum of three numbers:


#include<stdio.h>
void main()
{
int a=10, b=15,c=7,m;
if(a>b)
{
if(a>c)
{
m=a;
}
else
{
m=b;
}
}
else
{
if(b>c)
{
m=b;
}
else
{
m=c;
}
}
printf("Large number is %d",m);
}
Output:Large number is 15

Using switch-case-default keywords:


Suppose we have a number of statement sets and everyone is labeled by
unique integer or character value. We input any value from outsource so that
it can matches to any labels. When any first one label is matched then their
statements will be executes otherwise default statements executes. Switch-
case-default structure is used mainly for menu driven / choice based
programming.

मानाक हमारे लॉिजक मे कई सारे टे टम ट के समह


ू ह और \येक कसी अ1!वतीय
इंट8जर या कैरOटर वैQयू से लेबQड ह। हम कसी वैQयू को बाहर से इनपुट करते ह

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 36
C-Language Notes
िजससे क वह कसी लेबल से मैच हो सके। जब कोई पहला लेबल मैच हो जाता है
उसके टे टम ट Nयािवत हो जाएंगे अयथा Fडफ़ाQट टे टम ट Nयािवत हगे। switch-
case-default oOचर का उपयोग मु_यत: मेनू Fqवेन / चॉइस बे ड ोा&मंग मे कया
जाता है ।
Syntax: Flowchart:
switch(variable/expression)
{
case value1: Variable
statement1
break;
case value2: Match Value1
Statement1
statement2
break; Unmatched
……. Match
Statement2 Value2
…….
case value-n:
Unmatched
statement-n;
break; Statement-n

default:
default statement;
Statement-x
}

Example: Arithmetic calculator.


#include<stdio.h>
void main()
{
int a=20,b=15;
char ch='*';
switch(ch)
{
case '+':
printf("Sum=%d",a+b);
break;
case '-':
printf("Subtract=%d",a-b);
break;
case '*':
printf("Multiply=%d",a*b);
break;
case '/':
printf("Division=%d",a/b);
break;
default :
printf("\nInvalid operator");
}
}
Output: 5
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 37
C-Language Notes
Looping Control Statement:
When we have to execute one set of statements more than one times then
following looping control statements can be used in C language.

जब हमे कसी एक समह


ू के टे टम ट को एक से अeधक बार Nयािवत करवाना हो तब
सी भाषा म न.न लू!पंग कंoोल टे टम ट का उपयोग करते ह।

Condition1 False Statement-x

True

Statement1

• Using while keyword


• Using do-while keyword
• Using for keyword

Using while keyword:


Syntax:
while(condition1) Flowchart:
{
Statement1
Condition1 False
} Statement-x
Statement-x
True

Statement1

While is entry level and undefined loop structure. Initially condition1 is


checked. If it true then Statement1 executes and again condition1 is
checked. This process of execution will be continued until condition1
gives false value.
while एक एंo8 लेवेल और अंFडफ़ाइंड लप
ू oOचर है । इसमे सबसे पहले condition1 चेक

होती है । य3द यह स\य है तब ह8 statement1 Nयािवत होता है और फर से

condition1 चेक कE जाती है। यह Nया नरं तर बनी रहती है जब तक कE condition1


अस\य नह8ं हो जाती।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 38
C-Language Notes
Example: Show your name until you want.
#include<stdio.h>
void main()
{
char a='y';
while(a=='y' || a=='Y')
{
printf("\nDownload Ebooks: www.LRsir.net");
printf("\nPress y key to continue..");
fflush(stdin); //to clean input buffer
a=getche();
}
}
Output:
Download Ebooks: www.LRsir.net
Press y key to continue..y
Download Ebooks: www.LRsir.net
Press y key to continue..y
Download Ebooks: www.LRsir.net
Press y key to continue..n
<Stop loop>

Using do-while keyword:


Syntax:
do Flowchart:
{
Statement1 Statement1
}while(condition1)
Statement-x
True
Condition1 False Statement-x

do-while is exit level and undefined loop structure. Initially loop


statement1 executes then condition1 is checked. If it true then once again
statement1 executes and again condition1 is checked. This process of
execution will be continued until condition1 gives false value.
do-while एक एिOज़ट लेवेल और अंFडफ़ाइंड लप
ू oOचर है । इसमे सबसे पहले

statement1 Nयािवत होता है उसके बाद condition1 चेक होती है। य3द यह स\य है
तब ह8 statement1 फर से Nयािवत होता है और एक बार फर से condition1 चेक

होती है । यह Nया नरं तर बनी रहती है जब तक कE condition1 अस\य नह8ं हो जाती।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 39
C-Language Notes
Example: Show your name until you want.
#include<stdio.h>
void main()
{
char a;
do
{
printf("\nDownload Ebooks: www.LRsir.net");
printf("\nPress y key to continue..");
fflush(stdin); //to clean input buffer
a=getche();
}while(a=='y' || a=='Y')
}
Output:
Download Ebooks: www.LRsir.net
Press y key to continue..y
Download Ebooks: www.LRsir.net
Press y key to continue..y
Download Ebooks: www.LRsir.net
Press y key to continue..n
<Stop loop>
while v/s do-while loop: When initially condition1 is false then in case of
while loop, statement1 never execute whereas in case of do-while loop,
statement1 will executes first time.
जब ार.भ म ह8 condition1 अस\य हो जाती है तब while लूप कE ि थत मे
statement1 एक बार भी Nयािवत नह8ं होता है जबक do-while लूप कE ि थत
मे टे टम ट एक बार अव`य Nयािवत हो जाता है ।

Example:while loop Example:do-while loop


#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
char a='n'; char a='n';
while(a=='y'||a=='Y') while(a=='y'||a=='Y')
{ {
printf("I never run"); printf("I run one time");
} }while(
} }
Output: Nil
Output: I run one time

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 40
C-Language Notes
Using for keyword:
Syntax:
for(initial;condition1;modify) Flowchart:
{
Statement1
Initial
}
Statement-x
Condition1 False Statement-x

True
modify

Statement1

for is entry level and finite loop structure. for loop is consist using three
parts. First part set initial value, second part check condition1 and third
part modify value. If condition1 is true then Statement1 executes, after
then modify part executes then again condition1 is checked. This process
of execution will be continued until condition1 gives false value.
for एक एंo8 लेवल और फायनाइट लप
ू oOचर है । for लप
ू तीन भाग से &मलकर बना

ू कE ारि.भक वैQयू को सेट करता है , दस


होता है । पहला भाग लप ू रा भाग condition1 को

चेक करता है और तीसरा भाग वैQयू म बदलाव करता है । य3द condition1 स\य है तब ह8

statement1 Nयािवत होता है, इसके बाद मोFड़फाइ भाग Nयािवत होगा, उसके बाद
condition1 फर से चेक होगी। Nयावयन कE यह Nया नरं तर बनी रहती है जब तक
कE condition1 अस\य नह8ं हो जाती।

Example: Show your name 5 times.


#include<stdio.h>
void main()
{
int i;
for(i=1;i<=10;i++)
{
printf("\nDownload Ebooks: www.LRsir.net");
}
}
Output:
Download Ebooks: www.LRsir.net
Download Ebooks: www.LRsir.net
Download Ebooks: www.LRsir.net
Download Ebooks: www.LRsir.net
Download Ebooks: www.LRsir.net
<Stop loop>
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 41
C-Language Notes
Nesting loop:
When one loop structure is defined inside a loop structure then it is called
nesting of loop. We can create nesting loop in multiple ways.

जब एक लूप oOचर को कसी अय लूप oOचर मे प6रभा!षत करते ह तब इसे लूप
कE नेि टं ग कहा जाता है । लप
ू कE नेि टं ग को कई कार से न&म-त कर सकते ह जैस-े

1) while loop inside while loop


2) do-while loop inside do-while loop
3) for loop inside for loop
4) while loop inside for or do-while loop
5) for loop inside while or do-while loop
6) do-while loop inside for or while loop

We can also increase level of nesting loop up to any desired level.

लूप क नेि टं ग का तर आव`यकतानुसार बड़ाया भी जा सकता है ।

Example: print multiplication table from 1 to 10 using


Nesting of for loop
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=10;i++)
{
printf("\n");
for(j=1;j<=10;j++)
{
printf("%d\t",i*j);
}
}
}
Output:
1 2 3 4 5 ...10
2 4 6 8 10...20
========
========
10 20 30 40 50...100

Loop from i=1 to 10 called outer loop where as loop from j=1 to 10 called
inner loop. When condition of outer loop is true then condition of inner loop
checked. When condition of inner loop is false then condition of outer loop

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 42
C-Language Notes
will again checked. This process will be continued until condition of outer
loop becomes false.

i=1 से 10 तक आउटर लूप और j=1 से 10 तक ईनर लूप कहलाता है । जब आउटर


लूप कE कंFडशन स\य होती है तब ह8 ईनर लूप कE कंFडशन चेक होती है । और जब
ईनर लप
ू कE कंFडशन अस\य होती है तब ह8 आउटर लप
ू कE कंFडशन चेक होती है । यह
Nया नरं तर बनी रहती है जब तक क आउटर लूप क कंFडशन अस\य नह8ं हो
जाती।

Jumping Control Statements (break, continue and goto)


A statement that can be used to transfer program control to anywhere in
program so that execution may start from that point called jumping control
statement.
एक ऐसा टे टम ट जो ोाम कंoोल को ोाम म कसी अयK सेट कर सके िजससे
क Nयावयन उस Jबद ु से ार.भ हो सके, ऐसे टे टम ट को जं!पंग कंoोल टे टम ट
कहते है ।
In C language jumping of program control can be take place using following
three keywords.
सी भाषा म कंoोल कE जं!पंग को न.न तीन कEवड- कE सहायता से स.पन कया
जाता है ।
1) Using break keyword
2) Using continue keyword
3) Using goto keyword
Using break keyword:
It is applicable inside switch-case-default and loop structure (while, do-
while and for). When break executes then program control comes out
immediately from that structure and set at the bottom of structure.
Statements after break never execute.
break का उपयोग switch-case-default एवं लप
ू oOचर(while, do-while एवं for)
म ह8 कया जा सकता है । जब break Nयािवत होता है तब ोाम कंoोल उसी
समय उस oOचर से बाहर आ जाता है और oOचर के नीचे सेट हो जाता है । break
के Nयािवत होने के बाद शेष टे टम ट Nयािवत नह8ं होते है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 43
C-Language Notes
Syntax:
break;
break should be used conditionally.
ेक का उपयोग कंFडशन के साथ कया जाना चा3हए।

Example: find input number is prime or not.


#include<stdio.h>
void main()
{
int n,r,i;
printf("\nInput any integer number:");
scanf("%d",&n);
for(i=2; i<n; i++)
{
r=n%i;
if(r==0)
{
printf("\n%d is not prime number",n);
break;
}
}
if(i==n)
{
printf("\n%d is prime number",n);
}
}
Output:
Input integer number: 9<enter>
9 is not prime number
Input integer number: 5<enter>
5 is prime number
In this example, when break executes conditionally then program controller
comes out from for loop before false value of loop condition.
इस ोाम मे, जब break द8 गई कंFडशन के स\य होने पर Nयािवत होता है और
लूप कE कंFडशन के अस\य होने से पहले ह8 ोाम कंoोलर को लूप से बाहर कर दे ता
है ।

Using continue keyword:


It is applicable inside loop structure (while, do-while and for). When
continue executes then program control transfer to beginning of loop so that
condition can be checked or modify value in for loop. Statements after
continue never execute.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 44
C-Language Notes
continue का उपयोग लप
ू oOचर (while, do-while एवं for) म ह8 कया जा सकता
है । जब continue Nयािवत होता है तब ोाम कंoोल उसी समय लूप के ार.भ म

ु : चेक हो या for लूप मे वैQयू मोड़ीफ़ाई


चला जाता है िजससे क लूप कE कंFडशन पन
हो जाए। continue के Nयािवत होने के बाद शेष टे टम ट Nयािवत नह8ं होते है ।

Syntax:
continue;
continue should be used conditionally.
continue का उपयोग कंFडशन के साथ कया जाना चा3हए।

Example: print odd number from 1 to 10.


#include<stdio.h>
void main()
{
int i,r;
for(i=1; i<=10; i++)
{
r=i%2;
if(r==0)
{
continue;
}
printf("%d\t",i);
}
}
Output:1 3 5 7 9

In this example, when continue executes conditionally then program


controller transfer to beginning of for loop before executing remaining
statement.
इस ोाम मे, जब continue द8 गई कंFडशन के स\य होने पर Nयािवत होता है
और ोाम कंoोलर लप
ू के शेष टे टम ट को Nयािवत करने से पहले ह8 लप
ू के
ार.भ म चला जाता है ।

Using goto keyword:


It is applicable inside function. When goto executes then program control
immediately transfer to a point where label of goto is set and execution
begins from that point.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 45
C-Language Notes
goto का उपयोग फंOशन म कया जाता है । जब goto Nयािवत होता है तब ोाम
कंoोल उसी समय वहाँ चला जाता है जहाँ goto के साथ &लखा गया लेबल प6रभा!षत
है । इसके बाद Nयवयन उस Jबद ु से शु? हो जाता है ।

Syntax:

To skip execution of code: To repeat execution of code:


goto label; label:
statement1 statement-1
label: statement-x goto label;

goto should be used conditionally.


goto का उपयोग कंFडशन के साथ कया जाना चा3हए।

Example: print number from 1 to 10 using goto.


#include<stdio.h>
void main()
{
int i=1;
X: printf("%d\t",i);
if(i<=10)
{
goto X;
}
}
Output: 1 2 3 4 5 6 7 8 9 10
In this example, X is a label. When goto executes conditionally then
program controller transfer to X:
इस ोाम मे, X एक लेबल है । जब goto कंFडशन के स\य होने पर Nयािवत होता
है तब ोाम का कंoोलर लेबल X: पर oासफर हो जाता है ।

Avoid use of goto:


We can write a number of goto statements with same label name or different
label name but we must remember that one label must be defined only one
times because it may causes ambiguity error. Since size of program grows
then remembering flow of control using goto becomes very difficult for
programmer. Therefore goto should be used in rear cases like to come out
from innermost loop to outermost loop because break comes out from inner
loop but not outer loop.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 46
C-Language Notes
ोाम म हम आव`यकतानुसार कई सारे goto टे टम ट को एक ह8 लेबल नाम या
&भन नाम के लेबल के साथ &लख सकते है । कतु हमे इस बात का अनवाय-तः sयान
रखना होता है क, एक नाम का लेबल केवल एक ह8 बार प6रभा!षत हो अयथा
एि.बŸयट
ु 8 एरर ा/त हो सकती है । चंू क ोाम कE साइज़ बड़ने पर ोामर के &लए
goto टे टम ट के Bलो को याद रखना बहुत क3ठन हो जाता है । इस&लए goto का
उपयोग ोाम मे केवल !वशेष प6रि थतय म ह8 कया जाना चा3हए जैसे ोाम
कंoोलर को सबसे इनरमो ट लूप से सीधे आउटरमो ट लूप से बाहर करना हो, OयूंकE
break केवल इनर से बाहर कर सकता है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 47
C-Language Notes

PART5
Array:
One, two, Multi dimensional
String:
accessing string using %s
gets() and puts()
String manipulation function
Structure
Array of Structure
Structure within structure
Union
Enumeration.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 48
C-Language Notes
Array: (One, two and Multi dimensional)
Collection of same data arranged in continuous order called array. Array is
one example of derived data type of non primitive class. Array has
following types with example.

एक समान डाटा के ऐसे समह


ू जो नरं तर Nम मे jयवि थत ह उहे अरे कहा जाता है ।
अरे एक नॉन ी&म3टव Fडराईjड डाटा टाइप है । अरे 3 कार का होता है जो उधारण
स3हत न.न है ।

For example:

One Dimensional Two dimensional Multi dimensional


10 20 30
10 20 30 40 50 10 20 30 5 10 15 40 50 60
40 50 60 20 25 30 70 80 90
70 80 90 35 40 45

One dimensional array:


Collections of same data arrange in single row called one dimensional array.
ू जो केवल एक ह8 row मे jयवि थत ह उहे एक !वमीय
एक समान डाटा के ऐसे समह
अरे कहा जाता है ।

Syntax:
array-type array-name[size];
array-type = any data type like int / float / char
Example:
array of 5 integer: int a[5];
array of 5 float: float b[5];
Initialization of array:
int a[5]={10,20,30,40,50}; or
int a[]={10,20,30,40,50};
Size automatically set by counting number of values.
वैQयू कE सं_या के अनस
ु ार साइज़ वत: सेट हो जाती है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 49
C-Language Notes
Accessing array data: (Read / Write data)
Syntax:
array-name[i]
where i = index of array (0 to size-1)
0 is lower bound of array.
(size-1) is upper bound of array.
Example: read and show 5 integer data with maximum value
#include<stdio.h>
void main()
{
int a[5];
int max,i;
printf("\nInput 5 integer data:");
for(i=0; i<5; i++)
{
scanf("%d", &a[i]);
}
printf("\nArray data are:");
for(i=0; i<5; i++)
{
printf("%d\t", a[i]);
}
max=0;
for(i=0; i<5; i++)
{
if(max<a[i])
{
max=a[i];
}
}
printf("\nMaximum value=%d", max);
}
Output:
Input 5 integer data: 10 5 6 15 9<enter>
Array data are: 10 5 6 15 9
Maximum value=15
Memory Allocation:
a[0] a[1] a[2] a[3] a[4]
10 5 6 15 9

From this program it is clear that we can use for loop to access every data of
one dimensional array.
इस ोाम से प ट है क हम एक !वमीय अरे के \येक डाटा को एOसैस करने के
&लए for लूप का उपयोग कर सकते है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 50
C-Language Notes
Two dimensional array:
Collections of same data arrange in tabular / matrix / grid form that contain
multiple rows and each row contain equal columns called two dimensional
arrays.
एक समान डाटा के ऐसे समह
ू जो टे बल
ु र / मेo8Oस / eड के ?प मे jयवि थत ह
अथा-त िजसम डाटा कई सार8 rows ह और \येक row मे एक कॉलम कE सं_या एक
समान हो, ऐसे अरे को 1!व-!वमीय अरे कहा जाता है ।

Syntax:
array-type array-name[rows][columns];
array-type = any data type like int / float / char
rows = maximum rows
columns = maximum columns
Example:
array of 2x3 integer: int m[2][3];
array of 3x3 float: float b[3][3];
Initialization of array:
int m[2][3]={{10,20,30},{40,50,60}}; or
int m[][]={{10,20,30},{40,50,60}};
Size automatically set by counting number of values.
वैQयू कE सं_या के अनुसार साइज़ वत: सेट हो जाती है ।

Accessing array data: (Read / Write data)


Syntax:
array-name[i][j]
where-
i = index of row (0 to rows-1)
j = index of column (0 to columns-1)

[0][0] is lower bound, [rows-1][columns-1] is upper bound o


Example: read 3x4 matrix and show its transpose.
#include<stdio.h>
void main()
{
int m[3][4];
int i,j;
for(i=0; i<3; i++)
{
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 51
C-Language Notes
printf("\nInput 4 integer data row-wise:");
for(j=0; j<4; j++)
{
scanf("%d", &m[i][j]);
}
}
printf("\nTranspose of matrix=");
for(i=0; i<4; i++)
{
printf("\n");
for(j=0; j<3; j++)
{
printf("%d", m[i][j]);
}
}
Output:
Input 4 integer data row-wise:10 20 30 40<enter>
Input 4 integer data row-wise:50 60 70 80<enter>
Input 4 integer data row-wise:90 100 110 120<enter>
Transpose of matrix=
10 50 90
20 60 100
30 70 110
40 80 120
Memory Allocation:
m[0][0] m[0][1] m[0][2] m[0][3]
m[1][0] m[1][1] m[1][2] m[1][3]
m[2][1] m[2][2] m[2][2] m[2][3]

From this program it is clear that we can use for loop to access every data of
one dimensional array.
इस ोाम से प ट है क हम एक !वमीय अरे के \येक डाटा को एOसैस करने के
&लए for लूप का उपयोग कर सकते है ।

Multidimensional array:
Collection of two dimensional array called three or multi dimensional array.
एक समान 1!व !वमीय अरे के समह
ू को JK !वमीय या बहू !वमीय अरे कहते ह।
Example:
int cube[3][3][3];

Initialization of array:
int cube[2][2][2]=
{{{10,20},{30,40}},{{10,20},{30,40}}};

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 52
C-Language Notes
String & string manipulation function:
Array of characters terminated by NULL(/0) is called string. String is
written in "---". String is used to store information.
कैरOटस- का अरे िजसके अंत मे NULL (/0) हो उसे ि oं ग कहते है । ि oं ग को "---"
माक- मे &लखते ह। ि oं ग का उपयोग जानका6रय को टोर करने के &लए करते ह।

Syntax:
char str-name[size];
Example:
char fname[15];
char lname[20];
Initialization of string:
char str[]={'L','R','s','i','r','\0'}; or
char str[]="LRsir";
Size automatically set by counting number of characters.
कैरOटस- कE सं_या के अनुसार साइज़ वत: सेट हो जाती है ।

Accessing string: (Read / Write)


1) Using %s in printf & scanf:
#include<stdio.h>
void main()
{
char fname[15];
printf("\nInput your name:");
scanf("%s", fname);
printf("\nYour name is %s", fname);
}
Output: Input your name: LRsir
Your name is LRsir
Remark: & mark never used and %s read or show only single word.
2) Using gets() and puts():
#include<stdio.h>
void main()
{
char fname[15];
puts("\nInput your name:");
gets(fname);
puts("\nYour name is");
puts(fname);

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 53
C-Language Notes
}
Output: Input your name: LRsir
Your name is LRsir
Remark: gets() and puts() can read or show more words.
String manipulation function:
We can perform a number of operations on string using following library
functions. Include string.h header file before using them.
न.न फंOशस के 1वारा ि oं ग पर !व&भन ऑपरे शन कए जा सकते ह। उपयोग से
पहले string.h है डर फ़ाइल को शा&मल करना अनवाय- है ।
1) strlen(str): returns number of characters
यह ि oं ग मे कुल कैरOटर कE सं_या को दान करता है ।
2) strlwr(str): converts all upper case into lower case
यह ि oं ग के बड़े अlर को छोटे अlर मे बदल दे ता है ।
3) strupr(str): converts all lower case into upper case
यह ि oं ग के छोटे अlर को बड़े अlर मे बदल दे ता है ।
4) strcpy(str2,str1): To copy str1 into str2.
str1 को str2 मे कॉपी करने के &लए।
5) strcmp(str1,str2): To compare str1 and str2. If equal then return 0
value.
ि oं ग str1 और str2 कE तुलना करने के &लए। एक जैसी होने पर यह 0 वैQयू
दान करता है ।
6) strcat(str1,str2): To merge str2 at the end of str1.
यह ि oं ग str2 को str1 के अंत मे जोड़ सकती है ।
Example: find length and copy string
#include<stdio.h>
#include<string.h>
void main()
{
char str1[]="www.LRsir.net",str2;
int size;
size=strlen(str1);
printf("\nSize of string=%d",size);
strcpy(str2,str1);
puts(str2);
}
Output: Size of string=9
LRsir.net

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 54
C-Language Notes
Structure:
Continuous collections of same or different data called structure. Structure
is one example of user defined data type of non primitive class.
एक समान अथवा अलग-अलग कार के Nमश: डाटा के समह
ू को oOचर कहते ह।
oOचर एक नॉन ी&म3टव यस
ू र Fडफ़ाइंड डाटा टाइप है ।

Working with structure: Need three syntaxes.


Syntax1: Creating structure template
struct sname
{
type member1;
type member2;
============
type member-n;
};
Syntax2: Declare structure variable
struct sname svar1, svar2,…,svar-n;

Syntax3: Accessing member: Using dot (.) period/membership operator


svar.member-name
svar means svar1, svar2, ..,svar-n
member-name means member1, member2,member-n.

Initialization of structure:
struct student
{
int id;
char name[15];
float fee;
}std={101,"Rahul",1500.5};
Or
struct student std={101,"Rahul",1500.5};
Memory allocation and size of structure:
Separate memory spaces allocated to every data members and size of one
structure variable is total sum of size of every data members.
oOचर के \येक डाटा मे.बस- के &लए थक-थक मेमोर8 पेस आवं3टत होता है और
एक oOचर वे6रयबल कE साइज़ उसके \येक डाटा मे.बस- कE कुल साइज़ के योग के
बराबर होती है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 55
C-Language Notes
Example: read and show two students record.
#include<stdio.h>
void main()
{
struct student
{
int id;
char name[15];
float fee;
};
struct student std1,std2;
printf("\nInput id, name and fee:");
printf("\nFor first record:");
scanf("%d%s%f",&std1.id,std1.name,&std1.fee);
printf("\nFor second record:");
scanf("%d%s%f",&std2.id,std2.name,&std2.fee);
printf("\nid\tname\tfee");
printf("\n%d\t%s\t%f",std1.id,std1.name,std1.fee);
printf("\n%d\t%s\t%f",std2.id,std2.name,std2.fee);
}
Output:
Input id, name and fee:
For first record:101 rahul 1000.5<enter>
For second record:102 priyanka 2000.5<enter>
id name fee
101 rahul 1000.5
102 priyanka 2000.5

Memory Allocation:
std1.id std1.name std1.fee std1.id std1.name std1.fee
101 rahul 1000.5 102 Priyanka 2000.5
2B 15B 4B 2B 15B 4B
Total size=21B Total size=21B

From this program it is clear that separate memory space allocated to every
data members and size is sum of sizes of each data member.
इस ोाम से प ट है क \येक डाटा मे.बस- के &लए अलग अलग पेस होता है
और एक oOचर कE साइज़ \येक डाटा मे.बस- कE साइज़ के योग के बराबर होती है ।

Array of Structure:
Continuous collections of same structure data type called array of structure.
For example records of 10 students, list of players.
एक समान oOचर डाटा टाइप के नरं तर कलेOशन को oOचर का अरे कहते ह। जैसे
10 टूडस का रे कॉड-, 11 /लेयस- कE &ल ट आ3द।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 56
C-Language Notes
Working with array of structure: Need three syntaxes.
Syntax1: Creating structure template
struct sname
{
type member1;
type member2;
============
type member-n;
};
Syntax2: Declare array of structure
struct sname sarr[size];

Syntax3: Accessing member: Using dot (.) period/membership operator


sarr[i].member-name
sarr[i] means sarr[0] to sarr[size-1]
member-name means member1, member2,member-n.

Memory allocation :
Continuous memory spaces allocated to all structures of array.
अरे के सभी oOचर को लगातार मेमोर8 पेस आवं3टत होता है ।
Example: read and show two students record.
#include<stdio.h>
void main()
{
struct student
{
int id;
char name[15];
float fee;
};
struct student std[5];
int i;
for(i=0; i<size;i++)
{
printf("\nInput id, name and fee:");
scanf("%d%s%f",&std[i].id,std[i].name,&std[i].fee);

}
printf("\nStudent records:");
printf("\nid\tname\tfee");
for{i=0;i<size;i++)
{
printf("\n%d\t%s\t%f",std[i].id,std[i].name,std[i].fee);
}
}
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 57
C-Language Notes
Output:
Input id, name and fee:101 rahul 1000.5<enter>
Input id, name and fee:102 nilesh 2000.5<enter>
Input id, name and fee:103 mukesh 3000.5<enter>
Input id, name and fee:104 rakesh 4000.5<enter>
Input id, name and fee:105 ritesh 5000.5<enter>
Student Records
id name fee
101 rahul 1000.5
102 nilesh 2000.5
103 mukesh 3000.5
104 rakesh 4000.5
105 ritesh 5000.5

Memory Allocation:
std[0].id std[0].name std[0].fee std[4].id std[4].name std[4].fee
101 rahul 1000.5 105 ritesh 5000.5

From this program it is clear that we can create and access a list of records
of same type using array of structure.
इस ोाम से प ट है क oOचर का अरे बनाकर एक ह8 कार के कई सारे रे कॉp-स
को एOसैस कया जा सकता है ।

Structure within structure :( nesting of structure)


When a structure variable is member of another structure then it is called
structure within structure.
जब एक oOचर का वे6रयबल अय oOचर का मे.बर ह तब इसे oOचर का oOचर
कहते ह

Syntax1: Creating structure template


struct sname
{
type member1;
type member2;
============
type member-n;
};
struct ssname
{
struct sname svar;
============
============
};
Syntax2: Declare variable of nesting structure
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 58
C-Language Notes
struct ssname ssvar;

Syntax3: Accessing member: use dot (.) operator two times


ssvar.svar.member-name

Example: read and show two students record.


#include<stdio.h>
void main()
{
struct address
{
int hno,
char colony[10];
char city[10];
};
struct student
{
int id;
char name[15];
float fee;
struct address la,pa;
};
struct student std;
printf("\nInput id, name and fee:");
scanf("%d%s%f",&std.id,std.name,&std.fee);
printf("\nlocal:Input hno, colony and city");
scanf("%d%s%s",&std.la.hno,std.la.colony,std.la.city);
printf("\nparmanent:Input hno, colony and city");
scanf("%d%s%s",&std.pa.hno,std.pa.colony,std.pa.city);

printf("\nid\tname\tfee");
printf("\n%d\t%s\t%f",std.id,std.name,std.fee);
printf("\nLocal Address:");
printf("\n%d,%s,%s",std.la.hno,std.la.colony,std.la.city);
printf("\nParmanent Address:");
printf("\n%d,%s,%s",std.pa.hno,std.pa.colony,std.pa.city);
}
Output:
Input id, name and fee:
101 rahul 1000.5<enter>
local:Input hno, colony and city
11 rishinagar Ujjain<enter>
parmanent:Input hno, colony and city
233 chanakypuri dilli<enter>
id name fee
101 rahul 1000.5
Local Address:
11,rishinagar,Ujjain
Parmanent address:
233,chanakypuri,Ujjain
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 59
C-Language Notes
Union:
It also shows continuous collections of same or different data but a common
memory space is available to all data of union. Union is one example of user
defined data type of non primitive class.

Union भी एक समान अथवा अलग-अलग कार के Nमश: डाटा के समह


ू को jयOत
तो करता है कतु यूनयन के सभी डाटा के &लए एक ह8 कॉमन मेमोर8 पेस उपल7ध
होता है । यू नयन एक नॉन ी&म3टव यस
ू र Fडफ़ाइंड डाटा टाइप है ।

Working with union: Need three syntaxes.


Syntax1: Creating union template
union sname
{
type member1;
type member2;
============
type member-n;
};
Syntax2: Declare union variable
union uname uvar1, uvar2,…,uvar-n;

Syntax3: Accessing member: Using dot (.) period/membership operator


uvar.member-name
uvar means uvar1, uvar2, ..,uvar-n
member-name means member1, member2,member-n.

Memory allocation and size of union:


A common memory space is allocated to every data members therefore last
one data assigns into accurate form. Size of one union variable is maximum
size of any data member.
यूनयन के \येक डाटा मे.बस- के &लए एक ह8 मेमोर8 पेस आवं3टत होता है अत:
केवल अंतम असाइन वैQयू ह8 शु¡ ?प से टोर रहता है । एक यूनयन वे6रयबल कE
साइज़ उसके सबसे बड़े डाटा मे.बर कE साइज़ के बराबर होती है ।

Example: read and show three different values using union.


#include<stdio.h>
void main()
{
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 60
C-Language Notes
union cif
{
char a;
int b;
float c;
};
union cif uv;
printf("\nInput character, integer and float value:");
scanf("%c%d%f",&uv.c,&uv.i,&uv.f);
printf("\ncharacter\tinteger\tfloat");
printf("\n%c\t%d\t%f",uv.a,uv.b,uv.c);
}
Output:
Input character, integer and float value: x 11 2.5<enter>
Character integer float
G G 2.5
G means garbach value.

Memory Allocation:
uv.a / uv.b / uv.c
2.5
size=4B(maximum)

From this program it is clear that a common memory space allocated to


every data members therefore only last assign value will be stores and size
is equal to float members because its size is maximum(4B).
इस ोाम से प ट है क \येक डाटा मे.बस- के &लए एक ह8 कॉमन मेमोर8 पेस
उपल7ध रहता है इस&लए एक यूनयन वे6रयबल म अंतम बार असाइन कया गया डाटा
ह8 बना रहता है । एक यूनयन कE साइज़ सबसे बड़े डाटा मे.बर जैसे float कE
साइज़(4B) के बराबर होती है ।

Enumeration:
It is one example of user defined data type of non primitive class. It is used
to specify word for integer constant. When we use such enumerated word in
program then at run time it will replace by respected integer value.
यह भी एक नॉन ी&म3टव यूसर Fडफ़ाइंड डाटा टाइप है । इसका उपयोग इंट8जर क टं ट
को कसी श7द से प6रभा!षत करने के &लए करते ह। जब ऐसे एनम
ु रे टेड श7द का
उपयोग ोाम कोड मे करते है तब रन टाइम पर वह संबeधत इंट8जर वैQयू से 6र/लेस
हो जाती है ।

Example: read integer value and show predefine day name.


#include<stdio.h>
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 61
C-Language Notes
void main()
{
enum week
{
sun,mon,tue,wed,thus,fri,sat
};
enum week day;
printf("\nInput integer value from 0 to 6:");
scanf("%d",&day);
switch(day)
{
case sun:
printf("Sunday");
break;
case mon:
printf("Monday");
break;
case tue:
printf("Tuesday");
break;
case wed:
printf("Wednesday");
break;
case thus:
printf("Thursday");
break;
case fri:
printf("Friday");
break;
case sat:
printf("Saturday");
break;
default:
printf("\nInvalid");
}
}
Output:
Input integer value from 0 to 6: 2 <enter>
Tuesday
In this program sun has 0, mon has 1 and so on values are assigned.
इस ोाम म sun कE 0, mon कE 1 एवं Nमश: वैQयू असाइन होती जाती है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 62
C-Language Notes

PART6
Function:
Need of function
Types of function
Library function
User defines function
Function Definition
A return statement
Function Declaration
Function calling
Function terminology:
Calling function v/s Called function
Actual argument v/s Formal arguments
Function calling technique:
Call by value or pass by value
Call by reference or pass by reference
Recursion
Command line argument
Local scope v/s Global scope
Lifetime of a variable
Static variable
Storage class
Identifier, Modifier, Qualifier, sizeof, typedef.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 63
C-Language Notes
Function:
A block that contains some logic code called function.
एक ऐसा 7लॉक िजसम लॉिजक कोड होते ह उसे फंOशन कहा जाता है ।
Ex: printf(), scanf(), clrscr(), getch() etc.
Need of function:
1) To avoid rewriting repetitive codes many times.
दोहराए जाने वाले कोड को बार बार &लखने कE आव`यकता नह8ं होती है ।
2) To break a large program into a number of blocks.
एक बहुत बड़े ोाम को छोटे छोटे 7लॉक म !वभOत कर सकते है ।

Types of function: C language supports two types of functions.


सी भाषा न.न दो कार के फंOशन को सपोट- करती ह।

1) Library function (predefined / inbuilt)


2) User define function
Library function: A number of functions those are defined in C compiler
with different uses called library function. They are defined in object
(machine) code and declared in header files therefore we need to include
associated header file before using them.
सी कंपाइलर म कई सारे फंOशन !व&भन उपयोग के &लए पहले से ह8 प6रभा!षत रहते
ह िजह लाइेर8 फंOशन कहा जाता है। ऐसे फंOशन ऑ7जेOट(मशीन कोड) मे
प6रभा!षत रहते ह और है डर फ़ाइल म FडOलैर होते ह इस&लए इनका उपयोग करने से
पहले संबिधत है डर फ़ाइल को शा&मल करने कE आव`यकता होती है।

Example: printf(), scanf(), getchar(), putchar(), gets() and puts() are


declared in stdio.h header fule. getch(), getche(), putch() and clrscr() are
declared in conio.h header file.
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("\nInput an integer number:");
scanf("%d",&a);
printf("\nYour input %d",a);
getch();
}
Output: Input an integer number:22
You input 22

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 64
C-Language Notes
User defines function: A function that define and declared by user called
user defined function.
एक ऐसा फंOशन िजसे यूसर वयं प6रभा!षत और FडOलैर करता है, उसे यूसर Fडफ़ाइंड
फंOशन कहते ह।
Example:
void show()
{
printf("\nwww.LRsir.net/download ebooks");
}
Here show() is user defined function.
यहाँ show() एक यूसर प6रभा!षत फंOशन है ।

Working with user defined function:


We needs following three things when working with function.
जब फंOशन के साथ काय- करना हो तब हम न.न तीन चीज कE आव`यकता होती है ।
1) Function definition
2) Function declaration
3) Function calling
Function Definition: A block that contains executable code called function
definition. Function definition has following characteristics.
एक ऐसा 7लॉक िजसम Nयािवत होने वाले कोड को &लखा जाता है उसे प6रभा!षत
फंOशन कहते ह। प6रभा!षत फंOशन म न.न !वशेषताएँ होती है।

a) Function type / returning type: specify which type of value return


by a function.
फंOशन 1वारा कस कार कE वैQयू को लोटाया जाना है उसके कार को
प6रभा!षत करता है ।
b) Function name: user define name. One program has a number of
function with unique name.
यूसर 1वारा प6रभा!षत फंOशन का नाम। एक ोाम म अ1!वतीय नाम के कई
सारे फंOशन हो बनाए जा सकते ह।
c) List of arguments / parameters: To receive nil or more values from
outsources.
शूय या कई सार8 वैQयू को बाहर8 Kोत से ा/त करने के &लए।
d) Code block: contains actual logic code.
इसमे वा त!वक लॉिजक कोड को &लखते ह।
e) A return statement: used to return nil or one value.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 65
C-Language Notes
यह अeधकतम एक वैQयू को लोटता है ।
Syntax:
ftype fname(type1 arg1, type2 arg2,..)
{
Code block
return value/expression;
}
Here
ftype=function type / return type (void / int / char / float etc)
fname= user define function name(show / factorial etc.)
type1, type2,…= data type of arguments
arg1, arg2,…= formal argument to hold outsource values
Code block= Logic code
return= Return program control with nil or one resulting value.
Example:
int sum(int x, int y)
{
int z;
z=x+y;
return z;
}

Function Declaration: A single line of statement that tells about function


definition to the compiler called function declaration or function prototype.

एक लाइन का टे टम ट जो फंOशन के बार मे कंपाइलर को सeू चत कर सके उसे फंOशन


FडOलैरशन या फंOशन ोटोटाइप कहते ह।

Syntax:
ftype fname(type, type,..,type);
Example:
int sum(int, int);
It is declared before function calling.
इसे फंOशन को कॉल करने से पहले FडOलैर कया जाता है ।

Function calling: A statement that uses function by passing values


(arguments) and receives returning value called function calling or function
call.
एक ऐसा टे टम ट िजसके 1वारा फंOशन म वैQयूस (आगम
ु-  स) को पास करते ह और
6रटन- होने वाल8 को ा/त करते ह उसे फंOशन कॉ&लंग या फंOशन कॉल कहते ह।

Syntax: var=fname(value1, value2,..,value-n);

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 66
C-Language Notes
var= a variable that store returning value by function.
Example:
c=sum(10,20);
c=sum(5,2);
function calling can be made one or more times as per our requirements.
फंOशन कॉ&लंग को आव`यकतानस
ु ार एक या अeधक बार उपयोग म ला सकते ह।
A complete program that uses library functions as well as user
defines function:
#include<stdio.h>
#include<conio.h>
int sum(int,int); //function declaration
void main()
{
int a,b,c;
clrscr();
printf("\nInput two numbers:");
scanf("%d%d", &a, &b);
c=sum(a,b); //function calling
printf("\nResult=%d", c);
getch();
}
//function definition
int sum(int x, int y)
{
int z;
z=x+y;
return z;
}
Output: Input two numbers: 10 20 <enter>
Result=30
Function terminology:
1) Calling function v/s Called function: When function1 call another
function2 then function1 known as calling function and function2
known as called function. In above example, main() is calling
function whereas sum() is called function.
जब फंOशन1 कसी अय फंOशन2 को कॉल करता है तब फंOशन1 को कॉ&लंग
फंOशन कहते ह और फंOशन2 को कॉQड फंOशन कहते ह। उपरोOत उदाहरण म
main() कॉ&लंग फंOशन है जबक sum() कॉQड फंOशन है ।
2) Actual argument v/s Formal arguments: Passing arguments of
calling functions called actual arguments whereas receiving
arguments of called function called formal arguments. In above
example, <a,b> of main() are called actual arguments and <x,y> of
sum() called formal arguments.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 67
C-Language Notes
कॉ&लंग फंOशन के पा&संग आगम
ु-  स को एOचअ
ु ल आगम
ु-  स कहते ह जबक
कॉQड फंOशन के 6रसी!वंग आगम
ु-  स को फॉम-ल आगम
ु-  स कहते ह। उपरोOत
उदाहरण म main() फंOशन के <a,b>, एOचअ
ु ल आगम
ु-  स ह और
sum()फंOशन के <x,y> फॉम-ल आगम
ु-  स है ।

Function calling technique:


Arguments can be pass in following two techniques.
आगम
ु-  स को न.न दो तर8के से पास कया जा सकता है ।
1) Call by value or pass by value
2) Call by reference or pass by reference (reference / address / pointer)
Call by value: In this technique, calling function pass values of actual
arguments to the formal arguments of called function. When formal
arguments are changes then values of actual argument never changes.
इस तर8के म कॉ&लंग फंOशन एOचअ
ु ल आगम
ु- स कE वैQयूस को कॉQड फंOशन के
फॉम-ल आगम
ु-  स म पास करता है । जब फॉम-ल आगम
ु-  स म कोई बदलाव होता है तब
एOचअ
ु ल आगम
ु-  स कE वैQयूस कभी भी प6रवत-त नह8ं होती है ।
Example: Swapping of two numbers using call by value
#include<stdio.h>
void swap(int, int);
void main()
{
int a=10,b=20;
printf("\nBefore swapping a=%d\tb=%d",a,b);
swap(a,b);
printf("\nAfter swapping a=%d\tb=%d",a,b);
}
void swap(int x, int y)
{
int t;
t=x;
x=y;
y=t;
}
Output:
Before swapping a=10 b=20
After swapping a=10 b=20
main(): a b Actual Arguments
Calling function 10 20 Values
1000 2000 Addresses
Pass values
swap(): x y Formal argument
Called function 10 20 20 10 Values
3000 4000 Addresses
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 68
C-Language Notes
We observe that values of a and b do not changed because formal arguments
x and y are copy of actual arguments a and b.
उपरोOत ोाम से यह नyकष- नकलता है क a एवं b कE वैQयूस मे कोई प6रवत-न
ु-  स x एवं y दोन एOचअ
नह8ं होता है OयूंकE फॉम-ल आगम ु-  स a और b कE
ु ल आगम
कॉपी है ।

Call by reference: In this technique, calling function pass addresses of


actual arguments to the formal arguments of called function. When formal
arguments are changes then values of actual arguments always changes.
इस तर8के म कॉ&लंग फंOशन एOचअ
ु ल आगम
ु-  स के एqैस को कॉQड फंOशन के फॉम-ल
आगम
ु-  स म पास करता है । जब फॉम-ल आगम
ु-  स म कोई बदलाव होता है तब
एOचअ
ु ल आगम
ु-  स कE वैQयस
ू ह8 हमेशा प6रवत-त होती है।
Example: Swapping of two numbers using call by value
#include<stdio.h>
void swap(int*, int*);
void main()
{
int a=10,b=20;
printf("\nBefore swapping a=%d\tb=%d",a,b);
swap(&a,&b);
printf("\nAfter swapping a=%d\tb=%d",a,b);
}
void swap(int *x, int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
Output:
Before swapping a=10 b=20
After swapping a=20 b=10
main(): a b Actual Arguments
Calling function 10 20 20 10 Values
1000 2000 Addresses
Pass address
swap(): X Y Formal argument
Called function 1000 2000 Addresses of a and b
3000 4000 Addresses
We observe that values of a and b becomes changed because formal
arguments x and y are addresses of actual arguments a and b.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 69
C-Language Notes
उपरोOत ोाम से यह नyकष- नकलता है क a एवं b कE वैQयूस मे ह8 हमेशा
ु-  स x एवं y दोन एOचअ
प6रवत-न होता है OयूंकE फॉम-ल आगम ु-  स a और b
ु ल आगम
के एqैस को होQड कए हुये है ।

Recursion
When a function call own self called recursion. In this process calling
function and called function both are same.
जब कोई फंOशन वयं को ह8 कॉल करे तब इसे 6रकरसन कहते ह। इस Nया म
कॉ&लंग फंOशन और कॉQड फंOशन दोन एक ह8 फंOशन होते ह।

Example: factorial of number using recursion n!=n*(n-1)!


#include<stdio.h>
long factorial(int);
void main()
{
long fact;
printf("\nInput number:");
scanf("%d",&n);
fact=factorial(n);
printf("\nResult=%ld",fact);
}
long factorial(int n)
{
int f;
if(n==1)
{
return result;
}
else
{
f=n*factorial(n-1);
return f;
}
}
Output: Input number:4 <enter>
Result=24
factorial() is a recursive function because it has a statement that call own
self. This process will be continues until n == 1.
factorial() एक 6रकर&सव फंOशन है OयूंकE इसका एक टे टम ट वयं को कॉल करता
है । यह ोसैस n==1 होने तक नरं तर बनी रहती है ।

Demerits:
1) Slow down execution. Nयावयन कE गत को धीमी कर दे ता है ।
2) Consume large memory space. मेमोर8 पेस अeधक लगती है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 70
C-Language Notes
Command line argument:
Arguments of main() function called command line arguments.
main() फंOशन के आगम
ु-  स को कमांड लाइन आगम
ु-  स कहा जाता है ।
Syntax:
void main(int argc, char *argv[])
{
Code block
}
argc and argv called command line arguments.
argc और argv को कमांड लाइन आगम
ु-  स कहते ह।
argc=total arguments at command line.
कमांड लाइन पर कुल आगम
ु-  स कE सं_या
argv=pointer to strings at command line
कमांड लाइन पर ि oं Xस का पाइंटर
Example:
#include<stdio.h>
void main(int argc, char *argv[])
{
int i;
printf("\nTotal arguments at command line=%d",argc);
printf("\nArguments are:");
for(i=0;i<argc;i++)
{
printf("%s\t",argv[i]);
}
}
Running process:
Step1: save cmdline.c in c:\tc\bin\
Step2: Compile (alt+F9) then make exe file(F9).
Step3: Search cmdline.exe in c:\tc\ and copy into c:\ drive
Step4: open command prompt and move to c:\ drive
Step5: C:\cmdline.exe LRsir.net <enter>
Output:
Total arguments at command line=2
Arguments are: C:\cmdline.exe LRsir.net

Local scope v/s Global scope:


Local scope: When variable / array / structure / pointer / function are
declared inside function body then they are usable to only that function
called local scope of that one.
जब वे6रयबल / अरे / oOचर / पॉइंटर / फंOशन को फंOशन बॉडी के अंदर FडOलैर
कया जाता है तब इनका उपयोग केवल उसी फंOशन मे कया जा सकता है इसे ह8
लोकल कोप कहा जाता है ।
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 71
C-Language Notes
Global scope: When variable / array / structure / pointer/ function are
declared outside all the function at top most places then they are usable to
all functions of program called global scope of that one.

जब वे6रयबल / अरे / oOचर / पॉइंटर / फंOशन को फंOशन बॉडी के बाहर, सबसे ऊपर
FडOलैर कया जाता है तब इनका उपयोग ोाम के फंOशन कर सकते है, इसे ह8
Xलोबल कोप कहा जाता है ।

#include<stdio.h>
int x=10; //global
void fun1(); //global
void main()
{
int y=5; //local
printf("\nmain:Local %d",y);
printf("\nmain:Global %d",x);
fun1();
}
void fun1()
{
int y=22; //local
printf("\nfun1:Local %d",y);
printf("\nfun1:Global %d",x);
}
Output:
main: Local 5
main: Global 10
fun1: Local 22
fun1: Global 10

Lifetime of a variable: How long a variable alive throughout the


program execution called lifetime. Lifetime of global variable is
unlimited(begins to end) while lifetime of local variable is limited(until
program control resides in that function except static variable)

ोाम मे कोई वे6रयबल Nयावयन के समय कब तक जी!वत बना रहता है उसे ह8


लाइफटाइम कहते ह। Xलोबल वे6रयबल का का लाइफटाइम असी&मत होता है अथा-त
ार.भ से आटा तक जबक टे 3टक को छोड़कर सभी लोकल वे6रयबल का लाइफटाइम
सी&मत होता है अथा-त जब तक ोाम कंoोलर उस फंOशन मे मौजूद है ।

In above program, lifetime of x has unlimited whereas y has limited.

इस ोाम मे, x का लाइफटाइम असी&मत है जबक y का सी&मत।


Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 72
C-Language Notes
Static variable:
A variable that declare using static keyword called static variable.
एक ऐसा वे6रयबल जो static कEवड- का उपयोग कर FडOलैर करते ह उसे टे 3टक
वे6रयबल कहते ह।
Syntax: static type var;
Example: static int x;
Characteristics:
1) Default value is 0
इसकE Fडफ़ाQट वैQयू 0 होती है ।
2) Declare one time whether that function call many times.
एक ह8 बार FडOलैर होता है चाहे फंOशन को कतनी बार भी कॉल कर ।
3) It has local scope i.e. unknown by other function.
इसका कोप लोकल होता है अथा-त अय फंOशन के &लए अnात रहता है।
4) Used to store a value when come back to same function.
उसी फंOशन पर फर से लोटने पर !पछल8 वैQयू को उपयोग मे ले सकते ह।
Example: Count calling a function
#include<stdio.h>
void fun();
void main()
{
fun();
fun();
fun();
}
void fun()
{
static int x;
int y=0;
printf("\nx=%d\ty=%d", ++x, ++y);
}
Output:
x=1 y=1
x=2 y=1
x=3 y=1
Since x is a static therefore default value is 0 and always persist last assign
value whereas y is non static therefore we assign 0 and never persist last
assigned value because it create every time when a function calls.
चूं क x एक टे 3टक है इस&लए Fडफ़ाQट वैQयू 0 है और इसमे हमेशा अंतम बार टोर
कE गई वैQयू बनी रहती है जबक y एक नॉन टे 3टक है इस&लए अलग से 0 को
असाइन करते ह और यह अंतम बार टोर कE गई वैQयू कभी भी बनी नह8ं रहती
Oयंक
ू E \येक फंOशन कॉल पर यह न&म-त होता है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 73
C-Language Notes
Storage class:
Storage classes of variable are used to set following properties.
वे6रयबल कE टोरे ज Oलासेस का उपयोग उसमे न.न गुण को सेट करने के &लए करते
ह।
Memory: RAM / CPU Register
1)
Default value: 0 or garbage
2)
Scope: Local / Global
3)
Lifetime: Limited (until program control inside the function) /
4)
Unlimited (until program execution).
C supports four storage classes. There name, keywords, properties and
examples are following.
सी भाषा चार कार के टोरे ज Oलासेस को सपोट- करती ह। इनके नाम, केयवोड-, गुण
और उदाहरण न.न है ।
S Storage class Properties
N Memory Default scope lifetime
value
1 auto(default) RAM Garbage Local limited
Ex:
auto int x; or
int x;
2 register CPU Garbage Local limited
Ex: Register
register int x;
3 static RAM 0 Local Unlimited
Ex: after
static int x; creates
4 extern RAM Garbage global unlimited
Ex:
extern int x;

Identifier, Modifier, Qualifier, sizeof, typedef


Identifier: All user defined words called identifier. It includes name of
variable, function, array, structure and label.
यूसर 1वारा प6रभा!षत सभी श7द को identifier कहा जाता है । इसम वे6रयबल, लेबल,
फंOशन, अरे , oOचर, यू नयन, एनम
ु ारे शन आ3द का नाम शा&मल है ।
Rules for identifier: (ex: variable)
1) Maximum character length is 8.
2) Allowed characters are a-z, A-Z, 0-9 and _ (underscore only).
3) Digit can’t be used at first position.
4) Keywords are not allowed.
5) Blank space or comma not allowed.
Ex: Valid- principl, X_Y, a1, INT
Invalid- principle, X-Y, 1a, int, x y.
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 74
C-Language Notes
Modifier: Keywords which are used to change meaning of basic data type
called modifier.
सी भाषा के ऐसे कवp-स िजनका उपयोग आधारभूत डाटा टाइप म बदलाव करने के
&लए कर सके उहे modifier कहा जाता है ।

Ex: short, long, double, signed, unsigned


short int or int: for 2 byte space
long int or long: for 4 byte space
double float or double: for 8 byte space double
signed: to allow + or – data (default)
unsigned: to allow only + data
Qualifier: const keyword called qualifier and it is used to specify constant
value to a word so that it never changed during program execution.
const कEवड-, एक qualifier है िजसका उपयोग कसी श7द को constant बनाने के
&लए करते ह िजससे क वह ोाम के Nयावयन के समय बदल नह8ं सके।
syntax: const type cname=value;
Example:
Example: area and circumference of circle.
#include<stdio.h>
void main()
{
const float pi=3.14;
float r=10,a,c;
a=pi*r*r;
c=2*pi*r;
printf("\nArea=%f",a);
printf("\nCircumference=%f",c);
}
sizeof: It is a keyword used to find memory size in butes for any data type /
variable.
इस कEवड- का उपयोग कसी भी डाटा टाइप अथवा वर8यबल के साइज़ बाइट मे nात
करने के &लए कया जाता है ।

Syntax: sizeof(type / var)


Example:
#include<stdio.h>
void main()
{
struct student
{
int id;
char name[15];
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 75
C-Language Notes
float fee;
};
int bytes;
bytes=sizeof(struct student);
printf("\nSize of student data type=%dB",bytes);
}
Output:
Size of student data type=21B

typedef: It is a keyword used to renaming any data type for current


program..
इस कEवड- का उपयोग कर ट ोाम मे कसी भी डाटा टाइप को 6रनेम करने के &लए
करते है ।

Syntax: typedef type newname;


Example:
#include<stdio.h>
typedef int integer
void main()
{
integer n=10; //valid
printf("\nValue=%d",n);
}
Value=10
int renamed integer using typedef for this program.
इस ोाम के &लए int टाइप को integer मे typedef कEवड- 1वरा 6रनेम कर 3दया
है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 76
C-Language Notes

PART7
Pointer
Pointer operator
Advantages of pointer
DMA:
malloc()
calloc()
realloc()
free()
Pointer to pointer
Pointer Arithmetic
Pointer of array
Array of Pointer
Pointer of string
Passing pointer of data
Passing array to function
Returning pointer from function
Pointer of function
Pointer of structure
Self Referencial Structure
Passing structure to function
Returning structure from function
Copy all members of structure into another.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 77
C-Language Notes
Pointer:
Memory address of data is called pointer.
डाटा के मेमोर8 एqैस को ह8 पोइंटर कहा जाता है ।
Ex: int a=10;
a Variable
10 Value
1000 Address
Pointer variable: Base address of data can be hold to a pointer variable.
डाटा के ारि.भक एqैस को पोइंटर वे6रयबल म होQड कर सकते है ।
Syntax: ptrtype *ptr;
ptrtype= int / float / char etc.
ptr = pointer variables.
Example: int *ptr;
Pointer variable can hold only address of that data type. It means integer
pointer variable can hold only address of integer data.
पोइंटर वे6रयबल केवल उसी के कार डाटा का एqैस होQड कर सकता है । इसका अथ-
यह है क इंट8जर पोइंटर वे6रयबल केवल इंट8जर डाटा का ह8 एqैस होQड करता है ।

Pointer operator: There are following two operators.


पोइंटर ऑपरे टर न.न दो कार के होते है ।
1) & (Address of): This unary operator returns memory address of
variable.
यह यन
ू ेर8 ऑपरे टर वे6रयबल के एqैस को 6रटन- करता है ।
Syntax: &var

2) * (Value at address): This unary operator returns value at given


address.
यह यन
ू ेर8 ऑपरे टर 3दये गए एqैस पर टोर वैQयू को 6रटन- करता है ।
Syntax: *ptr
Example: find value using pointer variable.
#include<stdio.h>
void main()
{
int a=10,b;
int *p;
p=&a;
printf("\nAddress of a=%u", &a);
printf("\nValue of a=%d", *p);
}
Output:
Address of a= 1000(Assumed)
Value of a=10
%u for unsigned address and %d for unsigned address.
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 78
C-Language Notes
Advantages of pointer:

1. We can modify value local variable using other function.


हम लोकल वर8यबल कE वैQयू को अय फंOशन म प6रवत-त कर सकते है ।
2. We can return logically more than one value from any function.
एक फंOशन के 1वारा एक से अeधक वैQयूस को लॉिजकल8 6रटन- कर सकते है ।
3. Pass whole array into function of any size.
कसी भी साइज़ के स.पूण- अरे को अय फंOशन मे पास कर सकते है ।
4. Pass all structure data to function.
oOचर के सभी डाटा को फंOशन मे पास कर सकते है ।
5. Pass entire function to another.
एक फंOशन को ह8 अय फंOशन मे पास कर सकते ह।

DMA (Dynamic Memory Allocation):


At runtime we can allocate, extends and de allocate memory spaces for data
called dynamic memory allocation. In C language, following library
functions of alloc.h header file are used called DMA function.

रन टाइम पर हम डाटा के &लए मेमोर8 पेसेस को आवं3टत, !व ता6रत एवं र¢ कर


सकते ह, इसे ह8 डाइनै&मक मेमोर8 अलोकेशन कहते ह। सी भाषा म alloc.h है डर फ़ाइल
के न.न लाइर8 फंOशन का उपयोग करते ह, िजहे DMA फंOशन कहा जाता है ।

1) malloc()
2) calloc()
3) realloc()
4) free()

malloc(): In this DMA function, we pass one argument for required number
of bytes and it return pointer of allocates space. Default values of allocated
spaces are garbage.

इस DMA फंOशन म हम &सफ- एक ह8 आगम


ु-  ट को बाइट कE सं_या के &लए पास
करते है और यह पेस का आवंटन कर उसका पोइंटर 6रटन- कर दे ता है । आवं3टत
पेसेस कE Fडफ़ाQट वैQयू गारबेज होती है ।

Syntax: type *ptr = (type*)malloc(bytes);


Example: int *pi= (int *)malloc(5*2); //for 5 integer data
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 79
C-Language Notes
calloc(): In this DMA function, we pass two arguments; first for number of
required data and second for size of each data. Default values of allocated
spaces are zero.

ु-  स पास करते ह; पहला आव`यक डाटा कE सं_या


इस DMA फंOशन म हम दो आगम
और दस
ू रा \येक डाटा कE साइज़। आवं3टत पेसेस कE Fडफ़ाQट वैQयू ज़ीरो होती है ।

Syntax: type *ptr = (type*)calloc(n,size);


Example: int *pi= (int *)calloc(5,2); //for 5 integer data

realloc(): This is used to extend number of bytes that was allocated by


malloc().

इसका उपयोग malloc() 1वारा आवं3टत मेमोर8 पेस मे बाइट कE सं_या का और


अeधक !व तार करने के &लए करते ह।

Syntax: type *ptr = (type*)realloc(ptr,bytes);


Example: pi= (int *)realloc(pi,6); //for 3 integer data

free(): It is used to release memory space allocated by malloc() or calloc().

इसका उपयोग malloc() या calloc() 1वारा आवं3टत मेमोर8 पेस को मुOत करने के
&लए करते ह।

Syntax: free(ptr);
Example: free(pi); // release integer data

Pointer to pointer:
A pointer that holds address of lower level of pointer called pointer to
pointer.
एक ऐसा पोइंटर जो उससे न.न तर के पोइंटर को होQड करके रखता है उसे पोइंटर टु
पोइंटर कहते ह।
Example:
#include<stdio.h>
void main()
{
int a,*p,**pp;
a=10;
p=&a;
pp=&p;
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 80
C-Language Notes
printf("\nValue of a= %d", **pp);
}
Output: Value of a=10

a p pp
10 1000 2000
1000 2000 5000
Value of pp = address of p
Value of p = address of a
Value of a = 10
Thus **pp**2000(address of p)*1000(address of a)=10.
***ppp called pointer to pointer to pointer. We can increase pointer level as
per requirement.
***ppp को पोइंटर टु पोइंटर टु पोइंटर कहते ह। इसकार आव`यकतानस ु ार पोइंटर के
लेवेल को बड़ाया जा सकता है ।

Pointer Arithmetic:
Arithmetic operators are used to move from current address to another.
Following arithemetic operators can be appy on pointers.

अ6रथमे3टक ऑपरे टर के 1वारा कर ट एqैस से कसी अय एqैस पर जया जा सकता है ।
न.न अ6रथमे3टक ऑपरे टर का ह8 उपयोग पोइंटर के साथ कर सकते ह।

1) ptr + n : returns address of next nth location


अगल8 nth लोकेशन का एqैस दान करता है
2) ptr – n : returns address of previous nth location
!पछल8 nth लोकेशन का एqैस दान करता है
3) ptr1-ptr2 : number of locations between two addresses
दो एqैस के बीच कुल लोकेशन कE सं_या दान करता है ।
4) ++ptr/ptr++ : update current address by next address
कर ट एqैस को अगले एqैस से अपडेट करता है ।
5) --ptr/ptr-- : update current address by previous address
कर ट एqैस को !पछले एqैस से अपडेट करता है ।
6) ptr+=n : update current address by next nth address
th
कर ट एqैस को अगले n एqैस से अपडेट करता है ।
7) ptr-=n : update current address by previous nth address
th
कर ट एqैस को !पछले n एqैस से अपडेट करता है ।
Example:
#include<stdio.h>
void main()
{
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 81
C-Language Notes
int a=10,*ptr;
ptr=&a;
printf("\nCurrent address=%u", ptr);
printf("\nNext address=%u", ptr+1);
printf("\nPrevious address=%u", ptr-1);
}
Output:
Current address=2000 (assumed)
Next address=2002 (because int has 2 byte)
Previous address=1998

Pointer of array:
Base address of an array is called pointer of array. We can access all data
of array using pointer of array.

कसी भी अरे के बेस एqैस को अरे का पोइंटर कहा जाता है । अरे के पोइंटर कE सहायता
से उसके सभी डाटा को एOसैस कर सकते ह।

Example:

#include<stdio.h>
void main()
{
int a[5],i;
int *p;
p=&a;
printf("\nInput 5 integers:");
for(i=0;i<5;i++)
{
scanf("%d",&p[i]);
}
printf("\nArray data using pointer:");
for(i=0; i<5;i++)
{
printf("%d\t",p[i]);
}
}
Output: Input 5 integers: 10 20 30 40 50 <enter>
Array data using pointer: 10 20 30 40 50
Memory Allocation:
p a[0] a[1] a[2] a[3] a[4]
1000 10 20 30 40 50
1000 1002 1004 1006 1008

Remark:
1. a or &a[0] both are same that is base address of array
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 82
C-Language Notes
a या &a[0] दोन का अथ- एक मतलब एक जैसा ह8 है अथा-त अरे का बेस एqैस
दान करना ।
2. p[i] or *(p+i) both are same that access data of array
p[i] या *(p+i) दोन एक जैसे ह जो अरे के डाटा को एOसैस करते है ।

Array of Pointer:
An array in which we can hold addresses of data called array of pointer.
Using this feature we can access all data that are not store continuously in
memory.

एक ऐसा अरे िजसमे कई सारे डाटा के एqेसेस को होQड कर सके उसे ह8 पोइंटर का अरे
कहते ह। इस गुण के 1वारा हम ऐसे सभी डाटा को एक ह8 Nम मे एOसैस कर सकते है
जो अलग अलग मेमोर8 एqैस पर ि थत है ।

Example:

#include<stdio.h>
void main()
{
int a,b,c;
int p[3];
int i;
p[0]=&a;
p[1]=&b;
p[2]=&c;
printf("\nInput 3 integers:");
for(i=0;i<3;i++)
{
scanf("%d",p[i]);
}
printf("\ndata using array of pointer:");
for(i=0; i<3;i++)
{
printf("%d\t",*p[i]);
}
}
Output: Input 3 integers: 10 20 30 <enter>
data using array of pointer: 10 20 30
Memory Allocation:
p[0] p[1] p[2] a b C
1000 2000 3000 10 20 30
1000 2000 3000

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 83
C-Language Notes
Here p is array of pointer in which we first assign address of random places
data then access continuously using loop.
यहाँ p एक पोइंटर का ह8 अरे है िजसमे हमने सबसे पहले अलग अलग थान के डाटा
को असाइन कया फर लूप के 1वारा Nम से एOसैस कर &लया।

Pointer of string: (string pointer)


Base address of string is called pointer of string. using pointer of string, we
can copy one string into another by = (assignment operator) and perform
many operations.

कसी भी ि oं ग के बेस एqैस को ि oं ग का पोइंटर कहा जाता है । इसके उपयोग से एक


ि oं ग को अय ि oं ग मे = (असाइनम ट ऑपरे टर) के 1वारा कॉपी कर सकते ह। इसके
अलावा और भी ि oं ग ऑपरे शन सरलता से कए जा सकते है ।

Example:

#include<stdio.h>
void main()
{
char *str1, *str2;
printf("\nInput your name:");
scanf("%s",str1);
str2=str1;
printf("\n%s\t%s",str1,str2);
}
Output: Input your name: LRsir.net <enter>
LRsir.net LRsir.net
Memory Allocation:
str1 str2
1000 1000 L R s i r . n e t \0
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

Thus while coping, whole string never copied, only base address.
अत: कॉपी करते समय पूर8 ि oं ग कॉपी नह8ं होती, &सफ- बसे एqैस कॉपी होता है ।

Passing pointer of data: See call by reference


Passing array to function:
By passing base address and size of array, we can pass whole array to
function.
अरे के बेस एqैस और साइज़ को कसी फंOशन मे पास कर स.पूण- अरे को पास कर
सकते ह।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 84
C-Language Notes
Example:
#include<stdio.h>
Void passarray(int[], int);
void main()
{
int a[5],i;
printf("\nInput 5 integer numbers:");
for(i=0;i<5;i++)
{
scanf("%d",&a[i]);
}
passarray(a,5); //passing whole array
}

void passarray(int p[],int n)


{
int i;
printf("\nPassing Array data are:");
for(i=0;i<n;i++)
{
printf("%d\t",p[i]);
}
}
Output: Input 5 integer numbers:10 20 30 40 50<enter>
Passing Array data are:10 20 30 40 50

Here p[] is formal argument of passarray() function that hold base address
of passing array and n is size of array used for ending of array. In function if
we made any changes in array data then this changes made on actual array.
यहाँ passarray() एक फंOशन है िजसका फॉम-ल आगम
ु-  ट p[], पास कए गए अरे के
बेस एqैस को होQड करता है और n एक इंटेगर वे6रयबल है िजसके 1वरा अरे कE साइज़
को टोर करते है । फंOशन मे य3द अरे मे कोई बदलाव कया जाए तो वह वा तव मे
एOचअ
ु ल अरे पर ह8 होगा।

Returning pointer from function:


When address of a variable returns from a function then such variable
should be static because static variable persist value after removing program
controller from that function.
जब फंOशन से उसके कसी वे6रयबल के एqैस को 6रटन- कया जाता है तो ऐसा
वे6रयबल टे 3टक होना चा3हए Oयंक
ू E ोाम कंoोलर के फंOशन से हटते ह8 केवल
टे 3टक वे6रयबल मे वैQयू बनी रहती है।
Example:
#include<stdio.h>
int* preturn();

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 85
C-Language Notes
void main()
{
int *p;
p=preturn();
printf("\n:Value of other function=%d", *p);
}
int* preturn()
{
static int a;
printf("\ninput any integer value:");
scanf("%d",&a);
return &a; //return address
}
Output: input any integer value:212 <enter>
Value of other function=212

Here preturn() function returns address of static variable a (&a) therefore


function type is int*.
यहाँ preturn() फंOशन टे 3टक वे6रयबल a का एqैस (&a) को 6रटन- करता है इस&लए
फंOशन का टाइप int* है ।

Pointer of function:
When a function loads then it has also base address called pointer of
function. We can hold address of any function then call them by pointer.
जब कोई फंOशन लोड होता है तब उसका भी एक बेस एqैस होता है िजसे फंOशन का
पोइंटर कहा जाता है। हम कसी भी फंOशन के एqैस को होQड कर उसके पोइंटर 1वरा
फंOशन को कॉल कर सकते ह।
Example:
#include<stdio.h>
Void show();
void main()
{
void *ptrfun();
ptrfun=show;
ptrfun();
show();
}
void show()
{
printf("\nLRsir.net");
}
Output:
LRsir.net
LRsir.net

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 86
C-Language Notes
Here ptrfun is a pointer of function. It can hold address of any function
which has void type and without arguments like show. Only Function name
gives its address.
यहाँ ptrfun एक फंOशन का पोइंटर है । यह कसी भी ऐसे फंOशन का एqैस होQड कर
सकता है िजसका फंOशन टाइप void हो और आगम
ु-  ट नह8ं हो जैसे show। केवल
फंOशन का नाम उसके एqैस को दान करता है ।

Pointer of structure:
Base address of a structure variable is called pointer of structure. We can
access all member of structure using pinter of structure.

कसी भी oOचर के बेस एqैस को oOचर का पोइंटर कहा जाता है । oOचर के पोइंटर
कE सहायता से उसके सभी डाटा मे.बस- को एOसैस कर सकते ह।

Example:

#include<stdio.h>
struct student
{
int id;
char name[15];
float fee;
};
void main()
{
struct student std;
struct student *ptr;
ptr=&std;
printf("\nInput id, name and fee:");
scanf("%d%s%f",&ptr->id,ptr->name,&ptr->fee);
printf("\nStudent records:");
printf("%d\t%s\t%f",ptr->id,ptr->name,ptr->fee);
}
Output:
Input id, name and fee:101 rahul 1000.5<enter>
Student records:101 rahul 1000.5

Memory Allocation:
ptr std.id std.name std.fee
1000 101 rahul 1000.5
1000 1002 1017
Remark:
1. ptr is pointer of structure that hold only base address of student
structure type. (invaild for other structure type)
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 87
C-Language Notes
ptr एक oOचर का पोइंटर है जो &सफ- टूडट oOचर टाइप के बेस एqैस को
होQड करे गा अथा-त कसी अय oOचर टाइप के &लए अमाय होता है ।
2. -> is called reference operator that is apply with pointer of structure to
access members.
-> एक 6रdेस ऑपरे टर है िजसे केवल oOचर के पोइंटर के साथ मे.बस- को
एOसैस करने के &लए करते है ।

Self Referencial Structure:


A member of structure that can hold address of same structure type then
such structure called self referencial structure. That structure is used create a
link list.

कसी oOचर का ऐसा मे.बर जो उसी टाइप के कसी अय oOचर का एqैस होQड
करे तब ऐसे oOचर को सेQफ 6रdेिसयल oOचर कहते है । इस oOचर का उपयोग
&लंक &ल ट बनाने के &लए करते ह।

Example:

#include<stdio.h>
struct node
{
int data;
struct node *link;
};
void main()
{
struct node n1,n2,n3;
struct node *start,*ptr;
printf("\nInput three integer data:");
scanf("%d%d%d",&n1.data,&n2.data,&n3.data);
start=&n1;
n1.link=&n2;
n2.link=&n3;
n2.link=NULL;
printf("\nAll data using link list:");
ptr=start;
while(ptr!=NULL)
{
printf("-->%d",ptr->data);
}
}
Output:
Input three integer data:10 20 30 <enter>
All data using link list:-->10-->20-->30

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 88
C-Language Notes
Memory Allocation:
start ptr n1.data n1.link n2.data N3.link n3.data n3.link
1000 1000 10 2000 20 3000 30 NULL
1000 1002 2000 2002 3000 3002

start is initial pointer of link list. Every part of structure contains data and
link part. Link is used to store address of next node. Last node holds NULL
value in link part to indicate ending of link list.
ptr एक oOचर का पोइंटर है &लंक &ल ट का ारि.भक एqैस टाट- मे है । टे क
oOचर मे डाटा और &लंक पाट- होते है । &लंक पाट- मे अगल8 नोड के एqैस को टोर
करते है । अंतम नोड के &लंक पाट- मे NULL असाइन करते है िजससे नोड के ख\म
होने कE सूचना पट हो सके।

Passing structure to function(call by vale & call byreference),


returning structure from function, copy all members of
structure into another :
Pass structure using call byvalue & call by reference: When we pass one
structure variable then all members are automatically passed. If we want to
changes in actual structure then pass its address.
जब हम oOचर के कसी वे6रयबल को फंOशन मे पास करते ह तब उसके सभी मे.बस-
वत: ह8 पास हो जावगे। य3द हम एOचअ
ु ल oOचर मे कोई प6रवत-न चाहते है तब
उसका एqैस पास करते है ।

Returning Structure: When a function return structure variable then all


members are return.
जब कोई फंOशन कसी oOचर वे6रयबल को 6रटन- करता है तो उसके सभी डाटा
मे.बस- भी एक साथ 6रटन- हो जाते ह।
Copy structure: When a structure variable or returning structure is copy to
same type of structure variable using one = (assignment) opertor then all
members are automatically copied to other structure members.
जब एक oOचर वे6रयबल या 6रटन¨ग oOचर को उसी टाइप के अय oOचर
वे6रयबल मे = (असाइनम ट) ऑपरे टर 1वारा कॉपी करते है तो सारे डाटा मे.बस- वत:
कॉपी हो जाते है ।
Example:
#include<stdio.h>
struct student
{
int id;
char name[15];
float fee;
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 89
C-Language Notes
};
typedef struct student STD;
STD structfun(STD, STD*);
void main()
{
STD s1={101,"Rahul",1000.5},s2={102,"nilesh",2000.5},s3;
S3=Structfun(s1,&s2);
printf("\ns1 record:%d\t%s\t%f",s1.id,s1.name,s1.fee);
printf("\ns2 record:%d\t%s\t%f",s1.id,s1.name,s1.fee);
printf("\ns3 record:%d\t%s\t%f",s1.id,s1.name,s1.fee);
}
STD structfun(STD s,STD *ps)
{
s.id=103;
strcpy(s.name,"rakesh");
s.fee=3000.5;

ps->id=104;
strcpy(ps->name,"mukesh");
ps->fee=4000.5;

return s;
}
Output:
s1 record:101 Rahul 1000.5
s2 record:104 mukesh 4000.5
s3 record:103 rakesh 3000.5

Here s is copy of s1 and ps is pointer of s2. We made changes in s but


members of s1 are unchanged whereas changing using ps are made on s2.
When we return structure s then all members are copied into structure s3.
यहाँ s मे s1 कE कॉपी है और ps मे s2 का एqैस है । जब s के बलवाव s1
अभा!वत है जबक ps का बदलाव s2 पर ह8 हुआ है । जब oOचर s को 6रटन- करते
है तो उसके सभी मे.बस- को s3 मे = के 1वारा कॉपी कर लेते है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 90
C-Language Notes

PART8
File Handling:
Properties of file
Operation on file
Types of file
File Pointer and mode
C libray function for file handling:
fopen() and fclose()
fputc() & fgetc()
putw() & getw()
fputs() & fgets()
fprintf() & fscanf()
fwrite() & fread()
Sequential v/s Random access of file:
feof()
ftell()
rewind()
fseek().

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 91
C-Language Notes
File Handling:
File is a collection of characters and store into secondary storage device like
hard disk.
फ़ाइल बहुत सारे कैरOटर का एक समह ू होती है और कसी भी सेकंडर8 टोरे ज Fडवाइस
जैसे हाड- Fड क मे टोर कE जाती है ।

Properties of file:
1. File has a name with extension. Ex file1.txt, file2.c, file3.cpp
फ़ाइल का एक नाम और एOसट शन होता है जैसे file1.txt,file2.c, file3.cpp
2. File has a path. Ex: c:\tc\bin\file1.txt.
फ़ाइल का एक पाथ होता है ।
3. File has creting date & time.
फ़ाइल मे बनाई गई 3दनांक और टाइम दोन होते ह।
4. File has a size.
फ़ाइल कE साइज़ होती है ।
5. File has read / write option.
फ़ाइल मे र8ड / राइट !वकQप होते ह।

Operation on file:
Following operations can be apply on any file.
कसी भी फ़ाइल पर न.न ऑपरे शन अ/लाई कए जा सकते ह।
1) Create new file
नई फ़ाइल बनाना
2) Open and close file
फ़ाइल को ओपन और Oलोज़ करना
3) Read and write data to the file
फ़ाइल से डाटा को र8ड / राइट करना
4) Move read/write pointer in file
फ़ाइल मे र8ड / राइट पोइंटर को मव
ु करना।
5) Search , update and delete selected data.
फ़ाइल से कसी डाटा को सच-, अपडेट और Fडल8ट करना।

Types of file:
Operating system support following two type of files.

Sno Text file Binary file


1 All source code of program file All machine coded file called
store in ASCII called Text file. binary file.
ोाम कE सभी सोस- कोड फ़ाइल जो सभी मशीन कोड युOत फ़ाइल बाइन6र
ASCII मे टोर हो वे सभी टे O ट फ़ाइल होती है ।
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 92
C-Language Notes
फ़ाइल होती ह।
2 Newline character convert into Newline character stores in as it is
combination of carriage return form.
and linefeed characters. इसमे यू लाइन कैरOटर उसी ?प मे
इसमे यू लाइन कैरOटर सबसे पहले होता है ।
केर8ज़ 6रटन- और लाइनफEड मे
प6रवत-त होता है।
3 File is terminated using EOF (26) File termination is identified using
character. actual size of file.
फ़ाइल के अंत मे EOF(26) कैरOटर फ़ाइल के अंत को उसकE सीजे के 1वारा
होता है । पहचाना जाता है ।
4 In which any type of data are In which any type of data are store
store in character form therefore in as it is form
file size is large than actual. इसमे कसी भी टाइप का डाटा उसी ?प
इसमे कसी भी टाइप का डाटा कैरOटर मे टोर होता है ।
के ?प मे ह8 टोर होता है इस&लए
फ़ाइल कE साइज़ वा त!वक साइज़ से
अeधक हो जाती है ।
5 Example: .c/.cpp/.java/.txt etc Example: .obj/.exe/.com/.dat

File Pointer and mode:


we can read or write data of a file where file pointer is set. When a file is
open then file pointer is set on a fixed position depends on following some
mode.
फ़ाइल मे िजस थान पर पोइंटर सेट होता है वह8ं से डाटा को र8ड / राइट कया जा
सकता है । जब कोई फ़ाइल ओप होती है तब फ़ाइल पोइंटर एक नि`चत थान पर सेट
होता है जो न.न न.न तीन मोड पर नभ-र करता है ।

a. Read mode: read pointer set at the beginning of file and allow only
read data from file.
र8ड पोइंटर फ़ाइल के ार.भ मे सेट होता है और फ़ाइल से डाटा को केवल र8ड
कर सकते है ।
b. Write mode: write pointer set at the beginning of file and allow only
write data to the file.
राइट पोइंटर फ़ाइल के ार.भ मे सेट होता है और फ़ाइल मे डाटा को केवल कर
सकते है ।
c. Append mode: write pointer set at the end of file and allow only
write data to the file.
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 93
C-Language Notes
राइट पोइंटर फ़ाइल के अंत मे सेट होता है और फ़ाइल से डाटा को केवल राइट
कर सकते है ।
In C language, file pointer is declared as-
सी भाषा मे फ़ाइल पोइंटर को न.न कार से FडOलैर करते ह।
FILE *fp;
Here FILE is structure data type defined in stdio.h
यहाँ FILE एक oOचर डाटा टाइप है जो stdio.h मे प6रभा!षत है ।

C libray function for file handling:


1. fopen() and fclose()
2. fputc() and fgetc() / putc() and getc()
3. putw() and getw()
4. fputs() and fgets()
5. fprintf() and fscanf()
6. fwrite() and fread()
7. feof(), rewind() and fseek()

fopen() and fclose():


fopen():
This file function is used to open text file or binary file in read / write /
append mode.
इस फ़ाइल फंOशन का उपयोग टे O ट फ़ाइल / बाइन6र फ़ाइल को र8ड / राइट / अप ड
मोड मे ओपन करने के &लए करते ह।
Syntax:
fp = fopen("filename","mode");
Where-
fp : file pointer
filename : file name
mode :
"rt" / "rb" read mode
"wt" / "wb" read mode
"at" / "ab" read mode
t for text file (default)
b for binary file
Events when file is open:
When file is open in "w" or "a" mode and given file name is not found then
a new file is created of that name then open.
जब फ़ाइल को "w" या "a" मोड मे ओपन करते ह और द8 गई फ़ाइल बनी हुई नह8ं है
तो उसी नाम कE फ़ाइल बन जाती है उसके बाद ओपन होती है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 94
C-Language Notes
If file is open in "w" mode and given file name is found then all data
removes from the file.
जब फ़ाइल को "w" मोड मे ओपन करते ह और द8 गई फ़ाइल बनी हुई है तो उस
फ़ाइल के सारे डाटा नyट हो जाते ह।

When file is open is "r" mode and given file is not found then fp has NULL
value.
जब फ़ाइल को "r" मोड मे ओपन करते ह और द8 गई फ़ाइल बनी हुई नह8ं है तो fp मे
NULL वैQयू होती है ।

fclose():
This file function is used to unload a file from memory so that all file data
can saved successfully and we can reuse that file pointer to open another
file.
इस फ़ाइल फंOशन का उपयोग फ़ाइल को मेमोर8 से अनलोड करने के &लए करते ह
िजससे क फ़ाइल का सभी डाटा सफलता पव
ू क
- टोर हो जाए और हम उसी फ़ाइल
पोइंटर का उपयोग कसी अय फ़ाइल को ओपन करने के &लए कर सके।
Syntax: fclose(fp);
Where-
fp :file pointer.
Example of fopen() and fclose():
#include<stdio.h>
void main()
{
FILE *fp1,*fp2,*fp3;
fp1=fopen("file1.txt","r");
fp2=fopen("file2.txt","w");
fp3=fopen("file3.txt","a");
if(fp1==NULL)
{
printf("\nfile1.txt not exist");
return;
}
//perform read / write / append operation
fclose(fp1);
fclose(fp2);
fclose(fp3);
}
Output:
file1.txt not exist
<file2.txt and file3.txt creates in current location>

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 95
C-Language Notes
fputc() & fgetc()
fputc():
It is used to write one character in a file that open in write or append mode.
After then file pointer automatically move to next character.
इसका उपयोग राइट या अप ड मोड मे ओपन कE गई फ़ाइल मे एक कैरOटर को राइट
करने के &लए करते ह। इसके बाद फ़ाइल पोइंटर वत: ह8 अगल8 पो&सशन पर मुव हो
जाता है ।
Syntax: fputc(ch,fp); / putc(ch,fp);
Where-
fp = write file pointer
ch = character
putc() is macro of fputc()
Example:
#include<stdio.h>
void main()
{
char ch;
FILE *fp;
fp=fopen("file1.txt","w");
printf("\nInput a character:");
ch=getchar();
fputc(ch,fp);
fclose(fp);
printf("\nWrite Success!");
}
Output:
Input a character:L <enter>
Write success!
fgetc():
It is used to read one character from a file that open in read mode. After then
file pointer automatically move to next character.
इसका उपयोग र8ड मोड मे ओपन कE गई फ़ाइल से एक कैरOटर को र8ड करने के &लए
करते ह। इसके बाद फ़ाइल पोइंटर वत: ह8 अगल8 पो&सशन पर मुव हो जाता है ।
Syntax: ch=fgetc(fp); / ch=getc();
Where-
fp = read file pointer
ch = character variable
getc() is macro of fgetc()
fgetc() return EOF if on end of file or error.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 96
C-Language Notes
Example:
#include<stdio.h>
void main()
{
char ch;
FILE *fp;
fp=fopen("file1.txt","r");
if(fp!=NULL)
{
ch=fgetc(fp);
}
fclose(fp);
printf("\nFile data is:%c",ch);
}
Output:
File data is L

putw() & getw()


These two are most applicable for binary file.
ये दोन बाइन6र फ़ाइल के &लए सबसे अeधक उपयुOत ह।

putw():
It is used to write one integer in a file that open in write or append mode.
After then file pointer automatically move to next position.
इसका उपयोग राइट या अप ड मोड मे ओपन कE गई फ़ाइल मे एक इंट8जर को राइट
करने के &लए करते ह। इसके बाद फ़ाइल पोइंटर वत: ह8 अगल8 पो&सशन पर मुव हो
जाता है ।
Syntax: putw(n,fp);
Where:
fp = write file pointer
n = integer value
Example:
#include<stdio.h>
void main()
{
int n;
FILE *fp;
fp=fopen("file1.txt","wb");
printf("\nInput an integer:");
scanf("%d",&n);
fputw(n,fp);
fclose(fp);
printf("\nWrite Success!");
}
Output:
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 97
C-Language Notes
Input an integer:223 <enter>
Write success!
getw():
It is used to read one integer from a file that open in read mode. After then
file pointer automatically move to next position.
इसका उपयोग र8ड मोड मे ओपन कE गई फ़ाइल से एक इंट8जर को र8ड करने के &लए
करते ह। इसके बाद फ़ाइल पोइंटर वत: ह8 अगल8 पो&सशन पर मव
ु हो जाता है ।
Syntax: ch=getw(fp);
Where-
fp = read file pointer
n = integer variable
getw() return EOF if on end of file or error.
Example:
#include<stdio.h>
void main()
{
int n;
FILE *fp;
fp=fopen("file1.txt","rb");
if(fp!=NULL)
{
n=getw(fp);
}
fclose(fp);
printf("\nFile data is:%d",n);
}
Output:
File data is 223

fputs() & fgets()


fputs():
It is used to write one line of text / string in a file that open in write or
append mode. After then file pointer automatically move to next position.
इसका उपयोग राइट या अप ड मोड मे ओपन कE गई फ़ाइल मे एक इंट8जर को राइट
करने के &लए करते ह। इसके बाद फ़ाइल पोइंटर वत: ह8 अगल8 पो&सशन पर मुव हो
जाता है ।
Syntax: fputs(str,fp);
Where-
str= string
fp = write file pointer
Example:
#include<stdio.h>
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 98
C-Language Notes
void main()
{
char str[30];
FILE *fp;
fp=fopen("file1.txt","w");
printf("\nInput an string:");
scanf("%s",str);
fputs(str,fp);
fclose(fp);
printf("\nWrite Success!");
}
Output:
Input an string:LRsir.net <enter>
Write success!
fgets():
It is used to read one one line of text / string from a file that open in read
mode. After then file pointer automatically move to next position.
इसका उपयोग र8ड मोड मे ओपन कE गई फ़ाइल से एक ि oं ग को र8ड करने के &लए
करते ह। इसके बाद फ़ाइल पोइंटर वत: ह8 अगल8 पो&सशन पर मुव हो जाता है ।
Syntax: ch=fgets(str,n,fp);
Where-
str = name string variable
n = size of string
fp = read file pointer
fgets() return NULL if on end of file or error.
Example:
#include<stdio.h>
void main()
{
char str[30];
FILE *fp;
fp=fopen("file1.txt","r");
if(fp!=NULL)
{
fgets(str,30,fp);
}
fclose(fp);
printf("\nFile data is:%d",n);
}
Output:
File data is LRsir.net

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 99
C-Language Notes
fprintf() & fscanf()
fprintf():
It is used to write one or more data of any type in a file that open in write or
append mode. After then file pointer automatically move to next position.
इसका उपयोग राइट या अप ड मोड मे ओपन कE गई फ़ाइल मे एक या अeधक कसी भी
टाइप के डाटा को राइट करने के &लए करते ह। इसके बाद फ़ाइल पोइंटर वत: ह8
अगल8 पो&सशन पर मुव हो जाता है ।
Syntax: fprintf(fp,"Formatted code",var1,...);
Where-
fp = write file pointer
formatted code like %d %f %c %s
var1,…are list of variable
Example:
#include<stdio.h>
struct student
{
int id;
char name[10];
float fee;
};
void main()
{
struct student std;
FILE *fp;
fp=fopen("file1.txt","w");
printf("\nInput id,name and fee of student:");
scanf("%d%s%f",&std.id,std.name,&std.fee);
fprintf(fp,"%d\t%s\t%f",std.id,std.name,std.fee);
fclose(fp);
printf("\nWrite Success!");
}
Output:
Input id,name and fee of student:101 rahul 1000.5 <enter>
Write success!
fscanf():
It is used to read one or more data of any type from a file that open in read
mode. After then file pointer automatically move to next position.
इसका उपयोग र8ड मोड मे ओपन कE गई फ़ाइल से एक ि oं ग को र8ड करने के &लए
करते ह। इसके बाद फ़ाइल पोइंटर वत: ह8 अगल8 पो&सशन पर मुव हो जाता है ।
Syntax: fscanf(fp,"Formatted code",&var1,...);
Where-
fp = write file pointer
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 100
C-Language Notes
formatted code like %d %f %c %s
var1,…are list of variable
fscanf() return EOF on end of file and 0 if unable to read.
Example:
#include<stdio.h>
struct student
{
int id;
char name[10];
float fee;
};
void main()
{
struct student std;
FILE *fp;
fp=fopen("file1.txt","r");
if(fp!=NULL)
{
fscanf(fp,"%d%s%f",&std.id,std.name,&std.fee);
}
fclose(fp);
printf("\nFile dat is:%d\t%s\t%f", std.id,std.name,std.fee);
}
Output:
File data is 101 rahul 1000.5

Remark: If we have to any changes in structure then we have to also made


changes in all fprintf() and fascanf().
य3द oOचर मे कोई बदलाव करना हो तो सभी fprintf() और fscanf() मे भी बदलाव
होगा।

fwrite() & fread()


These two are most applicable for binary file.
ये दोन बाइन6र फ़ाइल के &लए सबसे अeधक उपयुOत ह।

fwrite():
It is used to write one or more data of any type in a file that open in write or
append mode. After then file pointer automatically move to next position.
इसका उपयोग राइट या अप ड मोड मे ओपन कE गई फ़ाइल मे एक या अeधक कसी भी
टाइप के डाटा को राइट करने के &लए करते ह। इसके बाद फ़ाइल पोइंटर वत: ह8
अगल8 पो&सशन पर मव
ु हो जाता है ।
Syntax: fwrite(ptr,sizeof(data),n,fp);
Where-

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 101
C-Language Notes
ptr = pointer of data
sizeof = operator to evaluate data size
n = number of data (in case of array)
fp = file pointer
Example:
#include<stdio.h>
struct student
{
int id;
char name[10];
float fee;
};
void main()
{
struct student std;
FILE *fp;
fp=fopen("file1.txt","wb");
printf("\nInput id,name and fee of student:");
scanf("%d%s%f",&std.id,std.name,&std.fee);
fwrite(&std,sizeof(std),1,fp);
fclose(fp);
printf("\nWrite Success!");
}
Output:
Input id,name and fee of student:101 rahul 1000.5 <enter>
Write success!
fread():
It is used to read one or more data of any type from a file that open in read
mode. After then file pointer automatically move to next character.
इसका उपयोग र8ड मोड मे ओपन कE गई फ़ाइल से एक ि oं ग को र8ड करने के &लए
करते ह। इसके बाद फ़ाइल पोइंटर वत: ह8 अगल8 पो&सशन पर मुव हो जाता है ।
Syntax: fread(ptr,sizeof(data),n,fp);
Where-
ptr = pointer of data
sizeof = operator to evaluate data size
n = number of data (in case of array)
fp = file pointer
fread() return 0 if on end of file or unable to read.
Example:
#include<stdio.h>
struct student
{
int id;
char name[10];
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 102
C-Language Notes
float fee;
};
void main()
{
struct student std;
FILE *fp;
fp=fopen("file1.txt","rb");
if(fp!=NULL)
{
fread(&std,sizeof(std),1,fp);
}
fclose(fp);
printf("\nFile dat is:%d\t%s\t%f", std.id,std.name,std.fee);
}
Output:
File data is 101 rahul 1000.5

Remark: If we have to any changes in structure then we have to not made


any changes in fread() and fwrite().
य3द oOचर मे कोई बदलाव करना हो तो कसी भी fwite() और fread() मे बदलाव
नह8ं कया जाता।

Sequential v/s Random access of file:


Sequential access: When a file is open then file pointer moves from
beginning to ending of the file while read/write operation then it is called
sequencial access of file.
जब कसी फ़ाइल को ओपन करते ह तब र8ड/राइट ऑपरे शन करते समय फ़ाइल पोइंटर
ार.भ से अंत तक मुव करता है, इसे फ़ाइल का सीOव&शयल एOसैस कहते ह।

Random access: If we want to access data from any position of the file then
we have to move file pointer on that position and then apply read/write
operation. It is called random access of file.
य3द हम फ़ाइल कE कसी भी पोजीशन पर डाटा को एOसैस करना हो तब हमे फ़ाइल
पोइंटर को उस पोजीशन पर ले जाना होगा और फर उस पर र8ड/राइट ऑपरे शन को
अ/लाई कर गे। इसे ह8 फ़ाइल का रै नडम एOसैस कहते ह।

For random access of file we need following file functions that perform on
file pointer.
फ़ाइल के रै नडम एOसैस के &लए हमे न.न फ़ाइल फंOशस कE आव`यकता होती है जो फ़ाइल
पोइंटर पर काय- करे ।

1. feof()
2. ftell()
3. rewind()
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 103
C-Language Notes
4. fseek()

feof():
This function checks file pointer position. If it is at EOF then return
true(non zero) value otherwise false(0).
यह फंOशन है फ़ाइल पोइंटर कE पोजीशन को चेक करता है । य3द यह EOF पर हो तो true
वैQयू 6रटन- करता है अयथा false।
Syntax: feof(fp)
Where- fp=file pointer
return true / false
Example: show all file data up to end of file
#include<stdio.h>
void main()
{
char ch;
FILE *fp;
fp=fopen("file1.c","r");
while(!feof(fp))
{
ch=fgetc(fp);
putchar(ch);
}
fclose(fp);
}
Output: www.LRsir.net download free ebooks

ftell():
returns current file pointer position in long integer value.
यह फ़ाइल पोइंटर कE कर ट पोजीशन को लॉग इंट8जर वैQयू मे 6रटन- करता है ।
Syntax: pos=ftell(fp);
Where- pos=long integer value
Example: show file pointer position after reading data
#include<stdio.h>
void main()
{
char ch;
long pos;
FILE *fp;
fp=fopen("file1.c","r");
ch=fgetc(fp);
putchar(ch);
pos=ftell(fp);
fclose(fp);
printf("\tCurrent file pointer at:%ld",pos);
}
Output: w Current file pointer at:1
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 104
C-Language Notes
rewind():
repositions file pointer at the beginning of file so that read/write operation
can be done from beginning.
यह फ़ाइल पोइंटर को फ़ाइल के सबसे ार.भ मे सेट कर दे ता है िजससे क र8ड/राइट
ऑपरे शन पन
ु : शु©आत से कर सक।
Syntax: rewind(fp);
Where- fp=file pointer
Example: Read file data two times
#include<stdio.h>
void main()
{
char ch;
FILE *fp;
fp=fopen("file1.c","r");
while(!feof(fp))
{
ch=fgetc(fp);
putchar(ch);
}
rewind(fp);
while(!feof(fp))
{
ch=fgetc(fp);
putchar(ch);
}
fclose(fp);
}
Output: www.LRsir.net download free ebooks
www.LRsir.net download free ebooks

fseek():
repositions file pointer at anywhere in file so that read/write operation can
be done from that position.
यह फ़ाइल पोइंटर को फ़ाइल मे कसी भी पोजीशन पर सेट कर दे ता है िजससे क
र8ड/राइट ऑपरे शन उस पोजीशन से कर सक।
Syntax: fseek(fp,n,position);
Where- fp=file pointer
n= number of bytes to move from position of file pointer.
Position: set file pointer when
0/SEEK_SET :At beginning position
1/SEEK_CUR :At current position
2/SEEK_END :At ending position
Example:
1. Set file pointer at the beginning:
fseek(fp,0,SEEK_SET);
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 105
C-Language Notes
2. Set file pointer at the ending:
fseek(fp,0,SEEK_END);
3. Set file pointer to next 2 byte from current position:
fseek(fp,2,SEEK_CUR);
4. Set file pointer to back 2 bytes from current position:
fseek(fp,-2,SEEK_CUR);
5. Set file pointer to next 2 byte from begining:
fseek(fp,2,SEEK_SET);
6. Set file pointer to back 2 byte from ending:
fseek(fp,-2,SEEK_END);
Example: print reverse file data
#include<stdio.h>
void main()
{
char ch;
FILE *fp;
fp=fopen("file1.c","r");
fseek(fp,0L,SEEK_END);
printf("\nSize of file is %ld bytes",ftell(fp));
fclose(fp);
}
Output: Size of file is 13 bytes

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 106
C-Language Notes

PART9
Preprocessor:
Preprocessor statements
File include
Macro Directives
Passing argument to macro
Conditional compilation:
#ifdef, #ifndef, #if, #elif, #else, #endif.

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 107
C-Language Notes
Preprocessor:
A program that processes all # statements (directive) of C program called
preprocessor. During compilation, any C program is first processed by
preprocessor then compiled into machine code.
यह एक ोाम होता है जो सी ोाम के केवल # टे टम ट (डायरे िOटव) को ह8 ोसैस
करता है उसे ीोसेसर कहते ह। कंपाइलेशन के समय कोई भी सी ोाम सबसे पहले
ीोसेसर 1वारा ोसैस होगा उसके बाद ह8 मशीन कोड मे कंपाइल होता है ।

C Program C-Preprocessor C Program C-Compiler Machine


with # without # coded

Preprocessor statements: (Directives)


A statement that begins with # but not terminated by semi colon(;) called
directives. It supports following type if directives.
ऐसे टे टम ट जो # से होते है कतु अंत ; से ना हो उसे डायरे िOटव कहते ह। यह
न.न कार के डायरे िOटव को सपोट- करता है ।
a. File include(#include)
b. Macro(#define)
c. Conditional compilation(#ifdef, #ifndef, #if, #else,#endif)
d. String preprocessor (#, ##, pragma etc)
File include preprocessor:
#include is a preprocessor statement that is used to include content of given
filename into current program code before comilation.
#include एक ऐसा ीोसेसर टे टम ट है िजसके 1वारा करं ट ोाम कोड मे कसी अय
फ़ाइल के कंटट को कंपाइल होने से पहले जोड़ सकते ह।

It is used in following two forms.


इसका उपयोग न.न दो कार से कर सकते ह।

1) #include<filename>
Search filename only in predefine path.(c:\tc\include\).
यह filename को केवल पूवप
- 6रभा!षत पाथ(c:\tc\include\) मे ह8 खोजता है ।
2) #include"filename"
Search filename first in current working directory(c:\tc\bin\). When
not found then search in predefine path.(c:\tc\include\)
यह filename को पहले उसी डाइरै Oट6र मे खोजता है िजसमे हम काय- कर रहे
ह(c:\tc\include\)। जब वह नह8ं &मलती तब पव
ू प
- 6रभा!षत पाथ(c:\tc\include\)
मे ह8 खोजता है ।

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 108
C-Language Notes
Example: include header files
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<alloc.h>
#include<stdlib.h>

Macro Directives:
#define is called macro that work as find and replace. If we want to made
many changes of same thing then we can use macro.
#define को ह8 मैNो कहते ह एवं यह फाइंड और 6र/लेस कE तरह काय- करता है । य3द
हमे एक जैसे कई सारे बदलाव करना हो तब मेNो का उपयोग कर सकते ह।

Syntax: #define MACRO expansion


MACRO is a macro name that replace by expansion by preprocessor.
MACRO एक मेNो नाम है जो ीोसेसर 1वारा expansion से 6र/लेस हो जाता है ।
Example:
#define PI 3.14
Here PI is macro. Every PI will be replacing by 3.14 in a program.
यहाँ PI एक मेNो है और ोाम का \येक PI, 3.14 से 6र/लेस हो जाएगा।

Macro v/s Function :( passing argument to macro)


Macro can be used just similar to function for single line of statement. It
means we can also pass arguments into macro just like function. Macro is
different than function because macro performs before compilation whereas
function performs after compilation at runtime.
एक ह8 लाइन के टे टम ट मे मेNो का उपयोग फंOशन के समान भी कर सकते है
अथा-त मेNो मे भी आगम
ु-  स को पास कया जा सकता है । &सफ- फक- इतना रहता है
क मेNो क ोसैस कंपाइल होने से पहले परू 8 हो जाती है जबक फंOशन कंपाइल होने
के बाद रन टाइम पर ोसैस होता है ।

Syntax: #define MACRO(var1,var2,..) expansion


Example: Find area and circumference of circle for given
radius.
#include<stdio.h>
#define PI 3.14
#define AREA(x) PI*x*x
#define CIRCUM(x) 2*PI*x
void main()
{
floar r,a;
printf("\nInput radious of circle:");
scanf("%d",&r);

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 109
C-Language Notes
a=AREA(r);
printf("\nArea=%d",a);
printf("\nCircumference=%d",a);
}
Output: Input radius of circle: 10
Circumference=62.8
During compilation, preprocessor first replace all PI by 3.14 then AREA(x)
replace by 3.14*r*r. argument x is replace by r.
कंपाइलेशन के पहले ीोसेसर सबसे पहले सभी PI को 3.14 से 6र/लेस करता है
उसके बाद AREA(x) 3.14*r*r से। आगम
ु-  ट x को r 6र/लेस करता है ।

Conditional compilation:
If we want to skip some part of code for compilation then it is done using
following syntax.
य3द हम ोाम कोड के कुछ 3ह स को ह8 कंपाइल करना चाहते ह तब इसे न.न
कंडीशनल डायरे िOटव से कर सकते ह।

Syntax1:
#ifdef MACRO
Code1
#else
Code2
#endif
When MACRO is defined then code1 compiled otherwise code2.
जब MACRO को प6रभा!षत कया जाता है तब ह8 code1 कंपाइल होगा अयथा
code2।

Syntax2:
#ifndef MACRO
Code1
#else
Code2
#endif
When MACRO is not defined then code1 compiled otherwise code2.
जब MACRO को प6रभा!षत नह8ं कया जाता है तब ह8 code1 कंपाइल होगा अयथा
code2।

Syntax3:
#if condition
Code1
#else
Code2
#endif
When condition (like 4>3) is true then code1 compiled otherwise code2.
जब condition (जैसे 4>3) true होगी तब ह8 code1 कंपाइल होगा अयथा code2।
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 110
C-Language Notes
Syntax4:
#if condition1
Code1
#elif condition2
Code2
#else
Code3
#endif
When condition1 is true then code1 is compiled otherwise if condition2 is
true then code2 compiled otherwise code3.
जब condition1 true होगी तब ह8 code1 कंपाइल होगा अयथा condition2 true
होगी तब ह8 code2 कंपाइल होगा अयथा code3।
Example:
#include<stdio.h>
#define PI 3.14
void main()
{
#ifdef PI
printf("\n#ifdef part compiled");
#else
printf("\n#else part compiled");
#ifdef
}
Output: #ifdef part compiled

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 111
C-Language Notes
C-Programs
1. Read any two integer number then perform all arithmatic
operation
2. Swapping of any two number using temp variable
3. Calculate Area and Circumference of a Circle.
4. Area of triangle
5. Even odd status of given number
6. Solve a Quadratic Equation - Ax^2 + Bx + C = 0
7. Read any three integer number then find greatest number
8. Read any 5 subject marks then print total, percentage
and division
9. Find input character is small case or capital or digit
or special symbol.
10. Develop menu driven program for all arithmatic
operation
11. Find addition of all number from 1 to 10
12. Display multiplication c number of any given number
13. Find factorial of any given number
14. y=x^n
15. Print all even numbers up to 100
16. Find sum of all digits, reverse and palindrome status
of an input number
17. Display pyramid using ( * ).
18. S=1+ x + x^2/2! + x^3/3! +----+ x^n/n!
19. Print all combination of 1,2 and 3
20. Prime numbers up to 100
21. LCM
22. Find large and small from 10 data.
23. sorting array
24. Transpose Matrix of a given Matrix of order 3 X 2.
25. Addition of two matrices of order 3 X 2.
26. Multiply matrices of order 3 X 2 by matrices of
order2X3
27. Swapping of any two number using call by values.
28. Swapping of any two number using call by reference /
pointer
29. Find factorial of input number(n!) using recursion.
30. Display the Fibonacci series up to 20 th term.
31. Create an array of structure to hold records of 20
cricketers that define age, number of played matches
and average runs. Read these records and display them
in ascending order by average runs.(Define functions
for each job)
32. Write any union program.
33. Copy one string into another and count number of
characters.
34. use Pointer and access operators
35. accessing an integer variable through pointer of
pointer.
36. Calculate the sum of Array elements through pointers.
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 112
C-Language Notes
37. Create dynamic array for n size and access all
elements.
38. Write a Program that allocate memory dynamicaly for
given number of students then enter total marks for
all, display each marks with grand total
39. Write a Program that stores adrress of any 5 integer
address in pointer array then display all values using
pointer array
40. Define a structure book with members name, author and
price. Access using pointers.
41. Write a Program that sorting a list of array by passing
entir array in to another function
42. Write a Program that call a function using function
pointer for calculating factorials of any number
43. Write a Program that calulate length of any string
without standard function
44. Write a Program that converts all character of string
to upper case without standard function
45. reverse a string
46. Write a Program that copy one string to another without
standard function
47. Read some text from keyboard then write to and read
from file.
48. count file character, space, tab and lines
49. write and read file
50. file copy
51. Read content of one file and copy to another file from
command line
52. use fprintf
53. use fscanf
54. use fprintf for store student records
55. use fscanf to read student records
56. use fwrite to store student records
57. use fread to read student records
58. Define a structure item with members particular, rate
and price. Read these data from keyboard to item
variable then write to and read from file.
59. search string in file
60. display calendar
61. Convert Decimal to Binary, Hexa_Descimal and octal
62. Convert Decimal to Binary, Hexa_Descimal and octal
63. Convert Binary, Hexa_Descimal and octal to Decimal
64. Convert Decimal to Binary, Hexa_Descimal and octal

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 113
C-Language Notes
/* Read any two integer number then perform all arithmatic
operation*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("\nInput any two numbers:");
scanf("%d%d",&a,&b);
printf("\nAddition=%d",a+b);
printf("\nSubtraction=%d",a-b);
printf("\nMultiplication=%d",a*b);
printf("\nDivision=%d",a/b);
printf("\nRemainder=%d",a%b);
getch();
}
/* Swapping of any two number using temp variable*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf("\nInput any two numbers:");
scanf("%d%d",&a,&b);
printf("\nBefore swapping a=%d,b=%d",a,b);
temp=a;
a=b;
b=temp;
printf("\nAfter swapping a=%d,b=%d",a,b);
getch();
}
/* Calculate Area and Circumference of a Circle.*/
#include<stdio.h>
#include<conio.h>
void main()
{
float r,a,c;
clrscr();
printf("\nInput radius of circle:");
scanf("%f",&r);
a=3.14*r*r;
c=2*3.14*r;
printf("\nArea=%f Circumference=%f",a,c);
getch();
}
/*Area of triangle*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 114
C-Language Notes
void main()
{
float a,b,c,s,area;
clrscr();
printf("\nInput value of a,b,c:");
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\nArea=%f",&area);
getch();
}
/* Even odd status of given number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\nInput any number:");
scanf("%d",&n);
if(n%2==0)
printf("\n%d is even",n);
else
printf("\n%d is Odd",n);
getch();
}
/*Solve a Quadratic Equation - Ax^2 + Bx + C = 0*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,x1,x2;
clrscr();
printf("\nInput value of a,b,c:");
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d<0)
printf("\nImaginary Result");
else
{
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("Root1=%f,Root2=%f",x1,x2);
}
getch();
}
/* Read any three integer number then find greatest
number*/
#include<stdio.h>
#include<conio.h>
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 115
C-Language Notes
void main()
{
int a,b,c;
clrscr();
printf("\nInput any three numbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("\n%d is greatest",a);
else
printf("\n%d is greatest",c);
}
else
{
if(b>c)
printf("\n%d is greatest",b);
else
printf("\n%d is greatest",c);
}
getch();
}
/* Read any 5 subject marks then print total, percentage
and division*/
#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,m4,m5,total;
float avg;
clrscr();
printf("\nInput five subject marks:");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
total=m1+m2+m3+m4+m5;
avg=(float)total/5;

printf("\n\nTotal=%d\nPercentage=%f\nDivision=",total,avg);
if(avg>=60)
printf("First");
else if(avg>=45)
printf("Second");
else if(avg>=33)
printf("Third");
else
printf("Fail");

getch();
}
/*Find input character is small case or capital or digit or
special symbol.*/
#include<stdio.h>
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 116
C-Language Notes
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("\nPress any key:");
fflush(stdin);
ch=getche();
if(ch>='A'&&ch<='Z')
printf("\nCapital Letter");
else if(ch>='a'&&ch<='z')
printf("\nSmall Letter");
else if(ch>='0'&&ch<='9')
printf("\ndigit");
else
printf("\nSpecial Symbole");
getch();
}
/* Develop menu driven program for all arithmatic
operation*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
char ch;
clrscr();
printf("\nInput any two numbers:");
scanf("%d%d",&a,&b);
printf("\nOperator:");
ch=getch();
switch(ch)
{
case '+':
printf("=%d",a+b);
break;
case '-':
printf("=%d",a-b);
break;
case '*':
printf("=%d",a*b);
break;
case '/':
printf("=%d",a/b);
break;
case '%':
printf("=%d",a%b);
break;
default:
printf("\nInvalid Operator");
}
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 117
C-Language Notes
getch();
}
/* Find addition of all number from 1 to 10*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
clrscr();
for(i=1;i<=10;i++)
sum+=i;

printf("\nSum from 1 to 10=%d",sum);


getch();
}
/* Display multiplication number of any given number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
printf("\nInput any number:");
scanf("%hd",&n);
for(i=1;i<=10;i++)
printf("\n%d*%d=%d",n,i,n*i);
getch();
}
/* Find factorial of any given number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
long f;
clrscr();
printf("\nInput any number:");
scanf("%d",&n);
f=1;
for(i=n;i>0;i--)
f=f*i;
printf("\nFactorial of %d is %ld",n,f);
getch();
}
/*y=x^n*/
#include<stdio.h>
#include<conio.h>
void main()
{
int x,n,y=1,i;
clrscr();
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 118
C-Language Notes
printf("Input value of x and n:");
scanf("%d%d",&x,&n);
for(i=1;i<=n;i++)
y=y*x;
printf("y=%d",y);
getch();
}
/*Print all even numbers up to 100*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
clrscr();
for(i=1;i<=100;i++)
{
if(i%2==0)
printf("%d ",i);
}
getch();
}
/*Find sum of all digits, reverse and palindrome status of
an input number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,x,s;
clrscr();
printf("Input any number:");
scanf("%d",&n);
x=n;
s=0;
r=0;
while(n>0)
{
s=s+(n%10);
r=r*10+n%10;
n=n/10;
}
printf("Sum of digits=%d,Reverse=%d",s,r);
if(r==x)
printf("\It is palindrom");
else
printf("\It is not palindrom");
getch();
}
/*Display pyramid using ( * ).*/
#include<stdio.h>
#include<conio.h>
void main()
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 119
C-Language Notes
{
int i,j,k;
clrscr();
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=5-i;j++)
printf(" ");
for(k=1;k<=i;k++)
printf("* ");
}
getch();
}
/*S=1+ x + x^2/2! + x^3/3! +----+ x^n/n!*/
#include<stdio.h>
#include<conio.h>
void main()
{
float x,y,t,s;
int f,n,i,j;
clrscr();
printf("\nInput value of x and last term number:");
scanf("%f%d",&x,&n);
s=1;
for(i=1;i<=n;i++)
{
y=1;
f=1;
for(j=1;j<=i;j++)
{
y=y*x;
f=f*j;
}
s=s+(y/f);
}
printf("\ns=%f",s);
getch();
}
/*Print all combination of 1,2 and 3*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
printf("\n");
for(k=1;k<=3;k++)
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 120
C-Language Notes
printf("%d%d%d\t",i,j,k);
}
}
getch();
}
/*Prime numbers up to 100*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=100;i++)
{
for(j=2;j<i;j++)
if(i%j==0)break;
if(j==i||i==1)
printf("%d ",i);
}
getch();
}
/*LCM*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,max;
clrscr();
printf("\nInput two number:");
scanf("%d%d",&a,&b);
max=a>b?a:b;
while(1)
{
if(max%a==0 && max%b==0)
{
break;
}
max++;
}
printf("LCM=%d",max);

getch();
}
/*Find large and small from 10 data.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,s,l;
clrscr();
printf("\nInput 10 data:");
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 121
C-Language Notes
for(i=0;i<10;i++)
scanf("%d",&a[i]);
l=0;
s=a[0];
for(i=0;i<10;i++)
{
if(l<a[i]) l=a[i];
if(s>a[i]) s=a[i];
}
printf("\n Large=%d,small=%d",l,s);
getch();
}
/*sorting array*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5];
int i,j,c;
clrscr();
printf("\nInput 5 data");
for(i=0;i<=4;i++)
{
scanf("%d",&a[i]);
}

for(i=0;i<=4;i++)
{
for(j=0;j<4-i;j++)
{
if(a[j]>a[j+1])
{
c=a[j];
a[j]=a[j+1];
a[j+1]=c;
}
}
}
printf("\nAll array \a\adata in increasing order:");
for(i=0;i<=4;i++)
{
printf("%d\t",a[i]);
}
getch();
}
/*Transpose Matrix of a given Matrix of order 3 X 2.*/
#include<stdio.h>
#include<conio.h>
#define ROW 3
#define COL 2
void main()
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 122
C-Language Notes
{
int m[ROW][COL],t[COL][ROW];
int r,c;
clrscr();
/* Read matrix*/
for(r=0;r<ROW;r++)
{
printf("\nInput Row-%d data:",r+1);
for(c=0;c<COL;c++)
scanf("%d",&m[r][c]);
}
/* logic for transpose*/
for(r=0;r<ROW;r++)
{
for(c=0;c<COL;c++)
t[c][r]=m[r][c];
}

/* Print transpose*/
for(r=0;r<COL;r++)
{
printf("\n");
for(c=0;c<ROW;c++)
printf("%d\t",t[r][c]);
}
getch();
}
/*Addition of two matrices of order 3 X 2.*/
#include<stdio.h>
#include<conio.h>
#define ROW 3
#define COL 2
void main()
{
int m1[ROW][COL],m2[ROW][COL],s[ROW][COL];
int r,c;
clrscr();

printf("\nInut First matrix");


for(r=0;r<ROW;r++)
{
printf("\nRow-%d data:",r+1);
for(c=0;c<COL;c++)
scanf("%d",&m1[r][c]);
}

printf("\nInut second matrix");


for(r=0;r<ROW;r++)
{
printf("\nRow-%d data:",r+1);
for(c=0;c<COL;c++)
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 123
C-Language Notes
scanf("%d",&m2[r][c]);
}

/* logic for Addition*/


for(r=0;r<ROW;r++)
{
for(c=0;c<COL;c++)
s[r][c]=m1[r][c]+m2[r][c];
}

printf("\nFirst Matrix=");
for(r=0;r<ROW;r++)
{
printf("\n");
for(c=0;c<COL;c++)
printf("%d\t",m1[r][c]);
}

printf("\nSecond Matrix=");
for(r=0;r<ROW;r++)
{
printf("\n");
for(c=0;c<COL;c++)
printf("%d\t",m2[r][c]);
}

printf("\nAddition of matrices=");
for(r=0;r<ROW;r++)
{
printf("\n");
for(c=0;c<COL;c++)
printf("%d\t",s[r][c]);
}

getch();
}
/*Multiply matrices of order 3 X 2 by matrices of order2X3
*/
#include<stdio.h>
#include<conio.h>
#define ROW 3
#define COL 2
#define COL1 3
void main()
{
int m1[ROW][COL],m2[COL][COL1],p[ROW][COL1];
int r,c,c1;
clrscr();

printf("\nInut First matrix");


for(r=0;r<ROW;r++)
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 124
C-Language Notes
{
printf("\nRow-%d data:",r+1);
for(c=0;c<COL;c++)
scanf("%d",&m1[r][c]);
}

printf("\nInut second matrix");


for(r=0;r<COL;r++)
{
printf("\nRow-%d data:",r+1);
for(c=0;c<COL1;c++)
scanf("%d",&m2[r][c]);
}

/* logic for Addition*/


for(r=0;r<ROW;r++)
{
for(c1=0;c1<COL1;c1++)
{
p[r][c1]=0;
for(c=0;c<COL;c++)
p[r][c1]+=m1[r][c]*m2[c][c1];
}
}

printf("\nFirst Matrix=");
for(r=0;r<ROW;r++)
{
printf("\n");
for(c=0;c<COL;c++)
printf("%d\t",m1[r][c]);
}
printf("\nSecond Matrix=");
for(c=0;c<COL;c++)
{
printf("\n");
for(c1=0;c1<COL1;c1++)
printf("%d\t",m2[c][c1]);
}

printf("\nMultiplication of matrices=");
for(r=0;r<ROW;r++)
{
printf("\n");
for(c1=0;c1<COL1;c1++)
printf("%d\t",p[r][c1]);
}

getch();
}
/*Swapping of any two number using call by values.*/
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 125
C-Language Notes

#include<stdio.h>
#include<conio.h>

void swap(int a,int b);

void main()
{
int a,b;
clrscr();
printf("\nInput two values:");
scanf("%d%d",&a,&b);
printf("\nBefore Swap a=%d,b=%d",a,b);
swap(a,b);
getch();
}

void swap(int a,int b)


{
int c;
c=a;
a=b;
b=c;
printf("\nAfter Swap a=%d,b=%d",a,b);
}
/*Swapping of any two number using call by reference /
pointer*/

#include<stdio.h>
#include<conio.h>

void swap(int *a,int *b);

void main()
{
int a,b;
clrscr();
printf("\nInput two values:");
scanf("%d%d",&a,&b);
printf("\nBefore Swap a=%d,b=%d",a,b);
swap(&a,&b);
printf("\nAfter Swap a=%d,b=%d",a,b);
getch();
}

void swap(int *a,int *b)


{
int c;
c=*a;
*a=*b;
*b=c;
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 126
C-Language Notes
}
/*Find factorial of input number(n!) using recursion.*/

#include<stdio.h>
#include<conio.h>

long fact(int n);

void main()
{
int n;
long f;
clrscr();
printf("\nInput any number:");
scanf("%d",&n);
f=fact(n);
printf("\n%d!=%ld",n,f);
getch();
}

long fact(int n)
{
long f;
if(n==1)
return 1;
else
f=n*fact(n-1);
return f;
}
/*Display the Fibonacci series up to 20 th term.*/

#include<stdio.h>
#include<conio.h>

void fibo(int f,int s);

void main()
{
int f,s;
clrscr();
fibo(1,1);
getch();
}

void fibo(int f,int s)


{
int sum;
if(f>32767/2)
return;
else
{
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 127
C-Language Notes
printf("%d+",f);
sum=f+s;
f=s;
s=sum;
fibo(f,s);
}
}
/*Create an array of structure to hold records of 20
cricketers that define age, number of played matches and
average runs. Read these records and display them in
ascending order by average runs.(Define functions for each
job) */

#include<stdio.h>
#include<conio.h>
#define SIZE 3

void read();
void display();
void asending();

struct player
{
char name[15];
int age;
int n;
int avg;
}p[SIZE];

void main()
{
clrscr();
read();
asending();
display();
getch();
}

void read()
{
int i;
for(i=0;i<SIZE;i++)
{
printf("\nInput palyer-%d Record",i+1);
printf("\nPlayer Name:");
scanf("%s",p[i].name);
printf("\nAge:");
scanf("%d",&p[i].age);
printf("\nTotal Played matched number:");
scanf("%d",&p[i].n);
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 128
C-Language Notes
printf("\nAverage run:");
scanf("%d",&p[i].avg);
}
}

void display()
{
int i;
clrscr();
printf("\nName\t\tAge\tPlay Matchs\tAverage Run");
for(i=0;i<SIZE;i++)

printf("\n%s\t\t%d\t%d\t\t\t%d",p[i].name,p[i].age,p[i].n,p
[i].avg);
}

void asending()
{
int i,j;
struct player t;
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE-i;j++)
if(p[j].avg<p[j+1].avg)
{
t=p[j];
p[j]=p[j+1];
p[j+1]=t;
}
}
/*Write any union program.*/

#include<stdio.h>
#include<conio.h>

union abc
{
char a;
int b;
float c;
}ch,i,f;

void main()
{
clrscr();
printf("\nInput char,integer and real data:");
scanf("%c%d%f",&ch.a,&i.b,&f.c);
printf("%c %d %f",ch.a,i.b,f.c);
getch();
}
/*Copy one string into another and count number of
characters.*/
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 129
C-Language Notes

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
char s1[15],s2[15];
int c;
clrscr();
printf("\nInput Your name:");
scanf("%s",s1);
strcpy(s2,s1);
c=strlen(s2);
printf("s1=%s s2=%s count characters=%d",s1,s2,c);
getch();
}
/*use Pointer and access operators*/
#include<stdio.h>
void main()
{
int x=10;
int *p;
p=&x;
printf("\nValue of x=%d",*p);
printf("\nValue of p=%u",p);
printf("\nAddress of x=%u",&x);
printf("\nAddress of p=%u",&p);
printf("\nValue of x=%d",x);
}
/* accessing an integer variable through pointer of
pointer.*/

#include<stdio.h>
#include<conio.h>

void main()
{
int a,*p,**pp;
clrscr();
printf("\nInput any number:");
scanf("%d",&a);
p=&a;
pp=&p;
printf("a=%d,*p=%d,**pp=%d",a,*p,**pp);
getch();
}
/*Calculate the sum of Array elements through pointers.*/
#include<stdio.h>
#include<conio.h>

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 130
C-Language Notes
void main()
{
int a[10],i,s=0,*p;
clrscr();
p=&a[0];
printf("\nInput any 10 number:");
for(i=0;i<10;i++)
scanf("%d",&p[i]);

for(i=0;i<10;i++)
s+=p[i];

printf("sum=%d",s);
getch();
}
/*Create dynamic array for n size and access all
elements.*/

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
int a,n,i,*p;
clrscr();

printf("\nInput size:");
scanf("%d",&n);
p=(int *)calloc(n,sizeof(int));
printf("Input %d data:",n);
for(i=0;i<n;i++)
scanf("%d",&p[i]);

for(i=0;i<n;i++)
printf("%d ",p[i]);
free(p);
getch();
}
/*Write a Program that allocate memory dynamicaly for given
number of students then enter total marks for all, display
each marks with grand total*/
#include<stdio.h>
int main()
{
int i,no,sum,*start,*ptr;
printf("\nEnter number of student:");
scanf("%d",&no);
start=(int *)malloc((sizeof(int))*no);
ptr=start;
for(i=1;i<=no;i++)
{
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 131
C-Language Notes
printf("\n%d:Enter marks:",i);
scanf("%d",ptr);
ptr++;
}
ptr=start;
printf("Marks:\n");
for(i=1,sum=0;i<=no;i++)
{
printf("%d\n",*ptr);
sum+=*ptr;
ptr++;
}
printf("Total Grand Marks=%d",sum);
return(0);
}
/*Write a Program that stores adrress of any 5 integer
address in pointer array then display all values using
pointer array */
#include<stdio.h>
int main()
{
int *ptr_arr[5];
int a,b,c,d,e,count;
printf("\nEnter any 5 integer number:");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
ptr_arr[0]=&a;
ptr_arr[1]=&b;
ptr_arr[2]=&c;
ptr_arr[3]=&d;
ptr_arr[4]=&e;
for(count=0;count<=4;count++)
printf("\nValue=%d",*ptr_arr[count]);
}
/*Define a structure book with members name, author and
price. Access using pointers.*/

#include<stdio.h>
#include<conio.h>

struct book
{
char name[15];
char aut[15];
int price;
}b,*p;

void main()
{
clrscr();

p=&b;
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 132
C-Language Notes
printf("\nInput Book name,writer and price:");
scanf("%s%s%d",p->name,p->aut,&p->price);

printf("\nBook name:%s\nwriter: %s\nprice:%d",


p->name,p->aut,p->price);

getch();
}
/*Write a Program that sorting a list of array by passing
entir array in to another function*/
#include<stdio.h>
int main()
{
int i,arr[10];
void sortfun(int *ptr,int size);
printf("\nEnter any 10 number:");
for(i=0;i<=9;i++)
scanf("%d",&arr[i]);
sortfun(&arr[0],10);
printf("\nSorted list:\n");
for(i=0;i<=9;i++)
printf("%d\t",arr[i]);
return(0);
}

void sortfun(int *ptr,int size)


{
int i,j,temp;
for(i=0;i<=size-1;i++)
{
for(j=i+1;j<=size-1;j++)
{
if(*(ptr+i)>*(ptr+j))
{
temp=*(ptr+i);
*(ptr+i)=*(ptr+j);
*(ptr+j)=temp;
}
}
}
}
/*Write a Program that call a function using function
pointer for calculating factorials of any number*/
#include<stdio.h>
int main()
{
int no;
long fact;
long fact_fun(int);
long (*func_ptr)(int);
printf("\nEnter any number:");
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 133
C-Language Notes
scanf("%d",&no);
func_ptr=fact_fun;
printf("\nAddress of fact_fun() Function is %u",func_ptr);
fact=(*func_ptr)(no);
printf("\n%d!=%ld",no,fact);
}

long fact_fun(int n)
{
long f;
int i;
for(i=1,f=1;i<=n;i++)
f=f*i;
return (f);
}
/*Write a Program that calulate length of any string
without standard function*/
#include<stdio.h>
int main()
{
char *str;
int l;
int strlenx(char *);
printf("\nType any message:");
gets(str);
l=strlenx(str);
printf("\nLength=%d",l);
}

int strlenx(char *s)


{
int count=0;
while(*s!=NULL)
{
count++;
s++;
}
return (count);
}
/*Write a Program that converts all character of string to
upper case without standard function*/
#include<stdio.h>
int main()
{
char *str;
void struprx(char *);
printf("\nType any message:");
gets(str);
struprx(str);
puts(str);
return (0);
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 134
C-Language Notes
}

void struprx(char *s)


{
while(*s!=NULL)
{
if(*s>='a' && *s<='z')
*s=*s-32;
s++;
}
}

/* reverse a string*/
#include<stdio.h>
#include<conio.h>
void main()
{
char str[15];
int i,l;
clrscr();
printf("\nInput string:");
scanf("%s",str);
l=0;
for(i=0;i<=14;i++)
{
if(str[i]=='\0')
{
break;
}
l++;
}
printf("%d",l);
for(i=l;i>=0;i--)
{
printf("%c",str[i]);
}
getch();
}
/*Write a Program that copy one string to another without
standard function*/
#include<stdio.h>
int main()
{
char *source,*target;
void strcpyx(char *,char *);
printf("\nType any message:");
gets(source);
strcpyx(target,source);
puts(target);
return 0;
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 135
C-Language Notes
}

void strcpyx(char *t,char *s)


{
while(*s!=NULL)
{
*t=*s;
++s;
++t;
}
*t=NULL;
}

/*Read some text from keyboard then write to and read from
file.*/

#include<stdio.h>
#include<conio.h>

void main()
{
char ch;
FILE *fp;
clrscr();
fp=fopen("file1.txt","a");
printf("\nInput some text:");
while(1)
{
ch=getche();
if(ch==13) break;
fputc(ch,fp);
}
fclose(fp);

printf("\File data is:\n");


fp=fopen("file1.txt","r");
while(feof(fp))
{
ch=fgetc(fp);
putch(ch);
}
fclose(fp);
getch();
}
/*count file character, space, tab and lines*/
#include<stdio.h>
#include<stdlib.h>
main()
{
char ch=NULL,filename[30];
int chr=0,tab=0,line=0;
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 136
C-Language Notes
FILE *fp;
puts("Type file name with path and extension");
gets(filename) ;
fp=fopen(filename,"rb");
if(fp==NULL)
{
printf("Unable");
exit(0);
}
while(1)
{
ch=getc(fp);
if(ch==EOF)
break;
if(ch=='\n')
line++;
if(ch=='\t')
tab++;
chr++;

}
fclose(fp);
printf("Char=%d Tab=%d Line=%d",chr,tab,line);
getch();
return (0);
}
/*write and read file*/
#include<stdio.h>
#include<stdlib.h>
main()
{
char ch=NULL;
FILE *fp;
fp=fopen("c:\\file1.txt","wt");
if(fp==NULL)
{
printf("Unable");
exit(0);
}
puts("Enter contents:");
while(1)
{
ch=getche();
if(ch==26)
break;
if(ch==13)
{
puts("\n");
ch='\n';
}
putc(ch,fp);
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 137
C-Language Notes

}
fclose(fp);

fp=fopen("c:\\file1.txt","rt");
if(fp==NULL)
{
printf("Unable");
exit(0);
}
puts("contents:");
ch=NULL;
while(1)
{
ch=getc(fp);
if(ch==EOF)
break;
if(ch=='\n')
puts("\n");
else
putch(ch);

}
fclose(fp);
getch();
return;
}
/*file copy*/
#include<stdio.h>
#include<stdlib.h>
main()
{
char ch=NULL,filename[30];
FILE *fp1,*fp2;
puts("Type exist file name with path and extension");
gets(filename) ;
fp1=fopen(filename,"r");
if(fp1==NULL)
{
printf("Unable");
exit(0);
}

puts("Type new file name with path and extension");


gets(filename) ;
fp2=fopen(filename,"w");
if(fp2==NULL)
{
printf("Unable");
exit(0);
}
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 138
C-Language Notes

while(1)
{
ch=getc(fp1);
if(ch==EOF)
break;
putc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
getch();
return (0);
}
/*Read content of one file and copy to another file from
command line*/
#include<stdio.h>
void main(int argc,char *argv[])
{
char ch;
FILE *fp1,*fp2;
if(argc!=3)
{
printf("\nUnsufficient files to copy!");
return;
}
fp1=fopen(argv[1],"r");
if(fp1==NULL)
{
printf("\n%s file not exist",argv[1]);
return;
}
fp2=fopen(argv[2],"w");

while(!feof(fp1))
{
ch=fgetc(fp1);
fputc(ch,fp2);
}
fclose(fp1);
fclose(fp2);
printf("\nFile copy success!");
}
Output:
Save as-fcopy.c
Make fcopy.exe in C:\
Run from command line:
C:\fcopy.exe file1.txt file2.txt <enter>
File copy success!
/*use fprintf*/
#include<stdio.h>
#include<conio.h>
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 139
C-Language Notes
struct emp
{
int id;
char name[15];
float sal;
};
void main()
{
struct emp e;
FILE *fw;
clrscr();

printf("\nInput id, name and salary:");


scanf("%d%s%f",&e.id,e.name,&e.sal);

fw=fopen("file1.txt","w");

fprintf(fw,"%d %s %f ",e.id,e.name,e.sal);
getch();
}
/*use fscanf*/
#include<stdio.h>
#include<conio.h>
struct emp
{
int id;
char name[15];
float sal;
};
void main()
{
struct emp e;
FILE *fr;
clrscr();

fr=fopen("file1.txt","r");
fscanf(fr,"%d%s%f",&e.id,e.name,&e.sal);

printf("%d %s %f ",e.id,e.name,e.sal);
getch();
}
/*use fprintf for store student records*/
#include<stdio.h>
#include<stdlib.h>
struct student
{
int rno;
char nm[30];
float fee;
};

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 140
C-Language Notes
main()
{
FILE *fp;
struct student s;
char ch='y';
fp=fopen("c:\\stu_list.dat","w");
if(fp==NULL)
{
printf("\n Unable to create");
exit(0);
}
while(ch=='y' || ch=='Y')
{
printf("\nRoll no, name, fee:");
scanf("%d%s%f",&s.rno,s.nm,&s.fee);
fprintf(fp,"%d %s %f\n",s.rno,s.nm,s.fee);
printf("\nPress Y key to continue:");
fflush(stdin);
ch=getche();
}
fclose(fp);
getch();
return(0);
}
/*use fscanf to read student records*/
#include<stdio.h>
#include<stdlib.h>
struct student
{
int rno;
char nm[30];
float fee;
};

main()
{
FILE *fp;
struct student s;
clrscr();
fp=fopen("c:\\stu_list.dat","r");
if(fp==NULL)
{
printf("\n Unable to open");
exit(0);
}
printf("\nRoll no name fee");
while(fscanf(fp,"%d%s%f",&s.rno,s.nm,&s.fee)!=EOF)
printf("\n %d\t%s\t%f",s.rno,s.nm,s.fee);

fclose(fp);
getch();
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 141
C-Language Notes
}
/*use fwrite to store student records*/
#include<stdio.h>
#include<stdlib.h>
struct student
{
int rno;
char nm[30];
float fee;
};

main()
{
FILE *fp;
struct student s;
char ch='y';
fp=fopen("c:\\stu_list.dat","wb");
if(fp==NULL)
{
printf("\n Unable to create");
exit(0);
}
while(ch=='y' || ch=='Y')
{
printf("\nRoll no, name, fee:");
scanf("%d%s%f",&s.rno,s.nm,&s.fee);
fwrite(&s,sizeof(s),1,fp);
printf("\nPress Y key to continue:");
fflush(stdin);
ch=getche();
}
fclose(fp);
getch();
}
/*use fread to read student records*/
#include<stdio.h>
#include<stdlib.h>
struct student
{
int rno;
char nm[30];
float fee;
};

main()
{
FILE *fp;
struct student s;
clrscr();
fp=fopen("c:\\stu_list.dat","rb");
if(fp==NULL)
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 142
C-Language Notes
{
printf("\n Unable to create");
exit(0);
}
printf("\nRoll no name fee");
while(fread(&s,sizeof(s),1,fp)!=0)
printf("\n %d %s
%f",s.rno,s.nm,s.fee);

fclose(fp);
getch();
}
/*Define a structure item with members particular, rate and
price. Read these data from keyboard to item variable then
write to and read from file.*/
#include<stdio.h>
#include<conio.h>
struct item
{
char p[15];
int r,q;
}i,o;
void main()
{
FILE *fp;
clrscr();
printf("\nInput Item name,rate,quantity");
scanf("%s%d%d",i.p,&i.r,&i.q);
fp=fopen("file1.dat","w");
fprintf(fp,"%s %d %d\n",i.p,i.r,i.q);
fclose(fp);

fp=fopen("file1.dat","r");
fscanf(fp,"%s%d%d",o.p,&o.r,&o.q);
printf("\nItem name=%s,rate=%d,quantity=%d",o.p,o.r,o.q);

fclose(fp);
getch();
}
/*search string in file*/
#include<stdio.h>
#include<stdlib.h>

main()
{
FILE *fp;
int cmp,count=0,len;
char str1[30],str2[30];
clrscr();
fp=fopen("c:\\find.txt","r");
if(fp==NULL)
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 143
C-Language Notes
{
printf("\n not found");
exit(0);
}
printf("\nenter any searched string:");
len=strlen(gets(str1));

while(fgets(str2,len+1,fp)!=NULL)
{
cmp=strcmp(str1,str2);
if(cmp==0)
{
printf("\nFound at %d",count);
break;
}
fseek(fp,++count,SEEK_SET);
}

fclose(fp);
getch();
return(0);
}
/* display calendar*/
#include<stdio.h>
#include<conio.h>

/* function returns last day number of given year*/

int first_day(int month,int year)


{
int prv_year,cent_year=0,hund_year=0,tenth_year=0;
int leap_year=0, ord_year=0;
int odd_day1=0,odd_day2=0,last_day,days;

prv_year=year-1;

cent_year=(prv_year/400)*400;
hund_year=(prv_year-cent_year)/100;
tenth_year=(prv_year-cent_year)%100;
leap_year=tenth_year/4;
ord_year=tenth_year-leap_year;

odd_day1=hund_year*5;
odd_day2=(leap_year*2)+ord_year;
last_day=(odd_day1+odd_day2)%7;

switch(month)
{
case 1: //jan
days=1;
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 144
C-Language Notes
break;
case 2://feb
days=31+1;
break;
case 3: //mar
days=31+28+1;
break;
case 4: //apr
days=31+28+31+1;
break;
case 5://may
days=31+28+31+30+1;
break;
case 6://jun
days=31+28+31+30+31+1;
break;
case 7://jul
days=31+28+31+30+31+30+1;
break;
case 8://aug
days=31+28+31+30+31+30+31+1;
break;
case 9://sep
days=31+28+31+30+31+30+31+31+1;
break;
case 10://oct
days=31+28+31+30+31+30+31+31+30+1;
break;
case 11://nov
days=31+28+31+30+31+30+31+31+30+31+1;
break;
case 12://dec
days=31+28+31+30+31+30+31+31+30+31+30+1;
break;
}
if(year%4==0&&month!=1&&month!=2)
days+=1;

return((last_day+days)%7);

/* function for returning day name of given day number*/


char * dayname(int n)
{
switch (n)
{
case 0:
return("Sunday");
case 1:
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 145
C-Language Notes
return("Monday");
case 2:
return("Tuesday");
case 3:
return("Wednusday");
case 4:
return("Thusday");
case 5:
return("Friday");
case 6:
return("Saturday");
}
}

int days_in_month(month,year)
{
switch(month)
{
case 1: //jan
return(31);
case 2://feb
if(year%4==0)
return(29);
else
return(28);
case 3: //mar
return(31);
case 4: //apr
return(30);
case 5://may
return(31);
case 6://jun
return(30);
case 7://jul
return(31);
case 8://aug
return(31);
case 9://sep
return(30);
case 10://oct
return(31);
case 11://nov
return(30);
case 12://dec
return(31);
}
}

char* monthname(month)
{
switch(month)
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 146
C-Language Notes
{
case 1: //jan
return("JANUARY");
case 2://feb
return("FEBRUARY");
case 3: //mar
return("MARCH");
case 4: //apr
return("APRIL");
case 5://may
return("MAY");
case 6://jun
return("JUNE");
case 7://jul
return("JULY");
case 8://aug
return("AUGUEST");
case 9://sep
return("SEPTEMBER");
case 10://oct
return("OCTOBER");
case 11://nov
return("NOVEMBER");
case 12://dec
return("DECEMBER");
}
}

void display(int day,int month, int year)


{
int total_day,days[42],i,j,r=16,c=20,count=0; //days of
the month
clrscr();
gotoxy(25,2);
printf("******* CALENDER ******");
gotoxy(30,8);
printf("%s: %d",monthname(month),year);
gotoxy(15,40);
printf("Developed By: Lokesh Rathore Mob:94250-34034");
textcolor(RED);
gotoxy(20,15);cprintf("SUN");
gotoxy(25,15);printf("MON");
gotoxy(30,15);printf("TUE");
gotoxy(35,15);printf("WED");
gotoxy(40,15);printf("THU");
gotoxy(45,15);printf("FRI");
gotoxy(50,15);printf("SAT");

total_day=days_in_month(month,year);

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 147
C-Language Notes
for(i=0;i<day;i++)
days[i]=NULL;
for(j=1; j<=total_day;j++)
days[i++]=j;
for(;i<42;i++)
days[i]=NULL;
for(i=0;i<42;i++)
{
if(i%7==0)
{
r+=2;
count=0;
}
if(days[i]!=NULL)
{
gotoxy(20+(count*5),r);
if(i%7==0) cprintf(" %2d ",days[i]);
else printf(" %2d ",days[i]);
}
count++;
}
}

void main()
{
int year,month=1,day;
char ch;
do
{
clrscr();
printf("\nEnter month number:");
scanf("%d",&month);
printf("\n Enter year:");
scanf("%d",&year);
day=first_day(month,year);
display(day,month,year);
gotoxy(25,50);
printf("\nPress Y key to continue:");
fflush(stdin);
ch=getche();
if(ch=='Y' || ch=='y')
continue;
else
break;
}while(1);
}

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 148
C-Language Notes
/* Convert Decimal to Binary, Hexa_Descimal and octal*/

#include<stdio.h>
#include<conio.h>
#include<math.h>

void oh_b(int *n,int count,int pair)


{
int arr[40],t[4],i=0,j=0,sum=0;

for(;count>=0;count--)
{
t[i++]=*(n+count)%2;

}
}
count=j-1;

for( ;count>=0;count--)
{
switch (arr[count])
{
case 10:
putch('A');
break;
case 11:
putch('B');
break;
case 12:
putch('C');
break;
case 13:
putch('D');
break;
case 14:
putch('E');
break;
case 15:
putch('F');
break;
default:
printf("%d",arr[count]);
}
}
}
void main()
{
int arr[20],i=0,count=0;
char ch;
clrscr();
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 149
C-Language Notes
gotoxy(10,2);
printf("**** CONVERSION: BINARY TO OCTAL & HEXA-DECIMAL
****");
printf("\n\nYou can perform:- \
\n\n\n Binary to Octal and Hexadecimal \
\n\nEnter Your Binary number:");
while((ch=getche())!=13)
{
if (ch<48 || ch>49)
{
printf("\nNot valid Binary digit");
getch();
exit(0);
}
if (ch=='0') arr[i++]=0;
if (ch=='1') arr[i++]=1;
}
count=i-1;

printf("\n\nOctal Equivalent="); bi_oh(arr,count,3);


getch();

printf("\n\nHexa-Decimal Equivalent="); bi_oh(arr,count,4);


getch();

}
/* Convert Decimal to Binary, Hexa_Descimal and octal*/

#include<stdio.h>
#include<conio.h>
#include<math.h>

void bi_oh(int *bi,int count,int pair)


{
int arr[20],i=0,j=0,sum=0;

for(;count>=0;count--)
{
sum+=*(bi+count)*pow(2,i++);
if(i==pair||count==0)
{
arr[j++]=sum;
i=0; sum=0;
}
}
count=j-1;

for( ;count>=0;count--)
{
switch (arr[count])
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 150
C-Language Notes
{
case 10:
putch('A');
break;
case 11:
putch('B');
break;
case 12:
putch('C');
break;
case 13:
putch('D');
break;
case 14:
putch('E');
break;
case 15:
putch('F');
break;
default:
printf("%d",arr[count]);
}
}
}

void main()
{
int arr[20],i=0,count=0;
char ch;
clrscr();
gotoxy(10,2);
printf("**** CONVERSION: BINARY TO OCTAL & HEXA-DECIMAL
****");
printf("\n\nYou can perform:- \
\n\n\n Binary to Octal and Hexadecimal \
\n\nEnter Your Binary number:");
while((ch=getche())!=13)
{
if (ch<48 || ch>49)
{
printf("\nNot valid Binary digit");
getch();
exit(0);
}
if (ch=='0') arr[i++]=0;
if (ch=='1') arr[i++]=1;
}
count=i-1;

printf("\n\nOctal Equivalent="); bi_oh(arr,count,3);


Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 151
C-Language Notes
getch();

printf("\n\nHexa-Decimal Equivalent="); bi_oh(arr,count,4);


getch();

}
/* Convert Binary, Hexa_Descimal and octal to Decimal*/

#include<stdio.h>
#include<conio.h>
#include<math.h>

void bi_dec()
{
int arr[10],i=0,j,count=0,deci;
char ch;
clrscr();
printf("\nEnter any Binary Number:");
while((ch=getche())!=13&&i<10)
{
if (ch<48 || ch>49)
{
printf("\nNot Enter Binary Digits");
getch();
return;
}
if (ch==48) arr[i]=0;
if (ch==49) arr[i]=1;
i++;
count++;
}

deci=0;
for(i=i-1,j=0;i>=0;i--,j++)
deci=deci+(arr[i]*pow(2,j));
printf("\nDecimal Value=%d",deci);
}

void oct_dec()
{
int arr[10],i=0,j,count=0,deci;
char ch;
clrscr();
printf("\nEnter any Octal Number up to 10 digits:");
while((ch=getche())!=13&&i<10)
{
if (ch<48 || ch>55)
{
printf("\nNot Enter Octal Digits");
getch();
return;
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 152
C-Language Notes
}
switch(ch)
{
case 48:
arr[i]=0;
break;
case 49:
arr[i]=1;
break;
case 50:
arr[i]=2;
break;
case 51:
arr[i]=3;
break;
case 52:
arr[i]=4;
break;
case 53:
arr[i]=5;
break;
case 54:
arr[i]=6;
break;
case 55:
arr[i]=7;
break;
}
i++;
count++;
}
}
void hex_dec()
{
int arr[10],i=0,j,count=0,deci;
char ch;
clrscr();
printf("\nEnter any Hexa Decimal Number up to 10 digits:");
while((ch=getche())!=13&&i<10)
{
if ((ch>=48 && ch<=57)||(ch>=65&&ch<='F'))
{
switch(ch)
{
case 48:
arr[i]=0;
break;
case 49:
arr[i]=1;
break;
case 50:
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 153
C-Language Notes
arr[i]=2;
break;
case 51:
arr[i]=3;
break;
case 52:
arr[i]=4;
break;
case 53:
arr[i]=5;
break;
case 54:
arr[i]=6;
break;
case 55:
arr[i]=7;
break;
case 56:
arr[i]=8;
break;
case 57:
arr[i]=9;
break;
case 65:
arr[i]=10;
break;
case 66:
arr[i]=11;
break;
case 67:
arr[i]=12;
break;
case 68:
arr[i]=13;
break;
case 69:
arr[i]=14;
break;
case 70:
arr[i]=15;
break;
}
i++;
count++;
}
else
{
printf("\nNot Valid Hexadecimal Digits");
getch();
return;
}
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 154
C-Language Notes
}
deci=0;
for(i=i-1,j=0;i>=0;i--,j++)
deci=deci+(arr[i]*pow(16,j));
printf("\nDecimal Value=%d",deci);
}

void main()
{
int ch;
clrscr();
gotoxy(10,2);
printf("**** CONVERSION: BINARY,OCTAL & HEXA-DECIMAL TO
DECIMAL ****");
printf("\n\nYou can perform following conversion\
\n\n1:Binary to Decimal\
\n\n2:Octal to Decimal\
\n\n3:Hexa-Decimal to Decimal\
\n\nEnter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
bi_dec();
break;
case 2:
oct_dec();
break;
case 3:
hex_dec();
break;
default:
printf("\nMade Wrong Choice");
}
getch();
}
/* Convert Decimal to Binary, Hexa_Descimal and octal*/

#include<stdio.h>
#include<conio.h>
#include<math.h>

void dec_boh(int deci,int base)


{
int arr[20],i=0,count=0;

while(deci!=0)
{
Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 155
C-Language Notes
arr[i++]=deci%base;
deci/=base;
}
count=i-1;
for( ;count>=0;count--)
{
switch (arr[count])
{
case 10:
putch('A');
break;
case 11:
putch('B');
break;
case 12:
putch('C');
break;
case 13:
putch('D');
break;
case 14:
putch('E');
break;
case 15:
putch('F');
break;
default:
printf("%d",arr[count]);
}
}
}

void main()
{
int deci;
clrscr();
gotoxy(10,2);
printf("**** CONVERSION: DECIMAL TO BINARY,OCTAL & HEXA-
DECIMAL ****");
printf("\n\nYou can perform:- \
\n\n\n Decimal to Binary,Octal and Hexadecimal \
\n\nEnter Your decimal number:");
scanf("%d",&deci);
printf("\n\nBinary Equivalent="); dec_boh(deci,2);
getch();

printf("\n\nOctal Equivalent="); dec_boh(deci,8);


getch();

printf("\n\nHexaDecimal Equivalent="); dec_boh(deci,16);


Author: Mr. Lokesh Rathore (MCA, MTech)
WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 156
C-Language Notes
getch();

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 157
C-Language Notes

Author: Mr. Lokesh Rathore (MCA, MTech)


WhatsApp&Call: 9425034034, website: www.LRsir.net, Email: [email protected]
P a g e | 158

You might also like