Professional Documents
Culture Documents
Programming in C: Shubham Computech CSC Academy
Programming in C: Shubham Computech CSC Academy
0
1
CHAPTER 1- INTRODUCTION
WHAT IS PROGRAMMING?
WHAT IS C?
USES OF C
BASIC STRUCTURE OF C PROGRAMMING
COMMENTS
COMPILATION AND EXECUTION
LIBRARY FUNCTIONS
C TOKENS
IDENTIFIERS
KEYWORDS
CONSTANTS
OPERATORS
DATA TYPES IN C
2
1. WHAT IS PROGRAMMING – 4. C is used to develop games an area where latency is
Computer Programming is a medium for us to very important. i.e. Computer has to react quickly
communicate with computers. Just like we use ‘Hindi’ on user input.
or ‘English’ to Communicate with each other, 1. प्रोग्रामिंग क्या है –
programming is a way for us to deliver our instructions कं प्यूटर प्रोग्रामिंग हमारे लिए कं प्यूटर के साथ संवाद करने का
to the computer. There is a close analogy between एक माध्यम है। जैसे हम एक-दूसरे के साथ संवाद करने के लिए 'हिंदी' या
learning English language and learning C language.
The classical method of learning English is to first learn
'अंग्रेजी' का उपयोग करते हैं, प्रोग्रामिंग हमारे लिए हमारे निर्देशों को कं प्यूटर
the alphabets used in the language, then learn to को वितरित करने का एक तरीका है। अंग्रेजी भाषा सीखने और C भाषा
combine these alphabets to form words, which in turn सीखने के बीच एक करीबी सादृश्य है। अंग्रेजी सीखने की शास्त्रीय विधि
are combined to form sentences and sentences are पहले भाषा में उपयोग किए जाने वाले वर्णों को सीखना है, फिर शब्दों को
combined to form paragraphs. Learning C is similar बनाने के लिए इन वर्णों को संयोजित करना सीखें, जो बदले में वाक्य बनाने
and easier. Instead of straight-away learning how to के लिए संयुक्त होते हैं और वाक्यों को
write programs, we
must first know what
पैराग्राफ बनाने के लिए जोड़ा जाता है। C
alphabets, numbers and सीखना समान और आसान है। सीधे प्रोग्राम
special symbols are लिखना सीखने के बजाय, हमें पहले यह
used in C, then how जानना चाहिए कि C में किस अक्षर, संख्या
using them constants, और विशेष प्रतीकों का उपयोग किया जाता
variables and है, फिर उनका उपयोग करके स्थिरांक, चर
keywords are
constructed, and finally
और कीवर्ड का निर्माण कै से किया जाता है,
how are these combined और अंत में इन्हें एक निर्देश बनाने के लिए
to form an instruction. A group of instructions would be कै से जोड़ा जाता है। निर्देशों के एक समूह को एक पोग्राम बनाने के लिए
combined to form a program. जोड़ा जाएगा।
2. WHAT IS C – 2. C क्या है –
C is a programming language. It is a one of the oldest C एक प्रोग्रामिंग भाषा है। यह सबसे पुरानी और बेहतरीन प्रोग्रामिंग
and finest programming languages’ was developed by भाषाओं में से एक है जिसे बेल प्रयोगशालाओं (पूर्व में AT & T बेल
Dennis M. Ritchie at Bell Laboratories (formerly
AT&T Bell Laboratories). It was initially designed for प्रयोगशालाओं) में डेनिस एम रिची द्वारा विकसित किया गया था। शुरू में
programming UNIX operating system. The software यह प्रोग्रामिंग यूनिक्स ऑपरेटिंग सिस्टम के लिए डिज़ाइन किया गया था।
tool as well as the C compiler is written in C. Major सॉफ्टवेयर टूल के साथ-साथ C कं पाइलर को C में लिखा गया है। विंडोज,
parts of operating systems like Windows, UNIX, Linux यूनिक्स, लिनक्स जैसे ऑपरेटिंग सिस्टम के प्रमुख हिस्सों को अभी भी C में
is still written in C. This is because even today when it
comes to performance (speed of execution) nothing
लिखा गया है। ऐसा इसलिए है क्योंकि आज भी जब प्रदर्शन (निष्पादन की
beats C. Moreover, if one is to extend the operating गति) की बात आती है तो कु छ भी C को हरा नहीं सकता है। इसके
system to work with new devices one needs to write अलावा, यदि कोई नए उपकरणों के साथ काम करने के लिए ऑपरेटिंग
device driver programs. These programs are सिस्टम का विस्तार करना चाहता है, तो उसे डिवाइस ड्राइवर प्रोग्राम
exclusively written in C.
लिखने की आवश्यकता है। ये कार्यक्रम विशेष रूप से C में लिखे गए हैं।
3. USES OF C –
C language is used to program a wide variety of 3. C भाषा का उपयोग –
systems some of the uses of C are as follows: C विभिन्न प्रकार के सिस्टम को प्रोग्राम करने के लिए किया जाता है C के
1. Major parts of windows, Linux and other operating कु छ उपयोग इस प्रकार हैं:
systems are written in C.
2. C is used to write driver programs for devices like
1. विंडोज, लिनक्स और अन्य ऑपरेटिंग सिस्टम के प्रमुख भागों को C
Tablets, printers etc. में लिखा जाता है।
3. C language is used to program embedded systems 2. C का उपयोग टैबलेट, प्रिंटर आदि जैसे उपकरणों के लिए ड्राइवर
where programs need to run faster in limited प्रोग्राम लिखने के लिए किया जाता है।
memory (Microwave, cameras etc.)
3
3. C भाषा का उपयोग एम्बेडेड सिस्टम को प्रोग्राम करने के लिए किया
जाता है जहां प्रोग्राम को सीमित मेमोरी (माइक्रोवेव, कै मरा आदि) में
तेजी से चलाने की आवश्यकता होती है।
4. C का उपयोग गेम विकसित करने के लिए किया जाता है, एक ऐसा
क्षेत्र जहां विलंबता बहुत महत्वपूर्ण है। यानी कं प्यूटर को उपयोगकर्ता
इनपुट पर जल्दी से प्रतिक्रिया करनी होती है।
4. BASIC STRUCTURE OF C 4. C प्रोग्रामिंग की मूल संरचना
PROGRAMMING
a. Documentation section
b. Link section
c. Definition section
d. Global declaration section
e. main()
{
f. Declaration part
g. Executable part
(statements)
}
h. Sub-program section
7. LIBRARY FUNCTIONS
Library functions are built-in functions that are
grouped together and placed in a common location
called library. Each function here performs a specific
All C programs have to follow a basic structure. operation. We can use this library functions to get the
There are some rules which are applicable to all the pre-defined output. All C standard library functions
C programs: are declared by using many header files. These library
Every program’s execution starts from main functions are created at the time of designing the com-
() function. pilers. We include the header files in our C program
All The Statements are terminated with a by using #include<filename.h>. Whenever the pro-
semicolon. gram is run and executed, the related files are in-
Instructions are case – sensitive. cluded in the C program.
Instructions are executed in the some order सभी C प्रोग्रामों को एक बुनियादी संरचना का पालन करना होगा। कु छ
in which they are written.
नियम हैं जो सभी C प्रोग्रामों पर लागू होते हैं:
5. COMMENTS प्रत्येक प्रोग्राम का निष्पादन मुख्य () फ़ं क्शन से शुरू होता है।
Comments are used to clarify something about the सभी कथन को (;) अर्धविराम के साथ समाप्त किया जाता
program in plain language. It is a way for us to add है।
notes to our program. There are two types of
comments in C. निर्देश मामले हैं - संवेदनशील।
1. Single line comment: - // this is a com- निर्देशों को कु छ क्रम में निष्पादित किया जाता है जिसमें वे
ment.
2. Multi – line comment :- /* This is a multi- लिखे जाते हैं।
line comment */ 5. टिप्पणियाँ
Comment in a C program are not executed and
are ignored. टिप्पणियों का उपयोग सादे भाषा में कार्यक्रम के बारे में कु छ स्पष्ट करने के
लिए किया जाता है। यह हमारे लिए हमारे कार्यक्रम में नोट्स जोड़ने का एक
6. COMPILATION AND EXECUTION तरीका है। सी में दो प्रकार की टिप्पणियां हैं।
A compiler is a 1. एकल पंक्ति टिप्पणी: - / / यह एक टिप्पणी है।
First.C C Compiler Execution
computer program which 2. बहु-पंक्ति टिप्पणी: - / * यह एक बहु-पंक्ति
converts a C program into machine language so that
it can be easily understood by the computer. A C
टिप्पणी है */
program is written in plain text. C प्रोग्राम में टिप्पणी निष्पादित नहीं की जाती है और उन्हें अनदेखा
किया जाता है।
Library के लिए उपयोग करें
Func- Heade के लिए उपयोग करें
tion r File Use for 6. संकलन और निष्पादन
printf() stdio.h printing desired स्क्रीन पर वांछित कं पाइलर एक कं प्यूटर प्रोग्राम होता है जो सी प्रोग्राम को मशीन भाषा
output on the आउटपुट मुद्रित करना में परिवर्तित First.exe करता है ताकि वह कं प्यूटर द्वारा
screen
scanf() stdio.h accepting dataउपयोगकर्ता से डेटा आसानी से समझा जा सके एक सी प्रोग्राम सादे
from the userस्वीकार करना टेक्स्ट में लिखा गया है। यह प्लेन टेक्स्ट एक विशेष अनुक्रम में
strcmp string.h comparing two दो स्ट्रिंग्स की तुलना निर्देशों का संकलन है। कं पाइलर कु छ बुनियादी जांच करता है और
strings
length() string.h finding out the लंबाई का पता लगाएं अंत में प्रोग्राम को निष्पादन योग्य में परिवर्तित करता है।
length of string
pow() math.h for calculating प्रतिपादक की गणना 7. लाइब्रेरी फ़ं क्शन
the exponent के लिए
5
sqrt() math.h for calculating वर्ग मूल की गणना के
the square root लिए
लाइब्रेरी फ़ं क्शन अंतर्निहित फ़ं क्शन होते हैं जिन्हें एक साथ समूहीकृ त
किया जाता है और लाइब्रेरी नामक एक सामान्य स्थान पर रखा जाता है।
यहां प्रत्येक फ़ं क्शन एक विशिष्ट ऑपरेशन करता है। हम पूर्व-परिभाषित
आउटपुट प्राप्त करने के लिए इस लाइब्रेरी फ़ं क्शन का उपयोग कर सकते
हैं। सभी C मानक लाइब्रेरी फ़ं क्शन कई हेडर फ़ाइलों का उपयोग करके
घोषित किए जाते हैं। ये लाइब्रेरी फ़ं क्शन कं पाइलर्स को डिजाइन करने के
समय बनाए जाते हैं। हम #include<filename.h> का उपयोग करके
अपने C प्रोग्राम में हेडर फ़ाइलों को शामिल करते हैं। जब भी प्रोग्राम
चलाया और निष्पादित किया जाता है, तो संबंधित फाइलें सी प्रोग्राम में
शामिल होती हैं।
6
8. C TOKENS 3. Variable names are case sensitive.
The smallest individual units are known as tokens. 4. A variable name contains maximum of 30
C has six types of tokens. characters/ Variable name must be upto 8
1: Identifiers characters.
2: Keywords 8. C टोकन
3: Constants सबसे छोटी व्यक्तिगत इकाइयों को टोकन के रूप में जाना जाता है। C में
4: Strings छह प्रकार के टोकन हैं।
5: Special Symbols
1: Identifiers
6: Operators
2: Keywords
A. Identifiers: 3: Constants
Identifiers refer to the names of variables, 4: Strings
constants, functions and arrays. These user-defined 5: Special Symbols
names are called Identifiers. 6: Operators
C. Keywords: C. कीवर्ड:
These are reserved words, whose meaning is ये आरक्षित शब्द हैं, जिनका अर्थ पहले से ही कं पाइलर को पता है। C
already known to the compiler. There are 32 में 32 कीवर्ड उपलब्ध हैं।
keywords available in C.
auto double int struct do goto sizeof unsigned
creak long else switch static volatile void float
case return enum typedef if continue for short
char register extern union while signed default const
D. ऑपरेटर्स:
D. Operators:
सी ऑपरेटर में प्रतीक होते हैं जिनका उपयोग गणितीय या तार्कि क
In C operators are symbols that are used to perform
mathematical or logical manipulations. The C pro- जोड़तोड़ करने के लिए किया जाता है। सी प्रोग्रामिंग भाषा अंतर्निहित
gramming language is rich with built-in operators. ऑपरेटरों के साथ समृद्ध है। ऑपरेटर डेटा और चर में हेरफे र करने के लिए
Operators take part in a program for manipulating एक कार्यक्रम में भाग लेते हैं और गणितीय या तार्कि क अभिव्यक्तियों का
data and variables and form a part of the mathemat-
ical or logical expressions. C offers various types of
एक हिस्सा बनाते हैं। सी विभिन्न प्रकार के ऑपरेटरों की पेशकश करता है
operators having different functioning capabilities. जिनके पास विभिन्न कार्य क्षमताएं होती हैं।
अंकगणितीय ऑपरेटर
Arithmetic Operators
रिलेशनल ऑपरेटर्स
Relational Operators
Logical Operators लॉजिकल ऑपरेटर्स
Assignment Operators असाइनमेंट ऑपरेटर्स
Conditional Operator
सशर्त ऑपरेटर
Bitwise Operators
Special Operators बिटवाइज ऑपरेटर्स
स्पेशल ऑपरेटर्स
Arithmetic Operators are used to perform अरिथमैटिक ऑपरेटर्स का उपयोग दिए गए ऑपरेंड पर गणितीय
mathematical operations such as addition, sub-
traction, multiplication, division, modulus, etc., संचालन जैसे जोड़, घटाव, गुणा, विभाजन, मापांक आदि करने के लिए
on the given operands. The C Arithmetic Oper- किया जाता है। सी अंकगणितीय ऑपरेटर दो प्रकार के होते हैं जो उनके
8
द्वारा काम किए जाने वाले ऑपरेंड की संख्या के आधार पर होते हैं। ये ii. --x: Pre decrement, first decrements and then
निम्नानुसार हैं: do the operation.
iii. x++: Post increment, first do the operation
1. बाइनरी अंकगणितीय ऑपरेटर
and then increment.
C में बाइनरी अंकगणितीय ऑपरेटर दो ऑपरेंड पर काम करते हैं या iv. x- - : Post decrement, first do the operation
काम करते हैं। सी अंकगणितीय कार्यों को करने के लिए 5 and then decrement.
द्विआधारी अंकगणितीय ऑपरेटर प्रदान करता है जो निम्नानुसार हैं:
9
ii. --x: प्री- डिक्रीमेंट, पहले डिक्रीमेंट और फिर ऑपरेशन करता
है।
iii. x++: पोस्ट इंक्रीमेंट में पहले ऑपरेशन होता है और बाद में
इन्क्रीमेंट होता है।
iv. x- - : पोस्ट डिक्रीमेंट में पहले ऑपरेशन होता है और बाद में
डिक्रीमेंट होता Operator Meaning Example Return value Logical Not: This
है। < is less than 2<9 1 operator reverses
<= is less than or 2<=2 1 the value of the
Relational Op- equal to expression it
erator checks the > is greater than 2>9 0 operates on i.e, it
relationship > = is greater than 3>=2 1
makes a true
between two oper- or equal to
expression false
ands. If the relation = = is equal to 2==3 0
and false
is true, it returns 1; != is not equal to 2!=2 0
expression true.
if the relation is false, it returns value 0. Operands
may be variables, constants or expressions.
Relational operators are used in decision mak- Op !Op
ing and loops. True False
False True
11
The output of bitwise OR is 1 if at least one
corresponding bit of two operands is 1. In C
Programming, bitwise OR operator is denoted by |.
कं डीशनल ऑपरेटर / टर्नेरी ऑपरेटर स्थिति की जांच करता है Bitwise XOR (exclusive OR) operator ^
The result of bitwise XOR operator is 1 if the
और स्थिति के आधार पर स्टेटमेंट निष्पादित करता है। एक कं डीशनल
corresponding bits of two operands are opposite. It is
ऑपरेटर एक टर्नेरी ऑपरेटर है, अर्थात, यह 3 ऑपरेंड पर काम करता denoted by ^.
है। कं डीशनल ऑपरेटर में दो प्रतीक होते हैं:
Bitwise complement operator (~)
प्रश्न चिह्न (?) Bitwise compliment operator is an unary operator
कोलन (:) (works on only one operand). It changes 1 to 0 and 0
Syntax: condition? exp1: exp2; to 1. It is denoted by ~.
यह पहले स्थिति का मूल्यांकन करता है, यदि यह सही है (गैर-शून्य) तो
Right Shift Operator
"exp1" का मूल्यांकन किया जाता है, यदि स्थिति गलत (शून्य) है तो Right shift operator shifts all bits towards right by cer-
"exp 2" का मूल्यांकन किया जाता है। tain number of specified bits. It is denoted by >>.
उदाहरण: (A> 100? 0: 1); उपरोक्त उदाहरण में, यदि A 100 से Left Shift Operator
अधिक है, तो 0 वापस कर दिया जाता है अन्यथा 1 वापस कर दिया Left shift operator shifts all bits towards left by cer-
जाता है। tain number of specified bits. It is denoted by <<.
बिटवाइज ऑपरेटर: बिट स्तर पर डेटा में हेरफे र करने के लिए Special Operators
उपयोग किया जाता है। यह के वल पूर्णांक पर काम करता है। यह 1) Comma Operator: The comma operator is used to
फ्लोट पर लागू नहीं किया जा सकता है। अरिथमेटिक लॉजिक separate the statement elements such as variables,
यूनिट (जो सीपीयू के भीतर है) में, गणितीय संचालन जैसे: जोड़, घटाव, constants or expressions, and this operator is used to
link the related expressions together, such expressions
गुणा और विभाजन बिट-स्तर में किए जाते हैं जो प्रसंस्करण को तेज can be evaluated from left to right and the value of
बनाता है और बिजली बचाता है। सी प्रोग्रामिंग में बिट-स्तरीय संचालन right most expressions is the value of combined ex-
करने के लिए, बिटवाइज ऑपरेटरों का उपयोग किया जाता है। pressions
Ex: val(a=3, b=9, c=77, a+c)
First asigns the value 3 to a, then assigns 9 to b, then
assigns 77 to c, and finaly80 (3+77) to value.
राइट शिफ्ट ऑपरेटर राइट शिफ्ट ऑपरेटर सभी बिट्स को निर्दिष्ट बिट्स
की निश्चित संख्या से दाईं ओर स्थानांतरित करता है। इसे >> द्वारा निरूपित
किया जाता है।
लेफ्ट शिफ्ट ऑपरेटर लेफ्ट शिफ्ट ऑपरेटर सभी बिट्स को निर्दिष्ट बिट्स
की निश्चित संख्या द्वारा बाईं ओर स्थानांतरित करता है। इसे << द्वारा
निरूपित किया जाता है।
विशेष ऑपरेटर
1) अल्पविराम ऑपरेटर: अल्पविराम ऑपरेटर का उपयोग चर, स्थिरांक
या अभिव्यक्ति जैसे कथन तत्वों को अलग करने के लिए किया जाता है,
और इस ऑपरेटर का उपयोग संबंधित अभिव्यक्तियों को एक साथ जोड़ने
के लिए किया जाता है, ऐसे अभिव्यक्तियों का मूल्यांकन बाएं से दाएं किया
जा सकता है और सबसे अधिक दाएं अभिव्यक्ति का मूल्य संयुक्त
अभिव्यक्तियों का मूल्य है। val(a=3,b = 9, c = 77, a + c) पहले मान
3 को a पर असाइन करता है, फिर 9 से b को असाइन करता है, फिर c को
77 और अंतिम 80 (3 + 77) मान देता है।
13
Operator precedence determines which operation is and % (Modulus) operators have the same precedence,
Operator Description Associativity
() Parentheses (function call) (see Note) left-to-right
[] Brackets (array subscript)
. Member selection via object name
-> Member selection via pointer
++ -- Postfix increment/decrement (see Note)
++ -- Prefix increment/decrement right-to-left
+- Unary plus/minus
!~ Logical negation/bitwise complement
(type) Cast (convert value to temporary value of type)
* Dereference
& Address (of operand)
sizeof Determine size in bytes on this implementation
*/% Multiplication/division/modulus left-to-right
+- Addition/subtraction left-to-right
<< >> Bitwise shift left, Bitwise shift right left-to-right
< <= Relational less than/less than or equal to left-to-right
> >= Relational greater than/greater than or equal to
== != Relational is equal to/is not equal to left-to-right
& Bitwise AND left-to-right
^ Bitwise exclusive OR left-to-right
| Bitwise inclusive OR left-to-right
&& Logical AND left-to-right
|| Logical OR left-to-right
?: Ternary conditional right-to-left
= Assignment right-to-left
+= -= Addition/subtraction assignment
*= /= Multiplication/division assignment
%= &= Modulus/bitwise AND assignment
^= |= Bitwise exclusive/inclusive OR assignment
<<= >>= Bitwise shift left/right assignment
, Comma (separate expressions) left-to-right
Note: Parentheses are also used to group sub-expressions to force a different precedence; such parenthetical expres-
sions can be nested and are evaluated from inner to outer. Postfix increment/decrement have high precedence, but the
actual increment or decrement of the operand is delayed (to be accomplished sometime before the statement completes
execution). So in the statement y = x * z++; the current value of z is used to evaluate the expression (i.e., z++ evaluates
to z) and z only incremented after all else is done.
performed first in an expression with more than one so the order of evaluation will be decided by associ-
operator with different precedence. For example, x = 7 ativity. According to the given table, the associativity
+ 3 * 2; here, x is assigned 13, not 20 because operator of the multiplicative operators is from Left to
Right. So, First / operator will be evaluated i.e (100 /
* has a higher precedence than +, so it first gets
5) is done, then expression will be 20 % 2. Now, the
multiplied with 3*2 and then adds into 7. % operator will be evaluated and result will be 0.
Operator associativity is used when two operators of ऑपरेटर वरीयता और संबद्धता
the same precedence appear in an expression. Associ-
ativity can be either from Left to Right or Right to ऑपरेटर वरीयता यह निर्धारित करती है कि अलग-अलग वरीयता वाले
Left. For example, 100 / 5 % 2; here both / (division) एक से अधिक ऑपरेटर के साथ अभिव्यक्ति में कौन सा ऑपरेशन पहले
14
किया जाता है। उदाहरण के लिए, X= 7 + 3 * 2; यहां, X को 13 quence in C language is a sequence of characters
असाइन किया गया है, न कि 20 क्योंकि ऑपरेटर * की वरीयता + की that doesn't represent itself when used inside string
literal or character. It is composed of two or more
तुलना में अधिक है, इसलिए इसे पहले 3 * 2 से गुणा किया जाता है और characters starting with backslash.
फिर 7 में जोड़ा जाता है। List of Escape Sequences in C:
ऑपरेटर एसोसिएशन का उपयोग तब किया जाता है जब एक ही वरीयता के
दो ऑपरेटर एक अभिव्यक्ति में दिखाई देते हैं। संबद्धता या तो बाएं से दाएं या
दाएं से बाएं हो सकती है। उदाहरण के लिए, 100 / 5 % 2; यहां / (डिवीजन)
और % (modulus) ऑपरेटरों दोनों की एक ही वरीयता है, इसलिए
मूल्यांकन का क्रम संबद्धता द्वारा तय किया जाएगा। दी गई तालिका के
अनुसार, गुणक ऑपरेटरों की संबद्धता बाएं से दाएं है। इसलिए, पहले /
ऑपरेटर का मूल्यांकन किया जाएगा यानी (100 / 5) किया जाता है, फिर
अभिव्यक्ति 20% 2 होगी। अब, % ऑपरेटर का मूल्यांकन किया जाएगा और
परिणाम 0 होगा।
9. ESCAPE SEQUENCE IN C
An
es- Escape विवरण:
cape Sequence Name Description
se- Alarm or इसका उपयोग सी प्रोग्राम में घंटी जैसी ध्वनि
It is used to generate a bell
\a
Beep sound in the C program. उत्पन्न करने के लिए किया जाता है।
It is used to move the cursor इसका उपयोग कर्सर को एक स्थान पीछे ले
\b Backspace
one place backward. जाने के लिए किया जाता है।
It is used to move the cursor to इसका उपयोग कर्सर को अगले तार्कि क पृष्ठ
\f Form Feed the start of the next logical की शुरुआत में ले जाने के लिए किया जाता
page. है।
It moves the cursor to the start यह कर्सर को अगली लाइन की शुरुआत में
\n New Line
of the next line. ले जाता है।
Carriage It moves the cursor to the start यह कर्सर को वर्तमान लाइन की शुरुआत में
\r
Return of the current line. ले जाता है।
It inserts some whitespace to यह कर्सर के बाईंओर कु छ व्हाइटस्पेस
Horizontal डालता है और कर्सर को तदनुसार
\t the left of the cursor and
Tab
moves the cursor accordingly. स्थानांतरित करता है।
It is used to insert vertical इसका उपयोग ऊर्ध्वाधर स्थान डालने के
\v Vertical Tab
space. लिए किया जाता है।
Use to insert backslash बैकस्लैश वर्ण सम्मिलित करने के लिए
\\ Backlash
character. उपयोग करें.
It is used to display a single इसका उपयोग एकल उद्धरण चिह्न प्रदर्शित
\’ Single Quote
quotation mark. करने के लिए किया जाता है।
Double It is used to display double इसका उपयोग दोहरे उद्धरण चिह्न प्रदर्शित
\”
Quote quotation marks. करने के लिए किया जाता है।
Question It is used to display a question इसका उपयोग प्रश्न चिह्न प्रदर्शित करने के
\?
Mark mark. लिए किया जाता है।
Octal It is used to represent an octal इसका उपयोग एक अष्टक संख्या का
\ooo
Number number. प्रतिनिधित्व करने के लिए किया जाता है।
Hexadecimal It represents the hexadecimal यह हेक्साडेसिमल संख्या का प्रतिनिधित्व
\xhh
Number number. करता है।
15
It represents the NULL यह NULL वर्ण का प्रतिनिधित्व करता है।
\0 NULL
character.
9. ESCAPE SEQUENCE IN C
एक एस्के प अनुक्रम वर्णों का एक अनुक्रम है जो स्ट्रिंग शाब्दिक या
चरित्र के अंदर उपयोग किए जाने पर 10.DATA TYPES IN C
स्वयं का प्रतिनिधित्व नहीं करता है। Data Type For Minimal Range S Si To store data the program
यह बैकस्लैश से शुरू होने वाले दो या mat iz ze must reserve space which is
Spe- e in done using datatype. A
दो से अधिक वर्णों से बना है। datatype is a keyword/pre-
cifie i b
defined instruction used for
C में एस्के प अनुक्रमों की सूची: r n yt allocating memory for data.
b es A data type specifies the
it type of data that a variable
unsigned %c 0 to 255 8 1 can store such as integer,
floating, character etc. It
char
used for declaring/defining
char %c -127 to 127 8 1 variables or functions of
different types before to
signed char %c -127 to 127 8 1 use in a program.
int %d, -32,767 to 32,767 1 2
%i 6
unsigned int %u 0 to 65,535 1 2
6
signed int %d, -32,767 to 32,767 1 2
%i (same as int) 6
short int %hd -32,767 to 32,767 1 2
6
unsigned %hu 0 to 65,535 1 2
short int 6
signed short %hd Same as short int 1 2
int 6
long int %ld, -2,147,483,647 to 3 4
%li 2,147,483,647 2
long long %lld -(2^63) to (2^63)-1 6 8
int , 4
%lli
signed long %ld, Same as long int 3 4
int %li 2
unsigned %lu 0 to 4,294,967,295 3 4
long int 2
unsigned %llu 0 to 6 8
long long 18,446,744,073,70 4
int 9,551,615
float %f 1.2E-38 to 3 4
3.4E+38 2
double %lf 1.7E-308 to 6 8
1.7E+308 4
long double %Lf 3.4E-4932 to 1 16
1.1E+4932 6
Program to declare variables and print their value
#include<stdio.h>
#include<conio .h>
void main()
{
//Create variables
10. C के डाटा टाइप्स int myNum = 5; // Integer (whole number)
//Program for taking
float myFloatNum = 5.99; // Floating point numb
डेटा संग्रहीत करने के लिए प्रोग्राम के स्थान को आरक्षित करना होगा जो user
char myLetter = 'D'; // Character
डेटाटाइप का उपयोग करके किया जाता है। डेटाटाइप एक कीवर्ड #include<stdio>
clrscr();
/पूर्वनिर्धारित निर्देश है जिसका उपयोग डेटा के लिए मेमोरी आवंटित करने के void main()
// Print variables
लिए किया जाता है। एक डेटा प्रकार उस डेटा के प्रकार को निर्दिष्ट करता है {
printf("%d\n", myNum);
जिसे एक चर संग्रहीत कर सकता है जैसे पूर्णांक, फ्लोटिंग, वर्ण आदि। यह int x;
printf("%f\n", myFloatNum);
एक प्रोग्राम में उपयोग करने से पहले विभिन्न प्रकार के चर या कार्यों को clrscr();
printf("%c\n", myLetter);
घोषित करने / परिभाषित करने के लिए उपयोग किया जाता है। printf("Enter a numbe
getch();
scanf("%d", &x);
}
printf("The number is
getch();
}
18
एक C कार्यक्रम निर्देशों का एक सेट है। एक नुस्खा की तरह - जिसमें
एक विशेष पकवान तैयार करने के निर्देश शामिल हैं।
निर्देशों के प्रकार –
I. टाइप डिक्लेरेशन निर्देश
II. अंकगणितीय निर्देश
III. नियंत्रण निर्देश
टाइप डिक्लेरेशन इंस्ट्रक्शन –
int a;
float b;
अन्य विविधताएं:
int i=10; int j=i; int a=2;
int j1=a+1-i;
float b=a+3; float a=1.1; (भूल! क्योंकि हम
‘a’ को परिभाषित करने से पहले ‘a’ का उपयोग करने की
कोशिश कर रहे हैं)
int a,b,c,d;
a=b=c=d=30; (a, b, c, और d प्रत्येक का मान
30 होगा)
अंकगणित निर्देश –
int i=(3*2)+1(*/+ ऑपरेटर है और 3,2,1 ऑपरेंड है)
ऑपरेंड इंट/फ्लोट आदि हो सकते हैं+ - * / अंकगणितीय ऑपरेटर हैं।
int b=z, c=3;
int z; Z=b*c; (ऐसा कर सकते हैं )
int Z; b*c=z; (अवैध (अनुमति नहीं है)))
मॉड्यूलर डिवीजन ऑपरेटर भाग करने पे शेषफल देता है, फ्लोट पर
लागू नहीं किया जा सकता चिह्न प्रगणक के समान है।
नोट-:
I. किसी भी ऑपरेटर को उपस्थित नहीं माना जाता है।
int i = ab (अमान्य)
int i = a*b (वैध)
II. C में प्रतिपादक प्रदर्शन करने के लिए कोई ऑपरेटर नहीं है,
हालांकि हम (बाद में) pow (x, y) का उपयोग कर सकते हैं
<math.h> से।
19
CHAPTER 2- FLOW CONTROL STATEMENTS
20
CONDITIONAL INSTRUCTIONS कं डीशनल(सशर्त) निर्देश
o Sometimes we want to watch comedy o कभी-कभी हम यूट्यूब पर कॉमेडी वीडियो देखना चाहते हैं अगर
videos on you Tube if the day is Sunday. दिन रविवार है।
o Sometimes we order junk food if it is our
o कभी-कभी हम हॉस्टल में अपने दोस्त का जन्मदिन होने पर जंक
friend’s birthday in the hostel.
o You might want to buy an umbrella if it’s फू ड ऑर्डर करते हैं।
raining and you have the money. o यदि बारिश हो रही है और आपके पास पैसा है तो आप छाता
o You order the meal if dal or your favorite खरीदना चाह सकते हैं।
bhindi is listed on the menu. o यदि दाल या आपका पसंदीदा मेनू पर सूचीबद्ध है तो आप भोजन
All these are decisions which depends on a का आदेश देते हैं।
condition being met.
In C language too, we must be alert to execute
ये सभी निर्णय हैं जो एक शर्त को पूरा करने पर निर्भर करते हैं। सी भाषा
instructions on a condition (s) being met. में भी, हमें एक शर्त (s) को पूरा करने पर निर्देशों को निष्पादित करने के
लिए तैयार होना चाहिए।
DECISION MAKING INSTRUCTIONS C में निर्णय लेने के निर्देश
IN C
if (अगर/यदि) कथन
if statement
if – else statement if-else(यदि-तो) कथन
Switch statement स्विच कथन
if (अगर) कथन –
if statement – यदि कथन सशर्त शाखाओं को नियंत्रित करता है। यदि किसी कथन का
The if statement controls conditional branching.
मुख्य भाग निष्पादित किया जाता है यदि अभिव्यक्ति का मान शून्य नहीं
The body of an if statement is executed if the value
of the expression is nonzero. Or if statement is है। या यदि शर्त सही होने पर कोड को निष्पादित करने के लिए कथन का
used to execute the code if condition is true. If the उपयोग किया जाता है। यदि अभिव्यक्ति / स्थिति का मूल्यांकन गलत
expression/condition is evaluated to false (0), (0) के लिए किया जाता है, तो if के अंदर के बयानों को निष्पादन से
statements inside the body of if is skipped from
execution.
छोड़ दिया जाता है।
The syntax of an if statement in C looks like: C में if स्टेटमेंट का सिंटेक्स इस प्रकार दिखता है:
If the condition/expression is true, then the true state-
ment will be executed otherwise the true statement
Code example/उदाहरण:
Syntax:
int a = 23;
if (Condition/expression)
if(a>18)
{ {
true statement; printf(“You can drive \n”);
} }
statement-x;
यदि शर्त / अभिव्यक्ति सही है, तो सही कथन निष्पादित किया जाएगा
block will be skipped and the execution will jump to
अन्यथा सही कथन ब्लॉक को छोड़ दिया जाएगा और निष्पादन
the statement-x. The “true statement” may be a single statement-x पर पहुंच जाएगा। “true statement” एक एकल कथन
statement or group of statement. If there is only one या कथन का समूह हो सकता है। यदि ब्लॉक में के वल एक कथन है, तो
statement in the if block, then the braces are optional.
ब्रेसिज़ वैकल्पिक हैं। लेकिन अगर एक से अधिक बयान हैं तो ब्रेसिज़
But if there is more than one statement the braces are
compulsory. अनिवार्य हैं।
21
if-else statement- अगर-अन्य कथन -
The if-else statement is an extension of the simple if if-else कथन if कथन का विस्तार है। if-else कथन कु छ कोड
statement. The if...else statement executes some निष्पादित करता है यदि परीक्षण अभिव्यक्ति सही है (नॉनज़ीरो) और
code if the test expression is true (nonzero) and
some other code if the test expression is false (0).
कु छ अन्य कोड यदि परीक्षण अभिव्यक्ति गलत है (0)।
Syntax: if(condition-1)
{
if (condition-2)
{ 22
statement-1;
अन्य-अगर सीढ़ी –
यदि और कथन का उपयोग कई स्थितियों से एक कोड को निष्पादित
करने के लिए किया जाता है।
Else if ladder-
The if else-if statement is used to execute one code
Syntax :
from multiple conditions.
if( condition-1) Code
{ int
statement-1; if (
यदि } p
शर्त else if(condition-2) else
-1 { p
statement-2; else
} p
else if(condition-3) else
{ p
statement-3; else
} p
else if(condition-n) else
{ p
statement-n; else
} p
else
{
गलत है, तो कथन -3 और कथन -x निष्पादित किया जाएगा। अन्यथा
यह दूसरा परीक्षण करना जारी रखता है। यदि शर्त -2 सत्य है, तो कथन - default-statement;
1 निष्पादित किया जाएगा अन्यथा कथन -2 निष्पादित किया जाएगा }
और फिर कथन-x में स्थानांतरित कर दिया जाएगा। statement-x;
23
The expression following the keyword switch is
Switch case– Syntax:
Switch – case is used when we have to make a
choice between numbers of alternatives for a given switch(expression)
variable. {
case value-1:
statement/block-1;
Start
break;
The Switch case value-2:
Statement statement/block t-2;
Expression
break;
case value-3:
Expression = value-1 statement/block -3;
Block- break;
1 case value-4:
Expression = value-2 statement/block -3;
Block- break;
2 default:
Expression = value-n
Block- default- statement/block t;
n break ;
Default (no match) }
Default
code any expression that must yield an integer value. It
Rest of code must be integer constants like 1, 2, and 3. The
keyword case is followed by an integer or a charac-
ter constant, each constant in each must be different
from all the other.
First the integer expression following the keyword
switch is evaluated. The value it gives is searched
against the constant values that follow the case
statements. When a match is found, the program ex-
ecutes the statements following the case. If no
match is found with any of the case statements, then
the statements following the default are executed.
Code Example:
Syntax:
i=0;
variable initialization ;
while( i<3 )
while (condition)
{
{
statements ; printf(“ %d ” , i);
variable increment or decrement ; i++;
} }
Output: 012
do-while loop:
The basic format of do-while loop statement is: do-while लूप:
do-while लूप स्टेटमेंट का मूल प्रारूप है:
Syntax:
Code Example:
variable initialization ;
i=0;
do
do
{
{
statements ;
variable increment or decrement ; printf(“ %d ” , i);
} while (condition) ; i++;
} while( i<3 )
Output: 012
UNCONDITIONAL CONTROL STATE-
MENTS बिना शर्त नियंत्रण कथन
In c, there are control statements that do not need सी में, नियंत्रण कथन हैं जिन्हें प्रोग्राम निष्पादन प्रवाह को नियंत्रित
any condition to control the program execution
flow. These control statements are called as uncon- करने के लिए किसी भी शर्त की आवश्यकता नहीं है। इन नियंत्रण
ditional control statements. C programming lan- कथनों को बिना शर्त नियंत्रण कथन कहा जाता है। सी प्रोग्रामिंग भाषा
guage provides the following unconditional control
निम्नलिखित बिना शर्त नियंत्रण कथन प्रदान करती है
statements...
ब्रेक
break
continue continue(जारी रखना)
goto
goto (जाओ);
The break; continue; and goto; statements are ब्रेक, कं टिन्यू, गोटु कथन का उपयोग किसी प्रोग्राम के सामान्य
used to alter the normal flow of a program. प्रवाह को बदलने के लिए किया जाता है। लूप कार्य का एक सेट को
Loops perform a set of repetitive task until text ex-
pression becomes false but it is sometimes desirable बार बार निष्पादित करते हैं जब तक कि पाठ अभिव्यक्ति झूठी नहीं हो
to skip some statement/s inside loop or terminate जाती है, लेकिन कभी-कभी लूप के अंदर कु छ कथन को छोड़ना या
the loop immediately without checking the test ex-
pression. In such cases, break and continue state-
परीक्षण अभिव्यक्ति की जांच किए बिना लूप को तुरंत समाप्त करना
ments are used. वांछनीय होता है। ऐसे मामलों में, ब्रेक और कं टिन्यू का उपयोग किया
जाता है।
while लूप:
27
break statement: name to move the program control to
In C programming, break state- int i; the specified label.
ment is used with conditional if
statement. The break statement is for (i = 0; i < 10; i++) ब्रेक (विराम) कथन:
used to perform the following two { सी प्रोग्रामिंग में, ब्रेक स्टेटमेंट का उपयोग सशर्त यदि
things: if (i == 4) कथन के साथ किया जाता है। ब्रेक स्टेटमेंट का उपयोग
break statement is used to ter-
minate the switch case state- { निम्नलिखित दो चीजों को करने के लिए किया जाता है:
ment. break; ब्रेक स्टेटमेंट का उपयोग स्विच के स स्टेटमेंट
break statement is also used to } को समाप्त करने के लिए किया जाता है।
terminate looping statements
printf("%d\n", i); ब्रेक स्टेटमेंट का उपयोग लूपिंग स्टेटमेंट को
like while, do-while and for.
}
समाप्त करने के लिए भी किया जाता है जैसे
continue statement: कि while, do-while और for।
It is sometimes desirable to skip some statements
कं टिन्यू कथन:
inside the loop. In such cases, continue statement is
used. Just like break, continue is also used with कभी-कभी लूप के अंदर कु छ बयानों को छोड़ना वांछनीय होता है। ऐसे
conditional if statement. The con- Code example: मामलों में, continue कथन का उपयोग किया जाता है। ब्रेक
tinue statement is used to move the int i; की तरह, कं टीन्यू का उपयोग सशर्त बयान के साथ भी किया
program execution control to the be- जाता है। continue कथन का उपयोग प्रोग्राम निष्पादन
for (i = 0; i < 10; i++)
ginning of the looping statement. नियंत्रण को लूपिंग कथन की शुरुआत में स्थानांतरित करने
{
When the continue statement is en- के लिए किया जाता है। जब लूपिंग स्टेटमेंट में continue
if (i == 4)
countered in a looping statement,
the execution control skips the rest { कथन का सामना किया जाता है, तो निष्पादन नियंत्रण लूपिंग
of the statements in the looping continue; ब्लॉक में बाकी बयानों को छोड़ देता है और सीधे लूप की
block and directly jumps to the be- } शुरुआत में कू द जाता है। continue कथन का उपयोग
ginning of the loop. The con- printf("%d\n", i); लूपिंग कथनों के साथ किया जा सकता है जैसे कि while,
tinue statement can be used with } do-while और for। यदि कोई निर्दिष्ट स्थिति उत्पन्न होती
looping statements like while, do- है, तो continue कथन एक पुनरावृत्ति (लूप में) को तोड़ देता
while and for. The continue state- है, और लूप में अगले पुनरावृत्ति के साथ जारी रहता है।
ment breaks one iteration (in the loop), if a spe-
cified condition occurs, and continues with the next
गोटू कथन:
iteration in the loop. "ब्रेक" और "कं टिन्यू" बयानों के विपरीत, गोटू स्टेटमेंट का उपयोग
प्रोग्राम के भीतर एक विशिष्ट स्थान पर प्रोग्राम नियंत्रण को स्थानांतरित
goto statement: करने के लिए किया जाता है। गोटू स्टेटमेंट का सबसे अधिक उपयोग तब
Unlike the "break" and "continue" statements, the किया जाता है जब हमें प्रोग्राम के सामान्य प्रवाह को बदलने की
goto statement is used to move program control to a आवश्यकता होती है। प्रोग्राम नियंत्रण को प्रोग्राम के किसी अन्य
specific location within the program. The goto अनुभाग में ले जाने के लिए, एक लेबल बनाएँ, और तब प्रोग्राम नियंत्रण
statement is most commonly used when we need to को निर्दिष्ट लेबल पर ले जाने के लिए लेबल नाम के बाद गोटू का
change the program's normal flow. To move the उपयोग करें।
program control to another
Syntax: Code example:
section of the program, cre-
goto label; int i=1;
ate a label, and then use
............. clrscr();
goto followed by the label
............. count: //This is Label
............. printf("%d\n",i);
label: statement; i++;
if(i<=10)
28
{
goto count; //This jumps to label "count:"
CHAPTER 3- ARRAY
DEFINATION
DECLARING ONE-D ARRAY
ACCESSING THE ELEMENTS OF THE ARRAY
INITIALIZE THE ARRAY
TWO-D ARRAY DECLARATION
ACCESSING THE ELEMENTS OF 2D ARRAY
INITIALIZE THE 2D ARRAY
STRING
STRING FUNCTIONS
29
data_type array_name[size] ={v1,v2,…vn/list of
values };
ARRAY ऐरे
Array is the collection of similar data types or col- ऐरे समान डेटा प्रकारों का संग्रह या सन्निहित मेमोरी स्थान में संग्रहीत
lection of similar entity stored in contiguous समान इकाई का संग्रह है। वर्ण की ऐरे एक स्ट्रिंग है। ऐरे के प्रत्येक डेटा
memory location. Array of character is a string.
Each data item of an array is called an element. And आइटम को एक एलिमेंट कहा जाता है। और प्रत्येक एलिमेंट अद्वितीय है
each element is unique and located in separated और अलग-अलग मेमोरी स्थान में स्थित है। एक ऐरे के प्रत्येक तत्व एक
memory location. Each of elements of an array चर साझा करते हैं लेकिन प्रत्येक तत्व में अलग-अलग सूचकांक संख्या
share a variable but each element having different
index no. known as subscript. An array can be a होती है। सबस्क्रिप्ट के रूप में जाना जाता है। एक ऐरे एक एकल
single dimensional or multi-dimensional and num- आयामी या बहु-आयामी हो सकती है और सबस्क्रिप्ट की संख्या इसके
ber of subscripts determines its dimension. And आयाम को निर्धारित करती है। और सबस्क्रिप्ट की संख्या हमेशा शून्य
number of subscript is always starts with zero. One
dimensional array is known as vector and two di- से शुरू होती है। एक आयामी ऐरे को वेक्टर के रूप में जाना जाता है और
mensional arrays are known as matrix. दो आयामी सरणियों को मैट्रिक्स के रूप में जाना जाता है। लाभ: सरणी
Advantages: Array variable can store more than
चर एक समय में एक से अधिक मान संग्रहीत कर सकता है जहां अन्य
one value at a time where other variable can store
one value at a time. चर एक समय में एक मान संग्रहीत कर सकते हैं। नुकसान: सरणी की
Disadvantage: Whatever size, we define at the time घोषणा के समय हम जो भी आकार परिभाषित करते हैं, हम सीमा को
of declaration of array, we can't exceed the limit.
So, it doesn't grow the size dynamically.
पार नहीं कर सकते। तो, यह गतिशील रूप से आकार नहीं बढ़ता है।
30
ऐरे तत्वों का संकलन समय प्रारंभ सामान्य चर प्रारंभिककरण के समान Eventhough compiler allocates 5 memory loca-
है। सिंटैक्स: data_type array_name[आकार]={v1,v2,... tions, using this declaration statement, the com-
vn/मानों की सूची}; piler initializes first two locations with 10 and
15, the next set of
a[0] a[1] a[2]
memory locations
Example 10 15 0
are automatically
int age[5]={22,25,30,32,35}; 1000 1002 1004
initialized to zero.
int marks[4]={ 67, 87, 56, 0 1 2 3 4
77 }; //integer array initializa- 22 25 30 32 35 उदाहरण
tion int age[5]={22,25,30,32,35};
float area[5]={ 23.4, 6.8, 5.5 }; //float array initial- int marks[4]={67, 87, 56, 77}; //इन्टिजर ऐरे के एलिमेंट्स
ization
int marks[4]={ 67, 87, 56, 77, 59 }; //Compile time को वैल्यू देना
error float area[5]={23.4, 6.8, 5.5}; //फ्लोट ऐरे के एलिमेंट्स को
वैल्यू देना
Different ways of initializing arrays:
marks[4]={67, 87, 56, 77, 59}; // कम्पाइल टाइम एरर
Initializing all specified memory locations
Partial array initialization.
Initialization without size.
ऐरे को प्रारंभ करने के विभिन्न तरीके :
1. Initializing all specified memory locations: If सभी निर्दिष्ट स्मृति स्थानों को प्रारंभ करना
the number of values to be initialized is equal to
आंशिक सरणी प्रारंभ।
size of array. Arrays can be initialized at the
time of declaration. Array elements can be ini- आकार के बिना प्रारंभ।
tialized with data items of type int, float, char, a. सभी निर्दिष्ट स्मृति स्थानों को प्रारंभ करना: यदि प्रारंभ किए
etc. जाने वाले मानों की संख्या सरणी के आकार के बराबर है. घोषणा के
Ex: consider integer initialization
समय सरणी शुरू की जा सकती है। सरणी तत्वों को प्रकार int,
int a[5]={10,20,30,40,50};
During compilation, 5 contiguous memory loca- फ्लोट, char, आदि के डेटा आइटम के साथ शुरू किया जा सकता
tions are reserved by the compiler for the vari- है।
able a and all these locations are initialized. उदाहरण: इन्टिजर ऐरे प्रारंभिक मान
The array a is initialized as: a[5]={10,20,30,40,50};
If the size of integer is 2 a[0] a[1] a[2] a[3] a[4] संकलन के दौरान, चर a के लिए
bytes, 10 bytes will be al- 10 20 30 40 50 कं पाइलर द्वारा 5 सन्निहित मेमोरी
located for the variable a. 1000 1002 1004 1006 1008 स्थान आरक्षित किए जाते हैं और इन
Ex: consider character ini-
tialization सभी स्थानों को प्रारंभिक मान दिया जाता है। सरणी a को इस प्रकार
char b[8] = {“C”,“O”, “M”, “P”, “U”, “T”, “E”,
“R”};
प्रारंभ किया गया है:
b[0] b[1] b[2] b[3] b[4] b[5] b[6] b[7] यदि पूर्णांक का आकार
The array
b is initialized C O M P U T E R
2 बाइट्स है, तो चर a
as: 7 bytes 1000 1001 1002 1003 1004 1005 1006 1007
के लिए 10 बाइट्स
will be alloc-
आवंटित किए जाएंगे।
ated for the variable b.
2. Partial Array Initialization: partial array ini- उदाहरण: वर्ण प्रारंभिककरण
tialization is possible in C language. If the num- char b[8] = {"C", "O", "M", "P", "U", "T", "E", "R"};
ber of values to be initialized is less than the सरणी b को इस प्रकार प्रारंभ किया गया है: चर b के लिए 7 बाइट्स
size of the array, then the elements are initial-
ized in the order from 0th location. The remain- आवंटित किए जाएंगे।
ing locations will be initialized to zero automat- b. आंशिक सरणी प्रारंभिककरण: सी भाषा में आंशिक सरणी प्रारंभ
ically. संभव है। यदि प्रारंभ किए जाने वाले मानों की संख्या सरणी के
Ex : Consider the partial initialization
int a[5]={10,15}; आकार से कम है, तो तत्वों को 0 वें स्थान से क्रम में प्रारंभ किया
31
जाता है। शेष स्थानों को स्वचालित रूप से शून्य पर प्रारंभ किया Reading out data from an
जाएगा। उदाहरण :आंशिक प्रारंभिकीकरण array
The elements of an array can Code:
int a[5]={10,15}; be read and display by using
for (int i=0; i<20; i++)
भले ही कं पाइलर 5 मेमोरी स्थानों को आवंटित करता है, इस any loop.
{
घोषणा कथन का उपयोग करके , कं पाइलर 10 और 15 के साथ printf("%d\n",
पहले दो स्थानों को शुरू करता है, मेमोरी स्थानों का अगला सेट mydata[x]);
स्वचालित रूप से शून्य पर शुरू किया जाता है। }
3. Initialization without size:
If the size of the array is omitted, an array just c. आकार के बिना :
big enough to hold the initialization is created. यदि सरणी का आकार छोड़ दिया जाता है, तो आरंभीकरण को
Therefore, if you write − पकड़ने के लिए पर्याप्त बड़ी सरणी बनाई जाती है।
int a[]={10,20,30,40,50}; इसलिए, यदि आप लिखते हैं –
You will create exactly the same array as you
int a[]={10,20,30,40,50};
did in the previous example.
आप बिल्कु ल वही सरणी बनाएंगे जो आपने पिछले उदाहरण में किया था।
Runtime Array initialization
An array can also be
Code Example:
initialized at runtime
using scanf() function. int arr[4];
This approach is usu- int i, j;
ally used for initializing printf("Enter array element");
large array, or to initial- for(i=0;i<4;i++)
ize array with user spe- {
cified values. scanf("%d",&arr[i]); //Run time array initialization
}
Accessing the ele- for(j=0;j<4;j++)
ments of an array {
The elements of an ar-
ray can be accessed by printf("%d\n",arr[j]);
indices/index. Array }
subscript (or index) getch();
can be used to access
any element stored in
array. Subscript starts
with 0, which means array_name[0] would be used
to access first element in an array. In general
array_name[n-1] can be used to access nth element
of an array, where n is any integer number.
Note:
Arrays have 0 as the first index not 1.
If the size of an array is n, to access the last ele-
ment, (n-1) index is used.
उपयोग करके पढ़ा और प्रदर्शित किया जा a[2] [0] a[2] [1] a[2] [2] a[2] [3]
सकता है।
35
उदाहरण:
कं पाइलर 0 से 9 तक के चर ‘a’ के लिए 10 बाइट्स आवंटित
करता है और "R", "A", "M", "A" वर्णों के साथ पहले चार
स्थानों को शुरू करता है। शेष स्थान स्वचालित रूप से NULL
वर्णों (यानी,\0) से भरे होते हैं।
String Input/output Functions
The strings can be read from the keyboard and can
be displayed onto the monitor using various func-
tions. The various input and output functions that
3. आकार के बिना आबंटन: char b[]={"C", "O", "M", "P", are associated with strings can be classified as:
"U", "T", "E", "R"} के साथ घोषणा पर विचार करें; इस
घोषणा में, कं पाइलर सरणी आकार को प्रारंभिक मानों की कु ल
संख्या यानी 8 पर सेट करेगा। वर्ण निर्दिष्ट क्रम में इन स्मृति स्थानों में
संग्रहीत किया जाएगा।
Code Example1:
char ch[13]={'c', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', i', „n‟, „g‟, „\0‟};
char ch2[13]="cprogramming";
printf("Char Array Value is: %s\n", ch);
printf("String Literal Value is: %s\n", ch2);
Output
Char Array Value is: cprogramming
String Literal Value is: cprogramming
Code Example2:
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name); 36
Output
Syntax : puts(char type of array variable);
37
3 gets() : gets() फ़ं क्शन का उपयोग मानक इनपुट डिवाइस 2. strcpy(string1,string2) – String Copy : This
(कीबोर्ड) से स्ट्रिंग (स्ट्रिंग वर्णों का एक समूह है) को पढ़ने के लिए function is used to copy the contents of one string
to another string.
किया जाता है। सिंटैक्स: gets(सरणी का नाम ); Syntax: strcpy (string1, string2);
4 puts(): puts() फ़ं क्शन का उपयोग स्ट्रिंग को मानक आउटपुट Where, string1: is the destination string.
डिवाइस (मॉनिटर) पर प्रदर्शित करने के लिए किया जाता है। string 2: is the source string.
सिंटैक्स: puts(character variable); i.e the contents of string2 is assigned to the con-
tents of string1.
2. char str1[]="REDDY";
char str2[10];
strcpy(str2,str1);
printf("The string1 is :%s\n",str1);
printf("The string2 is :%s\n",str2);
strcpy(str2,str1+1);
ये सभी फ़ं क्शंस string.h हेडर फ़ाइल में परिभाषित किए गए हैं। printf("The string1 is :%s\n",str1);
printf("The string2 is :%s",str2);
1. strlen (स्ट्रिंग) - स्ट्रिंग लंबाई: इस फ़ं क्शन का उपयोग स्ट्रिंग में 3. char str[]="JBREC";
मौजूद वर्णों की संख्या को गिनने और वापस करने के लिए किया strlwr(str);
जाता है। सिंटैक्स: var=strlen (स्ट्रिंग); printf("The lowercase is :%s\n",str);
38
2. strcpy(string1,string2) - स्ट्रिंग कॉपी: इस फ़ं क्शन का
उपयोग एक स्ट्रिंग की सामग्री को दूसरे स्ट्रिंग में कॉपी करने के
लिए किया जाता है। 4. char str[]="jbrec";
सिंटैक्स: strcpy(स्ट्रिंग 1, स्ट्रिंग 2); strupr(str);
यहाँ, स्ट्रिंग 1: गंतव्य स्ट्रिंग है। स्ट्रिंग 2: स्रोत स्ट्रिंग है। यानी printf("UpperCase is :%s\n",str);
स्ट्रिंग 2 की सामग्री स्ट्रिंग 1 की सामग्री को सौंपी गई है। 5. char str1[]="reddy";
char str2[]="reddy";
3. strlwr (स्ट्रिंग) – स्ट्रिंग लोअरके स: इस फ़ं क्शन का उपयोग int i,j,k;
स्ट्रिंग के ऊपरी के स अक्षरों को निचले के स अक्षरों में i=strcmp(str1,str2);
परिवर्तित करने के लिए किया जाता है। j=strcmp(str1,"subba");
सिंटैक्स: strlwr (स्ट्रिंग); k=strcmp(str2,"Subba");
printf("%5d%5d%5d\n",i,j,k);
4. strupr(स्ट्रिंग) – स्ट्रिंग अपरके स: इस फ़ं क्शन का उपयोग
स्ट्रिंग के निचले के स अक्षरों को ऊपरी के स अक्षरों में
परिवर्तित करने के लिए किया जाता है। 6. strcat(string1,string2) – String Concatenation:
This function is used to concatenate or combine,
सिंटैक्स: strupr (स्ट्रिंग); two strings together and forms a new concaten-
ated string.
5. strcmp (string1,string2) - स्ट्रिंग तुलना: इस फ़ं क्शन Syntax: strcat(sting1,string2);
का उपयोग दो स्ट्रिंग्स की तुलना करने के लिए किया जाता है Where, string1: is the first string1.
string2: is the second string2
ताकि यह पता लगाया जा सके कि वे समान हैं या अलग हैं।
When the above function is executed, string2 is
यदि दो स्ट्रिंग की तुलना वर्ण द्वारा की जाती है जब तक कि combined with string1 and it removes the null
स्ट्रिंग में से एक के अंत तक नहीं पहुंच जाता। यदि दो स्ट्रिंग character (\0) of string1 and places string2 from
समान हैं, तो strcmp() एक मान शून्य देता है। यदि वे समान there.
नहीं हैं, तो यह पहले गैर-मिलान वर्णों के बीच संख्यात्मक 7. strrev(string) - String Reverse :This function is
used to reverse a string. This function takes only
अंतर लौटाता है। one argument and return one argument.
Syntax: strrev(string);
6. char str1[10]="jbrec";
char str2[]="ece";
strcat(str1,str2);
printf("%s\n",str1);
printf("%s\n",str2);
7. char str[20]; O
printf("Enter the string:");
scanf("%s",str);
printf("The string reversed is:%s",strrev(str
39
6. strcat (string1,string2) - स्ट्रिंग कॉन्के टेनेशन: इस फ़ं क्शन
का उपयोग दो स्ट्रिंग को एक साथ जोड़ने या संयोजित करने के लिए
किया जाता है और एक नया शंकु बद्ध स्ट्रिंग बनाता है।
सिंटैक्स: strcat (sting1,string2);
यहाँ, स्ट्रिंग 1: पहला स्ट्रिंग है। स्ट्रिंग 2: दूसरा स्ट्रिंग है
जब उपरोक्त फ़ं क्शन निष्पादित किया जाता है, तो स्ट्रिंग 2 को स्ट्रिंग
1 के साथ जोड़ा जाता है और यह स्ट्रिंग 1 के शून्य वर्ण (\0) को
हटा देता है और स्ट्रिंग 2 को वहां से रखता है।
40