Online Computer Courses Classes and Training Program

C Programming for Beginners: सरल भाषा में C सीखे





अध्यायों का प्रारूप:

  1. परिचय (Introduction)
    • C क्या है और इसकी आवश्यकता।
    • इसका इतिहास और उपयोग।
    • "Hello World" प्रोग्राम।
  2. बेसिक्स ऑफ C (C की मूल बातें)
    • वेरिएबल्स, डेटा टाइप्स, और ऑपरेटर्स।
    • प्रिंटिंग और इनपुट।
  3. कंडीशनल स्टेटमेंट्स (If-Else, Switch)
    • लॉजिक बनाना और निर्णय लेना।
    • उदाहरण: नंबर बड़ा है या छोटा है।
  4. लूप्स (Loops in C)
    • For, While, Do-While।
    • प्रैक्टिस: पैटर्न प्रिंटिंग।
  5. एरेज़ और स्ट्रिंग्स (Arrays and Strings)
    • मल्टीपल डेटा स्टोर करना।
    • स्ट्रिंग के बेसिक फंक्शन्स।
  6. फंक्शन्स (Functions)
    • कोड रिपीट न करने के लिए।
    • रीकर्शन का परिचय।
  7. पॉइंटर्स और मेमोरी मैनेजमेंट (Pointers & Memory)
    • एडवांस उपयोग और डायनामिक मेमोरी।
  8. फाइल हैंडलिंग (File Handling)
    • डेटा को पढ़ना और लिखना।
    • उदाहरण: बैंक अकाउंट डिटेल स्टोर करना।
  9. प्रोजेक्ट्स और अभ्यास (Projects and Practice)
    • छोटे प्रोजेक्ट्स: कैलकुलेटर, स्टूडेंट मार्कशीट।
    • प्रैक्टिस क्वेश्चन।

अध्‍याय 1: C प्रोग्रामिंग भाषा की परिचय

1.1 C क्या है?

C एक high-level, structured, और शक्तिशाली programming भाषा है, जिसे 1970 के दशक में Dennis Ritchie द्वारा AT&T Bell Labs में विकसित किया गया था। यह एक general-purpose language है, जिसे मुख्य रूप से system programming के लिए डिज़ाइन किया गया था। C भाषा ने अन्य programming languages जैसे C++, Java, और Python के लिए मार्ग प्रशस्त किया है। इसे आज भी व्यापक रूप से embedded systems, game development, और networking जैसे क्षेत्रों में उपयोग किया जाता है।

1.2 C की आवश्यकता

C भाषा का मुख्य उद्देश्य छोटे, तेज़, और efficient प्रोग्राम बनाना है। यह भाषा अपनी structure के कारण system programming के लिए आदर्श है। C में लिखे गए programs portable होते हैं, इसका मतलब है कि एक बार लिखे गए code को विभिन्न operating systems और platforms पर बिना किसी बदलाव के चलाया जा सकता है।

C की महत्वपूर्ण विशेषताएँ:

  • Flexible और Portable: C को विभिन्न प्रकार के computer systems पर चलाया जा सकता है, जिससे यह एक portable language बन जाती है।
  • Efficient: C का design इसे system level पर काम करने के लिए उपयुक्त बनाता है। यह सीधे hardware और machine के नजदीक काम करता है, जिससे programs fast और effective होते हैं।
  • Structured Programming: C एक structured programming language है, जिसका मतलब है कि इसमें code को छोटे, आसानी से समझने योग्य blocks में बांटा जाता है, जिससे code को लिखने, पढ़ने और maintain करने में आसानी होती है।

1.3 C का इतिहास

C का इतिहास 1972 में शुरू हुआ, जब इसे Dennis Ritchie और उनके सहयोगियों ने AT&T Bell Labs में विकसित किया। C का primary उद्देश्य UNIX operating system के लिए एक programming language प्रदान करना था। UNIX के development में C की भूमिका ने इसे programming language के रूप में सफलता दिलाई। C के बाद, कई अन्य programming languages जैसे C++, Java, और Python, C के principles और structure से प्रभावित हुईं।

