-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathC_Programming.html
2088 lines (1930 loc) · 112 KB
/
C_Programming.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>C Programming Fundamentals - The Checklist</title>
<link href="https://api.fontshare.com/v2/css?f[]=nunito@400&f[]=bebas-neue@400&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"
integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w=="
crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="icon" href="./resources/Yellow Abstract Cooking Fire Free Logo.png" />
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="dark-mode.css">
<link rel="stylesheet" href="scrollbar.css">
</head>
<body>
<header>
<h1><a href="index.html">The Checklist</a></h1>
</header>
<nav>
<!-- may use this thing later -->
<!-- <ul>
<li><a href="checklist1.html">Checklist 1</a></li>
<li><a href="checklist2.html">Checklist 2</a></li>
</ul> -->
<!-- Add more checklists here -->
<!-- dark mode elements -->
<button class="dark-mode-toggle">
<i class="sun-icon"></i>
<i class="moon-icon"></i>
</button>
</nav>
<main>
<section class="checklist">
<h2>C Programming</h2>
<ul>
<h3>Chapter 1: Introduction to C Programming</h3>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<!-- section start -->
<div class="section-title">
<label class="section-title-label">
1.1 What is C Programming?
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<p>C is a versatile, high-level programming language that finds its application in a wide array
of software and application development, system programming, game development, web
programming, and more. It was in 1972 when Dennis M. Ritchie, working at the Bell Telephone
Laboratories, birthed this programming gem. Originally intended for programming the UNIX
operating system, C has grown to become one of the most widely used and influential
programming languages out there.</p>
</li>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<div class="section-title">
<label class="section-title-label">
1.2 History of C Programming
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<!-- Hidden content for the drop-down menu -->
<p>1. Genesis of C: C had its beginnings in the early 1970s at Bell Telephone Laboratories.
Dennis
Ritchie was the brain behind this creation.</p>
<p>2. B Language Predecessor: Before C, there was a programming language called B, which Ken
Thompson
developed. B was influenced by BCPL and was primarily used for the Multics operating system.
</p>
<p>3. Birth of C: Ritchie and his team wanted to enhance the capabilities and efficiency of B.
Their
efforts resulted in the birth of C, originally referred to as "NB" (New B). The work on C
started in 1972 and continued into 1973.</p>
<p>4. C for Unix: One of the primary goals of C was to provide a more efficient and versatile
language
for developing the Unix operating system. At the time, Unix was primarily written in
assembly
language, making it less portable across different computer architectures. C changed that.
</p>
<p>5. Portability: C's design for system programming allowed Unix to be easily ported to various
hardware platforms. This portability became one of the key factors in Unix's widespread
adoption.</p>
<p>6. The C Book: In 1973, Dennis Ritchie and Brian Kernighan published "The C Programming
Language,"
often referred to as "K&R C." This book became the definitive reference for the language and
played a significant role in popularizing C among programmers and institutions.</p>
</div>
</li>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<div class="section-title">
<label class="section-title-label">
1.3 Why Learn C Programming?
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<!-- Hidden content for the drop-down menu -->
<p>1. Fundamental Understanding: C is often considered the "mother" of all programming
languages. Learning C gives you a strong foundation in programming concepts, which can be
applied to other languages.</p>
<p>2. Efficiency: C is known for its efficiency. It allows you to write fast and optimized
code, making it suitable for system programming and applications where speed and resource
usage
are critical.</p>
<p>3. Portability: C code is highly portable across different platforms and operating systems.
Programs written in C can run on various hardware and software environments with minimal
modification.</p>
<p>4. Versatility: C is a versatile language used in a wide range of applications, from
embedded systems and game development to operating systems and high-performance computing.
</p>
<p>5. Low-Level Programming: Learning C allows you to work at a lower level of abstraction,
enabling you to understand how the computer's memory and processor work. This knowledge is
invaluable for system programming and debugging.</p>
<p>6. Problem-Solving Skills: C challenges you to manage memory, pointers, and other low-level
details. This hones your problem-solving skills and teaches you to write efficient code.</p>
<p>7. Career Opportunities: Proficiency in C programming is highly sought after by employers,
especially in industries like embedded systems, robotics, and game development.</p>
<p>8. Open Source and Legacy Code: Many open-source projects and legacy systems are written in
C. Learning C allows you to contribute to open-source software and maintain or modernize
older
codebases.</p>
<p>9. C++ and Embedded Systems: C is a precursor to C++, and learning C makes it easier to
transition to C++ for object-oriented programming. Additionally, C is commonly used in
embedded
systems, a growing field.</p>
<p>10. Security Knowledge: C requires careful memory management. Learning C can help you
understand and mitigate security vulnerabilities like buffer overflows and memory leaks.</p>
</div>
</li>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<div class="section-title">
<label class="section-title-label">
1.4 Setting up the Development Environment
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<P>1. Choose an Operating System:
You can develop C programs on various operating systems, including Windows, Linux, and
macOS.
Your choice depends on your preferences and the platform you intend to target.</P>
<P>2. Select a Text Editor or Integrated Development Environment (IDE):
You have several options for writing C code. You can choose a simple text editor or a
specialized C programming IDE. Some popular choices include:
- Text Editors:
- Notepad (Windows)
- Visual Studio Code
- Sublime Text
- Atom
- C-Specific IDEs:
- Code::Blocks
- Dev-C++
- CLion
- Eclipse C/C++ IDE
- Linux Development Tools:
- On Linux, you can use text editors like Vim or Emacs along with GCC (GNU Compiler
Collection)
for compiling.</P>
<P>3. Install a C Compiler:
You need a C compiler to compile and execute your C programs. The most commonly used C
compiler
is GCC (GNU Compiler Collection). Here's how to install GCC on different platforms:
- Linux: Most Linux distributions come with GCC pre-installed. If not, you can use your
package manager to install it. For example, on Ubuntu, run `sudo apt-get install
build-essential`.
- Windows: You can use MinGW (Minimalist GNU for Windows) to install GCC on Windows.
Download the MinGW installer and follow the installation instructions.
- macOS: On macOS, you can use the Xcode Command Line Tools, which include GCC. Open a
terminal and run `xcode-select --install` to install these tools.</P>
<P>4. Write and Compile Your First C Program:
Now that you have a text editor, C compiler, and development environment set up, you can
write
your first C program. Here's a simple "Hello, World!" example:
```c
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
```
Save this code in a file with a `.c` extension, such as `hello.c`.</P>
<P>5. Compile and Run Your Program:
- On the command line, navigate to the directory where you saved your C file.
- Compile the program using the C compiler. For GCC, you can use the following command: `gcc
-o hello hello.c` (This compiles `hello.c` and produces an executable named `hello`.)
- Run the program by entering `./hello` (on Linux/macOS) or `hello.exe` (on Windows).</P>
<P>6. Debugging Tools (Optional):
For debugging your C programs, consider using a debugger like GDB (GNU Debugger) or the
built-in debugging tools provided by your chosen IDE.
That's a basic overview of setting up a C programming development environment. As you gain
more experience, you can explore additional tools and libraries to enhance your development
process.</P>
</div>
</li> <br>
<h3>Chapter 2: Getting Started with C</h3>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<!-- section start -->
<div class="section-title">
<label class="section-title-label">
2.1 Your First C Program
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<!-- <img src="C Program Assets/first c program.png" alt=""> -->
<pre class="code-block">
#include< stdio.h >
int main(){
printf("Hello, World!");
return 0;
}
</pre>
<p>This program prints "Hello, World!" on the console.</p>
<p>#include< stdio.h> :- This line includes the stdio.h file, which contains the printf()
function.</p>
<p>int main() { :- This line defines the main() function, which is the entry point for all
C programs.</p>
<p>printf("Hello, World!"); :- This line prints the string "Hello, World!" to the console.</p>
<p>return 0; :- This line returns the value 0 to the operating system, indicating that the
program terminated successfully.</p>
}
</p>
</li>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<!-- section start -->
<div class="section-title">
<label class="section-title-label">
2.2 Basic Structure of a C Program
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<p>
There are 6 sections in a C.
</p>
<p>
// Documentation section
// Link section
// Definition section
// Global declaration section
// Main() function
// Subprograms
</p>
</li>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<!-- section start -->
<div class="section-title">
<label class="section-title-label">
2.3 Variables and Data Types
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<p>In C, there are different types of variables (defined with different keywords), for example:
</p>
<p>int - stores integers (whole numbers), without decimals, such as 123 or -123.</p>
<p>float - stores floating point numbers, with decimals, such as 19.99 or -19.99.</p>
<p>char - stores single characters, such as 'a' or 'B'.</p>
</li>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<!-- section start -->
<div class="section-title">
<label class="section-title-label">
2.4 Input and Output in C
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<p><u>scanf():</u></p>
<p>The scanf() method, in C, reads the value from the console as per the type specified. Syntax:
</p>
<p>"scanf(“%X”, &variableOfXType);" = where %X is the format specifier in C. It is a way to tell
the compiler what type of data is in a variable and & is the address operator in C</p>
<p><u>printf():</u></p>
<p>The printf() method, in C, prints the value passed as the parameter to it, on the console
screen. Syntax:</p>
<p>"printf(“%X”, variableOfXType);" = where %X is the format specifier in C. It is a way to tell
the compiler what type of data is in a variable and & is the address operator in C</p>
<p><b>Integer: </b></p>
<p>Input: <i>scanf("%d", &intVariable);</i></p>
<p>Output: <i>printf("%d", intVariable);</i></p>
<p><b>Float: </b></p>
<p>Input: <i>scanf("%f", &floatVariable);</i></p>
<p>Output: <i>printf("%f", folatVariable);</i></p>
<p><b>Character: </b></p>
<p>Input: <i>scanf("%c", &charVariable);</i></p>
<p>Output: <i>printf("%c", charVariable);</i></p>
</li>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<!-- section start -->
<div class="section-title">
<label class="section-title-label">
2.5 Comments and Formatting
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<p>Comments are used in C programming to provide information about the code, or to add notes for
yourself or other developers. Comments are ignored by the compiler, so they do not affect
the execution of the program.</p>
<p>There are two types of comments in C: single-line and multi-line.</p>
<p><u>Single-line comments</u> start with two forward slashes (//) and continue to the end of
the line.</p>
<p><u>Multi-line comments</u> start with a forward slash and an asterisk (/) and end with an
asterisk and a forward slash (/).</p>
<pre>/* This is a multi-line comment.
<p>It can span multiple lines. */</pre>
You can use comments to make your code more readable and understandable.</p>
<p><b><u>Formatting:</u></b></p>
<p>Formatting is also important in C programming. </p>
<p>Formatting your code can make it easier to read and understand. For example, you can use
spaces and tabs to indent your code, and you can use line breaks to separate different
sections of code.</p>
</li>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<!-- section start -->
<div class="section-title">
<label class="section-title-label">
2.6 Practice Problem
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<p>Question : </p>
<p>Write a C program to swap 2 numbers without using third variable</p><br>
<p>Solution : </p>
<pre class="code-block">
void main()
{
int a,b;
printf("Enter two numbers \n");
scanf("%d%d",&a,&b);
printf("Value of a before swapping is : %d\n",a);
printf("Value of b before swapping is : %d\n",b);
a=a+b;
b=a-b;
a=a-b;
printf("\nValue of a after swapping is : %d\n",a);
printf("Value of b after swapping is : %d\n",b);
}
</pre>
</li>
<br>
<h3>Chapter 3: Control Structures</h3>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<!-- section start -->
<div class="section-title">
<label class="section-title-label">
3.1 Conditional Statements (if, else if, switch)
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<p>Conditional statements in C are programming constructs that allow a program to execute
different blocks of code based on whether a certain condition is true or false. The most
common types of conditional statements in C are the if, else if, and else statements.</p>
<p><u>The syntax for the if statement is: </u></p>
<pre class="code-block">
if (condition) {
// block of code to execute if condition is true
}
</pre>
<p>The else if statement is used to execute a block of code if a certain condition is true, and
another block of code if the condition is false. The syntax for the <u>else if statement</u>
is:</p>
<pre class="code-block">
if (condition1) {
// block of code to execute if condition1 is true
} else if (condition2) {
// block of code to execute if condition2 is true
} else {
// block of code to execute if condition1 and condition2 are false
}
</pre>
<p><u>Switch Statement</u></p>
<p>The switch statement in C is a selection statement that allows you to transfer control to
different statements within the switch body depending on the value of the switch expression.
</p>
<pre class="code-block">
switch (expression) {
case value1:
// code to be executed;
break;
}
case value2:
// code to be executed;
break;
default:
// code to be executed if all cases are not matched;
</pre>
</li>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<!-- section start -->
<div class="section-title">
<label class="section-title-label">
3.2 Loops (for, while, do-while)
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<!-- for loop -->
<p><b><u>For Loop</u></b></p>
<p>The for loop is a repetition control structure that allows you to efficiently write a loop
that needs to execute a specific number of times.</p>
<p>The syntax of a for loop in C programming language is:</p>
<pre class="code-block">
for (initialization; condition; increment) {
// statement(s);
}
</pre>
<p>Here is the flow of control in a 'for' loop:</p>
<p>The initialization statement is executed once, before the loop begins.</p>
<p>The condition statement is evaluated. If it is true, the loop body is executed. If it is
false, the loop terminates.</p>
<p>The increment statement is executed after the loop body is executed, before the condition
statement is evaluated again.</p>
<p>The loop goes back to step 2 and repeats the process until the condition statement becomes
false.</p>
<p>Here is an <u>example of a for loop</u> that prints the numbers from 1 to 10:</p>
<pre class="code-block">
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}
</pre>
<p>This loop will print the numbers from 1 to 10, one per line.</p>
<!-- while loop in c -->
<p><b><u>While Loop</u></b></p>
<p>The while loop in C is a programming construct that allows us to execute a block of code
repeatedly, as long as a specified condition remains true. The syntax for a while loop in C
is:</p>
<pre class="code-block">
while (condition) {
// code block
}
</pre>
<p>The condition is a Boolean expression that is evaluated before each iteration of the loop. If
the expression evaluates to true, the code block is executed. If the expression evaluates to
false, the loop terminates.</p>
<p>Here is an example of a while loop in C that prints the numbers from 1 to 10:</p>
<pre class="code-block">
int i = 1;
while (i <= 10) {
printf("%d\n", i);
i++;
}
</pre>
<p>In this example, the variable i is initialized to 1. The loop then iterates as long as i is
less than or equal to 10. On each iteration, the value of i is printed to the console, and
then i is incremented by 1. The loop terminates when i is greater than 10.</p>
<!-- Do while loop in c -->
<p><b><u>Do - While Loop</u></b></p>
<p>The do-while loop is a programming construct that allows us to execute a block of code
repeatedly, until a specified condition is met. The syntax for a do-while loop in C is as
follows:
</p>
<pre class="code-block">
do {
// statements
} while (condition);
</pre>
<p>The do keyword is followed by a block of statements, which is executed repeatedly. The while
keyword is followed by a condition, which is evaluated after each iteration of the loop. If
the condition is true, the loop is executed again; otherwise, the loop terminates.</p>
<p>Here is an example of a do-while loop in C that prints the numbers from 1 to 10:</p>
<pre class="code-block">
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 10);
</pre>
<p>This loop will print the numbers from 1 to 10, inclusive</p>
<p>The do-while loop is a useful construct for situations where you need to execute a block of
code repeatedly, but you don't know how many times the loop should iterate. For example, you
could use a do-while loop to read input from a user until they enter a valid value.</p>
</li>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<!-- section start -->
<div class="section-title">
<label class="section-title-label">
3.3 Control Statements (break, continue)
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<p>The break and continue statements in C are used to control the flow of a loop. The break
statement causes the loop to terminate immediately, while the continue statement causes the
loop to skip the current iteration and continue with the next iteration.</p>
<p><b><u>Break Statement</u></b></p>
<p>Here is an example of how to use the break statement:</p>
<pre class="code-block">
int i;
for (i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("%d\n", i);
}
</pre>
<p>This code will print the numbers from 0 to 4, and then the break statement will cause the
loop to terminate.</p>
<p><b><u>Continue Statement</u></b></p>
<p>Here is an example of how to use the continue statement:</p>
<pre class="code-block">
int i;
for (i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
printf("%d\n", i);
}
</pre>
<p>This code will print the numbers from 0 to 9, skipping the number 5.</p>
<p>The break and continue statements can be used together to create more complex control flows.
For example, the following code will print the numbers from 0 to 9, but it will skip the
numbers 5 and 7:</p>
<img src="C Program Assets/continue example2.png" alt="">
<pre class="code-block">
int i;
for (i = 0; i < 10; i++) {
if (i == 5 || i == 7) {
continue;
}
printf("%d\n", i);
}
</pre>
<p>The break and continue statements are powerful tools that can be used to control the flow of
a program. However, they should be used with care, as they can easily lead to unexpected
results.</p>
</li>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<!-- section start -->
<div class="section-title">
<label class="section-title-label">
3.4 Conditional Expressions (Ternary Operator)
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<p>The ternary operator in C is a conditional operator that can be used to replace an if-else
statement.</p>
<code>The syntax for the ternary operator is-
? :</code>
<p>where ? is the conditional expression, :` is the separator, and expression1 and expression2
are the expressions to be evaluated if the condition is true or false, respectively.</p>
<p><b><u>Example:</u></b></p>
<p>For example, the following code uses the ternary operator to print "hello world" if the value
of x is greater than 10, and "goodbye world" otherwise:</p>
<pre class="code-block">
int x = 15;
printf("%s", x > 10 ? "hello world" : "goodbye world");
</pre>
<p>For example, the following code uses the ternary operator to assign the value of x to y if x
is greater than 10, and the value of 0 otherwise:</p>
<pre class="code-block">
int x = 15;
int y;
y = x > 10 ? x : 0;
</pre>
</li>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<!-- section start -->
<div class="section-title">
<label class="section-title-label">
3.5 Practice Problem
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<p>Question 1 : </p>
<p>Write a C program to check a number is odd or even </p><br>
<p>Solution : </p>
<pre class="code-block">
int main()
{
int num;
printf("Enter a number : ");
scanf("%d",&num);
if(num%2==0)
printf("Number is even \n");
else
printf("Number is odd \n");
return 0;
}
</pre>
<p>Question 2 : </p>
<p>Write a C program to find sum of series : 1+2+3....N</p><br>
<p>Solution : </p>
<pre class="code-block">
void main()
{
int i,N,sum=0;
printf("Enter the value of N\n");
scanf("%d",&N);
for(i=1;i<=N;i++)
{
sum=sum+i;
}
printf("Sum from 1 to %d is: %d\n",N,sum);
}
</pre>
<p>Question 3 : </p>
<p>Write a C program to find factorial of a number</p><br>
<p>Solution : </p>
<pre class="code-block">
void main()
{
int n,i,f=1;
printf("Enter a number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
if(n < 0)
printf("Factorial does not exist for negative number\n");
else
printf("Factorial is %d\n",f);
}
</pre>
<p>Question 4 : </p>
<p>Write a C program to find sum of digits of a number</p><br>
<p>Solution : </p>
<pre class="code-block">
void main()
{
int n,d,sum=0,copy;
printf("Enter a number\n");
scanf("%d",&n);
copy=n;
while(copy!=0)
{
d=copy%10;
sum=sum+d;
copy/=10;
}
printf("Sum of digits is : %d\n",sum);
}
</pre>
</li>
<br>
<h3>Chapter 4: Functions</h3>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<!-- section start -->
<div class="section-title">
<label class="section-title-label">
4.1 Defining and Calling Functions
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<p><b><u>What is Functions?</u></b></p>
<p>A function is a group of statements that together perform a task. Every C program has at
least one function, which is main(), and all the most trivial programs can define additional
functions.</p>
<p>A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function.</p>
<!-- Defining a function -->
<p><b><u>Defining a Function:</u></b></p>
<p>The general form of a function definition in C programming language is as follows −</p>
<pre class="code-block">
return_type function_name( parameter list ) {
//body of the function
}
</pre>
<p>A function definition in C programming consists of a function header and a function body.
Here are all the parts of a function −</p>
<pre class="code-block">
• Return Type - A function may return a value.
The return_type is the data type of the value the function returns.
Some functions perform the desired operations without returning a value.
In this case, thereturn_type is the keyword void.
• Function Name - This is the actual name of the function.
The function name and the parameter list together constitute the function signature.
Parameters - A parameter is like a placeholder.
When a function is invoked, you pass a value to the parameter.
This value is referred to as actual parameter or argument.
The parameter list refers to the type, order, and number of the parameters of a function.
Parameters are optional; that is, a function may contain no parameters.
• Function Body - The function body contains a collection of statements
that define what the function does.
</pre>
<p><b>Example:</b></p>
<p>Given below is the source code for a function called max(). This function takes two
parameters num1 and num2 and returns the maximum value between the two −</p>
<pre class="code-block">
/* function returning the max between two numbers */ int max(int num1, int num2) {
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
</pre>
<!-- calling a function -->
<p><b><u>Calling a Function:</u></b></p>
<p>While creating a C function, you give a definition of what the function has to do. To use a
function, you will have to call that function to perform the defined task.</p>
<p>When a program calls a function, the program control is transferred to the called function. A
called function performs a defined task and when its return statement is executed or when
its function-ending closing brace is reached, it returns the program control back to the
main program.</p>
<p>To call a function, you simply need to pass the required parameters along with the function
name, and if the function returns a value, then you can store the returned value. For
example −</p>
<pre class="code-block">
/* function declaration */ int max(int num1, int num2);
int main() {
/* local variable definition */
int a = 100;
int b = 200;
int ret;
/* calling a function to get max value */ ret = max(a, b);
printf("Max value is : %d\n", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2) {
/* local variable declaration */
int result;
if (num1> num2)
result = num1;
else
result = num2;
return result;
}
</pre>
<p><b>Output:</b> Max value is : 200</p>
</li>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<!-- section start -->
<div class="section-title">
<label class="section-title-label">
4.2 Function Parameters and Return Values
<span class="dropdown-arrow"></span>
</label>
</div>
</div>
<div class="hidden-text">
<p><b><u>Parameters in C functions</u></b></p>
<p><u>Pass by Value.</u> Pass by Value, means that a copy of the data is made and stored by way
of the
name of the parameter. Any changes to the parameter have NO affect on data in the calling
function.</p>
<p><u>Pass by Reference.</u> A reference parameter "refers" to the original data in the calling
function</p>
<p><b><u>Return Values in C functions</u></b></p>
<p>The return statement may or may not return a value depending upon the return type of the
function. For example, int returns an integer value, void returns nothing, etc.</p>
</li>
<li class="checklist-item">
<div class="section">
<div class="section-box">
<label class="section-box-label">
<input type="checkbox" class="checkbox" data-checklist="web-app">
<span class="checkmark"></span>
</label>
</div>
<!-- section start -->
<div class="section-title">
<label class="section-title-label">
4.3 Scope and Lifetime of Variables
<span class="dropdown-arrow"></span>
</label>
</div>
</div>