NotesOfCDS C
NotesOfCDS C
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
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
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
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
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
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
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
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