0% found this document useful (0 votes)
13 views25 pages

NotesOfCDS C

The document provides an overview of fundamental concepts in computer science, including flowcharts, algorithms, data structures (like stacks, queues, linked lists, trees, and graphs), and various programming techniques such as recursion and dynamic programming. It also outlines key characteristics of algorithms, types of sorting and searching algorithms, and the structure of a basic C program. Additionally, it discusses variables, constants, keywords, and comments in programming.

Uploaded by

Apurv Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views25 pages

NotesOfCDS C

The document provides an overview of fundamental concepts in computer science, including flowcharts, algorithms, data structures (like stacks, queues, linked lists, trees, and graphs), and various programming techniques such as recursion and dynamic programming. It also outlines key characteristics of algorithms, types of sorting and searching algorithms, and the structure of a basic C program. Additionally, it discusses variables, constants, keywords, and comments in programming.

Uploaded by

Apurv Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

2/23/25, 1:44 AM NotesOfCDS.

1 /*
2 <--- Flowchart --->
3 |
4 A flowchart is a type of diagram that represents a workflow or process.
5 A flowchart can also be defined as a diagrammatic representation of an
6 algorithm, a step-by-step approach to solving a task.
7 The flowchart shows the steps as boxes of various kinds, and their order
8 by connecting the boxes with arrows.
9
10
11
12 <--- Algorithm --->
13 |
14 An **algorithm** is a step-by-step procedure or set of rules designed to solve a
15 specific problem or perform a specific task. In computer science,
16 algorithms are used to process data, execute computations, and automate reasoning
tasks.
17 They are the foundation of programming and software development,
18 helping to create efficient, logical, and effective solutions.
19
20 ### Key Characteristics of Algorithms:
21 1. **Definiteness**: Each step of the algorithm is clear and unambiguous.
22 2. **Input**: An algorithm accepts input data, which may come in different forms
(numbers, characters, etc.).
23 3. **Output**: The algorithm produces one or more outputs, which are the results of
processing the input.
24 4. **Finiteness**: The algorithm must always terminate after a finite number of
steps.
25 5. **Effectiveness**: Every step of the algorithm must be simple enough to be
carried out.
26
27 ### Example of a Simple Algorithm:
28 - **Sorting Algorithms**: An example would be the **Bubble Sort**,
29 which compares adjacent elements in a list and swaps them until the entire list is
sorted in a specific order.
30
31 ### Types of Algorithms:
32 1. **Search Algorithms**: Used to retrieve data from a data structure (e.g.,
**Binary Search**).
33 2. **Sorting Algorithms**: Organize data (e.g., **Quick Sort**, **Merge Sort**).
34 3. **Graph Algorithms**: Work with graph structures (e.g., **Dijkstra's Algorithm**
for finding the shortest path).
35 4. **Mathematical Algorithms**: Perform mathematical calculations
36 (e.g., **Euclidean Algorithm** for finding the greatest common divisor).
37 5. **Machine Learning Algorithms**: Algorithms used to analyze data and make
predictions
38 (e.g., **Decision Trees**, **Neural Networks**).
39
40 In essence, an algorithm is a blueprint for solving problems efficiently and is the
core
41 concept of computation in both theoretical and applied computer science.
42
43
44 <--- Pseudocode --->
45 |
46 In computer science, pseudocode is a description of the steps
47 in an algorithm using a mix of conventions of programming languages
48 (like assignment operator, conditional operator, loop) with informal,
49 usually self-explanatory, notation of actions and conditions.

localhost:4649/?mode=clike 1/25
2/23/25, 1:44 AM NotesOfCDS.c

50 Pseudocode is used to describe the steps of an algorithm in a way that is easy to


understand.
51
52 <--- Stack --->
53 |
54 A Stack is a linear data structure that holds a linear, ordered sequence of
elements.
55 It is an abstract data type. A Stack works on the LIFO process (Last In First Out),
56 i.e., the element that was inserted last will be removed first.
57
58
59 <--- Queue --->
60 |
61 A Queue is a linear data structure that holds a linear, ordered sequence of
elements.
62 It is an abstract data type. A Queue works on the FIFO process (First In First Out),
63 i.e., the element that was inserted first will be removed first.
64
65
66 <--- Linked List --->
67 |
68 A linked list is a linear data structure where each element is a separate object.
69 Each element (we will call it a node) of a list is comprising of two items -
70 the data and a reference (or link) to the next node in the sequence. This structure
allows
71 for efficient insertion or removal of elements from any position in the sequence.
72
73
74 <--- Tree --->
75 |
76 A tree is a widely used abstract data type that simulates a hierarchical tree
structure,
77 with a root value and subtrees of children with a parent node, represented as a set
of linked nodes.
78
79
80 <--- Graph --->
81 |
82 A graph is a data structure that consists of a finite set of nodes (vertices) and
83 a set of edges that connect these nodes.
84 A graph can be directed or undirected, and it can be weighted or unweighted.
85
86 <--- Sorting --->
87 |
88 Sorting is the process of arranging elements in a specific order,
89 usually in ascending or descending order. Sorting is a fundamental operation in
computer science
90 and is used in various applications, such as searching, data analysis, and
optimization.
91
92 Types of Sorting Algorithms:-
93 1. Bubble Sort
94 2. Selection Sort
95 3. Insertion Sort
96 4. Merge Sort
97 5. Quick Sort
98 6. Heap Sort
99 7. Radix Sort
100 8. Counting Sort
101 9. Bucket Sort
localhost:4649/?mode=clike 2/25
2/23/25, 1:44 AM NotesOfCDS.c

102 10. Shell Sort


103 11. Comb Sort
104 12. Cocktail Sort
105 13. Gnome Sort
106 14. Pancake Sort
107 15. Cycle Sort
108 16. Stooge Sort
109 17. Bogo Sort
110 18. Sleep Sort
111 19. Bitonic Sort
112 20. Timsort
113 21. Smooth Sort
114 22. Odd-Even Sort
115 23. Bead Sort
116 24. Brick Sort
117 25. Library Sort
118 26. Patience Sort
119 27. Strand Sort
120 28. Spread Sort
121 29. Tree Sort
122 30. Cube Sort
123 31. Flash Sort
124
125 <--- Searching --->
126 |
127 Searching is the process of finding a specific element in a collection of elements.
128 There are various searching algorithms, each with its own characteristics and
efficiency.
129
130 Types of Searching Algorithms:-
131 1. Linear Search
132 2. Binary Search
133 3. Jump Search
134 4. Interpolation Search
135 5. Exponential Search
136 6. Fibonacci Search
137 7. Ternary Search
138 8. Sublist Search (Search a linked list in another list)
139 9. Hash Search
140 10. Depth-First Search (DFS)
141 11. Breadth-First Search (BFS)
142 12. Uniform Cost Search
143 13. A\* Search
144 14. Iterative Deepening Depth-First Search (IDDFS)
145 15. Bidirectional Search
146 16. Best-First Search
147 17. Hill Climbing
148 18. Simulated Annealing
149 19. Genetic Algorithms
150 20. Tabu Search
151 21. Beam Search
152 22. Ant Colony Optimization
153
154 <--- Recursion --->
155 |
156 Recursion is a programming technique where a function calls itself in its
definition.
157 It is used to solve problems that can be broken down into smaller, simpler problems
of the same type.
158
localhost:4649/?mode=clike 3/25
2/23/25, 1:44 AM NotesOfCDS.c

159 <--- Dynamic Programming --->


160 |
161 Dynamic programming is a method for solving complex problems by breaking them down
into simpler subproblems.
162 It is used when the subproblems are overlapping and can be solved independently.
163
164 <--- Greedy Algorithm --->
165 |
166 A greedy algorithm is an algorithmic paradigm that follows the problem-solving
heuristic of making the locally
167 optimal choice at each stage with the hope of finding a global optimum.
168
169 <--- Divide and Conquer --->
170 |
171 Divide and conquer is an algorithm design paradigm based on multi-branched
recursion.
172 ------------------------------------------------------------------------------------
173 ------------------------------------------------------------------------------------
174
175 // Fork Bomb in c
176
177 // Modified fork bomb
178 // #include <unistd.h>
179 // #include <malloc.h>
180
181 // int main()
182 // {
183 // //Infinite loop
184 // while (1)
185 // {
186 // // Generating child fork processes
187 // fork();
188 // }
189 // }
190 // This code will generate child processes infinitely until the system runs out of
resources and crashes.
191
192
193 // BASH Fork Bomb lINUX
194 // :(){ :|: & };:
195
196 // This code will generate child processes infinitely until the system runs out of
resources and crashes.
197 // The `:` function is a bash function that calls itself recursively, and the `|:`
and `&` operators
198
199 ------------------------------------------------------------------------------------
200 ------------------------------------------------------------------------------------
201
202 <--- Chapter 1 --->
203
204
205 <--- Variables --->
206 ||
207 Variable is the name of a memory
208 location which stores some data.
209
210 Memory :-
211
212 a --> Variable b --> Varibale
localhost:4649/?mode=clike 4/25
2/23/25, 1:44 AM NotesOfCDS.c

213 ____________________________________________
214 | | | | | |
215 | 2 5 | | | S | |
216 |________|_______|________|_______|________|
217
218
219 ---> Variables
220 Rules
221 |
222 |
223 a. Variables are case sensitive
224 b. 1st character is alphabet or '_'
225 c. no comma/blank space
226 d. No other symbol other than '_'
227
228 -----------------------------------------
229 ---------> Variables |
230 | |
231 <--- Data Types ---> |
232 | |
233 | |
234 Data Types Size in bytes |
235 | | |
236 char or singd char 1 bytes |
237 unsinged char 1 bytes |
238 int or singed int 2 bytes |
239 unsinged int 2 bytes |
240 short int or |
241 unsinged short int 2 bytes |
242 singed short int 2 bytes |
243 lont int or |
244 singed long int 4 bytes |
245 unsinged long int 4 bytes |
246 float 4 bytes |
247 double 8 bytes |
248 long double 10 bytes |
249 -----------------------------------------
250
251 <--- Constants --->
252 ||
253 ||
254 Values that don't change(fixed)
255
256 Types
257 |
258 |----------------|-----------------|
259 | | |
260 Integer Real Constants Character
261 Constants | Constants
262 | 1.0, 2.0, |
263 1, 2, 3, 0, 3.14, -24 'a','b','A','#','&
264 -1, -2
265
266 <--- Keywords --->
267 |
268 |
269 Reserved words that have special meaning to the compiler.
270 |
271 |
272 -------------------32 keywords in c-------|
localhost:4649/?mode=clike 5/25
2/23/25, 1:44 AM NotesOfCDS.c

273 |
274 --> Keywords : |
275 |
276 1. auto |
277 2. break |
278 3. case |
279 4. char |
280 5. const |
281 6. continue |
282 7. default |
283 8. do |
284 9. double |
285 10. else |
286 11. enum |
287 12. extern |
288 13. float |
289 14. for |
290 15. goto |
291 16. if |
292 17. int |
293 18. long |
294 19. register |
295 20. return |
296 21. short |
297 22. signed |
298 23. sizeof |
299 24. static |
300 25. struct |
301 26. switch |
302 27. typedef |
303 28. union |
304 29. unsigned |
305 30. void |
306 31. volatile |
307 32. while |
308 ------------------------------------------|
309
310 Program Structure :-
311 |
312 # include <stdio.h>
313 int main(){
314 printf("Hello World!");
315 return 0;
316 }
317
318 <--- Comments --->
319 ||
320 Lines that are not part of program
321
322 Single line cooment multi line comment
323 | |
324 | |
325 // This is a single line comment /* this is multi line comment
326
327 <--- Output --->
328 |
329 |
330 printf(" Hello World ");
331
332 newl line :-
localhost:4649/?mode=clike 6/25
2/23/25, 1:44 AM NotesOfCDS.c

333 printf(" kuch bhi \n");


334
335 <--- Output --->
336 |
337 |
338 CASES:-
339 1- Integers :-
340 printf(" age is %d ", age);
341 2- Real Numbers :-
342 printf(" value of pi is %f ", pi);
343 3- Characters :-
344 printf(" star looks like this %c ", star);
345
346 <--- input --->
347 |
348 |
349 scanf("%d", &age);
350
351 <--- Compiler --->
352 |
353 |
354 A computer program that translate c code into machine code.
355 or
356 A trnaslater that converts source code into machine code.
357
358 Hello.c ---> C Compiler ---> a.exe
359
360 Extra :-
361
362 <--- Turnary Operator --->
363 |
364 |
365 Turnary Operator works same as if-else statement.
366 condition ? if condition is true : if condition is false
367
368 Rule :- 1 - In the turnary operator there should be equal no of colon's
369 and question marks otherwise it gives error.
370 2 - Every colon should match with the just before question mark.
371 3 - Every question mark followed by colon not immediately but following.
372
373
374 E.g 1 :- age >= 18 ? printf("Adult \n"): printf("Teenager \n");
375 E.g 2 :- a = 3>4 ? 10 : 8>7 ? 20 : 30;
376 -------- -------
377 Rule 2 Rule 2
378 ----- ------ ------- ------- -----
379 true print | | |
380 false evaluate | |
381 true print |
382 false print
383
384
385 ------------------------------------------------------------------------------------
386 ------------------------------------------------------------------------------------
387
388 <--- Chapter 2 --->
389
390
391 <--- Instructions --->
392 ||
localhost:4649/?mode=clike 7/25
2/23/25, 1:44 AM NotesOfCDS.c

393 ||
394 These are statements in a Program
395 |
396 |
397 <--- Types --->
398 |
399 1- Type declaration Instructions
400 2- Arithmatic Instructions
401 3- Control Instructions
402
403
404 <--- Instructions --->
405 ||
406 Type Declaration Instructions Declare var before using it.
407
408 VALID INVALID
409 | |
410 int a = 22; int a = 22;
411 int b = a; int b = a;
412 int c = b + 1; int c = b + 2;
413 int d = 1, e; int d = 2, e;
414
415 int a,b,c; int a,b,c = 1;
416 a = b = c = 1;
417
418 <--- Arithmatic Instructions --->
419 |
420 a + b
421 |
422 ___________|_________
423 | | |
424 operand 1 | operand 2
425 |
426 Operator
427
428 NOTE - single variable on the LHS.
429
430 VALID INVALID
431 | |
432 a = b * c b + c = a
433 a = b * c a = bc
434 a = b / c a = b^c
435
436 NOTE - pow(x,y) for x to the power y
437
438 Modular Operator %
439 Returns remainder for int
440 3 % 2 = 1
441 -3 % 2 = -1
442
443 <--- Type Conversion --->
444 |
445 int op int ----> int
446
447 int op float ----> float
448
449 float op float ----> float
450
451
452 <--- Operator Precedence --->
localhost:4649/?mode=clike 8/25
2/23/25, 1:44 AM NotesOfCDS.c

453 |
454 *, /, % -----> x = 4 + 9 * 10
455 |
456 |
457 +, -
458 |
459 |
460 = -----> x = 4 * 3 / 6 * 2
461
462
463 <--- Associativity (for same precedence) --->
464 |
465 |
466 Left to Right
467 x = 4 * 3 / 6 * 2
468
469
470
471 <--- Instructions --->
472 |
473 Control Instructions
474 |
475 Used to determine flow of program
476
477 a. Sequence Control
478 b. Decision Control
479 d. Case Control
480 c. Loop Control
481
482
483 <--- Operators --->
484 |
485 a. Arithmetic Operators
486 b. Relational Operators
487 d. Bitwise Operators
488 c. Logical Operators
489 e. Assignment Operators
490 f. Ternary Operator
491
492
493 <--- Relational Operator --->
494 |
495 |
496 ==
497 >, >=
498 <, <=
499 !=
500
501 <--- Logical Operator --->
502 |
503 |
504 && AND
505 || OR
506 ! NOT
507
508 <--- Operator Precendence --->
509 | |
510 | |
511 Priority Operator
512 1 !
localhost:4649/?mode=clike 9/25
2/23/25, 1:44 AM NotesOfCDS.c

513 2 *, /, %
514 3 +, -
515 4 <, <=, >, >=
516 5 ==, !=
517 6 &&
518 7 ||
519 8 =
520
521 <--- Operators --->
522 |
523 |
524 <--- Assignment Operators --->
525 ||
526 ||
527 =
528 +=
529 -=
530 *=
531 /=
532 %
533 ------------------------------------------------------------------------------------
534 ------------------------------------------------------------------------------------
535
536 <--- Chapter 3 --->
537
538 <=-- Conditional Statements --=>
539 |
540 |
541 Types
542 |
543 ______|_______
544 | |
545 if-else Switch
546
547
548 if-else --->
549
550 if(Condition) {
551 //do something if TRUE
552 }
553 else {
554 //do something if FALSE
555 }
556
557
558 else if --->
559
560 if(Condition 1) {
561 //do something if TRUE
562 }
563 else if (Condition 2) {
564 //do something if 1st is FALSE & 2nd is TRUE
565 }
566
567
568 Conditional Operators --->
569
570 Ternary :-
571
572 Condition ? doSomething if TRUE : doSomething if FALSE;
localhost:4649/?mode=clike 10/25
2/23/25, 1:44 AM NotesOfCDS.c

573
574 ---> give 1 & 0 cases
575
576
577 Conditional Operators --->
578 |
579 |
580 switch(number) {
581 case C1: //do something
582 break;
583 case C2 : //do something
584 break;
585 default : //do something
586 }
587
588
589 Conditional Operators --->
590 |
591 |
592 switch Properties :-
593 |
594 a. Cases can be in any order
595
596
597 b. Nested switch (switch inside switch) are allowed
598
599 ------------------------------------------------------------------------------------
600 ------------------------------------------------------------------------------------
601
602 <--- Chapter 4 --->
603
604 <--- Loop Control Instructions --->
605 To repeat some parts of the program
606 |
607 |
608 |----------Types----------|
609 | | |
610 for | do-while
611 while
612
613
614 <--- for Loop --->
615 |
616 for(initialisation; condition; updation) {
617 //do something
618 }
619
620 <--- Special Things --->l
621 |
622 --> Increement Operator
623 --> Decrement Operator
624 --> Loop counter can be float
625 or even character
626 --> Infinite Loop
627
628
629 <--- While Loop --->
630 |
631 //initialisation
632 while(condition) {
localhost:4649/?mode=clike 11/25
2/23/25, 1:44 AM NotesOfCDS.c

633 //do something


634 //updation
635 }
636
637
638 <--- Do while Loop --->
639 |
640 //initialisation
641 do{
642 // do somthing
643 //updation
644 }
645 while(condition) ;
646
647
648 <--- Break Statement --->
649 |
650 |
651 Exit the loop
652
653
654 <--- Continue Statement --->
655 |
656 |
657 skip to next iteration .
658
659
660 <--- Nested Loops --->
661 |
662 for(...){
663 for(...){
664 }
665 }
666
667 ------------------------------------------------------------------------------------
668 ------------------------------------------------------------------------------------
669
670 <--- Chapter 5 --->
671
672 <--- Functions --->
673
674 1 - The purpose of the function is code re-utilization.
675 2 - The purpose of the loop is code repetition.
676 3 - The purpose of header file is function reutilization.
677 4 - If we want to reuse the function then we need to create
678 header file of that particular function an include that in the required
program.
679 |
680 Block of codes that perform perticular taks.
681
682 |-------|
683 Take ----> | do | -----> Return
684 Argument | work | result
685 |-------|
686
687 it can be used multiple times.
688 increase code reusability.
689
690 Function Prototype or Declaration :- The purpose of prototype is you are informing
691 the compiler that you are going to use this
localhost:4649/?mode=clike 12/25
2/23/25, 1:44 AM NotesOfCDS.c

692 function in the program


693
694
695 Prototype or Declaration Contains :-
696 |
697 1 - Name of Function
698 2 - Return type of Function
699 3 - Number of Arguments in function
700 4 - Type of Arguments in function
701
702
703 Syntax 1 :--
704
705 Function Prototype or Declaration :-
706 |
707 void printHello();
708
709 ---> tell the computer
710
711 Syntax 2 :--
712
713 Function Definition :-
714 |
715 void printHello(){
716 printf("Hello\n");
717 }
718
719 ---> Do the work
720
721 .
722
723 Syntax 3 :--
724
725 Function call :-
726 |
727 int main(){
728 printHello();
729 return 0;
730 }
731
732 ---> Use the work
733
734 Example :--
735
736 int add(int, int); // function prototype or declaration // function signature
737 // there are two arguments and both are integer type
738
739 void main(){
740 int x = 10, y = 20, z;
741 z = add(x, y); // function calling
742 // here x and y are actual parameters
743 // you are the passing the variables means you are passing the
values
744 // important thing is when you passing the int varibales -
745 // - should have same type of data type(int a, int b)
746
747 printf("Sum is %d\n", z);
748 }
749
750 int add(int a, int b){ // function definition
localhost:4649/?mode=clike 13/25
2/23/25, 1:44 AM NotesOfCDS.c

751 // Reciving the values also called as reciver


752 // and also called as formal parameters
753 // formal parameters and they are called local variables in the
fuction
754
755 int c;
756 c = a + b;
757 return c;
758
759
760 <--- Properties --->
761 |
762 --> Execution always start from main.
763 --> A function get called directly or indirectly from main.
764 --> There can be multiple function in the program.
765
766 function and recursion
767 syntax 1
768
769 function prototype
770 |
771 void printHello();
772
773 syntax 2
774
775 function definition
776 |
777 void printHello(){
778 printf("Hello");
779 }
780
781 function call
782 |
783 int main(){
784 printHello();
785 return 0;
786 }
787
788
789
790
791 function types :-
792
793 1--> Library function - Special Function inbuilt in ds - example -- printf,scanf
794 2--> User Defined Function - declared and defined by the progammer
795
796 Passing Arguments -
797 parameter -- functions can take value
798 return value -- functions can give some value
799
800
801 Arguments V/S Parameters
802 | |
803 --> values that are passed in fucntuion call --> values in function declaration and
definition
804 --> used to send value --> used to recive value
805 --> actual parameter --> Formal parameter
806
807 NOTE :--
808 a. Function can only return one value at a time
localhost:4649/?mode=clike 14/25
2/23/25, 1:44 AM NotesOfCDS.c

809 b. Changes to parameters in function don't change the values in calling function.
810 ---> Because a copy of argument is passed to the function
811
812
813 <=====Recursion======>
814 ||
815 ||
816 when a function calls itself,it's called recusrsion
817
818 recursion in math example --> calculate f(f(f(x))) => f(f(f(4))) => f(f(4(4))) =>
f(4(4(4))) => 4(4(4(4))) => 256
819 this function is called recursive function --)
820
821 Normal Function Call
822 main <--> f(x) <--> y(x)
823
824 Recursion Function Call
825 main <--> f(x) <--> f(x) <--> f(x)
826
827
828 Properties of Recursion----->
829
830 1--> Anything that can be done with Iteration, can be done with recursion and vice-
versa.
831 2--> Recusrsion can something give the most simple solution.
832 3--> Base case is the condition which stops recursion.
833 4--> Iteration has infinte loop ,recursion has stack overflow.
834
835 ------------------------------------------------------------------------------------
836 ------------------------------------------------------------------------------------
837
838 <---- Chapter 6 ---->
839
840 Pointer --> A variable that stores the memory address of another variable.
841
842 syntax of Pointer ==>
843
844 Normal Variable --> int age = 22;
845 Pointer Variable --> int *ptr = &age;
846 --> int_age = *ptr;
847
848 where * (astrix) --> value at address operator
849 & (ampersand) --> address of operator
850
851
852 Declaring Pointers --> int * ptr --> int age = 22;
853 --> char * ptr --> char star = '*';
854 --> float * ptr --> float price = 100.00;
855
856 Format Specifier --> printf("%p", &age);
857 --> printf("%p", ptr);
858 --> printf("%p", &ptr);
859
860 where %p is use for pointer not use %d here or use %u --> usinged int
861
862
863 Pointer to Pointer --> A variable that stores a memory address of another pointer.
864
865 example --> int x = 22;
866
localhost:4649/?mode=clike 15/25
2/23/25, 1:44 AM NotesOfCDS.c

867 Pointer Varibale --> *ptr = &x;


868 Pointer to Pointer --> **pptr = &ptr
869
870 Syntax of pointer --> int **pptr;
871 --> char **pptr;
872 --> float **pptr;
873
874 Pointer'a in function call -->
875 1 --> Call by Value --> we pass a value of variable as argument.
876 2 --> Call by reference --> we pass address of vraible as argument.
877
878 ------------------------------------------------------------------------------------
879 ------------------------------------------------------------------------------------
880 <---- Chapter 7 ---->
881
882 <------Array------>
883 ||
884 --> Collection of simillar data types stored at contiguous (continues-->store after
one...and goes on)
885 memory locations.
886
887 Syntax of Array --> int marks[3];
888 --> char name[10];
889 --> float price[2];
890
891 <---Inititailization of Array--->
892 ||
893 int marks [] = {88,77,66};
894 int marks [3] = {98,88,90};
895
896 int rollno [30]
897 |
898 4 bytes 4 * 30 = 120 bytes
899
900 char name [10]
901 |
902 1 bytes 1 * 10 = 10 bytes
903
904 <----Pointer Arithmatic---->
905 ||
906 --> Pointer can be incremented or decremented
907
908 CASE -> 1
909
910 int age = 22;
911 int *ptr = &age;
912 printf("ptr is : %u\n", ptr);
913 ptr++; ---> means ptr address can be incremented with 4 bytes ahead.
914
915 ptr--; ---> means ptr address can be decremented with 4 bytes.
916
917 --> we can also subtract one pointer from another.
918
919 ---> Example --: int ptr1 = &age1;
920 --: int ptr2 = &age2;
921 ptr2 - ptr1; // is valid statement
922
923 --> we can also compare 2 pointers.
924
925 Array is a pointer --> int *ptr = &arr[0];
localhost:4649/?mode=clike 16/25
2/23/25, 1:44 AM NotesOfCDS.c

926 or
927 int *ptr = arr; --> arr indicate 0th location of array
928 ....so we can say arr name is a pointer actually
929
930 --> we can also increment or decrement array name.
931
932 ---> Traverse an Array
933 |
934 --> int aadhar[10];
935 --> int *ptr = &aadhar[0];
936
937 <---- Araays as function argument ---->
938 ||
939 <-- Function Declaration -->
940 ||
941 void printNumbers(int arr[], int n); --> as square method
942 or
943 void printNumbers(int *arr, int n); --> as pointer method
944
945 function call -->
946 printNumbers(arr, n);
947
948 <----- Multidimensional Array ----->
949 ||
950 <--- 2d,3d,4d & 5d....nd Arrays --->
951 ---> Declaration
952 ||
953 Syntax --> int arr[][] = {{1,2},{3,4}};
954
955 ---> Access
956 ||
957 arr[0][0] --> 1
958 arr[0][1] --> 2
959 arr[1][0] --> 3
960 arr[1][1] --> 4
961 ==> each array has same size.
962
963
964 ------------------------------------------------------------------------------------
965 ------------------------------------------------------------------------------------
966
967 <---- Chapter 8 ---->
968
969 <---- String ---->
970 ||
971 ----> A character array terminated by a '\0' (null character).
972
973 ----> null character denotes a string termination.
974
975 char name [] = {'A','P','U','R','V',\0};
976 char class [] = {'A','P','U','R','V','','S','I','N','G','H',\0};
977
978 Note --> Strings associated with some special properties,so we can access some
special function.
979 --> But if you access those properties so need put '\0' null character on strings
array.
980
981 <----- String Initialisation ----->
982 ||
983 char name [] = {'A','P','U','R','V','\0'};
localhost:4649/?mode=clike 17/25
2/23/25, 1:44 AM NotesOfCDS.c

984 char name [] = "APURV";


985
986 char class [] = {'A','P','U','R','V','S','I','N','G','H','\0'};
987 char class [] = "APURV SINGH";
988
989 Note ---> '\0' Null character also stored in array in last location.
990 ---> example - if word is APURV so here last character is V stored in array at 2006
of address so null
991 character is store at 2007 address of array.
992 ---> If we write a single character in array we use single quotes(singel inverted
'' comma's) and
993 ---> if we write a string in array we used dual quotes (dual inverted "" comma's).
994
995
996 <----- String Format Specifier ----->
997 ||
998 %s
999 char name[] ="Format";
1000 printf("%s", name); --> here we not used '&' symbal with name beacuse string also a
pointer.
1001
1002 Format Specifier ---> The specify the format of data types.
1003
1004 ex --> for int --> %d --> doble data type
1005 for float --> %f --> float data type
1006 for char --> %c --> character data type
1007 for string --> %s --> string data type
1008 Note :- %s is automaticaly called \0 null character.
1009
1010 Important := scanf() can not take input multi-word string with spaces.
1011 --> here, gets() & puts() come into a picture.
1012 |
1013 fgets
1014
1015 <------- String Functions ------->
1016 ||
1017 gets(str) --> dangours and outdated (Not secure) (it's a hackable) -- so now we can
use -->
1018 | fgets(str,n,file)
1019 | |
1020 input a string stop when n-1 char input
1021 (even multi word) or new line is enterd
1022 |
1023 (taking a input like
scanf)
1024 puts(str)

1025 | where str --> srting


1026 output a string(use for output like printf function) n --> size of an string
1027 file --> sdtin
1028 syntax :- fgets(str); |
1029 puts(str); standard input
1030
1031 (str) --> max size of string
1032
1033 <----- String using Pointers ----->
1034 ||
1035 char *str = "Hello World"; --> where pointer string not uses square bracket
1036 char str[50] = "Hello World"; --> Where as a strig using square bracket
1037
localhost:4649/?mode=clike 18/25
2/23/25, 1:44 AM NotesOfCDS.c

1038 ----> Store string in memory & the assigned


1039 ----> address is stored in the char pointer 'str'
1040
1041 char *str = "Hello World"; --> can be reinitialized
1042 char str[ ] = "Hello World"; --> cannot be reinitialized
1043
1044 <----- Standard Library Functions 1 ----->
1045 ||
1046 1 strlen(str)
1047 ----> count number of characters excluding '\0'
1048
1049
1050 <----- Standard Library Functions 2 ----->
1051 |
1052 2 strcpy(newStr, oldStr)
1053 ----> copies value of old string to new string
1054
1055 <----- Standard Library Functions 3 ----->
1056 |
1057 strcat(firstStr, secstr)
1058 ----> concatenates first string with second string
1059
1060 <----- Standard Library Functions 4 ----->
1061 |
1062 strcmp(firstStr, secStr)
1063 ----> Compare two strings and return a value
1064 0 -> string equal
1065 positive -> first > second (ASCII)
1066 negative -> first < second (ASCII)
1067
1068 ---> Salting --> A technique to secure a password to pretend from hacker.

1069
1070 ------------------------------------------------------------------------------------
1071 ------------------------------------------------------------------------------------
1072
1073 <---- Chapter 9 ---->
1074
1075 <--- Structure --->
1076 |
1077 --> A collection of values of diffrent data types.
1078 example :- for a student store the following:
1079 name(string)
1080 roll no(integer)
1081 CGPA(float)
1082
1083 ---> Syntax:-
1084 struct student {
1085 char name[50];
1086 int roll no;
1087 float cgpa;
1088 }; --> statement terminator....it is mandatory
1089 like int n = 5;
1090 | |
1091 struct student s1; Where -: int = struct Student --> data type
1092 s1.name;
1093 s1.roll no;
1094 s1.cgpa;
1095
1096 where :- n = s1(it's a variable)
localhost:4649/?mode=clike 19/25
2/23/25, 1:44 AM NotesOfCDS.c

1097
1098
1099 <--- Structures in Memory --->
1100
1101 struct student{
1102 char name[100];
1103 int rollno;
1104 float cgpa;
1105 }
1106 *structures are stored in contigues memory locations.
1107
1108 ---> Benefits of Structures :-
1109
1110 1-> Save as from creating to many variables.
1111 2-> Save as from creating to many functions.
1112 3-> Save as from creating to many arrays.
1113 4-> Save as from creating to many pointers.
1114 5-> Save as from creating to many typedefs.
1115 6-> Save as from creating to many unions.
1116 7-> Save as from creating to many enums.
1117 8-> Good data management and organization.
1118 9-> Good data abstraction.
1119 10-> Good data encapsulation.
1120 11-> Good data hiding.
1121 12-> Good data reusability.
1122 13-> Good data portability.
1123 14-> Good data scalability
1124 15-> Good data maintainability.
1125 16-> Good data readability.
1126 17-> Good data writability.
1127 18-> Save memory space.
1128 19-> Easy to access and modify the data.
1129 20-> Easy to pass as arguments to functions.
1130 21-> Easy to return from functions.
1131 22-> Easy to store in arrays.
1132 23-> Easy to store in linked lists.
1133 24-> Easy to store in trees.
1134 25-> Easy to store in graphs.
1135 26-> Easy to store in stacks.
1136 27-> Easy to store in queues.
1137 28-> Easy to store in heaps.
1138 29-> Easy to store in hash tables.
1139 30-> Easy to store in sets.
1140 31-> Easy to store in maps.
1141 32-> Easy to store in vectors.
1142 33-> Easy to store in lists.
1143 34-> Easy to store in dictionaries.
1144 35-> Easy to store in tuples.
1145 36-> It can store multiple values of different data types in a single variable.
1146
1147
1148
1149 <--- Array of Structure --->
1150 |
1151 struct student ECE[100];
1152 struct student CSE[100];
1153 struct student IT[100];
1154
1155 How to Access data from array of structure:-
1156 |
localhost:4649/?mode=clike 20/25
2/23/25, 1:44 AM NotesOfCDS.c

1157 ---> ECE[0].rollno;


1158 ---> CSE[0].name;
1159 ---> IT[0].cgpa;
1160
1161 where [0] means no of student ---> access the data of 0th student out of 100 from an
array.
1162
1163 <--- Initializing Structures --->
1164 |
1165 struct student s1 = {"Apurv", 910, 9.5};
1166 struct student s2 = {"Aarav", 911, 9.6};
1167 struct student s3 = {0};
1168
1169 <--- Pointers to Structure --->
1170 |
1171 struct student s1;
1172 struct student *ptr;
1173 ptr = &s1;
1174
1175 <--- Arrow Operator --->
1176 |
1177 *ptr.code <----> ptr->code (Hyphon with greater than symbol)
1178
1179 <--- Passing Structure to Function --->
1180 |
1181 ---> Function Prototype:-
1182 |
1183 void printInfo(struct student s1); where :- void --> return type --> void is return
nothing
1184 | --> where as int return 0 or
something.
1185 ex:- void display(int a, char arr[]); --> where struct student = Data
type
1186 note :- Declare first before using it. --> s1 = varibale
1187
1188
1189 <--- Typedef Keyword --->
1190 |
1191 ---> use to create alias for data type
1192
1193 --> where alias means nick name
1194 or
1195 short name
1196
1197 Syntax:-
1198
1199 typedef data-type variable-name{};
1200 |
1201 typedef struct ComputerEngineeringStudent{
1202 --> Where typedef = keywrod
1203 char name[100]; --> struct = structure
1204 int roll; --> ComputerEngineeringStudent = variable
1205 float cgpa;
1206 } coe; ---------------------------------> alias for ComputerEngineeringStudent
1207 coe student1;
1208
1209 struct student s1 = stu
1210
1211 ------------------------------------------------------------------------------------
1212 ------------------------------------------------------------------------------------
localhost:4649/?mode=clike 21/25
2/23/25, 1:44 AM NotesOfCDS.c

1213
1214 <---- Chapter 10 ---->
1215
1216 <--- File IO --->
1217 ||
1218 ||
1219 --> where file is a collection of data stored in a computer
1220
1221 RAM HDD
1222 --------- ---------
1223 | | -----> | |
1224 | | <----- | |
1225 --------- ---------
1226 Volatile Non-Volatile
1227
1228
1229 <--- File IO --->
1230 |
1231 |
1232 File --> Container in a storage device to store data.
1233
1234
1235 --> RAM is volatie memory.
1236 --> Contents are lost when program terminates.
1237 --> File are used tp persist the data.
1238
1239 ** Volatile and non-volatile memory to tansfer a file between each other.
1240 ** Computer program always stored on a RAM(Random Access Memory or Volatile memory
or storage).
1241 ** other and unusual files are stored on non-volatile memory.
1242 ** Computer program can read and write operation perform a file of a non-volatile
memory.
1243 ** in a hardisk have diffrent containers is to be called chunk of memories.
1244 ** Every chunk of memories has a large amount of data and that called file also.
1245 or
1246 ----> File is a collection of data stored in a computer.
1247
1248
1249 --> HDD is non-volatile memory.
1250
1251 <--- Operation on File --->
1252 ||
1253 --> Create a file.
1254 --> Open a file.
1255 --> Read from a file.
1256 --> Write in a file.
1257 --> Close a file.
1258 --> Delete a file.
1259 --> Truncate a file.
1260 --> Seek a file.
1261 --> File IO operations are performed using file pointers.
1262 --> File pointers are used to read and write data from a file.
1263
1264 Type of Files :--
1265
1266 Text Files <--------> Binary Files
1267 | |
1268 Textual Data Binary Data
1269 ex. .text, .c, .java etc ex. .exe, .mp3, .mp4, .jpg etc
1270
localhost:4649/?mode=clike 22/25
2/23/25, 1:44 AM NotesOfCDS.c

1271 <---- File Pointer ---->


1272 |
1273 --> FILE is a (hidden) structure that needs to be created for opening a file.
1274 A FILE ptr that points to this structure & is issued to access the file.
1275
1276
1277 Syntax :-
1278 ||
1279 FILE *fptr; where :- FILE - is a type because file is a structure.
1280 | fptr - is a pointer to a file.
1281 we can also write like this
1282 ptr or fp or p or fptr
1283
1284 Opening a file :-
1285
1286 FILE *fptr;
1287 |
1288 fptr = fopen("filename", mode); where mode = 1- read
1289 2- write
1290 Closing a file :- filename = test.txt
1291 |
1292 fptr(fclose);
1293
1294 <--- File opening mode --->
1295 |
1296 --> "r" = open to read
1297 --> "rb" open to read in binary
1298 --> "w" = open to write
1299 --> "wb" open to write in binary
1300 --> "a" = open to append
1301 --> "ab" open to append in binary
1302 --> "r+" = open to read and write
1303 --> "wb+" open to read and write in binary
1304 --> "a+" = open to read and write in append mode
1305
1306
1307 <--- BEST Practice --->
1308 |
1309 check if a file exist before reading from it.
1310
1311 <--- Reading from a file --->
1312 ||
1313 Syntax:-
1314 char ch;
1315 fscanf("fptr, "%c", &ch");
1316
1317 where :- fptr --> file Pointer
1318 "%c" --> Format Specifier
1319 &ch --> addrsss of variale
1320 fscanf --> store input or for reading --> which is exculusive use for File.
1321
1322 <--- Writing to a File --->
1323 ||
1324 char ch = "S";
1325 fprintf(fptr, "%c", ch);
1326
1327 fptr --> file Pointer
1328 "%c" --> Format Specifier
1329 &ch --> addrsss of variale
1330 fprintf --> for output or for reading --> which is exclusive use for File.
localhost:4649/?mode=clike 23/25
2/23/25, 1:44 AM NotesOfCDS.c

1331
1332
1333 <--- Read & Write a char --->
1334 ||
1335 fgetc(fptr); ---> use for output like printf or fprintf.--> for reading
1336 |
1337 but it use for only character
1338
1339 fputc('A', fptr); ---> use for input like or fscanf.--> for writing
1340 |
1341 but it use for only character
1342
1343 where as if other data types like string,int,float etc is only uses fscanf or
fprintf.
1344
1345
1346 <--- End of File (EOF) --->
1347 ||
1348 fgetc returns EOF to show that the files has ended.
1349
1350 syntax :-- ch = fgetc(fptr);
1351 while( ch != EOF){
1352 printf("%c", ch);
1353 ch = fgetc(fptr);
1354 }
1355
1356 ------------------------------------------------------------------------------------
1357 ------------------------------------------------------------------------------------
1358
1359 <---- Chapter 11 ---->
1360
1361 <--- Dynamic Memory Allocation --->
1362 ||
1363 ||
1364 --> It is a way to allocate memory to a data structure during the runtime.
1365
1366 --> We need some functions to allocate & free memory dynamically.
1367
1368 <--- Functions for DMA --->
1369 ||
1370 ||
1371 a. malloc( ) -- Memory Allocation
1372 b. calloc( ) -- Continuous Allocation
1373 c. free( )
1374 d. realloc( ) -- Re-allocation
1375
1376 <---- malloc( ) ---->
1377 (memory allocation)
1378 ||
1379 ||
1380 --> takes number of bytes to be allocated & returns a pointer of type void.
1381
1382 --> ptr = (int*) malloc(5 * sizeof(int));
1383 ||
1384 --> this memory is allocated at runtime.
1385
1386
1387
1388 where --> ptr = pointer variable -- for access a memory
1389 (int*) = for typecast
localhost:4649/?mode=clike 24/25
2/23/25, 1:44 AM NotesOfCDS.c

1390 because malloc always return void pointer type so we can use typecast.
1391 5 * sizeof(int) = number of bytes to be allocated.
1392
1393 --> It is used to allocate memory for a single variable.
1394 --> It is used to allocate memory for an array of variables.
1395 --> It is used to allocate memory for a structure of varibales.
1396 --> It is used to allocate memory for a pointer of variables.
1397 --> It is used to allocate memory for a pointer of pointers of varibales.
1398
1399
1400 <---- calloc( ) ---->
1401 (continuous allocation)
1402 ||
1403 ||
1404 initializes with 0
1405 ptr = (*int) calloc(5, sizeof(int));
1406
1407 where --> ptr = pointer variable -- for access a memory
1408 (int*) = for typecast
1409 because calloc always return void pointer type so we can use typecast.
1410 5 * sizeof(int) = number of bytes to be allocated
1411 5 * sizeof(int) = 5*2 --> 20 size memory allocated
1412
1413 ** when you take null value so we can prefer calloc function.
1414
1415
1416 <---- free( ) ---->
1417 ||
1418 ||
1419 --> We use it to free memory that is allocated using malloc & calloc
1420
1421 Syntax :- free(ptr);
1422
1423 <---- realloc( ) ---->
1424 ||
1425 ||
1426 --> reallocate (increase or decrease) memory using the same pointer & size.
1427
1428 Syntax :- ptr = realloc(ptr, newSize);
1429
1430
1431 ---> Compiler always allocate memory when see which data type given by the users.
1432
1433 ------------------------------------------------------------------------------------
1434 ------------------------------------------------------------------------------------
1435
1436 */

localhost:4649/?mode=clike 25/25

You might also like