├── .gitignore ├── Chapter 0 └── 01_first.c ├── Chapter 1 - Practice Set ├── 01_problem1.c ├── 02_problem2.c ├── 03_problem3.c ├── 04_problem4.c └── c.json ├── Chapter 1 ├── 01_variables.c ├── 02_variable_rules.c ├── 03_data_types.c ├── 04_sizeof.c ├── 05_comments.c ├── 06_input.c └── 07_escape_sequence.c ├── Chapter 10 - Practice Set ├── 01_problem1.c ├── 02_problem2.c ├── 03_problem3.c ├── 04_problem4.c ├── 05_problem5.c ├── file.txt ├── harry.txt ├── harry3.txt ├── int.txt ├── salary.txt └── table.txt ├── Chapter 10 ├── 01_file.c ├── 02_quick_quiz.c ├── 03_file_write.c ├── 04_fgetc_fputc.c ├── 05_reading_cbyc.c └── harry.txt ├── Chapter 11 - Practice Set ├── 01_problem1.c ├── 02_problem2.c ├── 03_problem3.c ├── 04_problem4.c ├── 05_problem5.c └── 06_problem6.c ├── Chapter 11 ├── 01_dma.c ├── 02_quick_quiz.c ├── 03_quick_quiz.c ├── 04_free.c └── 05_realloc.c ├── Chapter 2 - Practice Set ├── 01_problem1.c ├── 02_problem2.c ├── 03_problem3.c ├── 04_problem4.c └── 05_problem5.c ├── Chapter 2 ├── 01_variables.c ├── 02_arithmetic_operators.c ├── 03_type_conversion.c ├── 04_associativity.c └── 05_typecasting.c ├── Chapter 3 - Practice Set ├── 01_problem1.c ├── 02_problem2.c ├── 03_problem3.c ├── 04_problem4.c ├── 05_problem5.c └── 06_problem.c ├── Chapter 3 ├── 01_conditionals.c ├── 02_if_else.c ├── 03_non_zero.c ├── 04_logical.c ├── 05_else_if.c ├── 06_ternary.c ├── 07_switch.c └── 08_quiz.c ├── Chapter 4 - Practice set ├── 01_problem1.c ├── 02_problem2.c ├── 05_problem5.c ├── 06_problem6.c ├── 07_problem7.c ├── 08_problem8.c ├── 09_problem9.c ├── 10_problem10.c └── 11_problem11.c ├── Chapter 4 ├── 01_loop.c ├── 02_while.c ├── 03_while_value.c ├── 04_infinite_loop.c ├── 05_quick_quiz.c ├── 06_increment_decrement.c ├── 07_do_while.c ├── 08_quick_quiz.c ├── 09_for.c ├── 10_quick_quiz.c ├── 11_decrementing_for_loop.c └── 12_break_continue.c ├── Chapter 5 - Practice Set ├── 01_problem1.c ├── 02_problem2.c ├── 03_problem3.c ├── 04_problem4.c ├── 05_problem5.c ├── 06_problem6.c └── 07_problem7.c ├── Chapter 5 ├── 01_function.c ├── 02_quick_quiz.c ├── 03_change.c ├── 04_quick_quiz.c └── 05_recursion.c ├── Chapter 6 - Practice Set ├── 01_problem1.c ├── 02_problem2.c ├── 03_problem3.c ├── 04_problem4.c ├── 05_problem5.c ├── 06_problem6.c └── 07_problem7.c ├── Chapter 6 ├── 01_pointer.c ├── 02_other_types.c ├── 03_pointer2pointer.c ├── 04_call_by_value.c ├── 05_call_by_reference.c └── 06_swap.c ├── Chapter 7 - Practice Set ├── 01_problem1.c ├── 03_problem3.c ├── 04_problem4.c ├── 05_problem5.c ├── 06_problem6.c ├── 07_problem7.c ├── 08_problem8.c ├── 09_problem9.c └── c.json ├── Chapter 7 ├── 01_arrays.c ├── 02_array_input.c ├── 03_other_array_init_ways.c ├── 04_pointer_arithmetic.c ├── 05_array_in_memory.c ├── 06_array_using_pointers.c └── 07_array_2d.c ├── Chapter 8 - Practice Set ├── 02_problem2.c ├── 03_problem3.c ├── 04_problem4.c ├── 05_problem5.c ├── 06_problem6.c ├── 07_problem7.c ├── 08_problem8.c └── 09_problem9.c ├── Chapter 8 ├── 01_strings.c ├── 02_printing_strings.c ├── 03_input.c ├── 04_gets.c └── 05_string_functions.c ├── Chapter 9 - Practice Set ├── 01_problem1.c ├── 02_problem2.c ├── 03_problem3.c ├── 04_problem4.c ├── 05_problem5.c ├── 08_problem8.c └── 09_problem9.c ├── Chapter 9 ├── 01_structure.c ├── 02_quick_quiz.c ├── 03_struct_arrays.c ├── 04_pointer_to_struct.c ├── 05_quick_quiz.c └── 06_typedef.c ├── Project 1 └── main.c ├── Project 2 └── main.c └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.exe 2 | .vscode -------------------------------------------------------------------------------- /Chapter 0/01_first.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | printf("Hello World"); 5 | return 0; 6 | } -------------------------------------------------------------------------------- /Chapter 1 - Practice Set/01_problem1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | // int length = 3; 5 | // int breadth = 6; 6 | int length, breadth; 7 | printf("Enter length\n"); 8 | scanf("%d", &length); 9 | 10 | printf("Enter breadth\n"); 11 | scanf("%d", &breadth); 12 | 13 | printf("The area of this rectangle is %d", length*breadth); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Chapter 1 - Practice Set/02_problem2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int r = 6; 5 | int height = 10; 6 | printf("The area of circle with radius %d is %f\n", r, 3.14*r*r); 7 | printf("The volume of cylinder with radius %d and height %d is %f", r, height, 3.14*r*r*height); 8 | return 0; 9 | } -------------------------------------------------------------------------------- /Chapter 1 - Practice Set/03_problem3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | float c = 37.0, f; 5 | 6 | f = ((9.0/5.0)*c) + 32; 7 | printf("The value in Fahrenheit is %f", f); 8 | 9 | return 0; 10 | } -------------------------------------------------------------------------------- /Chapter 1 - Practice Set/04_problem4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | float p = 34.1; 5 | int r = 8; 6 | int t = 5; 7 | printf("The value of simple interest is %f ", (p*r*t)/100); 8 | return 0; 9 | } -------------------------------------------------------------------------------- /Chapter 1 - Practice Set/c.json: -------------------------------------------------------------------------------- 1 | { 2 | // Place your snippets for c here. Each snippet is defined under a snippet name and has a prefix, body and 3 | // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are: 4 | // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 5 | // same ids are connected. 6 | // Example: 7 | "C boilerplate": { 8 | "prefix": "c boiler", 9 | "body": [ 10 | "#include \n", 11 | "int main(){", 12 | "\t$1", 13 | "\treturn 0;", 14 | "}", 15 | 16 | ], 17 | "description": "The C language boilerplate" 18 | } 19 | } -------------------------------------------------------------------------------- /Chapter 1/01_variables.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a; // Variable declaration 5 | a = 6; // Variable initialization 6 | printf("The output of this program is %d", a); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /Chapter 1/02_variable_rules.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int harry; 5 | int Harry; 6 | int harry_good; 7 | return 0; 8 | } -------------------------------------------------------------------------------- /Chapter 1/03_data_types.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | // int a; 6 | // a = 1; 7 | int a = 1; 8 | 9 | // float b = 1.4; 10 | float b; 11 | b = 1.4; 12 | 13 | // char c = 'a'; 14 | char c; 15 | c = 'a'; 16 | 17 | printf("The value of a is %d\n", a); 18 | printf("The value of b is %f\n", b); 19 | printf("The value of c is %c\n", c); 20 | } -------------------------------------------------------------------------------- /Chapter 1/04_sizeof.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | printf("Size of int: %zu bytes\n", sizeof(int)); 5 | printf("Size of char: %zu byte\n", sizeof(char)); 6 | printf("Size of float: %zu bytes\n", sizeof(float)); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /Chapter 1/05_comments.c: -------------------------------------------------------------------------------- 1 | /* 2 | Program: 05_comments.c 3 | Author: Harry 4 | Date: 3 October 2030 5 | */ 6 | #include 7 | 8 | int main(){ 9 | 10 | // I am writing this just for fun. This line doesnt do anything 11 | printf("Hello World"); 12 | // Return 0 is returning 0 to the OS 13 | return 0; // This is return 14 | } -------------------------------------------------------------------------------- /Chapter 1/06_input.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a; 5 | scanf("%d", &a); 6 | printf("The value of a is %d", a); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /Chapter 1/07_escape_sequence.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a; 5 | char ch = 'a'; 6 | ch = 'c'; 7 | ch = '\n'; // represents a new line 8 | printf("Hey I am good \\n nice"); 9 | return 0; 10 | } -------------------------------------------------------------------------------- /Chapter 10 - Practice Set/01_problem1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | FILE *fptr; 5 | int num1, num2, num3; 6 | fptr = fopen("file.txt", "r"); 7 | fscanf(fptr, "%d %d %d", &num1, &num2, &num3); 8 | printf("The values are %d %d %d \n", num1, num2, num3); 9 | 10 | fclose(fptr); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Chapter 10 - Practice Set/02_problem2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | FILE *fptr; 5 | int num=4; 6 | fptr = fopen("table.txt", "w"); 7 | 8 | for (int i = 0; i < 10; i++) 9 | { 10 | fprintf(fptr, "%d", num*(i+1)); 11 | fprintf(fptr, "%c", '\n'); 12 | } 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Chapter 10 - Practice Set/03_problem3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char ch; 6 | FILE *ptr; 7 | FILE *ptr2; 8 | 9 | ptr = fopen("harry.txt", "r"); 10 | ptr2 = fopen("harry3.txt", "a"); 11 | while (1) 12 | { 13 | 14 | ch = fgetc(ptr); 15 | // when all the content of a file has been read break 16 | if (ch == EOF) 17 | { 18 | break; 19 | } 20 | else{ 21 | fprintf(ptr2, "%c", ch); 22 | fprintf(ptr2, "%c", ch); 23 | printf("%c", ch); 24 | } 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Chapter 10 - Practice Set/04_problem4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | FILE *ptr; 6 | char name1[34], name2[34]; 7 | int salary1, salary2; 8 | ptr = fopen("salary.txt", "w"); 9 | 10 | printf("Enter the name of Employee \n"); 11 | scanf("%s", name1); 12 | 13 | printf("Enter the salary of Employee \n"); 14 | scanf("%d", &salary1); 15 | 16 | printf("Enter the name of Employee 2\n"); 17 | scanf("%s", name2); 18 | 19 | printf("Enter the salary of Employee 2\n"); 20 | scanf("%d", &salary2); 21 | 22 | fprintf(ptr, "%s", name1); 23 | fprintf(ptr, "%s", ", "); 24 | fprintf(ptr, "%d", salary1); 25 | fprintf(ptr, "%c", '\n'); 26 | fprintf(ptr, "%s", name2); 27 | fprintf(ptr, "%s", ", "); 28 | fprintf(ptr, "%d", salary2); 29 | fprintf(ptr, "%c", '\n'); 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Chapter 10 - Practice Set/05_problem5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | FILE *ptr; 6 | int num; 7 | ptr = fopen("int.txt", "r"); 8 | fscanf(ptr, "%d", &num); 9 | 10 | fclose(ptr); 11 | 12 | ptr = fopen("int.txt", "w"); 13 | fprintf(ptr, "%d", 2*num); 14 | 15 | fclose(ptr); 16 | 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Chapter 10 - Practice Set/file.txt: -------------------------------------------------------------------------------- 1 | 11 678 3 -------------------------------------------------------------------------------- /Chapter 10 - Practice Set/harry.txt: -------------------------------------------------------------------------------- 1 | Hey I am good -------------------------------------------------------------------------------- /Chapter 10 - Practice Set/harry3.txt: -------------------------------------------------------------------------------- 1 | HHeeyy II aamm ggoooodd -------------------------------------------------------------------------------- /Chapter 10 - Practice Set/int.txt: -------------------------------------------------------------------------------- 1 | 90 -------------------------------------------------------------------------------- /Chapter 10 - Practice Set/salary.txt: -------------------------------------------------------------------------------- 1 | Harry, 234 2 | Rohan, 3244 3 | -------------------------------------------------------------------------------- /Chapter 10 - Practice Set/table.txt: -------------------------------------------------------------------------------- 1 | 4 2 | 8 3 | 12 4 | 16 5 | 20 6 | 24 7 | 28 8 | 32 9 | 36 10 | 40 11 | -------------------------------------------------------------------------------- /Chapter 10/01_file.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | FILE *ptr; 6 | ptr = fopen("harry.txt", "r"); 7 | int num; 8 | fscanf(ptr, "%d", &num); 9 | printf("The value of num is %d \n", num); 10 | 11 | fscanf(ptr, "%d", &num); 12 | printf("The value of num is %d \n", num); 13 | 14 | fclose(ptr); 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Chapter 10/02_quick_quiz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | FILE *ptr; 6 | ptr = fopen("harry2.txt", "r"); 7 | 8 | if(ptr == NULL){ 9 | printf("The file does not exist sorry! \n"); 10 | 11 | } 12 | else{ 13 | 14 | int num; 15 | fscanf(ptr, "%d", &num); 16 | printf("The value of num is %d \n", num); 17 | 18 | fscanf(ptr, "%d", &num); 19 | printf("The value of num is %d \n", num); 20 | 21 | fclose(ptr); 22 | 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Chapter 10/03_file_write.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | FILE *fptr; 6 | fptr = fopen("harry.txt", "a"); 7 | int num = 432; 8 | fprintf(fptr, "%d", num); 9 | fclose(fptr); 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Chapter 10/04_fgetc_fputc.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | FILE *ptr; 6 | ptr = fopen("harry.txt", "a"); 7 | // char c = fgetc(ptr); // used to read a character from file 8 | // printf("%c", c); 9 | fputc('c', ptr); 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Chapter 10/05_reading_cbyc.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char ch; 6 | FILE *ptr; 7 | ptr = fopen("harry.txt", "r"); 8 | while (1) 9 | { 10 | ch = fgetc(ptr); 11 | printf("%c", ch); 12 | // when all the content of a file has been read break 13 | if (ch == EOF) 14 | { 15 | break; 16 | } 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Chapter 10/harry.txt: -------------------------------------------------------------------------------- 1 | The number is: 432432 2 | 3 | 432c -------------------------------------------------------------------------------- /Chapter 11 - Practice Set/01_problem1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | int n = 6; 6 | int* ptr; 7 | ptr = (int*) malloc(n*sizeof(int)); 8 | ptr[0] = 45; 9 | printf("%d \n", ptr[0]); 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Chapter 11 - Practice Set/02_problem2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n = 6; 7 | int *ptr; 8 | ptr = (int *)malloc(n * sizeof(int)); 9 | 10 | for (int i = 0; i < n; i++) 11 | { 12 | scanf("%d", &ptr[i]); 13 | } 14 | 15 | for (int i = 0; i < n; i++) 16 | { 17 | printf("%d \n", ptr[i]); 18 | } 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Chapter 11 - Practice Set/03_problem3.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n = 6; 7 | int *ptr; 8 | ptr = (int *)calloc(n, sizeof(int)); 9 | 10 | for (int i = 0; i < n; i++) 11 | { 12 | scanf("%d", &ptr[i]); 13 | } 14 | 15 | for (int i = 0; i < n; i++) 16 | { 17 | printf("%d \n", ptr[i]); 18 | } 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Chapter 11 - Practice Set/04_problem4.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n = 5; 7 | int *ptr; 8 | ptr = (int *)calloc(n, sizeof(int)); 9 | 10 | for (int i = 0; i < n; i++) 11 | { 12 | scanf("%d", &ptr[i]); 13 | } 14 | 15 | printf("The Array is \n"); 16 | 17 | for (int i = 0; i < n; i++) 18 | { 19 | printf("%d \n", ptr[i]); 20 | } 21 | 22 | n = 10; 23 | ptr = (int *)realloc(ptr, 10 * sizeof(int)); 24 | 25 | for (int i = 0; i < n; i++) 26 | { 27 | scanf("%d", &ptr[i]); 28 | } 29 | 30 | printf("The Array is \n"); 31 | 32 | for (int i = 0; i < n; i++) 33 | { 34 | printf("%d \n", ptr[i]); 35 | } 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Chapter 11 - Practice Set/05_problem5.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n = 10; 7 | int *ptr; 8 | ptr = (int *)malloc(n *sizeof(int)); 9 | 10 | for (int i = 0; i < n; i++) 11 | { 12 | ptr[i] = 7 * (i+1); 13 | } 14 | 15 | printf("The Array is \n"); 16 | 17 | for (int i = 0; i < n; i++) 18 | { 19 | printf("%d \n", ptr[i]); 20 | } 21 | 22 | n = 15; 23 | ptr = (int *)realloc(ptr, 10 * sizeof(int)); 24 | 25 | for (int i = 0; i < n; i++) 26 | { 27 | ptr[i] = 7 * (i+1); 28 | } 29 | 30 | printf("The Array is \n"); 31 | 32 | for (int i = 0; i < n; i++) 33 | { 34 | printf("%d \n", ptr[i]); 35 | } 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Chapter 11 - Practice Set/06_problem6.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n = 10; 7 | int *ptr; 8 | ptr = (int *)calloc(n, sizeof(int)); 9 | 10 | for (int i = 0; i < n; i++) 11 | { 12 | ptr[i] = 7 * (i+1); 13 | } 14 | 15 | printf("The Array is \n"); 16 | 17 | for (int i = 0; i < n; i++) 18 | { 19 | printf("%d \n", ptr[i]); 20 | } 21 | 22 | n = 15; 23 | ptr = (int *)realloc(ptr, 10 * sizeof(int)); 24 | 25 | for (int i = 0; i < n; i++) 26 | { 27 | ptr[i] = 7 * (i+1); 28 | } 29 | 30 | printf("The Array is \n"); 31 | 32 | for (int i = 0; i < n; i++) 33 | { 34 | printf("%d \n", ptr[i]); 35 | } 36 | 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Chapter 11/01_dma.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | int n; 6 | int* ptr; 7 | scanf("%d", &n); 8 | ptr = (int*) malloc(n * sizeof(int)); 9 | // int arr[n]; // Not allowed in c 10 | ptr[0] = 3; 11 | ptr [1]= 6; 12 | printf("%d", ptr[0]); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Chapter 11/02_quick_quiz.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | float n = 5; 6 | float* ptr; 7 | ptr = (float*) malloc(n * sizeof(float)); 8 | 9 | ptr[0] = 3.345; 10 | ptr [1]= 16.345; 11 | ptr [2]= 6.345; 12 | ptr [3]= 56.345; 13 | ptr [4]= 66.345; 14 | 15 | printf("%.2f\n", ptr[0]); 16 | printf("%.2f\n", ptr[1]); 17 | printf("%.2f\n", ptr[2]); 18 | printf("%.2f\n", ptr[3]); 19 | printf("%.2f\n", ptr[4]); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Chapter 11/03_quick_quiz.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | int n; 6 | int* ptr; 7 | scanf("%d", &n); 8 | ptr = (int*) calloc(n, sizeof(int)); 9 | // int arr[n]; // Not allowed in c 10 | ptr[0] = 3; 11 | printf("%d", ptr[0]); 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Chapter 11/04_free.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | int n; 6 | int* ptr; 7 | scanf("%d", &n); 8 | ptr = (int*) malloc(n * sizeof(int)); 9 | // int arr[n]; // Not allowed in c 10 | ptr[0] = 3; 11 | free(ptr); 12 | printf("%d", ptr[0]); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Chapter 11/05_realloc.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | int n = 5; 6 | int* ptr; 7 | ptr = (int*) malloc(n * sizeof(int)); 8 | ptr[0] = 3; 9 | printf("%d", ptr[0]); 10 | 11 | ptr = (int*) realloc(ptr, 10 * sizeof(int)); 12 | printf("%d", ptr[0]); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Chapter 2 - Practice Set/01_problem1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | // Which of the following is invalid in C? 6 | int a = 1; 7 | int b = a; 8 | int v = 3 * 3; 9 | // char dt = '21 dec 2020'; // Wrong! 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Chapter 2 - Practice Set/02_problem2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | float a = 3.0/8 - 2; 5 | printf("The value of a is %f", a); 6 | return 0; 7 | } -------------------------------------------------------------------------------- /Chapter 2 - Practice Set/03_problem3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | // int a = 2342354; 5 | int a = 3349895; 6 | printf("The value of a%97 is %d", a%97); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /Chapter 2 - Practice Set/04_problem4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | // Explain step by step evaluation of 3*x/y – z+k, where x=2, y=3, z=3, k=1 5 | int x=2, y=3, z=3, k=1; 6 | float e = 3*x/y - z+k; 7 | // 3*x/y - z+k; 8 | // 6/y - z+k; 9 | // 2-z+k; 10 | // -1+k 11 | // -1 + 1 12 | // 0 13 | printf("The value of e is %f", e); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Chapter 2 - Practice Set/05_problem5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | float a = 3.0 + 1 ; 5 | printf("The value of a is %f", a); 6 | return 0; 7 | } -------------------------------------------------------------------------------- /Chapter 2/01_variables.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int i = 10; // Declare and initialize 'i' with 10 6 | int j = i; 7 | int a = 2, b = 3, c = 4, d = 5; 8 | // %d is called format specifier 9 | // %d is for int, %f is for float and %c is for char 10 | printf("The value of i is %d and value of j is %d\n", i, j ); 11 | printf("The value of a is %d and value of b is %d\n", a, b ); 12 | printf("The value of c is %d and value of d is %d\n", c, d ); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Chapter 2/02_arithmetic_operators.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a = 5; 5 | int b = 3; 6 | int c = a + b; 7 | printf("The value of a is %d and value of b is %d and sum is %d\n", a, b, c); 8 | // Modulus operator is used to get the remainder 9 | printf("The remainder when a is divided by b is: %d\n", a%b); 10 | 11 | // This does not work for exponentiation in c 12 | // int d = a^b; 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Chapter 2/03_type_conversion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | float a = 9.0; 5 | int b = 2; 6 | float c = a/b; 7 | int d = 6.7; 8 | printf("The value of a/b is %f\n", c); 9 | printf("The value of d is %d", d); 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Chapter 2/04_associativity.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a = 3; 5 | int b = 6; 6 | int c = 9; 7 | 8 | printf("The value is %d\n", a*b/c + 7); 9 | printf("The value is %d", 3*b/2*c + 7*a); 10 | // 3*b/2*c + 7*a 11 | // 3*b/2*c + 21 12 | // 18/2*c + 21 13 | // 9*c + 21 14 | // 81 + 21 15 | // 102 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Chapter 2/05_typecasting.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n = 45; 5 | float m = 32.23; 6 | 7 | n = (int) m; // convert the data type to int 8 | printf("%d\n", n); 9 | return 0; 10 | } -------------------------------------------------------------------------------- /Chapter 3 - Practice Set/01_problem1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a = 10; 6 | if (a = 11) 7 | printf("I am 11"); 8 | else 9 | printf("I am not 11"); 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Chapter 3 - Practice Set/02_problem2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int marks1, marks2, marks3; 5 | // int value_of_harrys_marks; 6 | // int value_of_rohans_marks; 7 | // printf("%d",value_of_harrys_marks ) 8 | printf("Enter marks1: \n"); 9 | scanf("%d", &marks1); 10 | printf("Enter marks2: \n"); 11 | scanf("%d", &marks2); 12 | printf("Enter marks3: \n"); 13 | scanf("%d", &marks3); 14 | printf("The marks are %d %d and %d\n", marks1, marks2, marks3); 15 | 16 | if(marks1<33 || marks2<33 || marks3<33){ 17 | printf("You are failed due to less marks in individual subject(s)\n"); 18 | } 19 | else if((marks1 + marks2 + marks3)/3 <40){ 20 | printf("You are failed due less percentage\n"); 21 | } 22 | else{ 23 | printf("You are passed!"); 24 | } 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Chapter 3 - Practice Set/03_problem3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int income; 5 | float tax=0; 6 | printf("Enter income: \n"); 7 | scanf("%d", &income); 8 | if(income<=250000){ 9 | tax = 0; 10 | } 11 | else if(income>250000 && income<=500000){ 12 | tax = 0.05 * (income - 250000); 13 | } 14 | else if(income>500000 && income<=1000000){ 15 | tax = 0.05 * (500000 - 250000) + 0.2 * (income- 500000); 16 | } 17 | else { 18 | tax = 0.05 * (500000 - 250000) + 0.2 * (1000000- 500000) + 0.3 * (income - 1000000); 19 | } 20 | printf("The total tax you need to pay is %.3f", tax); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Chapter 3 - Practice Set/04_problem4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int year; 5 | printf("Enter year: \n"); 6 | scanf("%d", &year); 7 | 8 | if((year %4==0 && year%100!=0) || year %400==0){ 9 | printf("This is a leap year"); 10 | } 11 | else{ 12 | printf("This is not a leap year"); 13 | } 14 | 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Chapter 3 - Practice Set/05_problem5.c: -------------------------------------------------------------------------------- 1 | // https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html 2 | 3 | #include 4 | 5 | int main(){ 6 | char ch = 'A'; 7 | printf("The character is %c\n", ch); 8 | printf("The value of character is %d\n", ch); 9 | // 97, 122 10 | if(ch >= 97 && ch<=122){ 11 | printf("This character is lowercase\n"); 12 | } 13 | else{ 14 | printf("This character not lowecase \n"); 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Chapter 3 - Practice Set/06_problem.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a=4, b=2, c=116, d=32; 5 | if(a>b && a>c && a>d){ 6 | printf("The greatest of all is %d", a); 7 | } 8 | else if(b>a && b>c && b>d){ 9 | printf("The greatest of all is %d", b); 10 | } 11 | else if(c>a && c>b && c>d){ 12 | printf("The greatest of all is %d", c); 13 | } 14 | else if(d>a && d>c && d>b){ 15 | printf("The greatest of all is %d", d); 16 | } 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Chapter 3/01_conditionals.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int age = 15; 5 | 6 | if(age>10){ 7 | printf("We are inside if\n"); 8 | printf("Your age is greater than 10\n"); 9 | } 10 | if(age%5==0){ 11 | printf("We are inside another if\n"); 12 | printf("Your age is divisible by 50\n"); 13 | } 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Chapter 3/02_if_else.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int age = 5; 5 | 6 | if(age>10){ 7 | printf("We are inside if\n"); 8 | printf("Your age is greater than 10\n"); 9 | } 10 | else{ 11 | printf("Your age is not greater than 10"); 12 | } 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Chapter 3/03_non_zero.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | if(1){ 6 | printf("This if is executed!\n"); 7 | } 8 | if(2345){ 9 | printf("This if is also executed!\n"); 10 | } 11 | if(2.74){ 12 | printf("This if is also executed!\n"); 13 | } 14 | if('c'){ 15 | printf("This character inside if is also executed!\n"); 16 | } 17 | if(0){ 18 | printf("I am zero - I am not executed\n"); 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Chapter 3/04_logical.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a=1; int b=1; 5 | printf("The value of a and b is %d\n", a&&b); 6 | printf("The value of a or b is %d\n", a||b); 7 | printf("The value of not(a) is %d\n", !a); 8 | 9 | if(a && b){ 10 | printf("both are true\n"); 11 | } 12 | // is same as writing .... 13 | if(a){ 14 | if(b){ 15 | printf("both are true"); 16 | } 17 | } 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Chapter 3/05_else_if.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int age = 45; 5 | 6 | if(age>60){ 7 | printf("You can drive and you are a senior citizen"); 8 | } 9 | else if(age>40){ 10 | printf("You can drive and you are elder"); 11 | } 12 | else if(age>18){ 13 | printf("You can drive"); 14 | } 15 | else{ 16 | printf("You cannot drive"); 17 | } 18 | 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Chapter 3/06_ternary.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | // condition ? expression-if-true : expression-if-false 5 | int a = 345; 6 | int b= 345452; 7 | a>b?printf("a is greater"):printf("b is greater"); 8 | } -------------------------------------------------------------------------------- /Chapter 3/07_switch.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a; 5 | printf("Enter a: "); 6 | scanf("%d", &a); 7 | 8 | switch(a){ 9 | case 1: 10 | printf("You entered 1\n"); 11 | break; 12 | case 2: 13 | printf("You entered 2\n"); 14 | break; 15 | case 3: 16 | printf("You entered 3\n"); 17 | break; 18 | case 4: 19 | printf("You entered 4\n"); 20 | break; 21 | default: 22 | printf("Nothing matched"); 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Chapter 3/08_quiz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | /* 5 | Quick Quiz: Write a program to find grade of a student given his marks based on below: 6 | 90 – 100 => A 7 | 80 – 90 => B 8 | 70 – 80 => C 9 | 60 – 70 => D 10 | 50 – 60 => E 11 | <50 => F 12 | */ 13 | char grade; 14 | int marks = 46; 15 | if(marks<=100 && marks>=90){ 16 | grade = 'A'; 17 | } 18 | else if(marks<=90 && marks>=80){ 19 | grade = 'B'; 20 | } 21 | else if(marks<=80 && marks>70){ 22 | grade = 'C'; 23 | } 24 | else if(marks<=70 && marks>=60){ 25 | grade = 'D'; 26 | } 27 | else if(marks<=60 && marks>=50){ 28 | grade = 'E'; 29 | } 30 | else{ 31 | grade = 'F'; 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Chapter 4 - Practice set/01_problem1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n; 5 | scanf("%d", &n); 6 | for (int i = 1; i <= 10; i++) 7 | { 8 | printf("%d X %d = %d\n", n, i, n*i ); 9 | } 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Chapter 4 - Practice set/02_problem2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n = 10; 5 | for (int i = 10; i; i--) 6 | { 7 | printf("%d X %d = %d\n", n, i, n*i ); 8 | } 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Chapter 4 - Practice set/05_problem5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int i = 1; 5 | int sum = 0; 6 | while(i<=10){ 7 | sum +=i; 8 | i++; 9 | } 10 | printf("The sum of first 10 natural numbers is %d", sum); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Chapter 4 - Practice set/06_problem6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | // USING DO WHILE LOOP: 6 | // int i = 1; 7 | // int sum = 0; 8 | // do{ 9 | // sum += i; 10 | // i++; 11 | // } while (i <= 10); 12 | 13 | // USING FOR LOOP: 14 | int sum = 0; 15 | for (int i = 1; i <= 10; i++) 16 | { 17 | sum += i; 18 | } 19 | 20 | 21 | 22 | printf("The sum of first 10 natural numbers is %d", sum); 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Chapter 4 - Practice set/07_problem7.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int sum=0; 5 | for (int i = 1; i <= 10; i++) 6 | { 7 | sum += (8*i); 8 | } 9 | printf("The sum of the table of 8 is %d", sum); 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Chapter 4 - Practice set/08_problem8.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | // 8! = 1 X 2 X 3 X 4 X 5 X 6 X 7 X 8 5 | // 5! = 1 X 2 X 3 X 4 X 5 6 | // n! = 1 X 2 X 3 X 4 X 5 ..... X n 7 | // 0! = 1 8 | int product=1; 9 | int n = 1; 10 | for (int i = 1; i <= n; i++) 11 | { 12 | product *=i; 13 | } 14 | printf("The factorial is %d", product); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Chapter 4 - Practice set/09_problem9.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | // 8! = 1 X 2 X 3 X 4 X 5 X 6 X 7 X 8 5 | // 5! = 1 X 2 X 3 X 4 X 5 6 | // n! = 1 X 2 X 3 X 4 X 5 ..... X n 7 | // 0! = 1 8 | int i = 1; 9 | int product=1; 10 | int n = 5; 11 | while(i<=n) 12 | { 13 | product *=i; 14 | i++; 15 | } 16 | printf("The factorial of %d is %d", n, product); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Chapter 4 - Practice set/10_problem10.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n = 7; 6 | int not_prime = 0; 7 | 8 | if (n == 0 || n == 1) 9 | { 10 | not_prime = 1; 11 | } 12 | else 13 | { 14 | 15 | for (int i = 2; i < n; i++) 16 | { 17 | if (n % i == 0 && n != 2) 18 | { 19 | not_prime = 1; 20 | break; 21 | } 22 | } 23 | } 24 | if (not_prime) 25 | { 26 | printf("%d is not prime\n", n); 27 | } 28 | else 29 | { 30 | printf("%d is prime\n", n); 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Chapter 4 - Practice set/11_problem11.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n = 4; 6 | int not_prime = 0; 7 | 8 | if (n == 0 || n == 1) 9 | { 10 | not_prime = 1; 11 | } 12 | else 13 | { 14 | int i =2; 15 | // while (i < n) 16 | // { 17 | // if (n % i == 0 && n != 2) 18 | // { 19 | // not_prime = 1; 20 | // break; 21 | // } 22 | // i++; 23 | // } 24 | 25 | do{ 26 | if (n % i == 0 && n != 2) 27 | { 28 | not_prime = 1; 29 | break; 30 | } 31 | i++; 32 | }while (i < n); 33 | } 34 | if (not_prime) 35 | { 36 | printf("%d is not prime\n", n); 37 | } 38 | else 39 | { 40 | printf("%d is prime\n", n); 41 | } 42 | 43 | return 0; 44 | } -------------------------------------------------------------------------------- /Chapter 4/01_loop.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | // Problem: Print Happy Birthday 1 Lakh times 5 | printf("Happy Birthday!\n"); 6 | return 0; 7 | } -------------------------------------------------------------------------------- /Chapter 4/02_while.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int i = 0; 5 | while(i<4){ 6 | printf("Happy Birthday!\n"); 7 | // i = i + 1; 8 | i++; 9 | } 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Chapter 4/03_while_value.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int i = 0; 5 | while (i<10) { 6 | printf("The value of i is %d \n", i); 7 | i++; 8 | } 9 | return 0; 10 | } -------------------------------------------------------------------------------- /Chapter 4/04_infinite_loop.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int i = 0; 5 | while (2<10) { 6 | printf("The value of i is %d \n", i); 7 | i++; 8 | } 9 | return 0; 10 | } -------------------------------------------------------------------------------- /Chapter 4/05_quick_quiz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | /* 5 | Quick Quiz: Write a program to print natural numbers from 10 to 20 when initial loop 6 | counter is initialized to 0. 7 | */ 8 | 9 | int i = 0; 10 | while(i<=20){ 11 | if(i>=10){ 12 | printf("The value of i is %d\n", i); 13 | } 14 | i++; 15 | } 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Chapter 4/06_increment_decrement.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int i = 5; 5 | printf("The value of i is %d\n", i); 6 | 7 | i = i + 5; // 10 8 | printf("The value of i is %d\n", i); 9 | 10 | printf("The value of i is %d\n", i++); // 10 11 | printf("The value of i is %d\n", i); // 11 12 | 13 | 14 | i +=2; // Same as i = i + 2; 15 | printf("The value of i is %d\n", i); // 13 16 | 17 | // i++ prints i first and then increments i (Post increment Operator) 18 | // ++i increments i first and then prints i (Post increment Operator) 19 | 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Chapter 4/07_do_while.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int i = 0; 5 | do{ 6 | printf("The value of i is %d\n", i); 7 | i++; 8 | } while (i<4); 9 | 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Chapter 4/08_quick_quiz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n, i=1; 5 | scanf("%d", &n); 6 | do{ 7 | printf("%d\n", i); 8 | i++; 9 | }while(i<=n); 10 | 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Chapter 4/09_for.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n = 6; 5 | for(int i =0;i 2 | 3 | int main(){ 4 | int n = 12; 5 | for (int i = 1; i <= n; i++) 6 | { 7 | printf("%d\n", i); 8 | } 9 | 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Chapter 4/11_decrementing_for_loop.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n = 12; 5 | for (int i=n; i ; i--){ 6 | printf("%d\n",i); 7 | } 8 | 9 | return 0; 10 | } -------------------------------------------------------------------------------- /Chapter 4/12_break_continue.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | for (int i = 0; i < 15; i++) 5 | { 6 | if(i==5){ 7 | // break; // exit the loop now! 8 | continue; // skip this iteration now 9 | } 10 | printf("i is %d\n", i); 11 | } 12 | 13 | printf("For loop is done!"); 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Chapter 5 - Practice Set/01_problem1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float average(int a, int b, int c); 4 | 5 | float average(int a, int b, int c){ 6 | return (a+b+c)/3.0; 7 | } 8 | 9 | 10 | int main(){ 11 | int a = 3, b=6, c=5; 12 | printf("The average of a, b and c is %f", average(a, b, c)); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Chapter 5 - Practice Set/02_problem2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | float c2f(float); 4 | 5 | float c2f(float c){ 6 | return ((9*c)/5) + 32; 7 | } 8 | 9 | int main(){ 10 | float c = 45; 11 | printf("Celsius to Fahrenheit for %f is %.2f", c, c2f(c)); 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Chapter 5 - Practice Set/03_problem3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | float force(float); 5 | 6 | float force(float mass){ 7 | return mass*9.8; 8 | } 9 | 10 | int main(){ 11 | int m = 45; 12 | printf("The value of force is %f\n", force(m)); 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Chapter 5 - Practice Set/04_problem4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... 4 | // fibonacci(n) = fibonacci(n-1) + fibonacci(n-2); 5 | 6 | int fibonacci(int); 7 | 8 | int fibonacci(int n){ 9 | if(n == 1 || n==2){ 10 | return n-1; 11 | } 12 | return fibonacci(n-1) + fibonacci(n-2); 13 | } 14 | 15 | int main(){ 16 | int n = 7; 17 | printf("The value of fibonacci series at %d is %d", n, fibonacci(n)); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Chapter 5 - Practice Set/05_problem5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int a = 4; 5 | printf("%d %d %d \n", a, ++a, a++); 6 | // 6 6 4 7 | // 4 5 5 8 | return 0; 9 | } -------------------------------------------------------------------------------- /Chapter 5 - Practice Set/06_problem6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int sum_natural(int); 5 | 6 | int sum_natural(int n){ 7 | if(n==1){ 8 | return 1; 9 | } 10 | // sum_natural(n) = 1 + 2 + 3 + 4 + 5 + ... n-1 + n; 11 | // sum_natural(n) = sum(n-1) + n; 12 | return sum_natural(n-1) + n; 13 | 14 | } 15 | int main(){ 16 | printf("The sum of first 5 natural numbers is %d", sum_natural(5)); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Chapter 5 - Practice Set/07_problem7.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n = 3; 5 | for (int i = 0; i < n; i++) 6 | { 7 | // This loop runs from 0 to 2 8 | // if i = 0 ---> print 1 star 9 | // if i = 1 ---> print 3 stars 10 | // if i = 2 ---> print 5 stars 11 | // no_of_stars = (2*i+1) 12 | 13 | // This for loop prints (2*i+1) stars 14 | for(int j=0; j<2*i+1;j++){ 15 | printf("*"); 16 | } 17 | 18 | // This printf prints a new line 19 | printf("\n"); 20 | } 21 | 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Chapter 5/01_function.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Function prototype 4 | int sum(int, int); 5 | 6 | 7 | // Function definition 8 | int sum(int x, int y){ 9 | // printf("The sum is %d\n", x+y); 10 | return x+y; 11 | } 12 | 13 | int main(){ 14 | int a = 1; 15 | int b = 2; 16 | 17 | // int c = a + b; 18 | // printf("The sum is %d\n", c); 19 | int c = sum(a,b); // Function call 20 | printf("%d\n", c); 21 | 22 | int a1 = 12; 23 | int b1 = 23; 24 | 25 | // int c1 = a1 + b1; 26 | // printf("The sum is %d\n", c1); 27 | int c1 = sum(a1,b1); // Function call 28 | printf("%d\n", c1); 29 | 30 | int a2 = 2; 31 | int b2 = 27; 32 | 33 | // int c2 = a2 + b2; 34 | // printf("The sum is %d\n", c2); 35 | int c3 = sum(a2, b2); // Function call 36 | printf("%d\n", c3); 37 | 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Chapter 5/02_quick_quiz.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void good_morning(); 4 | void good_afternoon(); 5 | void good_evening(); 6 | 7 | 8 | void good_morning(){ 9 | printf("good morning\n"); 10 | } 11 | 12 | void good_afternoon(){ 13 | printf("good afternoon\n"); 14 | } 15 | 16 | void good_evening(){ 17 | printf("good evening\n"); 18 | } 19 | 20 | int main(){ 21 | good_morning(); 22 | good_afternoon(); 23 | good_evening(); 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Chapter 5/03_change.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int change(int a); 4 | 5 | int change(int a) { 6 | a = 77; // Misnomer 7 | return 0; 8 | } 9 | 10 | int main(){ 11 | int b=22; 12 | change(b); // The value of b remains 22 13 | printf("b is %d", b); 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Chapter 5/04_quick_quiz.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | int a = 5; 6 | printf("The area of this square is %f\n", pow(a, 2)); 7 | return 0; 8 | } -------------------------------------------------------------------------------- /Chapter 5/05_recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int factorial(int); 4 | // Factorial(5) = 1 X 2 X 3 X 4 X 5 5 | // Factorial(4) = 1 X 2 X 3 X 4 6 | // Factorial(3) = 1 X 2 X 3 7 | // Factorial(n) = 1 x 2 X 3 X .... X n-1 X n 8 | // = 1 x 2 X 3 X .... X n-1 9 | 10 | int factorial(int n){ 11 | if(n == 1 || n == 0){ // Base condition 12 | return 1; 13 | } 14 | // Factorial(n) = Factorial(n-1) X n 15 | return n * factorial(n-1); 16 | } 17 | 18 | int main(){ 19 | int a = 6; 20 | printf("The factorial of %d is %d", a, factorial(a)); 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Chapter 6 - Practice Set/01_problem1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int i = 2; 5 | int* ptr = &i; 6 | printf("The address of i is %u\n", &i); 7 | printf("The value of i is %d\n", *ptr); 8 | return 0; 9 | } -------------------------------------------------------------------------------- /Chapter 6 - Practice Set/02_problem2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int returning_5(int* ptr){ 5 | printf("The value of ptr is %d\n", ptr); 6 | printf("The value at ptr is %d\n", *ptr); 7 | return 5; 8 | } 9 | int main(){ 10 | int i = 2; 11 | int* ptr = &i; 12 | printf("The address of i is %u\n", &i); 13 | returning_5(ptr); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Chapter 6 - Practice Set/03_problem3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void change_to_ten_times(int*); 4 | 5 | void change_to_ten_times(int* a){ 6 | *a = *a * 10; 7 | } 8 | 9 | int main(){ 10 | int x = 45; 11 | printf("The value of x is %d\n", x); 12 | change_to_ten_times(&x); 13 | printf("The value of x is %d\n", x); 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Chapter 6 - Practice Set/04_problem4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void change_to_thirty_times(int*); 4 | 5 | void change_to_thirty_times(int* a){ 6 | *a = *a * 10; 7 | } 8 | 9 | int main(){ 10 | int x = 45; 11 | printf("The value of x is %d\n", x); 12 | change_to_thirty_times(&x); 13 | printf("The value of x is %d\n", x); 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Chapter 6 - Practice Set/05_problem5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int* sum(int a, int b){ 5 | int s = a+b; 6 | int* ptr = &s; 7 | printf("The sum is %d\n", s); 8 | return ptr; 9 | } 10 | 11 | float* average(int a, int b){ 12 | float avg = (a+b)/2.0; 13 | float * ptr = &avg; 14 | printf("The average is %f\n", avg); 15 | return ptr; 16 | } 17 | 18 | int main(){ 19 | int x = 4; 20 | int y = 6; 21 | int* ptr1; 22 | float* ptr2; 23 | 24 | ptr1 = sum(x,y); 25 | ptr2 = average(x,y); 26 | 27 | printf("The address of sum is %u and of average is %u", ptr1, ptr2 ); 28 | 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Chapter 6 - Practice Set/06_problem6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int i = 2; 5 | int* ptr1 = &i; 6 | int** ptr2 = &ptr1; 7 | printf("The address of i is %u\n", &i); 8 | printf("The value of i is %d\n", *ptr1); 9 | printf("The value of i is %d\n", **ptr2); 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Chapter 6 - Practice Set/07_problem7.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void change_to_thirty_times(int); 4 | 5 | void change_to_thirty_times(int a){ 6 | a = a * 10; 7 | } 8 | 9 | int main(){ 10 | int x = 45; 11 | printf("The value of x is %d\n", x); 12 | change_to_thirty_times(x); 13 | printf("The value of x is %d\n", x); 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Chapter 6/01_pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int i = 72; 5 | int* j = &i; // j is a pointer pointing to i (j is an integer pointer) 6 | int k = 67; 7 | printf("The address of i is %p\n", &i); 8 | printf("The address of i is %p\n", j); 9 | printf("The address of i is %p\n", &k); 10 | 11 | 12 | printf("The value at address j is %d\n", *(&i)); 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Chapter 6/02_other_types.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | char i = 'A'; 5 | char* j = &i; // j is a pointer pointing to i (j is a character pointer) 6 | 7 | float k = 5.232; 8 | float* k1 = &k; 9 | printf("The address of i is %p\n", &i); 10 | printf("The address of i is %p\n", j); 11 | printf("The address of i is %p\n", &k); 12 | 13 | 14 | printf("The value at address j is %d\n", *(&i)); 15 | 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Chapter 6/03_pointer2pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int i = 6; 5 | int* j = &i; 6 | int** k = &j; 7 | 8 | 9 | printf("The value of i is %d\n", i); 10 | printf("The value of i is %d\n", *j); 11 | printf("The value of i is %d\n", *(&i)); 12 | printf("The value of i is %d\n", **(&j)); 13 | 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /Chapter 6/04_call_by_value.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int sum(int, int); 5 | 6 | 7 | int sum(int a, int b){ 8 | a = 6; // Sum function cannot change x using a because copy of x is provided to sum in a 9 | return a + b; 10 | } 11 | 12 | int main(){ 13 | int x = 1, y=6; 14 | printf("The sum of 1 and 6 is %d\n", sum(x, y)); 15 | printf("The value of x is %d", x); 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Chapter 6/05_call_by_reference.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int sum(int *, int *); 5 | 6 | 7 | // Sum should change the value of x 8 | int sum(int* a, int* b){ 9 | *a = 6; 10 | return (*a + *b); 11 | } 12 | 13 | int main(){ 14 | int x = 1, y=6; 15 | printf("The sum of 1 and 6 is %d\n", sum(&x, &y)); 16 | printf("The value of x is %d", x); 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Chapter 6/06_swap.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void swap(int* a, int* b); 4 | 5 | void swap(int* a, int* b){ 6 | int temp; 7 | temp = *a; 8 | *a = *b; 9 | *b = temp; 10 | } 11 | int main(){ 12 | int a = 4, b = 6; 13 | swap(&a, &b); 14 | printf("The value of a is %d and the value of b is %d", a, b); 15 | return 0; 16 | } -------------------------------------------------------------------------------- /Chapter 7 - Practice Set/01_problem1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 6 | int *ptr = a; 7 | 8 | printf("The value at address %u is %d", ptr+2,*(ptr+2)); 9 | 10 | return 0; 11 | 12 | 13 | } -------------------------------------------------------------------------------- /Chapter 7 - Practice Set/03_problem3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int arr[10]; 5 | 6 | for (int i = 0; i < 10; i++) 7 | { 8 | arr[i] = 5* (i+1); 9 | } 10 | 11 | for (int i = 0; i < 10; i++) 12 | { 13 | printf("The value of 5 X %d = %d \n", i+1, arr[i]); 14 | } 15 | 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Chapter 7 - Practice Set/04_problem4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int n; 5 | scanf("%d", &n); 6 | int arr[10]; 7 | 8 | for (int i = 0; i < 10; i++) 9 | { 10 | arr[i] = n* (i+1); 11 | } 12 | 13 | for (int i = 0; i < 10; i++) 14 | { 15 | printf("The value of %d X %d = %d \n", n, i+1, arr[i]); 16 | } 17 | 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Chapter 7 - Practice Set/05_problem5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void printArray(int a[], int n){ 4 | for (int i = 0; i < n; i++) 5 | { 6 | printf("%d ", a[i]); 7 | } 8 | printf("\n"); 9 | } 10 | 11 | void reverse(int arr[], int n){ 12 | /* for i from 0 to n/2 13 | arr[i] arr[n-i-1] 14 | */ 15 | int temp; 16 | for (int i = 0; i < n/2; i++) 17 | { 18 | temp = arr[i]; 19 | arr[i] = arr[n-i-1]; 20 | arr[n-i-1] = temp; 21 | } 22 | 23 | } 24 | 25 | int main(){ 26 | int arr[] = {1, 2, 3, 4, 5, 6 }; 27 | printArray(arr, 6); 28 | reverse(arr, 6); 29 | printArray(arr, 6); 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Chapter 7 - Practice Set/06_problem6.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int count (int a[], int n){ 4 | int no_of_positive=0; 5 | for (int i = 0; i < n; i++) 6 | { 7 | if(a[i]>0){ 8 | no_of_positive++; 9 | } 10 | } 11 | return no_of_positive; 12 | } 13 | 14 | int main() 15 | { 16 | int a[] = {1, 2, 3, 4, -5, 6, 7, -8, 9, 10}; 17 | 18 | printf("The no of positive integers is %d", count(a, 10)); 19 | 20 | return 0; 21 | 22 | 23 | } -------------------------------------------------------------------------------- /Chapter 7 - Practice Set/07_problem7.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int arr[3][10]; 6 | int mul[] = {2, 7, 9}; 7 | for (int i = 0; i < 3; i++) 8 | { 9 | for (int j = 0; j < 10; j++) 10 | { 11 | arr[i][j] = mul[i] * (j + 1); 12 | } 13 | } 14 | 15 | for (int i = 0; i < 3; i++) 16 | { 17 | for (int j = 0; j < 10; j++) 18 | { 19 | printf("The value of arr[i][j] is %d\n", arr[i][j]); 20 | } 21 | printf("\n"); 22 | } 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Chapter 7 - Practice Set/08_problem8.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int n1, n2, n3; 6 | scanf("%d %d %d", &n1, &n2, &n3); 7 | int arr[3][10]; 8 | int mul[] = {n1, n2, n3}; 9 | for (int i = 0; i < 3; i++) 10 | { 11 | for (int j = 0; j < 10; j++) 12 | { 13 | arr[i][j] = mul[i] * (j + 1); 14 | } 15 | } 16 | 17 | for (int i = 0; i < 3; i++) 18 | { 19 | for (int j = 0; j < 10; j++) 20 | { 21 | printf("The value of arr[i][j] is %d\n", arr[i][j]); 22 | } 23 | printf("\n"); 24 | } 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Chapter 7 - Practice Set/09_problem9.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int arr[2][3][4]; 5 | for (int i = 0; i < 2; i++) 6 | { 7 | for (int j = 0; j < 3; j++) 8 | { 9 | for (int k = 0; k < 4; k++) 10 | { 11 | printf("%u ", &arr[i][j][k]); 12 | } 13 | 14 | } 15 | 16 | } 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Chapter 7 - Practice Set/c.json: -------------------------------------------------------------------------------- 1 | { 2 | // Place your snippets for c here. Each snippet is defined under a snippet name and has a prefix, body and 3 | // description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are: 4 | // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 5 | // same ids are connected. 6 | // Example: 7 | "C boilerplate": { 8 | "prefix": "c boiler", 9 | "body": [ 10 | "#include \n", 11 | "int main(){", 12 | "\t$0", 13 | "\treturn 0;", 14 | "}", 15 | 16 | ], 17 | "description": "The C language boilerplate" 18 | }, 19 | "myprintf": { 20 | "prefix": "myprintf", 21 | "body": [ 22 | "printf(\"$0 \\n\");\n", 23 | 24 | ], 25 | "description": "The C language boilerplate" 26 | } 27 | } -------------------------------------------------------------------------------- /Chapter 7/01_arrays.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int marks[90]; // Reserve space to store 90 integers 5 | 6 | marks[0] = 45; 7 | marks[1] = 95; 8 | // We can go all the way till marks[89] 9 | printf("Marks 0 and Marks 1 is %d %d", marks[0], marks[1]); 10 | return 0; 11 | } -------------------------------------------------------------------------------- /Chapter 7/02_array_input.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int marks[5]; 5 | 6 | printf("Enter marks of 5 students\n"); 7 | 8 | // scanf("%d", &marks[0]); 9 | // scanf("%d", &marks[1]); 10 | // scanf("%d", &marks[2]); 11 | // scanf("%d", &marks[3]); 12 | // scanf("%d", &marks[4]); 13 | for (int i = 0; i < 5; i++) 14 | { 15 | scanf("%d", &marks[i]); 16 | } 17 | 18 | for (int i = 0; i < 5; i++) 19 | { 20 | printf("The value of marks at index %d is %d\n", i, marks[i]); 21 | } 22 | 23 | 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Chapter 7/03_other_array_init_ways.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int cgpa[] = {9, 8, 8}; 5 | for (int i = 0; i < 3; i++) 6 | { 7 | printf("The value of array at index %d is %d\n", i, cgpa[i]); 8 | } 9 | return 0; 10 | } -------------------------------------------------------------------------------- /Chapter 7/04_pointer_arithmetic.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | 5 | // POINTER ARITHMETIC USING INTEGER POINTER 6 | // int a = 5; 7 | // int *ptr = &a; 8 | // printf("The address of a is %u\n", &a); 9 | // printf("The address of a is %u\n", ptr); 10 | // ptr++; 11 | // printf("The value of ptr is %u\n", ptr); 12 | 13 | // POINTER ARITHMETIC USING CHARACTER POINTER 14 | char a = 'A'; 15 | char *ptr = &a; 16 | printf("The address of a is %u\n", &a); 17 | printf("The address of a is %u\n", ptr); 18 | ptr++; 19 | printf("The value of ptr is %u\n", ptr); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Chapter 7/05_array_in_memory.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int marks[5]; 5 | 6 | printf("Enter marks of 5 students\n"); 7 | 8 | for (int i = 0; i < 5; i++) 9 | { 10 | scanf("%d", &marks[i]); 11 | } 12 | 13 | for (int i = 0; i < 5; i++) 14 | { 15 | printf("The address of marks at index %d is %u\n", i, &marks[i]); 16 | } 17 | 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Chapter 7/06_array_using_pointers.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | int marks[] = {12, 34, 53, 66}; 5 | 6 | int* ptr = &marks[0]; 7 | // int* ptr = marks; // Same as int* ptr = &marks[0]; 8 | 9 | for (int i = 0; i < 4; i++) 10 | { 11 | // printf("The marks at index %d is %d\n", i, marks[i]); 12 | printf("The marks at index %d is %d\n", i, *ptr); 13 | ptr++; 14 | } 15 | 16 | 17 | 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Chapter 7/07_array_2d.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int arr[3][2]; 6 | 7 | for (int i = 0; i < 3; i++) 8 | { 9 | for (int j = 0; j < 2; j++) 10 | { 11 | printf("Enter the value of arr[%d][%d]\n", i, j); 12 | scanf("%d", &arr[i][j]); 13 | } 14 | } 15 | 16 | // for (int i = 0; i < 3; i++) 17 | // { 18 | // for (int j = 0; j < 2; j++) 19 | // { 20 | // printf("The value of arr[%d][%d] is %d\n", i, j, arr[i][j]); 21 | // } 22 | // } 23 | 24 | for (int i = 0; i < 3; i++) 25 | { 26 | for (int j = 0; j < 2; j++) 27 | { 28 | printf("%d ", arr[i][j]); 29 | } 30 | printf("\n"); 31 | } 32 | 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Chapter 8 - Practice Set/02_problem2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | char str[6]; 5 | // scanf("%s", str); 6 | for (int i = 0; i < 5; i++) 7 | { 8 | scanf("%c", &str[i]); 9 | fflush(stdin); 10 | } 11 | str[5] = '\0'; 12 | 13 | printf("%s", str); 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Chapter 8 - Practice Set/03_problem3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | int strlen(char str[]){ 5 | int i=0, count; 6 | char c = str[i]; 7 | while(c!='\0'){ 8 | c = str[i]; 9 | i++; 10 | } 11 | count = i-1; 12 | return count; 13 | 14 | } 15 | int main(){ 16 | char str[] = "Harry bhai"; 17 | 18 | printf("%d", strlen(str)); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Chapter 8 - Practice Set/04_problem4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | char* slice(char str[], int m, int n){ 5 | int i=0, count; 6 | char *ptr1 = &str[m]; 7 | char *ptr2 = &str[n]; 8 | 9 | str = ptr1; 10 | str[n] = '\0'; 11 | return str; 12 | 13 | } 14 | int main(){ 15 | char str[] = "Harry bhai"; 16 | 17 | printf("%s", slice(str, 1, 7)); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Chapter 8 - Practice Set/05_problem5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int mystrlen(char str[]) 4 | { 5 | int i = 0, count; 6 | char c = str[i]; 7 | while (c != '\0') 8 | { 9 | c = str[i]; 10 | i++; 11 | } 12 | count = i - 1; 13 | return count; 14 | } 15 | 16 | void mystrcpy(char target[], char source[]) 17 | { 18 | for (int i = 0; i < mystrlen(source); i++) 19 | { 20 | target[i] = source[i]; 21 | } 22 | target[mystrlen(source)] = '\0'; 23 | } 24 | 25 | 26 | int main() 27 | { 28 | char source[] = "harry"; 29 | char target[30]; 30 | mystrcpy(target, source); // target now contains "harry" 31 | printf("%s %s", source, target); 32 | return 0; 33 | } -------------------------------------------------------------------------------- /Chapter 8 - Practice Set/06_problem6.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | char str[] = "Mera saara paisa takiye ke neeche black poly me hai"; 6 | for (int i = 0; i < strlen(str); i++) 7 | { 8 | str[i] = str[i] + 1; 9 | } 10 | 11 | printf("%s", str); 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Chapter 8 - Practice Set/07_problem7.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | char str[] = "Nfsb!tbbsb!qbjtb!ubljzf!lf!offdif!cmbdl!qpmz!nf!ibj"; 6 | for (int i = 0; i < strlen(str); i++) 7 | { 8 | str[i] = str[i] - 1; 9 | } 10 | 11 | printf("%s", str); 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Chapter 8 - Practice Set/08_problem8.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | char c = 'r'; 6 | int count = 0; 7 | char str[] = "Harry"; 8 | for (int i = 0; i < strlen(str); i++) 9 | { 10 | if(str[i] == c){ 11 | count++; 12 | } 13 | } 14 | 15 | printf("%d", count); 16 | 17 | return 0; 18 | } -------------------------------------------------------------------------------- /Chapter 8 - Practice Set/09_problem9.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(){ 5 | char c = 'd'; 6 | int contains = 0; 7 | char str[] = "Harry"; 8 | for (int i = 0; i < 56; i++) 9 | { 10 | printf("This is a nice character \n"); 11 | } 12 | 13 | for (int i = 0; i < strlen(str); i++) 14 | { 15 | if(str[i] == c){ 16 | contains = 1; 17 | break; // This break statement will exit the loop once the character is found! 18 | } 19 | } 20 | if(contains){ 21 | printf("Yes it contains!\n"); 22 | } 23 | else{ 24 | printf("Does not contain! \n"); 25 | 26 | } 27 | 28 | return 0; 29 | } -------------------------------------------------------------------------------- /Chapter 8/01_strings.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | // char st[] = {'a', 'b', 'c', '\0'}; 5 | char st[] = "abc"; // Same as doing char st[] = {'a', 'b', 'c', '\0'}; 6 | for (int i = 0; i < 3; i++) 7 | { 8 | printf("Character is %c \n", st[i]); 9 | } 10 | 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Chapter 8/02_printing_strings.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | // char st[] = {'a', 'b', 'c', '\0'}; 5 | char st[] = "abc"; // Same as doing char st[] = {'a', 'b', 'c', '\0'}; 6 | // for (int i = 0; i < 3; i++){ 7 | // printf("%c", st[i]); 8 | // } 9 | 10 | printf("%s", st); 11 | 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /Chapter 8/03_input.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(){ 4 | char st[4]; 5 | scanf("%s", st); 6 | 7 | printf("%s", st); 8 | 9 | return 0; 10 | } -------------------------------------------------------------------------------- /Chapter 8/04_gets.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | char st[30]; 6 | gets(st); // The entered string is stored in st! 7 | 8 | printf("%s", st); 9 | // puts(st); 10 | printf("hey"); 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Chapter 8/05_string_functions.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | char st[] = "Harry"; 7 | char s1[56] = "Harry"; 8 | char s2[56] = " bhai"; 9 | 10 | // printf("%d", strlen(st)); 11 | char target[30]; 12 | strcpy(target, st); // target now contains "Harry" 13 | // printf("%s %s", st, target); 14 | 15 | strcat(s1, s2); // s1 now contains "helloharry" 16 | // printf("%s", s1); 17 | 18 | int a = strcmp("deep", "joke"); // DJ is negative 19 | printf("%d", a); 20 | 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Chapter 9 - Practice Set/01_problem1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct vector{ 4 | int i; 5 | int j; 6 | }; 7 | 8 | int main(){ 9 | struct vector v = {1, 2}; 10 | printf("The value of vector is %di + %d j", v.i, v.j); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Chapter 9 - Practice Set/02_problem2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | typedef struct vector{ 5 | int i; 6 | int j; 7 | } V; 8 | 9 | V sumVector(V v1, V v2){ 10 | V v3 = {v1.i + v2.i, v1.j + v2.j}; 11 | return v3; 12 | } 13 | 14 | int main(){ 15 | V v1 = {1, 2}; 16 | V v2 = {5, 6}; 17 | V v3 = sumVector(v1, v2); 18 | printf("The value of vector v3 is %di + %d j", v3.i, v3.j); 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Chapter 9 - Practice Set/03_problem3.c: -------------------------------------------------------------------------------- 1 | #include 2 | typedef struct emp{ 3 | int salary; 4 | float score; 5 | } Employee; 6 | 7 | int main(){ 8 | Employee e1; 9 | Employee* ptr = &e1; 10 | (*ptr).salary = 56; 11 | (*ptr).score = 45.3; 12 | 13 | // ptr->salary = 56; 14 | // ptr->score = 45.3; 15 | 16 | printf("The value of salary is %d and the value of score is %.2f \n", ptr->salary, ptr->score); 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Chapter 9 - Practice Set/04_problem4.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct c{ 4 | int real; 5 | int imaginary; 6 | } Complex; 7 | 8 | int main(){ 9 | Complex c = {1, 2}; 10 | printf("The value of Complex number is %d + %di ", c.real, c.imaginary); 11 | return 0; 12 | } -------------------------------------------------------------------------------- /Chapter 9 - Practice Set/05_problem5.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct c 4 | { 5 | int real; 6 | int imaginary; 7 | } Complex; 8 | 9 | void display(Complex c) 10 | { 11 | printf("The value of Complex number is %d + %di \n", c.real, c.imaginary); 12 | } 13 | int main() 14 | { 15 | Complex carr[5]; 16 | for (int i = 0; i < 5; i++) 17 | { 18 | printf("Enter real part \n"); 19 | scanf("%d", &carr[i].real); 20 | printf("Enter imaginary part \n"); 21 | scanf("%d", &carr[i].imaginary); 22 | display(carr[i]); 23 | } 24 | 25 | return 0; 26 | } -------------------------------------------------------------------------------- /Chapter 9 - Practice Set/08_problem8.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct bankacc{ 4 | int accNo; 5 | char name[34]; 6 | char ifsc[12]; 7 | float balance; 8 | }; 9 | 10 | int main(){ 11 | 12 | return 0; 13 | } -------------------------------------------------------------------------------- /Chapter 9 - Practice Set/09_problem9.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct Date 4 | { 5 | int mm; 6 | int dd; 7 | int yyyy; 8 | } DT; 9 | 10 | int compare(DT d1, DT d2) 11 | { 12 | // if d1 is in the future, return 1 13 | if ((d1.yyyy == d2.yyyy) && (d1.mm == d2.mm) && (d1.dd == d2.dd)) 14 | { 15 | return 0; 16 | } 17 | 18 | if (d1.yyyy > d2.yyyy) 19 | { 20 | return 1; 21 | } 22 | else if (d1.yyyy < d2.yyyy) 23 | { 24 | return -1; 25 | } 26 | else if (d1.mm > d2.mm) 27 | { 28 | return 1; 29 | } 30 | else if (d1.mm < d2.mm) 31 | { 32 | return -1; 33 | } 34 | else if (d1.dd > d2.dd) 35 | { 36 | return 1; 37 | } 38 | else if (d1.dd < d2.dd) 39 | { 40 | return -1; 41 | } 42 | } 43 | 44 | int main() 45 | { 46 | DT d1 = {12, 4, 2154}; 47 | DT d2 = {12, 8, 2154}; 48 | printf("%d", compare(d1, d2)); 49 | return 0; 50 | } -------------------------------------------------------------------------------- /Chapter 9/01_structure.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct employee 5 | { 6 | int code; // This declares a new user defined data type! 7 | float salary; 8 | char name[10]; 9 | }; // semicolon is important 10 | 11 | int main(){ 12 | struct employee e1, e2; 13 | e1.code = 4511; 14 | strcpy(e1.name, "Harry"); 15 | e1.salary = 54.44; 16 | 17 | printf("%d %f %s", e1.code, e1.salary, e1.name); 18 | 19 | return 0; 20 | } -------------------------------------------------------------------------------- /Chapter 9/02_quick_quiz.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct employee 5 | { 6 | int code; // This declares a new user defined data type! 7 | float salary; 8 | char name[10]; 9 | }; // semicolon is important 10 | 11 | int main(){ 12 | struct employee e1, e2, e3; 13 | 14 | printf("Enter the value of code\n"); 15 | scanf("%d", &e1.code); 16 | 17 | printf("Enter the value of salary\n"); 18 | scanf("%f", &e1.salary); 19 | 20 | printf("Enter the value of name\n"); 21 | scanf("%s", &e1.name); 22 | printf("%d %f %s\n", e1.code, e1.salary, e1.name); 23 | 24 | 25 | printf("Enter the value of code\n"); 26 | scanf("%d", &e2.code); 27 | 28 | printf("Enter the value of salary\n"); 29 | scanf("%f", &e2.salary); 30 | 31 | printf("Enter the value of name\n"); 32 | scanf("%s", &e2.name); 33 | printf("%d %f %s\n", e2.code, e2.salary, e2.name); 34 | 35 | 36 | 37 | printf("Enter the value of code\n"); 38 | scanf("%d", &e3.code); 39 | 40 | printf("Enter the value of salary\n"); 41 | scanf("%f", &e3.salary); 42 | 43 | printf("Enter the value of name\n"); 44 | scanf("%s", &e3.name); 45 | printf("%d %f %s\n", e3.code, e3.salary, e3.name); 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Chapter 9/03_struct_arrays.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct employee 4 | { 5 | int code; // This declares a new user defined data type! 6 | float salary; 7 | char name[10]; 8 | }; // semicolon is important 9 | 10 | int main() 11 | { 12 | struct employee facebook[100]; // an array of structures 13 | // we can access the data using: 14 | facebook[0].code = 100; 15 | facebook[1].code = 77; 16 | struct employee harry = {100, 71.22, "harry"}; 17 | printf("%d %f %s", harry.code, harry.salary, harry.name); 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Chapter 9/04_pointer_to_struct.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | struct employee 4 | { 5 | int code; // This declares a new user defined data type! 6 | float salary; 7 | char name[10]; 8 | }; // semicolon is important 9 | 10 | int main() 11 | { 12 | struct employee e1; 13 | e1.code = 56; 14 | struct employee *ptr; 15 | ptr = &e1; 16 | // now we can print structure elements using: 17 | // printf("%d", (*ptr).code); 18 | printf("%d", ptr->code); // Exactly same as (*ptr).code 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Chapter 9/05_quick_quiz.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | struct employee 5 | { 6 | int code; // This declares a new user defined data type! 7 | float salary; 8 | char name[10]; 9 | }; // semicolon is important 10 | 11 | void show(struct employee e); // function prototype 12 | 13 | 14 | void show(struct employee e){ 15 | printf("Code is %d\nSalary is %f\nName is %s\n", e.code, e.salary, e.name); 16 | } 17 | 18 | int main() 19 | { 20 | struct employee e1; 21 | e1.code = 4511; 22 | strcpy(e1.name, "Harry"); 23 | e1.salary = 54.44; 24 | show(e1); 25 | 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Chapter 9/06_typedef.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct employee 5 | { 6 | int code; // This declares a new user defined data type! 7 | float salary; 8 | char name[10]; 9 | } Emp; // semicolon is important 10 | 11 | int main(){ 12 | // typedef int harry; 13 | // int a = 88; 14 | // printf("The value of a is %d \n", a); 15 | 16 | // typedef struct employee Emp; 17 | 18 | Emp e1; 19 | Emp* ptr1 = &e1; 20 | e1.code = 4511; 21 | strcpy(e1.name, "Harry"); 22 | e1.salary = 54.44; 23 | 24 | printf("%d %f %s\n", e1.code, e1.salary, e1.name); 25 | printf("%d %f %s\n", ptr1->code, ptr1->salary, ptr1->name); 26 | 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Project 1/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | // Initialize random number generator 8 | srand(time(0)); 9 | 10 | // Generate random number between 1 and 100 11 | int randomNumber = (rand() % 100) + 1; 12 | int no_of_guesses = 0; 13 | int guessed_number; 14 | 15 | // Print the random number 16 | // printf("Random Number: %d\n", randomNumber); 17 | 18 | do 19 | { 20 | printf("Guess the number"); 21 | scanf("%d", &guessed_number); 22 | if(guessed_number>randomNumber){ 23 | printf("Lower number please!\n"); 24 | } 25 | else if(guessed_number 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | srand(time(0)); 8 | int player, computer = rand() % 3; 9 | /* 10 | 0 --> Snake 11 | 1 --> Water 12 | 2 --> Gun 13 | */ 14 | printf("Choose 0 for Snake, 1 for water and 2 for Gun \n"); 15 | scanf("%d", &player); 16 | printf("Computer chose %d\n", computer); 17 | 18 | if (player == 0 && computer == 0) 19 | { 20 | printf("Its a Draw!\n"); 21 | } 22 | else if (player == 0 && computer == 1) 23 | { 24 | printf("You Win!\n"); 25 | } 26 | else if (player == 0 && computer == 2) 27 | { 28 | printf("You Lose!\n"); 29 | } 30 | else if (player == 1 && computer == 0) 31 | { 32 | printf("You Lose!\n"); 33 | } 34 | else if (player == 1 && computer == 1) 35 | { 36 | printf("Its a Draw!\n"); 37 | } 38 | else if (player == 1 && computer == 2) 39 | { 40 | printf("You win!\n"); 41 | } 42 | else if (player == 2 && computer == 0) 43 | { 44 | printf("You win!\n"); 45 | } 46 | else if (player == 2 && computer == 1) 47 | { 48 | printf("You Lose!\n"); 49 | } 50 | else if (player == 2 && computer == 2) 51 | { 52 | printf("Its a Draw!\n"); 53 | } 54 | else{ 55 | printf("Something went wrong!"); 56 | } 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # The Ultimate C Programming Handbook 2 | 3 | Welcome to [**The Ultimate C Programming Course!**](https://www.youtube.com/watch?v=aZb0iu4uGwA) 4 | 5 | This course is designed to take you from a beginner to an advanced C programmer. The repository contains all the source code, projects, problem sets, and additional resources to supplement your learning. Refer to this [video](https://www.youtube.com/watch?v=aZb0iu4uGwA) to watch my C course. 6 | 7 | ## Table of Contents 8 | 9 | - [The Ultimate C Programming Handbook](#the-ultimate-c-programming-handbook) 10 | - [Table of Contents](#table-of-contents) 11 | - [Introduction](#introduction) 12 | - [Chapters](#chapters) 13 | - [Projects](#projects) 14 | - [Problem Sets](#problem-sets) 15 | - [Additional Resources](#additional-resources) 16 | - [How to Use This Repository](#how-to-use-this-repository) 17 | 18 | ## Introduction 19 | 20 | This repository is part of **The Ultimate C Programming Handbook**. The course aims to provide a comprehensive guide to learning C programming. 21 | 22 | ## Chapters 23 | 24 | The course is divided into several chapters, each focusing on different aspects of C programming: 25 | 26 | - **Chapter 1: Variables, Constants & Keywords** 27 | - Learn how to declare and initialize variables in C. 28 | - Understand different types of constants. 29 | - Familiarize yourself with reserved words in C. 30 | - **Chapter 2: Instructions and Operators** 31 | - Different types of instructions in C. 32 | - Arithmetic, relational, and logical operators. 33 | - Implicit and explicit type conversions. 34 | - **Chapter 3: Conditional Instructions** 35 | - Conditional execution of code using if-else statements. 36 | - Evaluate conditions using relational and logical operators. 37 | - **Chapter 4: Loop Control Instructions** 38 | - `while`, `do-while`, and `for` loops. 39 | - Control statements like `break` and `continue`. 40 | - **Chapter 5: Functions and Recursion** 41 | - Definition, declaration, and calling of functions. 42 | - Concept and usage of recursive functions. 43 | - **Chapter 6: Pointers** 44 | - Declaration and usage of pointers. 45 | - Operations involving pointers. 46 | - **Chapter 7: Arrays** 47 | - Single-dimensional and multi-dimensional arrays. 48 | - Accessing array elements using pointers. 49 | - **Chapter 8: Strings** 50 | - Initialization and manipulation of strings. 51 | - Standard library functions for strings. 52 | - **Chapter 9: Structures** 53 | - Definition and usage of structures. 54 | - Accessing structure members using pointers. 55 | - **Chapter 10: File I/O** 56 | - Reading from and writing to files. 57 | - Handling files using pointers. 58 | - **Chapter 11: Dynamic Memory Allocation** 59 | - `malloc()`, `calloc()`, `realloc()`, and `free()` functions. 60 | - Managing dynamically allocated memory. 61 | 62 | ## Projects 63 | 64 | - **Project 1: Number Guessing Game** 65 | - A fun game where the player tries to guess a randomly generated number. 66 | - **Project 2: Snake Water Gun Game** 67 | - A variation of the classic Rock-Paper-Scissors game. 68 | 69 | ## Problem Sets 70 | 71 | Each chapter contains problem sets to test your understanding and to practice coding. The problem sets include various challenges and exercises relevant to the chapter's content. 72 | 73 | ## Additional Resources 74 | 75 | - **[Download the Handbook (Scroll to the bottom of the page and navigate to Handbooks section)](https://www.codewithharry.com/notes)** 76 | - **[Download the Handwritten Notes](https://www.codewithharry.com/notes)** 77 | - **[Download the Cheatsheet](https://www.codewithharry.com/blogpost/c-cheatsheet/)** 78 | - **[Watch the Course Video](https://www.youtube.com/watch?v=aZb0iu4uGwA)** 79 | 80 | ## How to Use This Repository 81 | 82 | 1. **Clone the repository** to your local machine using: 83 | ```sh 84 | git clone https://github.com/CodeWithHarry/The-Ultimate-C-Programming-Course.git 85 | ``` 86 | 2. **Navigate through the chapters** to find the relevant lessons and code examples. 87 | 3. **Complete the problem sets** provided at the end of each chapter to solidify your understanding. 88 | 4. **Work on the projects** to apply your knowledge in real-world scenarios. 89 | 90 | --- 91 | 92 | Feel free to explore each chapter and work through the exercises to reinforce your learning. Happy coding! 93 | --------------------------------------------------------------------------------