-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpro.html
3486 lines (3320 loc) · 174 KB
/
cpro.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>
<!-- ad sense tag-->
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-1733919132650092"
crossorigin="anonymous"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-FBJL1SB5QM"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'G-FBJL1SB5QM');
</script>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="title" content="Coding-nation">
<meta name="description"
content="CodingNation is a comprehensive code tutorial website, providing beginner to advanced coding tutorials, coding exercises, and coding challenges to help you learn to code and build your skills.">
<meta name="keywords"
content="Coding-nation, CodeNation, HTML tutorials, python tutorials, css tutorials, c tutorials,React js tutorials, javascript tutorials,java tutorials, php tutorials, code editor, online code editor, Codebot,chatbot,quizzes">
<meta name="robots" content="index, follow">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="language" content="English">
<meta name="author" content="coding-nation.com">
<title>C Language Course | coding-nation</title>
<link rel="icon" type="image/png" sizes="32x32" href="../images/favicon.png">
<!-- Latest compiled and minified CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet" />
<!-- Latest compiled JavaScript -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css" rel="stylesheet" />
<link rel="stylesheet" href="../Styles/Notes.css" />
<script>
function changeTitle(newTitle) {
document.title = newTitle;
}
</script>
</head>
<body>
<!--for whole page-->
<div class="contain">
<!--navigation bar-->
<header class="fixed-navbar">
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<div class="container-fluid">
<a class="navbar-brand" href="../index.html">CodingNation</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav ms-auto" id="navbarNavLinks">
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown"
aria-controls="navbarNav" aria-expanded="false"
aria-label="Toggle navigation">Courses</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item" href="../Python.html">Python</a>
</li>
<li>
<a class="dropdown-item" href="../React.html">React JS</a>
</li>
<li>
<a class="dropdown-item" href="../html.html">HTML</a>
</li>
<li><a class="dropdown-item" href="../css.html">CSS</a></li>
<li>
<a class="dropdown-item" href="../jascri.html">JavaScript</a>
</li>
<li><a class="dropdown-item" href="../cpro.html">C</a></li>
<li>
<a class="dropdown-item" href="../java.html">Java</a>
</li>
<li><a class="dropdown-item" href="../php.html">PHP</a></li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button"
data-bs-toggle="dropdown">Practice</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item" href="../HTMLQuiz.html" target="_blank">HTML
Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../CSSQuiz.html" target="_blank">CSS
Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../JSQuiz.html" target="_blank">JavaScript
Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../CQuiz.html" target="_blank">C Quiz</a>
</li>
<li>
<a class="dropdown-item" href="../JAVAQuiz.html" target="_blank">Java
Quiz</a>
</li>
</ul>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" role="button"
data-bs-toggle="dropdown">Exercises</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item" href="../HTMLExer.html" target="_blank">HTML Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../CSSExer.html" target="_blank">CSS Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../JsExer.html" target="_blank">JavaScript
Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../CExer.html" target="_blank">C Exercise</a>
</li>
<li>
<a class="dropdown-item" href="../JAVAExer.html" target="_blank">Java Exercise</a>
</li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="../codebot.html" target="_blank">CodeBot</a>
</li>
<li class="nav-item">
<a class="nav-link" href="../Editor.html" target="_blank">Editor</a>
</li>
<li class="nav-item">
<a class="nav-link" href="../About.html">About</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<!--sidebar-->
<button class="topics" onclick="toggleSidebar()">Topics▼</button>
<aside class="fixed-sidebar fixed" id="myNav">
<div class="menu">
<h2>Introduction</h2>
<button class="single" data-index="0" onclick="changeTitle('C Overview | CodingNation')">C
Overview</button>
<button class="single" data-index="1"
onclick="changeTitle('Getting Started with C | CodingNation')">Getting Started with C</button>
<button class="single" data-index="2"
onclick="changeTitle('Basic Structure & Syntax | CodingNation')">Basic Structure & Syntax</button>
<button class="single" data-index="3" onclick="changeTitle('C Comments | CodingNation')">C
Comments</button>
<button class="single" data-index="4" onclick="changeTitle('C Variables | CodingNation')">C
Variables</button>
<h2 style="font-weight: 500;">C Data Types,<br> Operators & User I/O</h2>
<button class="single" data-index="5" onclick="changeTitle('C Data Types & Constants | CodingNation')">C
Data Types & Constants</button>
<button class="single" data-index="6" onclick="changeTitle('C Operators | CodingNation')">C
Operators</button>
<button class="single" data-index="7"
onclick="changeTitle('Format Specifiers & Escape Sequences | CodingNation')">Format Specifiers &
<br>Escape Sequences</button>
<button class="single" data-index="8" onclick="changeTitle('User Input/Output | CodingNation')">User
Input/Output</button>
<h2>Conditional Statements</h2>
<button class="single" data-index="9" onclick="changeTitle('C if...else statements | CodingNation')">C
if...else statements</button>
<button class="single" data-index="10"
onclick="changeTitle('Switch Case Statements | CodingNation')">Switch Case Statements</button>
<h2>Iteration Statements</h2>
<button class="single" data-index="11" onclick="changeTitle('C Loops | CodingNation')">C Loops</button>
<button class="single" data-index="12" onclick="changeTitle('while Loop | CodingNation')">while
Loop</button>
<button class="single" data-index="13" onclick="changeTitle('do-while Loop | CodingNation')">do-while
Loop</button>
<button class="single" data-index="14" onclick="changeTitle('for Loop | CodingNation')">for
Loop</button>
<button class="single" data-index="15" onclick="changeTitle('CC Break/Continue | CodingNation')">C
Break/Continue</button>
<h2>Arrays</h2>
<button class="single" data-index="16" onclick="changeTitle('Array Basics | CodingNation')">Array
Basics</button>
<button class="single" data-index="17" onclick="changeTitle('Array Operations | CodingNation')">Array
Operations</button>
<h2>Strings</h2>
<button class="single" data-index="18" onclick="changeTitle('String Basics | CodingNation')">String
Basics</button>
<button class="single" data-index="19" onclick="changeTitle('String Functions | CodingNation')">String
Functions</button>
<h2>C Functions</h2>
<button class="single" data-index="20" onclick="changeTitle('Function Basics | CodingNation')">Function
Basics</button>
<button class="single" data-index="21"
onclick="changeTitle('Function Parameters | CodingNation')">Function Parameters</button>
<button class="single" data-index="22"
onclick="changeTitle('Functions Declaration | CodingNation')">Functions Declaration</button>
<button class="single" data-index="23"
onclick="changeTitle('Recursive Functions | CodingNation')">Recursive Functions</button>
<h2>C Pointers</h2>
<button class="single" data-index="24" onclick="changeTitle('C Pointers | CodingNation')">C
Pointers</button>
<button class="single" data-index="25"
onclick="changeTitle('Operations on Pointers | CodingNation')">Operations on Pointers</button>
<button class="single" data-index="26" onclick="changeTitle('C VOID Pointer | CodingNation')">C VOID
Pointer</button>
<button class="single" data-index="27" onclick="changeTitle('C NULL Pointer | CodingNation')">C NULL
Pointer</button>
<button class="single" data-index="28" onclick="changeTitle('Dangling Pointer | CodingNation')">Dangling
Pointer</button>
<button class="single" data-index="29" onclick="changeTitle('Wild Pointer | CodingNation')">Wild
Pointer</button>
<h2>C Memory Management</h2>
<button class="single" data-index="30" onclick="changeTitle('C Static Variables | CodingNation')">C
Static Variables</button>
<button class="single" data-index="31" onclick="changeTitle('C Memory Layout | CodingNation')">C Memory
Layout</button>
<button class="single" data-index="32" onclick="changeTitle('C Memory Allocation | CodingNation')">C
Memory Allocation</button>
<h2>C Structures & Unions</h2>
<button class="single" data-index="33" onclick="changeTitle('C Structures | CodingNation')">C
Structures</button>
<button class="single" data-index="34" onclick="changeTitle('C Unions | CodingNation')">C
Unions</button>
<button class="single" data-index="35" onclick="changeTitle('C Typedef | CodingNation')">C
Typedef</button>
<h2>C File Handling</h2>
<button class="single" data-index="36" onclick="changeTitle('File Handling Basics | CodingNation')">File
Handling Basics</button>
<button class="single" data-index="37"
onclick="changeTitle('Operations on Files | CodingNation')">Operations on Files</button>
<button class="single" data-index="38" onclick="changeTitle('Files I/O | CodingNation')">Files
I/O</button>
</div>
</aside>
<!--for content side-->
<main class="main-content">
<div class="content">
<!--previous and next buttons-->
<button id="previous">Previous</button>
<button id="next">Next </button>
<!--Notes-->
<div class="notes" id="div1 ">
<h2>C Overview</h2><br>
<h3>What is C?</h3><br>
<li>Since the late 19th century, C has been a popular programming language for general-purpose use.
</li>
<li>C language was developed by Dennis M. Ritchie at bell laboratory in early 1970s</li>
<li>Its applications are very diverse. It ranges from developing operating systems to databases and
all.It is system programming language used to do low level programming (e.g., driver or kernel)
</li>
<li>Even if it’s old, it is still a very popular programming language.</li>
<li>As the whole UNIX operating system was written in C, it has a strong association with the
operating system.</li>
<li>C has also been used widely while creating iOS and Android kernels.</li>
<li>MySQL database is written using C.</li>
<li>Ruby and Pearl are mostly written using C.</li>
<li>Most part of Apache and NGINX is written using C.</li>
<li>Embedded Systems are created using C</li><br><br>
<h3>Why should we learn C/ Features of C?</h3><br>
<li>As mentioned above, it is one of the most popular programming languages in the world.</li>
<li>Learning any other popular programming language such as Python or C++ becomes a cakewalk already
if you know C.</li>
<li>C is a flexible language and that gets proven by the fact that it can be used in a variety of
applications as well as technologies.</li>
<li>C is very fast when compared to other programming languages be it Java or Python.</li>
<li>C takes only significant CPU time for interpretation. That is why a lot of Python libraries such
as NumPy, pandas, Scikit-learn, etc. are built using C.</li>
<li>Being close to Machine language, some of its functions include direct access to machine-level
hardware APIs.</li>
<li>It is a structural language (Follows a specific Structure) /Compiled language this point should
be added.</li>
<li>It is procedural programming language (POP) Procedurall Programming is the use of code in a
step-wise procedure to develop applications.</li><br><br>
<h3>How is it different from C++?</h3><br>
<li>The syntax of C++ is almost identical to that of C, as C++ was developed as an extension of C.
</li>
<li>In contrast to C, C++ supports classes and objects, while C does not.</li>
<li>C gives most of the control to the hand of users. Things like memory allocation and manipulation
are totally in the hands of the programmer. Being a flexible language, it provides more access
to the programmer because of which it is more efficient.</li>
<li>.C is POP(procedure oriented programming) whereas c++ is OOP(Object oriented programming)</li>
<br><br>
</div>
<div class="notes" id="div2" style="display: none;">
<h2>Getting Started with C</h2><br>
<h3>Requirements before you start</h3><br>
<li>To start using C, you need two things:</li>
<li>A text editor, like Notepad, or an IDE, like VSCode to act as a platform for you to write C code
</li>
<li>A compiler, like GCC to translate the C code you have written which is a high-level language
into a low-level language that the computer will understand.</li><br><br>
<h3>What is an IDE?</h3><br>
<li>IDE stands for an Integrated Development Environment.</li>
<li>It is nothing more than an enhanced version of a text editor that helps you write more efficient
and nicer code.</li>
<li>It helps to differentiate different parts of your codes with different colors and notifies you
if you are missing some semicolon or bracket at some place by highlighting that area.</li>
<li>A lot of IDEs are available, such as DEVC++ or Code Blocks, but we will prefer using VS Code for
this tutorial series.</li><br><br>
<h3>Installing VSCode</h3><br>
<li>Visit <a
href="https://code.visualstudio.com/download">https://code.visualstudio.com/download</a>
</li>
<li>Click on the download option as per your operating system.</li>
<li>After the download is completed, open the setup and run it by saving VS Code in the default
location without changing any settings.</li>
<li>You will need to click the next button again and again until the installation process begins.
</li><br><br>
<h3>What is a Compiler?</h3><br>
<li>A compiler is used to run the program of a certain language which is generally high-level by
converting the code into a language that is low-level that our computer could understand.</li>
<li>There are a lot of compilers available, but we will proceed with teaching you to use MinGW for
this course because it will fulfill all of our requirements, and also it is recommended by
Microsoft itself.</li><br><br>
<h3>Setting up the compiler</h3><br>
<li>Visit <a
href="https://code.visualstudio.com/docs/languages/cpp">https://code.visualstudio.com/docs/languages/cpp</a>
</li>
<li>Select C++ from the sidebar.</li>
<li>Choose “GCC via Mingw-w64 on Windows” from the options shown there.</li>
<li>Select the install sourceforge option.</li>
<li>After the downloading gets completed, run the setup and choose all the default options as we did
while installing VS Code.</li><br>
<br>
<h3>Setting Path for Compiler</h3><br>
<li>Go to the C directory. Navigate into the Program Files. Then, open MinGW-64. Open MinGW-32. And
then the bin folder. After reaching the bin, save the path or URL to the bin.</li>
<li>Then go to the properties of ‘This PC’.</li>
<li>Select ‘Advance System Settings’.</li>
<li>Select the ‘Environment Variable’ option.</li>
<li>Add the copied path to the Environment Variable.</li>
<li>And now, you can visit your IDE and run your C programs on it. The configuration part is done.
</li><br><br>
</div>
<div class="notes" id="div3" style="display: none;">
<h2>Basic Structure & Syntax</h2><br>
<p>Programming in C involves following a basic structure throughout. Here’s what it can be broken
down to.</p><br>
<li>Pre-processor commands</li>
<li>Functions</li>
<li>Variables</li>
<li>Statements</li>
<li>Expressions</li>
<li>Comments</li><br>
<h3>Pre-processor commands</h3><br>
<p>Pre-processor commands are commands which tell our program that before its execution, it must
include the file name mentioned in it because we are using some of the commands or codes from
this file.
<br>
They add functionalities to a program.
<br>
One example could be,
</p><br>
<pre class="highlight">
<code class="language-html">
#include <math.h>
</code>
</pre>
<br>
<br>
<p>We include math.h to be able to use some special functions like power and absolute. #include
(<filename.h>) is how we include them into our programs.
<br>
Detailed explanations of everything else in the structure willfollow in the later part of the
tutorial.
</p><br><br>
<h3>Header files:</h3><br>
<li> Collection of predefined/built in functions developed.</li>
<li> It is always declares on heading side of program hence it is called header file.</li>
<li>It is identified with the extension(.h)</li>
<li>It gets installed while installing IDE(integrated development environment)</li>
<li> It stores functions as per their categories hence they are called library</li><br><br>
<h3>Syntax</h3><br>
<p>An example below shows how a basic C program is written.</p><br>
<pre class="highlight">
<code class="language-js" style="font-size: 16px;">
Declaration of header file //name of the header files
main() /*it is called main function which stores the execution of program*/
{ //start of the program
//program statements
} //end of the program
</code>
</pre>
<br>
<br>
<li>Here, the first line is a pre-processor command including a header file stdio.h.</li>
<li>C ignores empty lines and spaces.</li>
<li>There is a main() function then, which should always be there.</li><br><br>
<p>A C program is made up of different tokens combined. These tokens include:</p><br>
<li>Keywords</li>
<li>Identifiers</li>
<li>Constants</li>
<li>String Literal</li>
<li>Symbols</li><br><br>
<h3>Keywords</h3><br>
<p>Keywords are reserved words that can not be used elsewhere in the program for naming a variable
or a function. They have a specific function or task and they are solely used for that. Their
functionalities are pre-defined.</p><br>
<p>One such example of a keyword could be return which is used to build return statements for
functions. Other examples are auto, if, default, etc.</p><br>
<p>Whenever we write any keyword in IDE their colour slightly changes and it looks different from
other variables or functions for example in turbo c all keywords are turns into white colour .
</p><br><br>
<h3>Identifiers</h3><br>
<p>Identifiers are names given to variables or functions to differentiate them from one another.
Their definitions are solely based on our choice but there are a few rules that we have to
follow while naming identifiers. One such rule says that the name can not contain special
symbols such as @, -, *, <, etc. <br>
C is a case-sensitive language so an identifier containing a capital letter and another one
containing a small letter in the same place will be different. For example, the three words:
Code, code, and cOde can be used as three different identifiers.</p><br>
<p>Rules for naming identifier-</p><br>
<li> One should not name any identifier starting with numeric value or symbol. It should start only
with underscore or alphabet.</li>
<li>They should not contain space</li>
<li>Giving logical names is recommended as per our program</li><br>
<br>
<h3>Constants</h3><br>
<p>Constants are very similar to a variable and they can also be of any data type. The only
difference between a constant and a variable is that a constant’s value never changes. We will
see constants in more detail in the upcoming tutorial.</p><br>
<h3>String Literal</h3><br>
<p>String literals or string constants are a sequence of characters enclosed in double quotation
marks. For example, “This is a string literal!” is a string literal. C method printf() utilizes
the same to format the output.</p><br><br>
</div>
<div class="notes" id="div4" style="display: none;">
<h2>C Comments</h2><br>
<p>Comments can be used to insert any informative piece which a programmer does not wish to be
executed. It could be either to explain a piece of code or to make it more readable. In
addition, it can be used to prevent the execution of alternative code when the process of
debugging is done.
<br>
Comments can be singled-lined or multi-lined.
</p><br><br>
<h3>Single Line Comments</h3><br>
<li>Single-line comments start with two forward slashes (//).</li>
<li>Any information after the slashes // lying on the same line would be ignored (will not be
executed).</li><br><br>
<p>An example of how we use a single-line comment</p><br>
<pre class="highlight">
<code class="language-js">
#include <stdio.h>
int main()
{
//This is a single line comment
printf("Hello World!");
return 0;
}
</code>
</pre>
<br>
<br>
<h3>Multi-line comments</h3><br>
<li>A multi-line comment starts with /* and ends with */.</li>
<li>Any information between /* and */ will be ignored by the compiler.</li><br><br>
<p>An example of how we use a multi-line comment</p><br>
<pre class="highlight">
<code class="language-js">
#include <stdio.h>
int main()
{
/* This is a multi-line comment */
printf("Hello World!");
return 0;
}
</code>
</pre>
<br>
<br>
</div>
<div class="notes" id="div5" style="display: none;">
<h2>C Variables</h2><br>
<p>Variables are containers for storing data values.</p><br><br>
<p>In C, there are different types of variables.
<br>
For example
</p>
<li>an integer variable defined with the keyword int stores integers (whole numbers), without
decimals, such as 91 or -13.</li>
<li>a floating point variable defined with keyword float stores floating point numbers, with
decimals, such as 99.98 or -1.23.</li>
<li>a character variable defined with the keyword char stores single characters, such as 'A' or 'z'.
Char values are bound to be surrounded by single quotes.</li><br><br>
<h3>Declaration</h3><br>
<p>We cannot declare a variable without specifying its data type. The data type of a variable
depends on what we want to store in the variable and how much space we want it to hold. The
syntax for declaring a variable is simple:</p><br>
<pre class="highlight">
<code class="language-js">
data_type variable_name;
</code>
</pre>
<br>
<br>
<p>OR</p><br>
<pre class="highlight">
<code class="language-js">
data_type variable_name = value;
</code>
</pre>
<br>
<br>
<h3>Naming a variable</h3><br>
<p>There is no limit to what we can call a variable. Yet there are specific rules we must follow
while naming a variable:</p><br>
<li>A variable name can only contain alphabets, digits, and underscores(_).</li>
<li>A variable cannot start with a digit.</li>
<li>A variable cannot include any white space in its name.</li>
<li>The name should not be a reserved keyword or any special character.</li><br><br>
<p>A variable, as its name is defined, can be altered, or its value can be changed, but the same is
not true for its type. If a variable is of integer type, then it will only store an integer
value through a program. We cannot assign a character type value to an integer variable. We can
not even store a decimal value into an integer variable.</p><br><br>
</div>
<div class="notes" id="div6" style="display: none;">
<h2>C Data Types & Constants</h2><br>
<p>As explained previously, a variable in C must be a specified data type, and you must use a format
specifier inside the printf function to display the value present in the variable
<br>
The data type specifies the size and type of information the variable will store.
</p><br>
<h3>Datatype</h3><br>
<p>It is the type of value which the variable holds.
<br>
Here, we will focus on the most basic ones:
</p><br>
<br>
<table>
<tr>
<th>Data Type</th>
<th>Size</th>
<th>Description</th>
<th>Format Specifier</th>
</tr>
<tr>
<td>int</td>
<td>2 or 4 bytes</td>
<td>Stores whole numbers, without decimals</td>
<td>%d or %i</td>
</tr>
<tr>
<td>Float</td>
<td>4 bytes</td>
<td>Stores fractional numbers, containing one or more decimals. Sufficient for storing 7
decimal digits</td>
<td>%f</td>
</tr>
<tr>
<td>double</td>
<td>8 bytes</td>
<td>Stores fractional numbers, containing one or more decimals. Sufficient for storing 15
decimal digits</td>
<td>%lf</td>
</tr>
<tr>
<td>char</td>
<td>1 byte</td>
<td>Stores a single character/letter/number, or ASCII values</td>
<td>%c</td>
</tr>
</table>
<br>
<p>Below are some sub datatypes</p><br>
<br>
<table>
<tr>
<th>Data type</th>
<th>Size</th>
<th>Description</th>
<th>Format Specifier</th>
</tr>
<tr>
<td>Short int</td>
<td>2 bytes</td>
<td>Stores whole numbers, without decimals</td>
<td>%sd</td>
</tr>
<tr>
<td>Long int</td>
<td>4 bytes</td>
<td>Stores fractional numbers, containing one or more decimals. Sufficient for storing 7
decimal digits</td>
<td>%ld</td>
</tr>
<tr>
<td>unsigned short int</td>
<td>2 bytes</td>
<td>Stores fractional numbers, containing one or more decimals. Sufficient for storing 15
decimal digits</td>
<td>%usd</td>
</tr>
<tr>
<td>Unsigned long int</td>
<td>4 bytes</td>
<td>Stores a single character/letter/number, or ASCII values</td>
<td>%uld</td>
</tr>
</table><br>
<p>Here’s how we make use of the data type of a variable to print it.</p><br>
<pre class="highlight">
<code class="language-js">
#include <stdio.h>
int main()
{
// Creating variables having different data types
int integer = 26;
float floating = 39.32;
char character = 'A';
// Printing variables with the help of their respective format specifiers
printf("%d\n", integer);
printf("%f\n", floating);
printf("%c\n", character);
}
</code>
</pre>
<br>
<br>
<p>Output:</p><br>
<p>26
<br>
39.320000
<br>
A
</p><br>
<br>
<h3>C Constants</h3><br>
<li>When you don't want the variables you declare to get modified intentionally or mistakenly in the
later part of your program by you or others, you use the const keyword (this will declare the
variable as "constant", which means unchangeable and read-only).</li>
<li>You should always declare the variable as constant when you have values that are unlikely to
change, like any mathematical constant as PI.</li>
<li>When you declare a constant variable, it must be assigned a value.</li><br>
<p>Here’s an example of how we declare a constant.</p><br>
<pre class="highlight">
<code class="language-js">
#include <stdio.h>
int main()
{
const int MOD = 10000007;
}
</code>
</pre><br>
<br>
</div>
<div class="notes" id="div7" style="display: none;">
<h2>C Operators</h2><br>
<p>Special symbols that are used to perform actions or operations are known as operators. They could
be both unary or binary.
<br>
For example, the symbol asterisk (*) is used to perform multiplication in C so it is an operator
and it is a binary operator.
<br>
This section covers all types of operators.
</p><br>
<br>
<h3>Arithmetic Operators</h3><br>
<p>Arithmetic operators are used to perform mathematical operations such as addition, subtraction,
etc. A few of the simple arithmetic operators are</p><br>
<table>
<tr>
<th>Operator</th>
<th>Description</th>
</tr>
<tr>
<td>+</td>
<td>Addition</td>
</tr>
<tr>
<td>-</td>
<td>Subtraction</td>
</tr>
<tr>
<td>*</td>
<td>Multiplication</td>
</tr>
<tr>
<td>/</td>
<td>Division</td>
</tr>
<tr>
<td>%</td>
<td>Modulus</td>
</tr>
</table><br>
<p>We all must already know their purpose and how they are used in simple mathematics. Their purpose
and functionality are the same.
<br>
Modulus(%) operator- this operator returns the remainder of two operands when they are been
divided
</p><br>
<pre class="highlight">
<code class="language-js">
#include<stdio>
int main()
{
int x;
x=5%2;
printf("remainder is %d",x);
}
</code>
</pre><br>
<br>
<p>Let’s see their implementation in C.</p><br>
<pre class="highlight">
<code class="language-js">
#include <stdio.h>
int main()
{
int a = 2;
int b = 3;
printf("a + b = %d\n", a + b);
}
</code>
</pre>
<br><br>
<p>Output:</p><br>
<pre class="highlight">
<code class="language-js">
a + b = 5
</code>
</pre>
<br>
<br>
<h3>Relational Operators</h3><br>
<p>Relational operators are used for the comparison between two or more numbers or even expressions
in cases. Same as Java, C also has six relational operators and their return value is of a
Boolean type that is, either True or False (1 or 0).</p><br>
<table>
<tr>
<th>Operator</th>
<th>Description</th>
</tr>
<tr>
<td>></td>
<td>Greater than</td>
</tr>
<tr>
<td>
< </td>
<td>Less than</td>
</tr>
<tr>
<td>>=</td>
<td>Greater than or equal to</td>
</tr>
<tr>
<td>
<=< /td>
<td>Less than or equal to</td>
</tr>
<tr>
<td>==</td>
<td>Is equal to</td>
</tr>
<tr>
<td>!=</td>
<td>Is not equal to</td>
</tr>
</table><br>
<p>Let’s see their implementation in C.</p><br>
<pre class="highlight">
<code class="language-js">
#include <stdio.h>
int main()
{
int a = 2;
int b = 3;
printf("a == b = %d\n", a == b);
}
</code>
</pre><br>
<br>
<p>output</p><br>
<pre class="highlight">
<code class="language-js">
a == b = 0
</code>
</pre><br>
<br>
<p>The output is 0, since a and b are not equal.</p><br>
<br>
<h3>Logical Operators</h3><br>
<p>There are three logical operators i.e. AND, OR, and NOT. They can be used to compare Boolean
values but are mostly used to compare conditions to see whether they are satisfying or not.</p>
<br>
<li>AND: it returns true when both operators are true or 1.</li>
<li>OR: it returns true when either operator is true or 1.</li>
<li>NOT: it is used to reverse the logical state of the operand.</li>
<br>
<table>
<tr>
<th>Operator</th>
<th>Description</th>
</tr>
<tr>
<td>&&</td>
<td>AND Operator</td>
</tr>
<tr>
<td>||</td>
<td>OR Operator</td>
</tr>
<tr>
<td>!</td>
<td>NOT Operator</td>
</tr>
</table><br>
<p>Let’s see their implementation in C.</p><br>
<pre class="highlight">
<code class="language-js">
#include <stdio.h>
int main()
{
int a = 1;
int b = 0;
printf("a or b = %d\n", a || b);
}
</code>
</pre><br><br>
<pre class="highlight">
<code class="language-js">
a or b = 1
</code>
</pre><br>
<br>
<p>The output is 1, since either a or b is not equal to zero.</p>
<br>
<h3>Bitwise Operators</h3><br>
<p>A bitwise operator is used to performing operations at the bit level. To obtain the results, they
convert our input values into binary format and then process them using whatever operator they
are being used with.</p><br>
<table>
<tr>
<th>Operator</th>
<th>Description</th>
</tr>
<tr>
<td>&</td>
<td>Bitwise AND</td>
</tr>
<tr>
<td>|</td>
<td>Bitwise OR</td>
</tr>
<tr>
<td>^</td>
<td>Bitwise XOR</td>
</tr>
<tr>
<td>~</td>
<td>Bitwise Complement</td>
</tr>
<tr>
<td>>></td>
<td>Shift Right Operator</td>
</tr>
<tr>
<td>
<<< /td>
<td>Shift Left Operator</td>
</tr>
</table><br>
<p>Let’s see their implementation in C.</p><br>
<pre class="highlight">
<code class="language-js">
#include <stdio.h>
int main()
{
int a = 2; //10
int b = 3; //11
printf("a xor b = %d\n", a ^ b);
}
</code>
</pre><br>
<br>
<p>Output</p><br>
<pre class="highlight">
<code class="language-js">
a or b = 1
</code>
</pre><br>
<br>
<p>The output is 1, since a xor b is 01 in binary, which is 1 in decimal.</p><br>
<h3>Assignment Operators</h3><br>
<p>Assignment operators are used to assign values. We will use them in almost every program we
develop.</p><br>
<pre class="highlight">
<code class="language-js">
int a = 0;
int b = 1;
</code>
</pre><br>
<br>
<p>Equal to (=) is the assignment operator here. It is assigning 0 to a and 1 to b in the above
example.</p><br>
<table>
<tr>
<th>Operator</th>
<th>Description</th>
</tr>
<tr>
<td>=</td>
<td>It assigns the right side operand value to the left side operand.</td>
</tr>
<tr>
<td>+=</td>
<td>It adds the right operand to the left operand and assigns the result to the left