Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

0x01 C - Variables, if, else, while

The document provides an overview of C programming concepts, including data types, operators, control structures (if, while, for, do-while loops), and the use of comments. It explains the syntax and usage of arithmetic, logical, and relational operators, as well as how to declare and print variables. Additionally, it covers the significance of GCC flags -m32 and -m64 for specifying target architectures in compiled binaries.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

0x01 C - Variables, if, else, while

The document provides an overview of C programming concepts, including data types, operators, control structures (if, while, for, do-while loops), and the use of comments. It explains the syntax and usage of arithmetic, logical, and relational operators, as well as how to declare and print variables. Additionally, it covers the significance of GCC flags -m32 and -m64 for specifying target architectures in compiled binaries.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

ALX LESSON

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

What are the logical operators (sometimes called


boolean operators) and how to use them

What the relational operators and how to use them


C What values are considered TRUE and FALCE in C
Programming What are the boolean operators and how to use
Topics
them

How to use if , if .... else satements

How to use comments


How to declare variables of types char, int,
unsigned int

How to assign values to variables

How to print the values of variables of type


char, int, unsigned int, with printf
C
How to use the while loop
Programming How to use variables with the while loop
Topics
How to print variables using printf

What is the ASCII character set

What are the purpose of the gcc flags


-m32 and -m64
How to use the for loop

C How to use variables with the for loop

Programming How to use the do while loop


Topics How to use variables with the do while loop
Slides On Telegram

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, there are several arithmetic operators that can be used


to perform mathematical operations on variables and values.
These operators include:

+ (addition) - adds two operands together


- (subtraction) - subtracts one operand from another
* (multiplication) - multiplies two operands together
/ (division) - divides one operand by another
% (modulus) - returns the remainder of a division operation

Here's an example program that demonstrates the use of these


arithmetic operators:
OUTPUT: What are the arithmetic operators and how to use them
x + y = 13
x-y=7
x * y = 30 #include <stdio.h>
x/y=3
x%y=1 int main() {
int x = 10;
Note that the modulus operator int y = 3;
% returns the remainder of the
division operation. In the printf("x + y = %d\n", x + y);
example above, x % y returns 1 printf("x - y = %d\n", x - y);
because 10 divided by 3 leaves a printf("x * y = %d\n", x * y);
remainder of 1. printf("x / y = %d\n", x / y);
printf("x %% y = %d\n", x % y);
Arithmetic operators can also be
used with variables of other data return 0;
types, such as float or double, as }
well as with literal values. It's
important to keep in mind that
division by zero is undefined in C
and can lead to runtime errors.
What are the logical operators (sometimes called boolean operators)
and how to use them

In C, there are three logical operators (also called boolean


operators) that can be used to perform logical operations on
boolean values (true or false). These operators are:

&& (logical AND) - returns true if both operands are true,


otherwise returns false

|| (logical OR) - returns true if either operand is true, otherwise


returns false

! (logical NOT) - returns true if the operand is false, otherwise


returns false

Here's an example program that demonstrates the use of these


logical operators:
What are the logical operators (sometimes called boolean operators)
OUTPUT: and how to use them
a && b = 0
a || b = 1 #include <stdio.h>
!a = 0 #include <stdbool.h>
!b = 1
int main() {
bool a = true;
Logical operators can also be bool b = false;
used in conjunction with
comparison operators (e.g. ==, !=, printf("a && b = %d\n", a && b);
<, >, <=, >=) to create complex printf("a || b = %d\n", a || b);
logical expressions. It's printf("!a = %d\n", !a);
important to keep in mind the printf("!b = %d\n", !b);
order of operations when using
logical operators in conjunction return 0;
with comparison operators, and }
to use parentheses to group
expressions when necessary to
avoid ambiguity.
What values are considered TRUE and FALCE in C

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");
}

if (num > 10) {


printf("num is greater than 10\n");
} else if (num > 5) {
printf("num is greater than 5\n");
} else {
printf("num is less than or equal to 5\n");
}

return 0;
}
How to use comments

// comment

// this is main function

/* multi line comment */

/* This program takes age input from the user


It stores it in the age variable
And, print the value using printf() */

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;

// Print the value of a char variable


printf("The value of my_char is: %c\n", my_char);

// Print the value of an int variable


printf("The value of my_int is: %d\n", my_int);

// Print the value of an unsigned int variable


printf("The value of my_unsigned_int is: %u\n", my_unsigned_int);
How to use the while loop
In C programming, the while loop is used to execute a block of code repeatedly as long
as a condition is true. The syntax of the while loop is as follows:

while (condition) {
// code to be executed
}

Here, condition is an expression that is evaluated at the beginning of each iteration of


the loop. If the condition is true, the code inside the loop is executed. This continues
until the condition becomes false.
How to use the while loop and variables with it

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.

The output of this code will be:


12345
Note that it's important to make sure that the condition in the while loop will
eventually become false, otherwise the loop will execute indefinitely and the program
will hang.
How to use variables with the while loop
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.

The output of this code will be:


12345
Note that it's important to make sure that the condition in the while loop will
eventually become false, otherwise the loop will execute indefinitely and the program
will hang.
for loop
The for loop is a control flow statement in C programming that allows you to execute a
block of code repeatedly for a fixed number of times.

The syntax of the for loop is as follows:


for (initialization; condition; increment/decrement) {
// code to be executed
}
Initialization: The first expression is executed only once, before the loop starts. It is
typically used to initialize the loop variable.

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>

#include <stdio.h> int main() {


int i;
int main() {
// print numbers from 1 to 10 // print numbers from 1 to 10
for (int i = 1; i <= 10; i++) { for (i = 1; i <= 10; i++) {
printf("%d ", i); printf("%d ", i);
} }

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);

printf("You entered a negative integer. Goodbye!\n");


return 0;
}
What is the ASCII character set
Hexadecimal Numbering System
what are the purpose of the gcc flags -m32 and -m64

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

You might also like