Everything Basics of C Flashcards
If you don’t want others (or yourself) to change existing variable values, you can use the keyword.
This will declare the variable as “constant”, which means unchangeable and read-only
const
When you declare a constant variable, it must be assigned with a value
const int myNum = 15; // myNum will always be 15
myNum = 10; // error: assignment of read-only variable ‘myNum’
In C, the bool type is not a built-in data type, like int or char.
It was introduced in C99, and you must import the following header file to use it:
include <stdbool.h></stdbool.h>
Use the if statement to specify a block of code to be executed if a condition is true
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
}
Short hand if else
variable = (condition) ? expressionTrue : expressionFalse
int time = 20;
(time < 18) ? printf(“Good day.”) : printf(“Good evening.”);
Instead of writing many if..else statements, you can use the .
The switch statement selects one of many code blocks to be executed
Switch Statements
switch (expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
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 … statement breaks out of the switch block and stops the execution
The … statement is optional, and specifies some code to run if there is no case match
Break
default
The … loop loops through a block of code as long as a specified condition is true
while
while (condition) {
// code block to be executed
}
int i = 0;
while (i < 5) {
printf(“%d\n”, i);
i++;
}
The … 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/while
do {
// code block to be executed
}
while (condition);
int i = 0;
do {
printf(“%d\n”, i);
i++;
}
while (i < 5);
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.
Expression 2 defines the condition for executing the code block.
Expression 3 is executed (every time) after the code block has been executed
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”
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)
}
}
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues with the next iteration in the loop
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
printf(“%d\n”, i);
}
You can also use break and continue in while loops
int i = 0;
while (i < 10) {
if (i == 4) {
break;
}
printf(“%d\n”, i);
i++;
}
Continue Example
int i = 0;
while (i < 10) {
if (i == 4) {
i++;
continue;
}
printf(“%d\n”, i);
i++;
}
are used to store multiple values in a single variable, instead of declaring separate variables for each value.
To create an array, define the data type (like int) and specify the name of the array followed by square brackets []
Arrays
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;
printf(“%d”, myNumbers[0]);
Change an array element
int myNumbers[] = {25, 50, 75, 100};
int i;
for (i = 0; i < 4; i++) {
printf(“%d\n”, myNumbers[i]);
}
You can loop through the array elements with the for loop.
int myNumbers[] = {10, 25, 50, 75, 100};
printf(“%lu”, sizeof(myNumbers)); // Prints 20
Get Array Size or Length
It is because the sizeof operator returns the size of a type in bytes
A 2D array is also known as a … (a table of rows and columns)
matrix
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
A 2D array
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
printf(“%d”, matrix[0][2]); // Outputs 2
Access the Elements of a 2D Array
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf(“%d\n”, matrix[i][j]);
}
}
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf(“%d\n”, matrix[i][j]);
}
}
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
matrix[0][0] = 9;
printf(“%d”, matrix[0][0]);
Change Elements in a 2D Array
char greetings[] = “Hello World
Strings
char greetings[] = “Hello World!”;
printf(“%c”, greetings[0])
Access Strings
Since strings are actually arrays in C, you can access a string by referring to its index number inside square brackets []
char greetings[] = “Hello World!”;
greetings[0] = ‘J’;
printf(“%s”, greetings);
Modify Strings
char carName[] = “Volvo”;
int i;
for (i = 0; i < 5; ++i) {
printf(“%c\n”, carName[i]);
}
Loop Through a String
char carName[] = “Volvo”;
int length = sizeof(carName) / sizeof(carName[0]);
int i;
for (i = 0; i < length; ++i) {
printf(“%c\n”, carName[i]);
}
Loop Through a String
char greetings[] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘ ‘, ‘W’, ‘o’, ‘r’, ‘l’, ‘d’, ‘!’, ‘\0’};
char greetings2[] = “Hello World!”;
printf(“%lu\n”, sizeof(greetings)); // Outputs 13
printf(“%lu\n”, sizeof(greetings2));
Escape character Result Description
' ‘ Single quote
" “ Double quote
\ \ Backslash
The sequence … inserts a single quote in a string
'
char txt[] = “It's alright.”
The sequence … inserts a single backslash in a string
char txt[] = “The character \ is called backslash.”;
Escape Character Result
\n New Line
\t Tab
\0 Null
C also has many useful string functions, which can be used to perform certain operations on strings.
To use them, you must include the … header file in your program:
<string.h>
</string.h>