C प्रोग्रामिंग एक व्यापक और बुनियादी प्रोग्रामिंग भाषा है जिसे 1970 के दशक में डेनिस रिची द्वारा विकसित किया गया था। यह भाषा सिस्टम प्रोग्रामिंग, जैसे ऑपरेटिंग सिस्टम और एम्बेडेड सिस्टम डेवलपमेंट, के लिए बहुत उपयोगी है। C प्रोग्रामिंग को समझने के लिए निम्नलिखित बुनियादी जानकारी महत्वपूर्ण है:
C प्रोग्राम आमतौर पर निम्नलिखित हिस्सों में विभाजित होता है:
Header Files: प्रोग्राम की शुरुआत में #include के माध्यम से लाइब्रेरी फाइलें जोड़ी जाती हैं, जैसे #include <stdio.h>।
Main Function: main() फंक्शन प्रोग्राम का एंट्री पॉइंट होता है।
Statements और Code Blocks: { } के अंदर सभी निर्देश लिखे जाते हैं।
उदाहरण:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
C में विभिन्न प्रकार के डेटा टाइप्स हैं:
int: पूर्णांक (integer) जैसे 10, -5
float: दशमलव संख्या जैसे 3.14
char: एकल अक्षर जैसे 'A'
double: उच्च परिशुद्धता वाली दशमलव संख्या
उदाहरण:
int a = 10;
float b = 3.14;
char c = 'A';
वेरिएबल्स: डेटा को स्टोर करने के लिए उपयोग किए जाते हैं। उदाहरण: int age = 25;
कॉन्स्टेंट्स: अपरिवर्तनीय मान। उदाहरण: const float PI = 3.14;
C में विभिन्न प्रकार के ऑपरेटर उपलब्ध हैं:
Arithmetic Operators: +, -, *, /, %
Relational Operators: ==, !=, <, >, <=, >=
Logical Operators: &&, ||, !
उदाहरण:
int a = 10, b = 5;
int sum = a + b; // sum = 15
C में प्रोग्राम के प्रवाह को नियंत्रित करने के लिए विभिन्न स्टेटमेंट्स हैं:
if-else: निर्णय लेने के लिए।
switch: कई विकल्पों में से चुनने के लिए।
loops: जैसे for, while, do-while, पुनरावृत्ति के लिए।
उदाहरण (if-else):
int num = 10;
if (num > 0) {
printf("Positive number");
} else {
printf("Negative number");
}
प्रोग्राम को छोटे हिस्सों में विभाजित करने के लिए फंक्शन्स का उपयोग किया जाता है।
उपयोगकर्ता-परिभाषित और प्री-डिफाइंड फंक्शन्स होते हैं।
उदाहरण:
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3);
printf("Sum: %d", result);
return 0;
}
Arrays: समान प्रकार के डेटा का संग्रह। उदाहरण: int arr[5];
Pointers: मेमोरी एड्रेस को स्टोर करने के लिए उपयोगी। उदाहरण: int *ptr;
उदाहरण (Pointers):
int a = 10;
int *ptr = &a;
printf("Value of a: %d", *ptr);
C में डेटा को फाइल्स में स्टोर और पढ़ने के लिए फाइल हैंडलिंग का उपयोग होता है।
फाइल खोलना: fopen()
फाइल पढ़ना: fscanf() या fgets()
फाइल लिखना: fprintf() या fputs()
फाइल बंद करना: fclose()
malloc(), calloc(), realloc() और free() का उपयोग डायनामिक मेमोरी आवंटन के लिए किया जाता है।
सिंपल प्रोग्राम्स से शुरुआत करें, जैसे जोड़ना, गुणा करना।
कंट्रोल स्टेटमेंट और लूप्स का अभ्यास करें।
प्रोजेक्ट्स जैसे कैलकुलेटर या नंबर गेम बनाएं।
डेटा स्ट्रक्चर्स (Arrays, Linked Lists) का उपयोग करें।
यदि आपको किसी विशेष विषय पर विस्तार से जानकारी चाहिए, तो बताएं। 😊
C programming is a foundational language developed in the 1970s by Dennis Ritchie. It is widely used for system programming, such as developing operating systems, embedded systems, and other performance-critical applications. Below is a detailed overview of C programming fundamentals:
A basic C program consists of:
Header Files: Libraries included at the start with #include, e.g., #include <stdio.h>.
Main Function: The main() function serves as the entry point of the program.
Statements and Code Blocks: Instructions are written inside { }.
Example:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
C provides various data types:
int: For integers (e.g., 10, -5)
float: For decimal numbers (e.g., 3.14)
char: For single characters (e.g., 'A')
double: For high-precision decimal numbers
Example:
int a = 10;
float b = 3.14;
char c = 'A';
Variables: Used to store data values. Example: int age = 25;
Constants: Immutable values. Example: const float PI = 3.14;
C provides different types of operators:
Arithmetic Operators: +, -, *, /, %
Relational Operators: ==, !=, <, >, <=, >=
Logical Operators: &&, ||, !
Example:
int a = 10, b = 5;
int sum = a + b; // sum = 15
Control the program flow using:
if-else: For decision-making.
switch: For choosing between multiple cases.
Loops: Repetition using for, while, do-while.
Example (if-else):
int num = 10;
if (num > 0) {
printf("Positive number");
} else {
printf("Negative number");
}
Functions break a program into smaller reusable components.
User-defined functions: Functions created by the programmer.
Built-in functions: Provided by C libraries.
Example:
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3);
printf("Sum: %d", result);
return 0;
}
Arrays: Store a collection of elements of the same type. Example: int arr[5];
Pointers: Store memory addresses. Example: int *ptr;
Example (Pointer):
int a = 10;
int *ptr = &a;
printf("Value of a: %d", *ptr);
C allows data to be stored and retrieved using files:
Open a file: fopen()
Read data: fscanf() or fgets()
Write data: fprintf() or fputs()
Close a file: fclose()
Dynamic memory allocation functions in C include:
malloc(), calloc(), realloc(), and free().
Example:
int *ptr;
ptr = (int*) malloc(5 * sizeof(int)); // Allocating memory
if (ptr == NULL) {
printf("Memory not allocated.\n");
} else {
printf("Memory allocated successfully.\n");
}
free(ptr); // Free allocated memory
Start with simple programs like addition or multiplication.
Practice control statements and loops.
Build projects like a calculator or a number guessing game.
Explore data structures like arrays and linked lists.
C program ek particular structure ko follow karta hai. Sabse pehle, #include directive hota hai jo libraries ko include karta hai. Phir main() function hota hai jahan se program execution start hota hai.
c
code
#include <stdio.h> // Standard Input/Output library
int main() { // Main function
printf("Hello, World!"); // Print statement
return 0; // Return value, program successfully ended
}
C mein different types of data store kiye ja sakte hain, jaise integers, floating-point numbers, characters, etc.
int: Whole numbers
float: Decimal numbers
char: Single characters
double: Double precision floating-point numbers
c
code
#include <stdio.h>
int main() {
int a = 5; // Integer
float b = 4.5; // Floating point number
char c = 'A'; // Character
double d = 5.55555; // Double precision floating-point number
printf("Integer: %d\n", a);
printf("Float: %.2f\n", b);
printf("Character: %c\n", c);
printf("Double: %.5f\n", d);
return 0;
}
Variables ko hum kisi bhi type ka value store karne ke liye declare karte hain. Jaise:
c
code
int x = 10; // Declare and initialize variable
float y = 2.5;
if-else Statement:
Yeh statement conditions ko check karta hai aur appropriate action leta hai.
c
code
#include <stdio.h>
int main() {
int num = 5;
if (num > 0) {
printf("Positive Number");
} else {
printf("Negative Number");
}
return 0;
}
switch-case Statement:
Multiple conditions ko handle karne ke liye switch use hota hai.
c
code
#include <stdio.h>
int main() {
int choice = 2;
switch (choice) {
case 1:
printf("Choice 1\n");
break;
case 2:
printf("Choice 2\n");
break;
default:
printf("Invalid Choice\n");
}
return 0;
}
for Loop:
Loop ko use karke hum ek statement ko baar-baar execute karte hain.
c
code
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
return 0;
}
while Loop:
Jab tak condition true ho, tab tak loop chalta rehta hai.
c
code
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
return 0;
}
do-while Loop:
Pehle code execute hota hai, fir condition check hoti hai.
c
code
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5);
return 0;
}
Functions ka use code ko modular banane ke liye kiya jata hai. Ek function ka definition aur call dono hota hai.
c
code
#include <stdio.h>
int add(int a, int b) { // Function Definition
return a + b;
}
int main() {
int result = add(5, 10); // Function Call
printf("Sum = %d", result);
return 0;
}
Array ek collection of elements hota hai, jisme elements same data type ke hote hain.
c
code
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Pointers memory locations ko store karte hain.
c
code
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a; // Pointer to variable a
printf("Value: %d, Address: %p", *ptr, ptr);
return 0;
}
Structure ek custom data type hota hai jisme different types ke variables ko store kiya ja sakta hai.
c
code
#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
int main() {
struct Student s1 = {"John", 20, 85.5};
printf("Name: %s, Age: %d, Marks: %.2f", s1.name, s1.age, s1.marks);
return 0;
}
C mein file operations ko handle karne ke liye fopen(), fprintf(), fclose() jaise functions ka use hota hai.
c
code
#include <stdio.h>
int main() {
FILE *fptr = fopen("example.txt", "w"); // Open file in write mode
if (fptr == NULL) {
printf("Error opening file.");
return 1;
}
fprintf(fptr, "Hello, file handling!"); // Write to file
fclose(fptr); // Close the file
return 0;
}
malloc() aur free() functions ka use karke hum dynamic memory allocate aur deallocate karte hain.
c
code
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(5 * sizeof(int)); // Dynamically allocating memory
if (ptr == NULL) {
printf("Memory allocation failed!");
return 1;
}
for (int i = 0; i < 5; i++) {
ptr[i] = i + 1;
printf("%d ", ptr[i]);
}
free(ptr); // Freeing memory
return 0;
}
Recursion mein ek function apne aap ko call karta hai jab tak base condition nahi milti.
c
code
#include <stdio.h>
int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int result = factorial(5);
printf("Factorial: %d", result);
return 0;
}
Command line arguments ko handle karne ke liye argc aur argv[] use hota hai.
c
code
#include <stdio.h>
int main(int argc, char *argv[]) {
for (int i = 0; i < argc; i++) {
printf("Argument %d: %s\n", i, argv[i]);
}
return 0;
}
C Language Fundamentals:
History of C: C programming language ko Dennis Ritchie ne 1972 mein develop kiya tha. Yeh structured programming language hai.
Character Set: C mein characters ko represent karne ke liye ASCII code ka use hota hai.
C Tokens: Yeh smallest unit hoti hai C program ki, jaise keywords, operators, constants, variables, etc.
Keywords & Identifiers: Keywords reserved words hote hain, jaise int, if, return, etc. Identifiers user-defined names hote hain, jaise variable names.
Variables & Constants: Variables ko values store karne ke liye declare kiya jata hai. Constants wo values hoti hain jo change nahi hoti.
Data Types: int, float, char, etc., jo data store karte hain.
Types of Operators:
Arithmetic Operators: +, -, *, /, %
Relational Operators: >, <, >=, <=, ==, !=
Logical Operators: &&, ||, !
Bitwise Operators: &, |, ^, <<, >>
Unary Operators: ++, --, etc.
Assignment Operators: =, +=, -=, etc.
Operator Precedence & Associativity: Operators ki priority kaunse pehle execute hote hain, yeh determine karta hai operator precedence.
Decision Making (Conditional Statements):
if, if-else, nested if-else, switch-case: Yeh structures decision making mein use hote hain.
Example:
c
Code
if (x > 0) {
printf("Positive");
} else {
printf("Negative");
}
Loop Control Structures:
while, do-while, for loops: Loop structures ko use karke hum same task ko multiple times execute karte hain.
Example (for loop):
c
code
for (int i = 0; i < 5; i++) {
printf("%d ", i);
}
Other Statements:
break, continue, goto, exit ka use loop ko control karne ke liye hota hai.
Problem Solving Techniques:
Trial & Error, Brainstorming, Divide & Conquer: Yeh methods problems ko solve karne mein madad karte hain.
Steps in Problem Solving:
Define the problem
Analyze the problem
Explore solutions
Algorithms & Flowcharts:
Definition: Algorithm ek step-by-step procedure hota hai jo problem ko solve karta hai.
Flowchart: Problem-solving process ko graphically represent karne ka tarika.
Example: Addition of two numbers:
Algorithm:
Start
Read a, b
sum = a + b
Print sum
End
Flowchart:
Start → Input a, b → sum = a + b → Print sum → End
Examples of Simple Problems:
Addition, multiplication of integers
Checking if a number is positive/negative/even/odd
Maximum of two or three numbers
Sum of first n numbers
Prime number check
Factorial of a number
Types of Functions:
Basic Functions: Functions jo specific task perform karte hain, like addition, subtraction, etc.
Function Declaration & Definition:
Declaration tells the compiler about the function name, return type, and parameters.
Definition contains the actual body of the function.
Example:
c
code
int add(int a, int b) {
return a + b;
}
Types of Function Calls:
Call by Value: Function ke arguments ka actual value pass hota hai.
Call by Reference: Function ko arguments ka address pass hota hai, jo directly modify hote hain.
Recursion: Ek function apne aap ko call karta hai jab tak base condition nahi milti.
Example:
c
Copy code
int factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);
}
C Language Fundamentals:
History of C:
C programming language ko Dennis Ritchie ne 1972 mein develop kiya tha. Yeh structured programming language hai aur C ke features aaj bhi modern languages jaise C++, Java mein use hote hain.
Character Set:
C language mein characters ko represent karne ke liye ASCII (American Standard Code for Information Interchange) ka use hota hai.
Character Set: Letters, digits, punctuation symbols, and special characters.
C Tokens:
Tokens C program ke smallest units hain. Inhe Keywords, Identifiers, Constants, Strings, Operators etc. ke roop mein divide kiya jata hai.
Keywords: Reserved words jo C language ke specific functions ko represent karte hain, jaise int, if, return, etc.
Identifiers: User-defined names, jaise variable, function, and array names.
Constants: Fixed values jo program ke execution ke dauran change nahi hoti, jaise const int a = 5;.
Operators: Mathematical or logical operations perform karne wale symbols, jaise +, -, *, &&, etc.
Variables & Constants:
Variables memory locations hote hain jahan data store kiya jata hai.
Constants wo values hoti hain jo program ke execution ke dauran change nahi hoti.
Data Types:
Basic Data Types: int, float, char, double, etc.
Derived Data Types: Array, Pointers, Structures, Unions.
Void Data Type: A type used when no value is returned by a function.
Comments:
C mein 2 tarah ke comments hote hain:
Single-line comment: // comment here
Multi-line comment: /* comment here */
Types of Operators:
Arithmetic Operators:
+ (addition), - (subtraction), * (multiplication), / (division), % (modulus). Example:
c
code
int sum = 5 + 3; // sum = 8
Relational Operators:
> (greater than), < (less than), == (equal), != (not equal), >= (greater than or equal), <= (less than or equal). Example:
c
code
if (a > b) { printf("a is greater than b"); }
Logical Operators:
&& (AND), || (OR), ! (NOT). Example:
c
code
if (a > 0 && b < 10) { printf("Condition true"); }
Bitwise Operators:
& (AND), | (OR), ^ (XOR), << (left shift), >> (right shift). Example:
c
code
int result = a & b; // AND operation on a and b
Unary Operators:
++ (increment), -- (decrement), sizeof (size of variable). Example:
c
code
int x = 5;
x++; // x becomes 6
Assignment Operators:
= (simple assignment), += (addition assignment), -= (subtraction assignment), etc. Example:
c
code
a += 5; // a = a + 5
Decision Making:
if-else:
Decision making structure hai jo ek condition check karta hai. Agar condition true hai toh if block execute hota hai, warna else block. Example:
c
code
if (a > b) {
printf("a is greater");
} else {
printf("b is greater");
}
Nested if-else:
Ek if-else ke andar doosra if-else likha jaata hai. Example:
c
code
if (a > b) {
if (a > c) {
printf("a is the greatest");
} else {
printf("c is the greatest");
}
}
switch-case:
Multiple conditions ko check karne ke liye use hota hai. Example:
c
code
switch (day) {
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
// other cases
default:
printf("Invalid day");
}
Loop Control Structures:
for loop:
A loop that repeats a block of code for a specific number of times. Example:
c
code
for (int i = 0; i < 5; i++) {
printf("%d ", i); // Output: 0 1 2 3 4
}
while loop:
A loop that repeats as long as the condition is true. Example:
c
code
int i = 0;
while (i < 5) {
printf("%d ", i);
i++;
}
do-while loop:
Like the while loop, but it executes the block of code at least once, even if the condition is false initially. Example:
c
code
int i = 0;
do {
printf("%d ", i);
i++;
} while (i < 5);
break, continue, goto:
break: Loop ko terminate karta hai.
continue: Loop ki current iteration ko skip karta hai aur next iteration pe jump karta hai.
goto: Program ko ek specific label pe jump karne ke liye use hota hai.
Problem-Solving Techniques:
Trial and Error: Problem ko solve karte waqt galat solutions ko test karna aur correct solution tak pahuncha.
Brainstorming: Different ideas ko gather karke sabse best solution choose karna.
Divide and Conquer: Problem ko chhote chhote parts mein divide karke solve karna.
Algorithms & Flowcharts:
Algorithm: Step-by-step procedure jo kisi problem ko solve karta hai.
Flowchart: Graphical representation of an algorithm using different symbols.
Example of an algorithm for adding two numbers:
Start
Read numbers a and b
Calculate sum = a + b
Print sum
End
Examples:
Addition of two numbers:
Algorithm to add two numbers and print result.
Multiplication of numbers
Checking if a number is positive or negative
Finding maximum of two numbers
Sum of first n natural numbers
Prime number check
Factorial calculation using recursion
Types of Functions:
Function Declaration: Function ka signature bataata hai, jaise return type, function name, aur parameters.
Function Definition: Function ka body define karta hai, jo actual task perform karta hai.
Function Call: Jab function ko call kiya jata hai, toh function execute hota hai.
Example:
c
code
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3);
printf("%d", result); // Output: 8
return 0;
}
Recursion:
Ek function apne aap ko call karta hai jab tak base condition nahi milti. Example of factorial using recursion:
c
code
int factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);
}
Chaliye, C programming ke important topics ko revise karte hain examples ke saath. 😊
c
code
#include <stdio.h> // Header file
int main() { // Main function
printf("Hello, World!"); // Print statement
return 0; // Program ends here
}
c
code
#include <stdio.h>
int main() {
int a = 10; // Integer type
float b = 5.5; // Float type
char c = 'A'; // Character type
printf("a = %d, b = %.1f, c = %c", a, b, c);
return 0;
}
c
code
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("Number is positive.\n");
} else {
printf("Number is non-positive.\n");
}
return 0;
}
a) For Loop
c
code
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
return 0;
}
b) While Loop
c
code
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
return 0;
}
c) Do-While Loop
c
code
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5);
return 0;
}
c
code
#include <stdio.h>
int add(int a, int b) { // Function definition
return a + b;
}
int main() {
int result = add(5, 10); // Function call
printf("Sum = %d", result);
return 0;
}
c
code
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
c
code
#include <stdio.h>
int main() {
int a = 10;
int *p = &a; // Pointer stores address of variable
printf("Value of a = %d, Address of a = %p", *p, p);
return 0;
}
c
code
#include <stdio.h>
int main() {
FILE *fptr = fopen("file.txt", "w"); // Open file in write mode
fprintf(fptr, "Hello, File Handling!"); // Write to file
fclose(fptr); // Close file
return 0;
}
c
code
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(5 * sizeof(int)); // Allocating memory for 5 integers
for (int i = 0; i < 5; i++) {
ptr[i] = i + 1;
printf("%d ", ptr[i]);
}
free(ptr); // Freeing allocated memory
return 0;
}
short () integer short box
int () integer box
long () intger long box
float () riyal nu. box
char () charter box
scanf- input uger se lene ke liye ......scanf("%d %f %c",&x,&y,&z);
printf- outport uger ko dene ke liye ..printf("%d %f %c",x,y,z);
char () short chartchar box
getchar - input uger se lene ke liye ... x=gatcher()
putchar - output uger ke dene ke liye...putchar(x)