C को बनाने के बाद से, यह programming की दुनिया में एक महत्वपूर्ण language बन गई है। इसका उपयोग operating systems, embedded systems, networking applications, और game development जैसे विभिन्न क्षेत्रों में किया जाता है।

1.4 C का उपयोग

C का उपयोग आज भी विभिन्न प्रकार के applications और systems में किया जाता है, जैसे:

  • System Programming: C का सबसे प्रमुख उपयोग operating systems, drivers, और अन्य system software के निर्माण में होता है।
  • Embedded Systems: C का उपयोग embedded systems में किया जाता है, जो छोटे और विशिष्ट tasks को पूरा करते हैं, जैसे microcontroller based devices।
  • Non-Operating System Applications: C का उपयोग विभिन्न प्रकार के applications में भी किया जाता है, जैसे data processing, graphics, और game development।
  • Networking: C का उपयोग networking applications और protocols को develop करने में किया जाता है, जैसे web servers और client applications।

1.5 C की संरचना और सिंटैक्स

C भाषा की संरचना काफी simple और clear होती है, जिससे इसे सीखना और समझना आसान हो जाता है। C program में तीन मुख्य components होते हैं:

  1. Statements: यह उन actions को define करता है जिन्हें program execute करेगा।
  2. Functions: C में प्रत्येक program में कम से कम एक main function होता है, जिसमें सभी code लिखा जाता है।
  3. Variables और Data Types: C विभिन्न प्रकार के data को handle करता है, जैसे integers, floats, और characters आदि।

1.6 "Hello World" प्रोग्राम

C programming language का पहला program आमतौर पर "Hello World" program होता है, जो screen पर "Hello, World!" message print करता है। यह program C की basic structure और syntax को समझने का सबसे अच्छा तरीका है।

#include <stdio.h>  // Input और Output operations के लिए library

int main() {  // Main function से program शुरू होता है
    printf("Hello, World!");  // Screen पर "Hello, World!" print करेगा
    return 0;  // Program का successful termination
}

व्याख्या:

  • #include <stdio.h>: यह line C के standard input-output library को include करती है, जो printf() जैसी functions को allow करती है।
  • int main(): यह main function है, जहां से program का execution शुरू होता है।
  • printf("Hello, World!");: यह line screen पर "Hello, World!" display करने के लिए printf() function का use करती है।
  • return 0;: यह program के successful termination को indicate करता है।

1.7 C की विशेषताएँ

C language की कुछ प्रमुख विशेषताएँ इस प्रकार हैं:

  1. Simple और Powerful: C एक simple language है, लेकिन यह system programming के लिए बहुत powerful है।
  2. Portability: C program को एक computer से दूसरे computer पर बिना किसी बदलाव के run किया जा सकता है।
  3. Memory Management: C language users को memory management की अधिक स्वतंत्रता देती है, जिससे यह high-performance applications बनाने के लिए उपयुक्त बनती है।
  4. Efficient Programming: C का syntax और structure programmers को efficient और effective code लिखने में मदद करती है।

1.8 निष्कर्ष

C एक foundational और powerful programming language है, जो आज भी विभिन्न क्षेत्रों में widely used है। यह language programming के fundamental concepts को समझने में मदद करती है और programmers को अधिक complex programming languages में proficiency हासिल करने का रास्ता प्रदान करती है। C के अध्ययन से programming की गहरी समझ विकसित होती है, जो किसी भी programming language के study में सहायक होती है।


अध्याय 1: C प्रोग्रामिंग भाषा का परिचय – उत्तर सहित समाधान

