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

C Programming Notes

The document provides examples of relational operators, if-else statements, and nested if-else statements in C programming. It demonstrates how to compare values, print outputs based on conditional logic, and nest if-else statements for multi-level comparisons. Key concepts covered include using relational operators like ==, !=, >, <, increment/decrement operators, the syntax of if-else and if-else ladder statements, and how to nest if-else statements for more complex conditional logic.

Uploaded by

zshabbir029
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

C Programming Notes

The document provides examples of relational operators, if-else statements, and nested if-else statements in C programming. It demonstrates how to compare values, print outputs based on conditional logic, and nest if-else statements for multi-level comparisons. Key concepts covered include using relational operators like ==, !=, >, <, increment/decrement operators, the syntax of if-else and if-else ladder statements, and how to nest if-else statements for more complex conditional logic.

Uploaded by

zshabbir029
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab 2nd Week

Relational operators
Practice the following operators

////////////////////////////////

#𝑖𝑛𝑐𝑙𝑢𝑑𝑒 < 𝑠𝑡𝑑𝑖𝑜. ℎ >


𝑖𝑛𝑡 𝑚𝑎𝑖𝑛()
{
𝑖𝑛𝑡 𝑎 = 10;
𝑝𝑟𝑖𝑛𝑡𝑓("%𝑑 \𝑛", + + 𝑎);
𝑝𝑟𝑖𝑛𝑡𝑓("%𝑑 \𝑛", 𝑎);
𝑝𝑟𝑖𝑛𝑡𝑓("%𝑑 \𝑛", 𝑎 + +);
𝑝𝑟𝑖𝑛𝑡𝑓("%𝑑 \𝑛", 𝑎);
𝑟𝑒𝑡𝑢𝑟𝑛 0;
}
/////////////Output///////////////////

11

11

11

12

////////////////////////////////////

Observe the difference between ~ and !

////////////// Code ///////////////////

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':

/tmp/jXGQqQUwhc.c:8:51: error: expected ')' before '~' token

8| printf("The result of %d != %d is %d", a, b, a~=b);

| ~ ^

| )

///////////////////////////////////////////

///////// Correct code for bit-wise not ///////

#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.

For example, the ASCII value of 'A' is 65.

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.

Program to Print ASCII Value

#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);

// %d displays the integer value of a character


// %c displays the actual character
printf("ASCII value of %c = %d", 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.

How if statement works in C programming?


To learn more about when test expression is evaluated to true (non-zero value) and false (0), check

Example 1: if statement

// Program to display a number if it is negative

#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

You entered -2.


C if...else Statement
The if statement may have an optional else block. The syntax of the if..else statement is:

if (test expression) {
// run code if test expression is true
}
else {
// run code if test expression is false
}

How if...else statement works?


If the test expression is evaluated to true, statements inside the body of if are executed. statements
inside the body of else are skipped from execution. If the test expression is evaluated to false,
statements inside the body of else are executed statements inside the body of if are skipped from
execution.

How if...else statement works in C programming?


Example 2: if...else statement

// Check whether an integer is odd or even

#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.

Syntax of if...else Ladder

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

// Program to relate two integers using =, > or < symbol

#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 ///////

Enter two integers: 12

23

Result: 12 < 23

Nested if...else
It is possible to include an if...else statement inside the body of another if...else statement.

Example 4: Nested if...else

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

if (number1 >= number2) {


if (number1 == number2) {
printf("Result: %d = %d",number1,number2);
}
else {
printf("Result: %d > %d", number1, number2);
}
}
else {
printf("Result: %d < %d",number1, number2);
}
return 0;
}
///// Check the output yourself and resolve errors if any ///////////
If the body of an if...else statement has only one statement, you do not need to use brackets {}.

You might also like