In C, variables are used to store data.
Different data
types determine the type of data that can be stored.
Some common data types include:
1. int: Integer data type (e.g., 5, -10)
2. float: Floating-point data type (e.g., 3.14, -0.5)
3. char: Character data type (e.g., 'a', 'X')
4. double: Double-precision floating-point data type
(e.g., 3.14159)
Declare Variable & Create Variable
// Declare a variable
int myNum;
// Assign a value to the variable
myNum = 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);
3/3/2024 3
Operators are symbols used to perform
operations on variables and values. Some
common operators in C include:
+ (addition), - (subtraction), * (multiplication), /
(division), % (modulo), = (assignment), ==
(equality), > (greater than), < (less than), &&
(logical AND), || (logical OR)
In C, booleans represent true or false values. They
are typically represented using the 'int' data type,
where 0 represents false and any non-zero value
represents true.
// Create boolean variables
bool isProgrammingFun = true;
bool isFishTasty = false;
// Return boolean values
printf("%d", isProgrammingFun); // Returns 1
(true)
printf("%d", isFishTasty); // Returns 0 (false)
If...else statements are used for decision making in
C. They allow the program to execute different
code blocks based on specified conditions.
Example
int myNum = 10; // Is this a positive or negative
number?
if (myNum > 0) {
printf("The value is a positive number.");
} else if (myNum < 0) {
printf("The value is a negative number.");
} else {
printf("The value is 0.");
The while loop in C is used to execute a block of code repeatedly as long
as a specified condition is true.
Example
Print "Yatzy!" If the dice number is 6:
int dice = 1;
while (dice <= 6) {
if (dice < 6) {
printf("No Yatzy\n");
} else {
printf("Yatzy!\n");
}
dice = dice + 1;
}
The for loop in C is used to execute a block of code a
specified number of times. It consists of three parts:
initialization, condition, and increment/decrement.
Example
int number = 2;
int i;
// Print the multiplication table for the number 2
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", number, i, number * i);
}
return 0;
int number = 2;
int i;
// Print the multiplication table for the number 2
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", number, i, number *
i);
}
return 0;