बेसिक स्तर के प्रश्न

  1. C प्रोग्रामिंग भाषा क्या है?
    उत्तर: C एक सामान्य उद्देश्य वाली प्रोग्रामिंग भाषा है, जिसका उपयोग सॉफ़्टवेयर विकास, सिस्टम प्रोग्रामिंग, और एम्बेडेड सिस्टम्स में किया जाता है। यह एक procedural और structured प्रोग्रामिंग भाषा है।

  2. C प्रोग्रामिंग की आवश्यकता क्यों पड़ी?
    उत्तर: C भाषा को विकसित करने की आवश्यकता इसलिए पड़ी ताकि low-level programming को आसान बनाया जा सके। इसका उपयोग हार्डवेयर और सॉफ़्टवेयर के बीच संचार के लिए किया गया।

  3. C भाषा को किसने और कब विकसित किया?
    उत्तर: C भाषा को Dennis Ritchie ने 1972 में Bell Laboratories में विकसित किया।

  4. C प्रोग्रामिंग को "मदर लैंग्वेज" क्यों कहा जाता है?
    उत्तर: C को "मदर लैंग्वेज" कहा जाता है क्योंकि इसका उपयोग कई आधुनिक भाषाओं जैसे C++, Java, और Python के लिए आधार के रूप में किया गया है।

  5. C प्रोग्रामिंग किन क्षेत्रों में उपयोग होती है?
    उत्तर:

    • ऑपरेटिंग सिस्टम्स (जैसे Linux, Windows)
    • एम्बेडेड सिस्टम्स
    • गेम डेवलपमेंट
    • डेटाबेस (जैसे MySQL)
    • कंपाइलर और इंटरप्रेटर

