Welcome to
| C LANGUAGE |
|---|
C Language एक सामान्य उद्देश्य वाली कंप्यूटर प्रोग्रामिंग भाषा है। जो सिस्टम मेमोरी को निम्न स्तरीय Access प्रदान करती है। C Programming Language 1970 Dennis Ritchie द्वारा बनाया गया था। C Language में लिखे गए प्रोग्राम को C Compiler के माध्यम से चलाना चाहिए, ताकी इस भाषा के निष्पादन योग्य में परिवर्तन किया जा सके। मूल रूप से C Language सभी कंप्यूटर प्रोग्रामर के लिए एक उपयोगी भाषा है, अगर आप एक बेहतरीन प्रोग्रामर या सॉफ्टवेयर इंजीनियर बनना चाहते है तो आपके लिए C Language सिखना सबसे बेहतर विकल्प है। C Language सीखने का सबसे बड़ा फायदा ये है कि C Language को दुनिया भर में मान्यता प्राप्त है। और शुरुआत में आपके लिए C Language सीखना ही बुद्धिमानी है। अगर आप C Language को अच्छी तरह से सीख लेते है तो, आपको और अन्य Languages को सीखने में कोई दिक्कत नहीं होगी, और आप Language अच्छी तरह सीख सकते है, C Language के द्वारा कई एप्लीकेशन सॉफ्टवेयर बना सकते है, C एक प्रोसीजर ओरिएंटेड प्रोग्रामिंग Language होती है। C Language ऑपरेटिंग सिस्टम जैसे (Windows) बनाने में C Language का काफी प्रयोग किया जाता है, उदहारण जैसे -Desktop Windows, Linux, Unix और कंप्यूटर, मोबाइल, टेबलेट आदि में ऑपरेटिंग सिस्टम होता है, जो इन डिवाइस को बुनियादी कार्यक्षमता प्रदान करता है। C language का प्रयोग एप्लीकेशन सॉफ्टवेयर बनाने में काफी इस्तेमाल किया जाता है, यह एक सरल, लोकप्रिय और बहुत Powerful Language है, क्योंकि यह कई प्रकार के डेटा टाइप और फंक्शन प्रदान करता है। C language का उपयोग Low-Level और High-Level Programming दोनों में ही किया जाता है। दोस्तों मैं आज मेरे ब्लॉग पर आपको C Language Programming के एकदम सरल और उपयोगी Code की जानकारी सम्पूर्ण प्रदान करूँगा। आप मेरी पोस्ट को पूरा पढ़ते रहे, और C Language की जानकारी प्राप्त करके Coding अच्छीं तरह सीखे, तो शुरू करते है
C Language Code Basic
प्रोग्राम कैसे काम करता है? #include एक प्रीप्रोसेसर कमांड है जो कंपाइलर को बताता है कि प्रोग्राम में stdio.h (स्टैंडर्ड इनपुट और आउटपुट) फ़ाइल के कंटेंट को शामिल करना है। stdio.h फ़ाइल में इनपुट लेने और आउटपुट दिखाने के लिए :- scanf() और printf() जैसे फ़ंक्शन होते हैं। अगर आप #include < stdio.h > लिखे बिना printf() फ़ंक्शन का इस्तेमाल करते हैं, तो प्रोग्राम कंपाइल नहीं होगा। C प्रोग्राम का एग्जीक्यूशन main() फ़ंक्शन से शुरू होता है।
printf() स्क्रीन पर फ़ॉर्मेटेड आउटपुट भेजने के लिए एक लाइब्रेरी फ़ंक्शन है। इस प्रोग्राम में, printf() स्क्रीन पर "Hello, My Students!" टेक्स्ट दिखाता है। return 0; स्टेटमेंट प्रोग्राम का "एग्जिट स्टेटस" है। आसान शब्दों में, प्रोग्राम इसी स्टेटमेंट के साथ खत्म होता है।
नीचे दिए गए कोड को आप अपने रजिस्टर या डायरी में नोट कर ले, और फिर इस कोड को अपने कम्प्युटर या लैपटॉप में C Language Software को ओपन करके उसमें ये कोड टाइप करें और रन करें। फिर आपको कंप्यूटर या लैपटॉप में सी लैंग्वेज के द्वारा F5 (Run) करने पर Computer या Laptop की स्क्रीन के सामने Result दिखाई देगा।
Program to Display "Hello My Students!" #include < stdio.h >
int main() {
// printf() displays the string inside quotation
printf("Hello My Students, How Are You!");
return 0;
}
#include < stdio.h >
int main() {
int number;
printf("Enter an integer: ");
// reads and stores input
scanf("%d", &number);
// displays output
printf("You entered: %d", number);
return 0;
}
इस प्रोग्राम में, एक इंटीजर वेरिएबल Number डिक्लेयर किया जाता है। फिर, यूज़र से एक इंटीजर नंबर डालने के लिए कहा जाता है। यह नंबर वेरिएबल में स्टोर किया जाता है। आखिर में, Number में स्टोर की गई वैल्यू को स्क्रीन पर दिखाया जाता है।
#include < stdio.h >
int main() {
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
// calculate the sum
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
return 0;
}
इस प्रोग्राम में, यूज़र से दो इंटीजर (पूर्णांक) डालने के लिए कहा जाता है। ये दोनों इंटीजर :- Number1 और Number2 वेरिएबल में स्टोर किए जाते हैं। इसके बाद, + ऑपरेटर का इस्तेमाल करके इन दोनों नंबरों को जोड़ा जाता है और नतीजे को Sum वेरिएबल में स्टोर किया जाता है।
#include < stdio.h >
int main() {
double a, b, product;
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
// Calculating product
product = a * b;
// %.2lf displays number up to 2 decimal point
printf("Product = %.2lf", product);
return 0;
}
इस प्रोग्राम में, यूज़र से दो नंबर डालने के लिए कहा जाता है, जिन्हें :- a और b वेरिएबल्स में स्टोर किया जाता है। इसके बाद, a और b का गुणनफल (Product) निकाला जाता है और नतीजे को Product में स्टोर किया जाता है। आखिर में, printf() का इस्तेमाल करके Product को स्क्रीन पर दिखाया जाता है। ध्यान दें कि %.2lf कन्वर्ज़न कैरेक्टर का इस्तेमाल करके नतीजे को दशमलव के दूसरे स्थान तक राउंड-ऑफ किया जाता है।
#include < stdio.h >
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
// %d displays the integer value of a character // %c displays the actual character
printf("ASCII value of %c = %d", c, c);
return 0;
}
इस प्रोग्राम में, यूज़र से एक कैरेक्टर डालने के लिए कहा जाता है। यह कैरेक्टर वेरिएबल C में स्टोर होता है। जब %d फ़ॉर्मैट स्ट्रिंग का इस्तेमाल किया जाता है, तो 71 (G की ASCII वैल्यू) दिखाई देती है। जब %c फ़ॉर्मैट स्ट्रिंग का इस्तेमाल किया जाता है, तो G ही दिखाई देता है।
#include < stdio.h >
int main() {
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", & dividend);
printf("Enter divisor: ");
scanf("%d", &divisor);
// Computes quotient
quotient = dividend / divisor;
// Computes remainder
remainder = dividend % divisor;
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);
return 0;
}
इस प्रोग्राम में, यूज़र से दो इंटीजर (डिविडेंड और डिवाइज़र) डालने के लिए कहा जाता है। इन्हें :- Dividend और Divisor वेरिएबल्स में स्टोर किया जाता है। फिर / (डिविज़न ऑपरेटर) का इस्तेमाल करके भागफल (Quotient) निकाला जाता है और उसे Quotient में स्टोर किया जाता है। इसी तरह % (मॉड्यूलो ऑपरेटर) का इस्तेमाल करके शेषफल (Remainder) निकाला जाता है और उसे Remainder में स्टोर किया जाता है। आखिर में Printf() का इस्तेमाल करके भागफल और शेषफल को दिखाया जाता है।
#include < stdio.h >
int main() {
int intType;
float floatType;
double doubleType;
char charType;
// sizeof evaluates the size of a variable
printf("Size of int: %zu bytes\n", sizeof(intType));
printf("Size of float: %zu bytes\n", sizeof(floatType));
printf("Size of double: %zu bytes\n", sizeof(doubleType));
printf("Size of char: %zu byte\n", sizeof(charType));
return 0;
}
इस प्रोग्राम में, intType, floatType, doubleType और charType नामक चार चर घोषित किए गए हैं। फिर, sizeof ऑपरेटर का उपयोग करके प्रत्येक चर का आकार ज्ञात किया जाता है।
#include < stdio.h >
int main() {
int a;
long b; // equivalent to long int b;
long long c; // equivalent to long long int c;
double e;
long double f;
printf("Size of int = %zu bytes \n", sizeof(a));
printf("Size of long int = %zu bytes\n", sizeof(b));
printf("Size of long long int = %zu bytes\n", sizeof(c));
printf("Size of double = %zu bytes\n", sizeof(e));
printf("Size of long double = %zu bytes\n", sizeof(f));
return 0;
}
इस प्रोग्राम में, sizeof ऑपरेटर का उपयोग int, long, long long, double और long double वेरिएबल्स का आकार ज्ञात करने के लिए किया जाता है। जैसा कि आप देख सकते हैं, long int और long double वेरिएबल्स का आकार क्रमशः int और double वेरिएबल्स से अधिक है। वैसे, sizeof ऑपरेटर size_t (अहस्ताक्षरित पूर्णांक प्रकार) लौटाता है। size_t डेटा प्रकार का उपयोग किसी ऑब्जेक्ट के आकार को दर्शाने के लिए किया जाता है। size_t के लिए प्रयुक्त प्रारूप विनिर्देशक %zu है। ध्यान दें: long कीवर्ड का उपयोग float और char प्रकारों के साथ नहीं किया जा सकता है।
👇
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
// value of first is assigned to temp temp = first;
// value of second is assigned to first first = second;
// value of temp (initial value of first) is assigned to second second = temp;
// %.2lf displays number up to 2 decimal points
printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf", second);
return 0;
}
ऊपर दिए गए प्रोग्राम में, temp वेरिएबल को पहले वेरिएबल की वैल्यू असाइन की जाती है। फिर, पहले वेरिएबल की वैल्यू दूसरे वेरिएबल को असाइन की जाती है। आखिर में, temp (जिसमें पहले वेरिएबल की शुरुआती वैल्यू होती है) को दूसरे वेरिएबल को असाइन किया जाता है। इससे स्वैपिंग की प्रक्रिया पूरी हो जाती है।
👇
int main() {
double a, b;
printf("Enter a: ");
scanf("%lf", &a);
printf("Enter b: ");
scanf("%lf", &b);
// swapping
// a = (initial_a - initial_b)
a = a - b;
// b = (initial_a - initial_b) + initial_b = initial_a
b = a + b;
// a = initial_a - (initial_a - initial_b) = initial_b
a = b - a;
// %.2lf displays numbers up to 2 decimal places
printf("After swapping, a = %.2lf\n", a);
printf("After swapping, b = %.2lf", b);
return 0;
}
#include
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// true if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.", num);
else printf("%d is odd.", num);
return 0;
}
एक सम संख्या वह पूर्णांक है जो 2 से पूर्णतः विभाज्य होती है। उदाहरण के लिए: 0, 8, -24 एक विषम संख्या वह पूर्णांक है जो 2 से पूर्णतः विभाज्य नहीं होती है। उदाहरण के लिए: 1, 7, -11, 15 प्रोग्राम में, उपयोगकर्ता द्वारा दर्ज किया गया पूर्णांक num नामक चर में संग्रहित किया जाता है। फिर, मॉड्यूलो % ऑपरेटर का उपयोग करके यह जांचा जाता है कि num, 2 से पूर्णतः विभाज्य है या नहीं। यदि संख्या 2 से पूर्णतः विभाज्य है, तो परीक्षण व्यंजक number%2 == 0 का मान 1 (सत्य) होता है। इसका अर्थ है कि संख्या सम है। हालांकि, यदि परीक्षण व्यंजक का मान 0 (असत्य) होता है, तो संख्या विषम है।
#include < stdio.h >
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
(num % 2 == 0) ? printf("%d is even.", num) : printf("%d is odd.", num);
return 0;
}
#include < stdio.h >
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);
// evaluates to 1 if variable c is a lowercase vowel lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 if variable c is a uppercase vowel uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// evaluates to 1 (true) if c is a vowel if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c); else
printf("%c is a consonant.", c);
return 0;
}
पांच अक्षर A, E, I, O और U स्वर कहलाते हैं। इन पांच स्वरों को छोड़कर बाकी सभी अक्षर व्यंजन कहलाते हैं। यह प्रोग्राम मानता है कि उपयोगकर्ता हमेशा एक अक्षर ही दर्ज करेगा। उपयोगकर्ता द्वारा दर्ज किया गया अक्षर 'c' नामक चर में संग्रहित किया जाता है। 'lowercase_vowel' चर का मान 1 (सही) होता है यदि 'c' एक छोटा स्वर है और अन्य सभी अक्षरों के लिए 0 (गलत) होता है। इसी प्रकार, 'uppercase_vowel' चर का मान 1 (सही) होता है यदि 'c' एक बड़ा स्वर है और अन्य सभी अक्षरों के लिए 0 (गलत) होता है। यदि 'lowercase_vowel' या 'uppercase_vowel' में से किसी एक का मान 1 (सही) है, तो दर्ज किया गया अक्षर स्वर है। हालांकि, यदि 'lowercase_vowel' और 'uppercase_vowel' दोनों चरों का मान 0 है, तो दर्ज किया गया अक्षर व्यंजन है। नोट: यह प्रोग्राम मानता है कि उपयोगकर्ता एक अक्षर दर्ज करेगा। यदि उपयोगकर्ता कोई गैर-अक्षर अक्षर दर्ज करता है, तो यह प्रदर्शित करता है कि वह अक्षर व्यंजन है। इस समस्या को हल करने के लिए, हम isalpha() फ़ंक्शन का उपयोग कर सकते हैं। isalpha() फ़ंक्शन यह जांचता है कि कोई अक्षर वर्णमाला है या नहीं।
#include < stdio.h >
int main() {
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3) printf("%.2f is the largest number.", n1);
// if n2 is greater than both n1 and n3, n2 is the largest
if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);
// if n3 is greater than both n1 and n2, n3 is the largest
if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);
return 0;
}
यहाँ हमने 3 अलग-अलग 'if' स्टेटमेंट्स का इस्तेमाल किया है। पहला स्टेटमेंट यह चेक करता है कि क्या n1 सबसे बड़ी संख्या है। दूसरा और तीसरा 'if' स्टेटमेंट :- यह चेक करते हैं कि क्या n2 और n3 सबसे बड़ी संख्याएँ हैं। इस प्रोग्राम की सबसे बड़ी कमी यह है कि चाहे कोई भी संख्या सबसे बड़ी हो, तीनों 'if' स्टेटमेंट्स एग्जीक्यूट होते हैं। हालाँकि, हम सिर्फ़ एक ही 'if' स्टेटमेंट एग्जीक्यूट करना चाहते हैं। हम 'if...else' लैडर का इस्तेमाल करके ऐसा कर सकते हैं।
#include < stdio.h >
int main() {
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf("%.2lf is the largest number.", n1);
// if n2 is greater than both n1 and n3, n2 is the largest
else if (n2 >= n1 && n2 >= n3)
printf("%.2lf is the largest number.", n2);
// if both above conditions are false, n3 is the largest else
printf("%.2lf is the largest number.", n3);
return 0;
}
इस प्रोग्राम में, जब n1 सबसे बड़ा होता है, तो सिर्फ़ 'if' स्टेटमेंट ही एक्ज़ीक्यूट होता है। इसी तरह, जब n2 सबसे बड़ा होता है, तो सिर्फ़ 'else if' स्टेटमेंट एक्ज़ीक्यूट होता है, और इसी क्रम में आगे भी ऐसा ही होता है।
#include < stdio.h >
int main() {
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// outer if statement
if (n1 >= n2) {
// inner if...else
if (n1 >= n3)
printf("%.2lf is the largest number.", n1);
else printf("%.2lf is the largest number.", n3);
}
// outer else statement
else {
// inner if...else
if (n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
}
return 0; }
इस प्रोग्राम में, सबसे बड़ी संख्या खोजने के लिए हमने नेस्टेड if...else स्टेटमेंट का इस्तेमाल किया है। आइए देखें कि वे कैसे काम करते हैं।
1. बाहरी if स्टेटमेंट सबसे पहले, बाहरी if स्टेटमेंट और उसके अंदर मौजूद अंदरूनी if...else स्टेटमेंट को देखें: यहाँ, हम जाँच रहे हैं कि क्या n1, n2 से बड़ा या उसके बराबर है। अगर ऐसा है, तो प्रोग्राम का कंट्रोल अंदरूनी if...else स्टेटमेंट पर चला जाता है। अंदरूनी if स्टेटमेंट जाँचता है कि क्या n1, n3 से भी बड़ा या उसके बराबर है। अगर ऐसा है, तो n1 या तो n2 और n3 दोनों के बराबर है, या फिर यह n2 और n3 दोनों से बड़ा है, यानी n1 >= n2 >= n3। इसलिए, n1 सबसे बड़ी संख्या है। नहीं तो, n1, n2 से बड़ा या उसके बराबर है लेकिन n3 से छोटा है, यानी n3 > n1 >= n2। इसलिए, n3 सबसे बड़ी संख्या है।
2. बाहरी else स्टेटमेंट बाहरी else स्टेटमेंट तब चलता है जब n2 > n1 होता है: प्रोग्राम के इस हिस्से का अंदरूनी if...else भी वही लॉजिक इस्तेमाल करता है जो पहले वाले में किया गया था। यहाँ बस इतना फ़र्क है कि हम जाँच रहे हैं कि क्या n2, n3 से बड़ा है। ऊपर दिए गए सभी प्रोग्राम का आउटपुट एक जैसा ही होगा।
#include < stdio.h >
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else {
realPart = -b / (2 * a);
imagPart = sqrt(-discriminant) / (2 * a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);
}
return 0;
}
इस प्रोग्राम में, किसी संख्या का वर्गमूल (square root) निकालने के लिए sqrt() लाइब्रेरी फ़ंक्शन का इस्तेमाल किया जाता है।
#include < stdio.h >
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// leap year if perfectly divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}
return 0;
}
#include < stdio.h >
int main() {
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num <= 0.0) {
if (num == 0.0)
printf("You entered 0.");
else
printf("You entered a negative number.");
}
else
printf("You entered a positive number.");
return 0;
}


No comments:
Post a Comment