0x01 C - Variables, if, else, while
0x01 C - Variables, if, else, while
0x01. C
Variables, if,
else, while
C - Programming
TABLE OF CONTENTS
01 02
Overview Learning
topics Objectives
03 04
Quiz hands on lab
questions practice
01
OVERVIEW topics
What are the arithmetic operators and how to use
them
https://t.me/alx_2023
C
Programming
Topics
02
Learning Objectives
Data Types
Data Types
Data Types (byte = 8 bit)
Data Types (byte = 8 bit)
What are the arithmetic operators and how to use them
In C, the values 0 and false are considered false, while any other value (including negative numbers
and non-zero values) is considered true. This applies to all data types in C, including integers,
floating point numbers, and pointers.
However, in C99 and later versions, a new header file called stdbool.h was introduced that defines a
new data type called bool, which can have two possible values: true and false. These values are
defined as macros that evaluate to integer constants (true is defined as 1 and false is defined as 0).
When using the bool data type, it's recommended to include the stdbool.h header and use the true
and false macros instead of the integer constants 1 and 0.
OUTPUT: What values are considered TRUE and FALCE in C
a is true
b is false #include <stdio.h>
#include <stdbool.h>
int main() {
bool a = true;
bool b = false;
if (a) {
printf("a is true\n");
} else {
printf("a is false\n");
}
if (b) {
printf("b is true\n");
} else {
printf("b is false\n");
}
return 0;
}
How to use if , if .... else satements
In C, the if statement is used to conditionally execute a block of code. The syntax of the if statement
is as follows:
if (condition) {
// code to be executed if the condition is true
}
The condition in the parentheses can be any expression that evaluates to a boolean value (true or
false). If the condition is true, then the block of code inside the curly braces is executed. If the
condition is false, then the block of code is skipped and execution continues with the next statement
after the if block.
How to use if , if .... else satements
#include <stdio.h>
int main() {
OUTPUT: int num = 5;
num is positive
if (num > 0) {
num is odd printf("num is positive\n");
num is less than or equal to 5 }
if (num % 2 == 0) {
printf("num is even\n");
} else {
printf("num is odd\n");
}
return 0;
}
How to use comments
// comment
documenting
/**
* Main Entry Point
*/
How to declare variables of types char, int, unsigned int
You can also initialize the variables at the time of In C, you can declare variables of types
declaration by assigning a value to them: char, int, and unsigned int using the
following syntax:
// Declare and initialize a variable of type char
char my_char = 'a'; // Declare a variable of type char
char my_char;
// Declare and initialize a variable of type int
int my_int = -10; // Declare a variable of type int
int my_int;
// Declare and initialize a variable of type unsigned int
unsigned int my_unsigned_int = 20; // Declare a variable of type unsigned int
unsigned int my_unsigned_int;
How to print the values of variables of type char, int, unsigned
int, with printf
char my_char = 'a';
int my_int = 10;
unsigned int my_unsigned_int = 20;
while (condition) {
// code to be executed
}
Here's an example of how to use the while loop to print the numbers 1 to 5:
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
In this example, the loop will continue to execute as long as the value of i is less than
or equal to 5. Inside the loop, the value of i is printed using the printf() function, and
then i is incremented by 1 using the i++ statement.
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
In this example, the loop will continue to execute as long as the value of i is less than
or equal to 5. Inside the loop, the value of i is printed using the printf() function, and
then i is incremented by 1 using the i++ statement.
Condition: The second expression is a condition that is checked before each iteration of
the loop. If the condition evaluates to true, the loop body is executed. If it evaluates to
false, the loop terminates.
Increment/decrement: The third expression is executed after each iteration of the loop. It
is typically used to update the loop variable.
How to use variables with the for loop
#include <stdio.h>
return 0; return 0;
} }
How to use variables with the for loop
#include <stdio.h>
int main() {
// print numbers from 1 to 10
int i = 1;
for (;i <= 10;) {
printf("%d ", i);
i++;
}
return 0;
}
do while
The do-while loop is another type of loop in C programming that is similar to the while
loop. The difference is that the do-while loop executes the loop body at least once before
checking the loop condition. The basic syntax of the do-while loop is as follows:
do {
// loop body
} while (condition);
In this syntax, the loop body is executed first, and then the condition is checked. If the
condition is true, the loop body is executed again, and the process repeats until the
condition becomes false. Note that the loop body is guaranteed to be executed at least
once, regardless of the condition.
How to use variables with the do while loop
#include <stdio.h>
int main() {
int num;
do {
printf("Enter a positive integer (or a negative integer to exit): ");
scanf("%d", &num);
} while (num >= 0);
The GCC flags -m32 and -m64 are used to specify the target architecture for the compiled
binary.
The -m32 flag tells the compiler to generate code for a 32-bit architecture, which is
capable of addressing up to 4GB of memory. This flag is typically used on older systems
or when compatibility with 32-bit libraries or operating systems is required.
The -m64 flag, on the other hand, tells the compiler to generate code for a 64-bit
architecture, which is capable of addressing much larger amounts of memory than a 32-
bit architecture. This flag is typically used on newer systems or when the application
needs to access more than 4GB of memory.
It's important to note that using these flags alone will not necessarily make the program
faster or more efficient. The choice of architecture depends on the specific requirements
of the application and the system it will be running on. Additionally, not all processors
support both 32-bit and 64-bit architectures, so it's important to check the system
specifications before choosing which flag to use.
04
Hands on lab Practice
Have a Question
Leave a Comment!
Subscribe
To stay updated with latest
videos
Share
To let the others know more
Thanks