CP training notes
CP training notes
What is C?
C is a general-purpose programming language created by Dennis Ritchie at the
Bell Laboratories in 1972.
It is a very popular language, despite being old. The main reason for its
popularity is because it is a fundamental language in the field of computer
science.
Why Learn C?
It is one of the most popular programming languages in the world
If you know C, you will have no problem learning other popular
programming languages such as Java, Python, C++, C#, etc, as the syntax
is similar
C is very fast, compared to other programming languages,
like Java and Python
C is very versatile; it can be used in both applications and technologies
C Quickstart
Let's create our first C file.
#INCLUDE <STDIO.H>
INT MAIN() {
PRINTF("HELLO WORLD!");
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.
SINGLE-LINE COMMENTS
Any text between // and the end of the line is ignored by the compiler (will not
be executed).
C MULTI-LINE COMMENTS
EXAMPLE
/* The code below will print the words Hello World!
to the screen, and it is amazing */
printf("Hello World!");
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:
SYNTAX
TYPE VARIABLENAME = VALUE;
So, to create a variable that should store a number, look at the following
example:
EXAMPLE
Create a variable called myNum of type int and assign the value 15 to it:
// DECLARE A VARIABLE
INT MYNUM;
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.
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
EXAMPLE
// 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);
To declare more than one variable of the same type, use a comma-
separated list:
EXAMPLE
int x = 5, y = 6, z = 50;
printf("%d", x + y + z);
C VARIABLE NAMES
DATA TYPES
EXAMPLE
// Create variables
int myNum = 5; // 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);
The data type specifies the size and type of information the variable will store.
The character must be surrounded by single quotes, like 'A' or 'c', and we use
the %c format specifier to print it:
EXAMPLE
char myGrade = 'A';
printf("%c", myGrade);
NUMERIC TYPES
Use int when you need to store a whole number without decimals, like 35 or
1000, and float or double when you need a floating point number (with
decimals), like 9.99 or 3.14515.
INT
int myNum = 1000;
printf("%d", myNum);
TYPE CONVERSION
Sometimes, you have to convert the value of one data type to another type. This
is known as type conversion.
For example, if you try to divide two integers, 5 by 2, you would expect the
result to be 2.5. But since we are working with integers (and not floating-point
values), the following example will just output 2:
EXAMPLE
int x = 5;
int y = 2;
int sum = 5 / 2;
printf("%d", sum); // Outputs 2
IMPLICIT CONVERSION
EXAMPLE
// Automatic conversion: int to float
float myFloat = 9;
EXPLICIT CONVERSION
Considering our problem from the example above, we can now get the right
result:
EXAMPLE
// Manual conversion: int to float
float sum = (float) 5 / 2;
CONSTANTS
If you don't want others (or yourself) to change existing variable values, you can
use the const keyword.
EXAMPLE
const int myNum = 15; // myNum will always be 15
myNum = 10; // error: assignment of read-only variable 'myNum'
Arithmetic operators
Assignment operators
Comparison operators
Logical operators
Bitwise operators
Arithmetic Operators
You have already learned that C supports the usual logical conditions from
mathematics:
You can use these conditions to perform different actions for different
decisions.
THE IF STATEMENT
SYNTAX
if (CONDITION) {
// block of code to be executed if the condition is true
}
In the example below, we test two values to find out if 20 is greater than 18. If
the condition is true, print some text:
EXAMPLE
if (20 > 18) {
printf("20 is greater than 18");
}
SWITCH STATEMENT
Instead of writing many if..else statements, you can use the switch statement.
SYNTAX
switch (EXPRESSION) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
EXAMPLE
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;
}
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no
need for more testing.
The default keyword specifies some code to run if there is no case match:
LOOPS
Loops are handy because they save time, reduce errors, and they make code
more readable.
WHILE LOOP
The while loop loops through a block of code as long as a specified condition
is true:
SYNTAX
while (CONDITION) {
// code block to be executed
}
In the example below, the code in the loop will run, over and over again, as long
as a variable (i) is less than 5:
EXAMPLE
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
SYNTAX
do {
// code block to be executed
}
while (CONDITION);
The example below uses a do/while loop. The loop will always be executed at
least once, even if the condition is false, because the code block is executed
before the condition is tested:
EXAMPLE
int i = 0;
do {
printf("%d\n", i);
i++;
}
while (i < 5);
FOR LOOP
When you know exactly how many times you want to loop through a block of
code, use the for loop instead of a while loop:
SYNTAX
for (expression 1; expression 2; expression 3) {
// code block to be executed
}
Expression 1 is executed (one time) before the execution of the code block.
EXAMPLE
int i;
NESTED LOOPS
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":
EXAMPLE
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)
}
}
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.
EXAMPLE
int 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.
EXAMPLE
int i;
Happy Coding