थ्योरी आधारित प्रश्न

  1. C भाषा के मुख्य फीचर्स (features) क्या हैं?
    उत्तर:

    • सरल और समझने में आसान
    • Platform Independent (सिर्फ source code)
    • High-level और Low-level प्रोग्रामिंग का संयोजन
    • तेज़ और कुशल
    • Rich Library Functions
  2. C प्रोग्रामिंग की विशेषताएं अन्य भाषाओं से इसे कैसे अलग बनाती हैं?
    उत्तर:

    • यह low-level hardware तक सीधा पहुंच देती है।
    • Memory Management (Pointers) का उपयोग।
    • Procedural programming के लिए उपयुक्त।
    • इसे cross-platform उपयोग के लिए design किया गया है।
  3. C प्रोग्रामिंग में "Hello World" प्रोग्राम क्यों महत्वपूर्ण है?
    उत्तर: "Hello World" प्रोग्राम शुरुआती learners को प्रोग्राम की syntax, structure और execution process समझाने के लिए उपयोग किया जाता है।

  4. C प्रोग्राम के लिए आवश्यक बुनियादी घटक (basic components) क्या हैं?
    उत्तर:

    • Header Files (#include)
    • Main Function (int main())
    • Statements और Expressions
    • Curly Braces ({})
  5. C प्रोग्रामिंग में कंपाइलर का क्या कार्य है?
    उत्तर: कंपाइलर एक सॉफ़्टवेयर है जो C प्रोग्राम को human-readable code से machine-readable code (binary code) में परिवर्तित करता है।

लॉजिकल और प्रैक्टिकल प्रश्न

  1. पहला C प्रोग्राम "Hello World" कैसे लिखा और चलाया जाता है?
    उत्तर:
#include <stdio.h>
int main() {
    printf("Hello, World!");
    return 0;
}

इसे कंपाइल और रन करने के लिए किसी भी C कंपाइलर (जैसे GCC) का उपयोग करें।

  1. C प्रोग्राम को रन करने के लिए किस सॉफ़्टवेयर की आवश्यकता होती है?
    उत्तर: C प्रोग्राम को रन करने के लिए कंपाइलर (जैसे GCC, Turbo C) और IDE (जैसे Code::Blocks, Visual Studio Code) की आवश्यकता होती है।

  2. एक सिंपल C प्रोग्राम के हिस्सों को समझाइए।
    उत्तर:

    • #include <stdio.h>: Standard Input/Output functions के लिए।
    • int main(): Program execution का entry point।
    • printf(): Output को print करने के लिए।
    • return 0;: Program successful termination का संकेत।
  3. C प्रोग्राम में एक syntax error का क्या अर्थ है?
    उत्तर: Syntax error तब होती है जब कोड C के नियमों का पालन नहीं करता। उदाहरण: Semicolon भूल जाना।

  4. C प्रोग्रामिंग में डेटा प्रकार (data types) क्या हैं?
    उत्तर:

    • int: Integer values के लिए।
    • float: Decimal numbers के लिए।
    • char: Characters के लिए।
    • double: Large decimal numbers के लिए।

उन्नत प्रश्न (Higher-Level Thinking)

  1. C प्रोग्रामिंग अन्य भाषाओं (जैसे Python या Java) से कैसे अलग है?
    उत्तर:

    • Python और Java high-level languages हैं, जबकि C low-level features भी प्रदान करता है।
    • C में memory management हाथ से करना पड़ता है।
    • C में execution speed तेज़ होती है।
  2. क्या आप C प्रोग्रामिंग के उपयोग के लिए एक वास्तविक जीवन उदाहरण दे सकते हैं?
    उत्तर: Linux Kernel को C भाषा में लिखा गया है।

  3. C भाषा में procedural programming का क्या अर्थ है?
    उत्तर: Procedural programming में प्रोग्राम को functions में विभाजित किया जाता है। यह step-by-step निर्देशों के आधार पर काम करता है।

  4. C में preprocessor directives का क्या उपयोग है?
    उत्तर: Preprocessor directives (#include, #define) का उपयोग कंपाइल होने से पहले कोड को modify करने के लिए किया जाता है।

  5. "Structured Programming Language" के रूप में C को कैसे परिभाषित किया जा सकता है?
    उत्तर: C में कोड को छोटे-छोटे मॉड्यूल्स (functions) में विभाजित किया जाता है, जिससे कोड को पढ़ना और प्रबंधन करना आसान होता है।

डिस्कशन और ओपन-एंडेड प्रश्न

  1. C प्रोग्रामिंग सीखने के क्या फायदे हैं?
    उत्तर: यह प्रोग्रामिंग की मूलभूत समझ विकसित करता है, और हार्डवेयर के करीब काम करने की सुविधा देता है।

  2. C प्रोग्रामिंग का भविष्य में उपयोग सीमित होगा या विस्तृत?
    उत्तर: C प्रोग्रामिंग का उपयोग भविष्य में भी रहेगा क्योंकि ऑपरेटिंग सिस्टम, एम्बेडेड सिस्टम, और कंपाइलर विकास में इसकी भूमिका महत्वपूर्ण है।

  3. C प्रोग्रामिंग क्यों एक मजबूत आधार (strong foundation) मानी जाती है?
    उत्तर: यह low-level और high-level दोनों programming features प्रदान करती है, जो अन्य भाषाएं सीखने में मदद करती हैं।

  4. C प्रोग्रामिंग का उपयोग करके ऑपरेटिंग सिस्टम क्यों बनाए गए हैं?
    उत्तर: C भाषा तेज़, कुशल और low-level hardware control प्रदान करती है, जो ऑपरेटिंग सिस्टम के लिए आदर्श है।

  5. C भाषा का उपयोग करके आप कौन-कौन से प्रोजेक्ट बना सकते हैं?
    उत्तर:

    • ऑपरेटिंग सिस्टम
    • एम्बेडेड सिस्टम्स प्रोजेक्ट्स
    • गेम्स
    • डेटाबेस मैनेजमेंट सिस्टम




बेसिक्स ऑफ C (C की मूल बातें)

1. Variables

Definition:
Variables memory में एक नामित स्थान होते हैं, जिनका उपयोग डेटा को स्टोर करने के लिए किया जाता है। इनका उपयोग हम किसी मान को (value) स्टोर करने और फिर उस डेटा को उपयोग करने के लिए करते हैं।

Syntax to Declare a Variable in C:
C में वेरिएबल्स को डिक्लेयर करने के लिए पहले उसका डेटा टाइप (Data Type) और फिर उसका नाम (Variable Name) दिया जाता है।

data_type variable_name;

Example:

int age;        // Integer type variable
float salary;   // Floating point type variable
char grade;     // Character type variable

यहां, int, float, और char वेरिएबल्स के लिए डेटा टाइप्स हैं, और age, salary, और grade इनके नाम हैं।

Variable Initialization:
Variable को declare करने के बाद, उसे एक मान (value) से इनिशियलाइज किया जाता है ताकि वह डेटा स्टोर कर सके।

int age = 25;
float salary = 50000.50;
char grade = 'A';

Using Variables:
Variables का उपयोग हम किसी मान को स्टोर और फिर उस पर कुछ operations करने के लिए करते हैं। उदाहरण के लिए:

#include <stdio.h>
int main() {
    int age = 25;
    float salary = 50000.5;
    char grade = 'A';

    printf("Age: %d\n", age);
    printf("Salary: %.2f\n", salary);
    printf("Grade: %c\n", grade);

    return 0;
}

Output:

Age: 25  
Salary: 50000.50  
Grade: A  

Rules for Naming Variables:

  1. Variable का नाम सिर्फ alphabets (A-Z, a-z), numbers (0-9) और underscore (_) से हो सकता है।
  2. Variable का नाम कभी भी number से शुरू नहीं हो सकता।
  3. Variable का नाम C के reserved keywords (जैसे int, float, if, return) से मेल नहीं खा सकता।

2. Data Types

Definition:
Data Types यह निर्धारित करते हैं कि variable किस प्रकार का डेटा स्टोर कर सकता है। C में कई प्रकार के डेटा टाइप्स होते हैं, जिनका उपयोग हम विभिन्न प्रकार के डेटा को स्टोर करने के लिए करते हैं।

Types of Data Types in C:

  1. Primitive Data Types:

    • int: यह integer values (पूर्णांक) को store करता है, जैसे -1, 0, 25 आदि।
    • float: यह floating point numbers (दशमलव संख्या) को store करता है, जैसे 3.14, 5.5 आदि।
    • char: यह single character (एकल वर्ण) को store करता है, जैसे 'A', 'b', '9' आदि।
    • double: यह double precision floating point numbers (अधिक सटीक दशमलव संख्या) को store करता है, जैसे 3.141592, 2.71828183 आदि।
  2. Derived Data Types:

    • Arrays, Structures, Pointers, Enums, आदि (इनका विस्तार आगे किया जाएगा)।

Example:

#include <stdio.h>
int main() {
    int num = 10;            // Integer type variable
    float pi = 3.14;         // Float type variable
    char letter = 'C';       // Character type variable
    double bigPi = 3.141592; // Double type variable

    printf("Integer: %d\n", num);             // %d for integer
    printf("Float: %.2f\n", pi);             // %.2f for float with 2 decimal places
    printf("Character: %c\n", letter);       // %c for character
    printf("Double: %.6lf\n", bigPi);        // %.6lf for double with 6 decimal places

    return 0;
}

Output:

Integer: 10  
Float: 3.14  
Character: C  
Double: 3.141592  

Explanation of Format Specifiers:

  • %d is used for printing integers.
  • %.2f is used for printing floating point numbers with 2 decimal places.
  • %c is used for printing characters.
  • %.6lf is used for printing double precision floating point numbers with 6 decimal places.

3. Operators

Definition:
Operators का उपयोग दो या दो से अधिक operands पर operation (गणना) करने के लिए किया जाता है। C में कई प्रकार के ऑपरेटर्स होते हैं, जैसे Arithmetic Operators, Relational Operators, और Logical Operators।

Types of Operators in C:

  1. Arithmetic Operators (अंकगणितीय ऑपरेटर्स):
    • + : Addition (जोड़ना)
    • - : Subtraction (घटाना)
    • * : Multiplication (गुणा)
    • / : Division (भाग)
    • % : Modulo (भागफल, remainder)

Example:

#include <stdio.h>
int main() {
    int a = 10, b = 20;
    int sum = a + b;        // Addition
    int difference = a - b; // Subtraction
    int product = a * b;    // Multiplication
    int quotient = b / a;   // Division
    int remainder = b % a;  // Modulo (Remainder)

    printf("Sum: %d\n", sum);
    printf("Difference: %d\n", difference);
    printf("Product: %d\n", product);
    printf("Quotient: %d\n", quotient);
    printf("Remainder: %d\n", remainder);

    return 0;
}

Output:

Sum: 30  
Difference: -10  
Product: 200  
Quotient: 2  
Remainder: 0  

4. Printing and Input (प्रिंटिंग और इनपुट)

Printing:
C में किसी भी डेटा को स्क्रीन पर दिखाने के लिए printf() function का उपयोग किया जाता है। इसमें हम format specifiers का उपयोग करते हुए अलग-अलग प्रकार के डेटा को प्रिंट कर सकते हैं।

Input:
C में डेटा इनपुट करने के लिए scanf() function का उपयोग किया जाता है।

Example:

#include <stdio.h>
int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);  // Input integer value

    printf("Your age is: %d\n", age);  // Output the entered value

    return 0;
}

