C Programming Fundamentals
C Programming Fundamentals
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
Output (Print Text)
To output values or print text in C, you can use
the printf() function:
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
New Lines
To insert a new line, you can use the \n character:
#include <stdio.h>
int main() {
printf("Hello World!\n");
printf("I am learning C.");
return 0;
}
Comments in C
Comments can be used to explain code, and to make it more
readable. It can also be used to prevent execution when testing
alternative code.
Comments can be singled-lined or multi-lined.
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by the
compiler (will not be executed).
// This is a comment
printf("Hello World!");
Comments in C
C Multi-line Comments
Multi-line comments start with /* and ends with */.
Any text between /* and */ will be ignored by the compiler
// Create variables
int myNum = 15; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character
// Print variables
printf("%d\n", myNum);
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
C Operators
Operators
Operators are used to perform operations on
variables and values.
Example: int myNum = 100 + 50;
In the example above, we use the + operator to add
together two values.
C Operators
C divides the operators into the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
C Operators
#include <stdio.h>
int main() {
int a = 10, b = 3;
return 0;
}
Assignment Operators
Assignment operators are used to assign values to
variables.
In the example below, we use the assignment operator (=)
to assign the value 10 to a variable called x:
int x = 10;
#include <stdio.h>
int main() {
int a = 10;
a += 5;
printf("After a += 5: a = %d\n", a);
a -= 3;
printf("After a -= 3: a = %d\n", a);
a *= 2;
printf("After a *= 2: a = %d\n", a);
a /= 4;
printf("After a /= 4: a = %d\n", a);
a %= 5;
printf("After a %%= 5: a = %d\n", a);
return 0;
}
Comparison Operators
Comparison operators are used to compare two values (or variables).
This is important in programming, because it helps us to find answers
and make decisions.
The return value of a comparison is either 1 or 0, which means true (1)
or false (0). These values are known as Boolean values.
In the following example, we use the greater than operator (>) to find
out if 5 is greater than 3:
int x = 5;
int y = 3;
printf("%d", x > y); // returns 1 (true) because 5 is greater
than 3
#include <stdio.h>
int main() {
int a = 10, b = 20;
return 0;
}
Logical Operators
You can also test for true or false values with logical
operators.
Logical operators are used to determine the logic between
variables or values, by combining multiple conditions:
C If ... Else
Conditions and If Statements
You have already learned that C supports the usual
logical conditions from mathematics:
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Equal to a == b
Not Equal to: a != b
C If ... Else
C has the following conditional statements:
Use if to specify a block of code to be executed, if a
specified condition is true
Use else to specify a block of code to be executed,
if the same condition is false
Use else if to specify a new condition to test, if
the first condition is false
Use switch to specify many alternative blocks of
code to be executed
C If ... Else
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
return 0;
}`
For Loop
• The for loop in C is a control flow statement that allows
you to iterate over a block of code multiple times.
• It is especially useful when the number of iterations is
known beforehand.
• Syntax of for Loop:
• for (initialization; condition; increment/decrement)
{
// Code to be executed
}
For Loop
Initialization: This step is executed once before the loop
starts. It usually initializes a loop counter.
Condition: This is checked before each iteration. If it's
true, the loop body executes; if it's false, the loop stops.
Update: This is executed after each iteration and typically
increments or decrements the loop counter.
For Loop
#include <stdio.h>
int main()
{
for (int i = 1; i <= 5; i++)
{ printf("i = %d\n", i);
}
return 0;
}
Problem 1
#include <stdio.h>
int main() {
for (int i = 100; i >= 0; i =i- 10) {
printf("%d\n", i);
}
return 0;
}
Problem 2
#include <stdio.h>
int main() {
int sum = 0;
return 0;
Nested Loop
It is also possible to place a loop inside another loop. This
is called a nested loop.
The "inner loop" will be executed one time for each
iteration of the "outer loop":
Nested Lopp
for (initialization; condition; increment/decrement)
{
// Outer loop code
for (initialization; condition; increment/decrement)
{
// Inner loop code
}
}
Nested Loop
#include<stdio.h>
int main(){
int i, j;
// Outer loop
for (i = 1; i <= 2; ++i) {
printf("Outer: %d\n", i); // Executes 2 times
// Inner loop
for (j = 1; j <= 3; ++j) {
printf(" Inner: %d\n", j); // Executes 6 times (2 * 3)
}
}
}
While Loop
The while loop loops through a block of code as long as a
specified condition is true:
while (condition) {
// code block to be executed
}
C++ While Loop
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
The Do/While Loop
• The do/while loop is a variant of the while loop.
• This loop will execute the code block once, before
checking if the condition is true, then it will repeat the
loop as long as the condition is true.
• do {
// code block to be executed
}
while (condition);
The Do/While Loop
int i = 0;
do {
printf("%d\n", i);
i++;
}
while (i < 5);
C Break
• You have already seen the break statement used in an
earlier chapter of this tutorial. It was used to "jump out"
of a switch statement.
• The break statement can also be used to jump out of a
loop.
• This example jumps out of the loop when i is equal to 4:
• int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
break;
}
printf("%d\n", i);
}
Continue
The continue statement breaks one iteration (in the loop), if a
specified condition occurs, and continues with the next
iteration in the loop.