C Programming Notes
C Programming Notes
Relational operators
Practice the following operators
////////////////////////////////
11
11
11
12
////////////////////////////////////
int a = 10, b = 5;
printf("The result of %d != %d is %d", a, b, a!=b);
/////////// Output ///////////////
The result of 10 != 5 is 1
////////////////////////////////////////
int a = 10, b = 5;
printf("The result of %d != %d is %d", a, b, a~=b);
/////////////// Output ////////
/tmp/jXGQqQUwhc.c: In function 'main':
| ~ ^
| )
///////////////////////////////////////////
#include <stdio.h>
#include <stdbool.h> // Include the stdbool.h library
int main() {
bool binaryVar1, binaryVar2; // Declare a binary variable using bool
binaryVar2 = false; // Assign a false value
printf("binaryVar2 = %d\n", binaryVar2);
binaryVar1 = true; // Assign a true value
printf("~binaryVar1.binaryVar2 = %d\n", binaryVar2 && ~binaryVar1);
printf("~binaryVar1.binaryVar2 = %d\n", binaryVar2 || ~binaryVar1);
return 0;
}
///////////// Output //////////////////
binaryVar2 = 0
~binaryVar1.binaryVar2 = 0
~binaryVar1.binaryVar2 = 1
////////// Code 2 /////////////
#include <stdio.h>
int main() {
printf("Output = %d\n", ~35);
printf("Output = %d\n", ~-12);
return 0;
}
//////// Output /////////////
Output = -36
Output = 11
///// Explanation of the above //////
+35 = 0100011 => 1’s (bit-wise) complement 1011100 (-36) => 2’s comp to check = 0100100
= +36
+12 = 01100 => (2’s complement to get -12) 10100 => 1’s (bit-wise) complement 01011 =
+11
ASCII Codes
In C programming, a character variable holds ASCII value (an integer number between 0 and 127)
rather than that character itself. This integer value is the ASCII code of the character.
What this means is that, if you assign 'A' to a character variable, 65 is stored in the variable rather
than 'A' itself.
Now, let's see how we can print the ASCII value of characters in C programming.
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
return 0;
}
//////////// Output /////////////
Enter a character: G
ASCII value of G = 71
In this program, the user is asked to enter a character. The character is stored in variable c. When %d
format string is used, 71 (the ASCII value of G) is displayed. When %c format string is used, 'G' itself is
displayed.
C if...else Statement
C if Statement
The syntax of the if statement in C programming is:
𝑖𝑓 (𝑡𝑒𝑠𝑡 𝑒𝑥𝑝𝑟𝑒𝑠𝑠𝑖𝑜𝑛)
{
// 𝑐𝑜𝑑𝑒
}
How if statement works?
The if statement evaluates the test expression inside the parenthesis (). If the test expression is
evaluated to true, statements inside the body of if are executed. If the test expression is evaluated
to false, statements inside the body of if are not executed.
Example 1: if statement
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// true if number is less than 0
if (number < 0) {
printf("You entered %d.\n", number);
}
printf("The if statement is easy.");
return 0;
}
//////// Output /////////////
Enter an integer: -2
if (test expression) {
// run code if test expression is true
}
else {
// run code if test expression is false
}
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}
return 0;
}
/////////// Output ////////////
Enter an integer: 7
7 is an odd integer.
C if...else Ladder
The if...else statement executes two different codes depending upon whether the test expression is
true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder
allows you to check between multiple test expressions and execute different statements.
if (test expression1) {
// statement(s)
}
else if(test expression2) {
// statement(s)
}
else if (test expression3) {
// statement(s)
}
.
.
else {
// statement(s)
}
Example 3: C if...else Ladder
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if the two integers are equal.
if(number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
//checks if number1 is greater than number2.
else if (number1 > number2) {
printf("Result: %d > %d", number1, number2);
}
//checks if both test expressions are false
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
///// Output ///////
23
Result: 12 < 23
Nested if...else
It is possible to include an if...else statement inside the body of another if...else statement.
This program given below relates two integers using either <, > and = similar to the if...else ladder's
example. However, we will use a nested if...else statement to solve this problem.
#include <stdio.h>
int main() {
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);