Output:

Enter your age: 25  
Your age is: 25  

Explanation:

  • printf() function का उपयोग स्क्रीन पर किसी डेटा को दिखाने के लिए किया जाता है।
  • scanf() function का उपयोग स्क्रीन से डेटा लेने के लिए किया जाता है।
  • &age इसका मतलब है कि हम age वेरिएबल के पते (address) पर डेटा स्टोर कर रहे हैं।

5. Conclusion

C प्रोग्रामिंग की बुनियादी जानकारी जैसे Variables, Data Types, Operators, और Input/Output functions को समझना बहुत ज़रूरी है, क्योंकि ये C में किसी भी प्रोग्राम को लिखने की नींव हैं। C में काम करते वक्त इनका सही उपयोग आपके प्रोग्राम को अधिक प्रभावी और समझने में आसान बना सकता है।



यहां कुछ प्रश्न दिए गए हैं जो "Basics of C" विषय से संबंधित हैं और छात्रों को C programming के बुनियादी सिद्धांतों को समझने में मदद करेंगे:

1. Variables (वेरिएबल्स)

  1. What is a variable in C? How is it declared? (C में वेरिएबल क्या होता है? इसे कैसे डिक्लेयर किया जाता है?)
  2. Declare and initialize the following variables in C: age, salary, grade (C में निम्नलिखित वेरिएबल्स को डिक्लेयर और इनिशियलाइज करें: age, salary, grade)
  3. What are the rules for naming variables in C? (C में वेरिएबल के नाम रखने के क्या नियम होते हैं?)

