C Syntax
#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
/* The code below will print the words Hello World!
to the screen, and it is amazing */
printf("Hello World!");
C Variables
Variables are containers for storing data values, like numbers
and characters.
In C, there are different types of variables (defined with
different keywords), for example:
int - stores integers (whole numbers), without decimals, such as
123 or -123
float - stores floating point numbers, with decimals, such as
19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Characters are
surrounded by single quotes
C Variables
// Declare a variable
int myNum;
// Assign a value to the variable
myNum = 15;
C Format Specifiers
Format specifiers are used together with the printf() function to
tell the compiler what type of data the variable is storing. It is
basically a placeholder for the variable value.
A format specifier starts with a percentage sign %, followed by
a character.
For example, to output the value of an int variable, use the
format specifier %d surrounded by double quotes (""), inside
the printf() function:
Example
int myNum = 15;
printf("%d", myNum); // Outputs 15
C Format Specifiers
To print other types, use %c for char and %f for float:
// 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;
printf("Addition: %d + %d = %d\n", a, b, a + b);
printf("Subtraction: %d - %d = %d\n", a, b, a - b);
printf("Multiplication: %d * %d = %d\n", a, b, a * b);
printf("Division: %d / %d = %d\n", a, b, a / b);
printf("Modulus: %d %% %d = %d\n", a, b, a % b);
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;
printf("Initial value: a = %d\n", a);
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;
printf("a == b: %d\n", a == b);
printf("a != b: %d\n", a != b);
printf("a > b: %d\n", a > b);
printf("a < b: %d\n", a < b);
printf("a >= b: %d\n", a >= b);
printf("a <= b: %d\n", a <= b);
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
}
if (20 > 18) {
printf("20 is greater than 18");
}
C If ... Else
Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
int time = 20;
if (time < 18) {
printf("Good day.");
} else {
printf("Good evening.");
}// Outputs "Good evening."
C If ... Else
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and
condition2 is true
} else {
// block of code to be executed if the condition1 is false and
condition2 is false
}
C If ... Else
int time = 22;
if (time < 10) {
printf("Good morning.");
} else if (time < 20) {
printf("Good day.");
} else {
printf("Good evening.");
}
// Outputs "Good evening."
C Switch
Switch Statement
• Instead of writing many if..else statements, you can use
the switch statement.
• The switch statement selects one of many code blocks to
be executed:
C Switch
switch (expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
C Switch
• The switch expression is evaluated once
• The value of the expression is compared with the values
of each case
• If there is a match, the associated block of code is
executed
• The break statement breaks out of the switch block
and stops the execution
• The default statement is optional, and specifies some
code to run if there is no case match
C Switch
switch (expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
#include <stdio.h>
int main() {
int day = 4;
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
}
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;
for (int i = 30; i <= 120; i++) {
if (i % 3 == 0 && i % 5 == 0) {
sum += i;
}
}
printf("Summation of numbers divisible by 3 and 5 between 30 and 120 is: %d\n", sum);
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.
This example skips the value of 4:
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
printf("%d\n", i);
}
return 0;
}