MSC MCA BCA-C Language-Lokesh Rathore
MSC MCA BCA-C Language-Lokesh Rathore
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.
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
ु र भाषा कहते ह। जैसे बाइन6र, एस.7ल8, सी,सी++, जावा,सी#, पीएचपी, !वजअ
उसे क./यट ु ल
बे&सक आ3द।
Hard disk: Secondary storage device used to store progam and result in the
form of file parmanently.
यह एक सेकंडर8 टोरे ज उपकरण है िजसका उपयोग ोाम और प6रणाम को फ़ाइल के ?प मे
थायी ?प से टोर करने के &लए करते ह।
CPU
ALU+CU+Registers
Hard Disk
(Data Files)
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.
!वशेषताएँ:
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.
Input-n
i=n, Fact=1
Is F
Print-Fact
i>0 ?
T
Fact=Fact*i End
i=i-1
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
अंतत: सबसे अंत मे 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 लाइन मे भी &लखा जा सकता है ।
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
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 लाइन से ोाम रन होना शु? होता है और अंतम लाइन
पर ख\म होता है ।
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द)।
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: Arithmetic (+, -, *, /, %), Relational (<,<=, >, >=, ==,!=), Logical (&&,
||, !), Assignment (=, +=, -=, *=, /=, %=), Increment (++), decrement (--),
Conditional (? :), Bitwise (&, |, ~, ^, >>, <<)
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.
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
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
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 टाइप कवश-न कहते ह।
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.
Statement1 Statement2
Statement-x
Statement-x
}
:::::: False
else Statement-n
{
Statement-n
} Statement-x
Statement-x
default:
default statement;
Statement-x
}
True
Statement1
Statement1
statement1 Nयािवत होता है उसके बाद condition1 चेक होती है। य3द यह स\य है
तब ह8 statement1 फर से Nयािवत होता है और एक बार फर से condition1 चेक
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 लप
ू तीन भाग से &मलकर बना
चेक करता है और तीसरा भाग वैQयू म बदलाव करता है । य3द condition1 स\य है तब ह8
statement1 Nयािवत होता है, इसके बाद मोFड़फाइ भाग Nयािवत होगा, उसके बाद
condition1 फर से चेक होगी। Nयावयन कE यह Nया नरं तर बनी रहती है जब तक
कE condition1 अस\य नह8ं हो जाती।
जब एक लूप oOचर को कसी अय लूप oOचर मे प6रभा!षत करते ह तब इसे लूप
कE नेि टं ग कहा जाता है । लप
ू कE नेि टं ग को कई कार से न&म-त कर सकते ह जैस-े
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
Syntax:
continue;
continue should be used conditionally.
continue का उपयोग कंFडशन के साथ कया जाना चा3हए।
Syntax:
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.
For example:
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 सं_या के अनस
ु ार साइज़ वत: सेट हो जाती है ।
From this program it is clear that we can use for loop to access every data of
one dimensional array.
इस ोाम से प ट है क हम एक !वमीय अरे के \येक डाटा को एOसैस करने के
&लए for लूप का उपयोग कर सकते है ।
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 सं_या के अनुसार साइज़ वत: सेट हो जाती है ।
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}}};
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 सं_या के अनुसार साइज़ वत: सेट हो जाती है ।
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 कुल साइज़ के योग के
बराबर होती है ।
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द।
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सैस कया जा सकता है ।
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.
Memory Allocation:
uv.a / uv.b / uv.c
2.5
size=4B(maximum)
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र/लेस
हो जाती है ।
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.
Syntax:
ftype fname(type, type,..,type);
Example:
int sum(int, int);
It is declared before function calling.
इसे फंOशन को कॉल करने से पहले FडOलैर कया जाता है ।
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शन होते ह।
Demerits:
1) Slow down execution. Nयावयन कE गत को धीमी कर दे ता है ।
2) Consume large memory space. मेमोर8 पेस अeधक लगती है ।
जब वे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
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.
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.
इसका उपयोग 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 उपयोग पोइंटर के साथ कर सकते ह।
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
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ैस कॉपी होता है ।
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 होगा।
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
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सैस करने के &लए करते है ।
कसी 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
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 सूचना पट हो सके।
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
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().
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.
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रभा!षत है ।
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>
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
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-
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
PART9
Preprocessor:
Preprocessor statements
File include
Macro Directives
Passing argument to macro
Conditional compilation:
#ifdef, #ifndef, #if, #elif, #else, #endif.
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 खोजता है ।
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ो का उपयोग कर सकते ह।
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
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;
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("\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("\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 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();
}
#include<stdio.h>
#include<conio.h>
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();
}
#include<stdio.h>
#include<conio.h>
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 main()
{
int f,s;
clrscr();
fibo(1,1);
getch();
}
#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>
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);
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);
}
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);
}
/* 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
}
/*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);
}
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);
}
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();
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;
};
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>
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);
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");
}
}
total_day=days_in_month(month,year);
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);
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
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;
}
/* Convert Decimal to Binary, Hexa_Descimal and octal*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
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;
}
/* 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>
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();