2. Data Types (डेटा टाइप्स)

  1. Explain the difference between int, float, char, and double data types in C. (C में int, float, char, और double डेटा टाइप्स के बीच अंतर बताइए।)
  2. Write a C program using different data types (such as int, float, char, double) and print their values. (विभिन्न डेटा टाइप्स (जैसे int, float, char, double) का उपयोग करके C प्रोग्राम लिखें और उनके मान को प्रिंट करें।)
  3. Declare the following data types in C: price, distance, initial (C में निम्नलिखित डेटा टाइप्स को डिक्लेयर करें: price, distance, initial)
  4. What is the difference between double and float data types in C? (C में double और float डेटा टाइप्स के बीच अंतर क्या है?)

3. Operators (ऑपरेटर्स)

  1. Explain the use of +, -, *, /, % operators in C and show an example. (C में +, -, *, /, % ऑपरेटर्स का उपयोग कैसे किया जाता है और एक उदाहरण के साथ समझाइए।)
  2. Implement the following operations in a C program:
    • Addition (जोड़ना)
    • Subtraction (घटाना)
    • Multiplication (गुणा)
    • Division (भाग)
    • Modulo (Remainder) (भागफल)
  3. How are relational operators used in C? Write an example comparing two numbers. (C में रिलेशनल ऑपरेटर्स का उपयोग कैसे किया जाता है? दो संख्याओं की तुलना करने का एक उदाहरण लिखिए।)

4. Printing and Input (प्रिंटिंग और इनपुट)

  1. Which function is used to print data on the screen in C? Explain with an example. (C में डेटा को स्क्रीन पर प्रिंट करने के लिए किस function का उपयोग किया जाता है? उदाहरण के साथ समझाइए।)
  2. Which function is used to take input from the user in C? Explain with an example. (C में उपयोगकर्ता से डेटा लेने के लिए किस function का उपयोग किया जाता है? उदाहरण के साथ बताइए।)
  3. Write a C program that takes input of two numbers from the user and calculates their sum, difference, product, and quotient. (C प्रोग्राम लिखिए जो उपयोगकर्ता से दो संख्याएं इनपुट लेकर उनका जोड़, घटाव, गुणा और भाग निकाले।)

5. Mixed Topics (मिश्रित विषय)

  1. Write a simple C program that asks the user for their name and age and prints the input data on the screen. (C प्रोग्राम लिखिए जो उपयोगकर्ता से उनका नाम और आयु पूछे और इनपुट डेटा को स्क्रीन पर प्रिंट करे।)
  2. How can variables be initialized in C? Provide examples of different methods. (C में वेरिएबल्स को कैसे इनिशियलाइज किया जा सकता है? विभिन्न तरीकों के उदाहरण दीजिए।)
  3. What is the importance of format specifiers in printf() and scanf() functions? Explain with examples. (C के printf() और scanf() functions में format specifiers का क्या महत्व है? उदाहरण के साथ समझाइए।)
  4. How can you change the value of a variable in C? Explain with an example. (C में वेरिएबल के मान को कैसे बदल सकते हैं? उदाहरण के साथ समझाइए।)


यहां "Basics of C" से संबंधित प्रश्नों को हल करके दिया गया है:

1. Variables (वेरिएबल्स)

1. What is a variable in C? How is it declared?
A variable in C is a named memory location used to store data. It can store different types of data, such as integers, floating-point numbers, and characters. A variable is declared by specifying its data type followed by its name.

Syntax:

data_type variable_name;

Example:

int age;      // Integer variable
float salary; // Floating point variable
char grade;   // Character variable

2. Declare and initialize the following variables in C: age, salary, grade.

int age = 25;       // Initializing integer variable
float salary = 50000.50; // Initializing floating point variable
char grade = 'A';   // Initializing character variable

3. What are the rules for naming variables in C?

  • A variable name must begin with a letter (A-Z, a-z) or an underscore (_).
  • The name can contain letters, digits (0-9), and underscores.
  • It cannot start with a digit.
  • Reserved keywords in C (e.g., int, float, return) cannot be used as variable names.
  • C variable names are case-sensitive (e.g., age and Age are different).

2. Data Types (डेटा टाइप्स)

1. Explain the difference between int, float, char, and double data types in C.

  • int: Used to store integer values (whole numbers), e.g., 1, 100, -5.
  • float: Used to store floating-point numbers (decimal values), e.g., 3.14, 5.6.
  • char: Used to store a single character, e.g., 'A', 'b', '1'.
  • double: Used to store larger floating-point numbers with double precision, e.g., 3.14159265359.

2. Write a C program using different data types (such as int, float, char, double) and print their values.

#include <stdio.h>

int main() {
    int age = 25;
    float salary = 50000.50;
    char grade = 'A';
    double pi = 3.141592;

    printf("Age: %d\n", age);
    printf("Salary: %.2f\n", salary);
    printf("Grade: %c\n", grade);
    printf("Pi: %.6lf\n", pi);

    return 0;
}

Output:

Age: 25
Salary: 50000.50
Grade: A
Pi: 3.141593

3. Declare the following data types in C: price, distance, initial.

float price;
double distance;
char initial;

4. What is the difference between double and float data types in C?

  • float stores floating-point numbers with a precision of 6-7 digits.
  • double stores floating-point numbers with a higher precision of 15-16 digits.

3. Operators (ऑपरेटर्स)

1. Explain the use of +, -, *, /, % operators in C and show an example.

  • +: Addition (adds two numbers).
  • -: Subtraction (subtracts one number from another).
  • *: Multiplication (multiplies two numbers).
  • /: Division (divides one number by another).
  • %: Modulo (gives the remainder of division).

Example:

#include <stdio.h>

int main() {
    int a = 10, b = 5;
    printf("Addition: %d\n", a + b);
    printf("Subtraction: %d\n", a - b);
    printf("Multiplication: %d\n", a * b);
    printf("Division: %d\n", a / b);
    printf("Modulo: %d\n", a % b);
    
    return 0;
}

Output:

Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulo: 0

2. Implement the following operations in a C program:

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Modulo (Remainder)
#include <stdio.h>

int main() {
    int a = 20, b = 4;
    printf("Addition: %d\n", a + b);
    printf("Subtraction: %d\n", a - b);
    printf("Multiplication: %d\n", a * b);
    printf("Division: %d\n", a / b);
    printf("Modulo: %d\n", a % b);
    
    return 0;
}

Output:

Addition: 24
Subtraction: 16
Multiplication: 80
Division: 5
Modulo: 0

3. How are relational operators used in C? Write an example comparing two numbers.
Relational operators are used to compare two values. They return a boolean value (true or false).

Example:

#include <stdio.h>

int main() {
    int a = 10, b = 20;

    if (a < b) {
        printf("a is less than b\n");
    }

    if (a == b) {
        printf("a is equal to b\n");
    }

    return 0;
}

Output:

a is less than b

4. Printing and Input (प्रिंटिंग और इनपुट)

1. Which function is used to print data on the screen in C? Explain with an example.
In C, the printf() function is used to print data to the screen.

Example:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Output:

Hello, World!

2. Which function is used to take input from the user in C? Explain with an example.
In C, the scanf() function is used to take input from the user.

Example:

#include <stdio.h>

int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    printf("Your age is: %d\n", age);
    return 0;
}

Output:

Enter your age: 25
Your age is: 25

3. Write a C program that takes input of two numbers from the user and calculates their sum, difference, product, and quotient.

#include <stdio.h>

int main() {
    int num1, num2;
    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    printf("Sum: %d\n", num1 + num2);
    printf("Difference: %d\n", num1 - num2);
    printf("Product: %d\n", num1 * num2);
    printf("Quotient: %d\n", num1 / num2);

    return 0;
}

Output:

Enter two numbers: 10 5
Sum: 15
Difference: 5
Product: 50
Quotient: 2

5. Mixed Topics (मिश्रित विषय)

1. Write a simple C program that asks the user for their name and age and prints the input data on the screen.

#include <stdio.h>

int main() {
    char name[50];
    int age;
    
    printf("Enter your name: ");
    scanf("%s", name);
    printf("Enter your age: ");
    scanf("%d", &age);

    printf("Name: %s\n", name);
    printf("Age: %d\n", age);

    return 0;
}

Output:

Enter your name: John
Enter your age: 25
Name: John
Age: 25

2. How can variables be initialized in C? Provide examples of different methods.
Variables can be initialized in C in the following ways:

  • During declaration:
int age = 25;
  • Separately:
int age;
age = 25;

3. What is the importance of format specifiers in printf() and scanf() functions? Explain with examples.
Format specifiers tell the printf() and scanf() functions how to interpret and display the data.
For example:

  • %d is used for integers.
  • %f is used for floating-point numbers.
  • %c is used for characters.
  • %s is used for strings.

Example:

int num = 10;
float pi = 3.14;
printf("Integer: %d\n", num);
printf("Float: %.2f\n", pi);

**4. How can you change the value of a variable in C? Explain with

an example.**
You can change the value of a variable by simply assigning a new value to it.

Example:

int age = 25;
age = 30; // Changing the value of age
printf("New Age: %d\n", age);

Output:

New Age: 30





Post a Comment

0 Comments