(C Programming) C Programming Extra Notes (Part 01) @BIJZLI
(C Programming) C Programming Extra Notes (Part 01) @BIJZLI
Precedence rule:
Operator precedence determines the order in which operators are evaluated.
Operators with higher precedence are evaluated first.
Associativity Rule
Associativity: In programming languages, the associativity (or fixity) of an
operator is a property that determines how operators of the same precedence
are grouped in the absence of parentheses.
bracket () Highest
Unary minus -
Problem Solving
Question 1
#include <stdio.h>
int main(){
float x;
x = 7*2.0/2+10/3;
printf("%f", x);
return 0;
}
The value of x is ___
(A) 10 (b) 10.0 (c) 10.33 (d) 11.0
Question 2
#include<stdio.h>
int main(){
int x;
x= -2 + 11 - 7 * 9 % 6 / 12;
printf("%d",x);
return 0 ;
}
The value of x is ___
(A) 6 (b) 7 (c) 8 (d) 9
Question 3
#include<stdio.h>
int main(){
int x;
x= 3/2*4+3/8+3;
printf("%d",x);
return 0 ;
}
The value of x is ___
(A) 6 (b) 7 (c) 8 (d) 9
Question 4
#include<stdio.h>
int main(){
int x;
x= 4+2%-8;
printf("%d",x);
return 0 ;
}
The value of x is ___
(A) 6 (b) 7 (c) 8 (d) 9
Question 5
#include<stdio.h>
int main(){
int x;
x= 2 * 3/4+4/4 +8-2+5/8;
printf("%d",x);
return 0 ;
}
The value of x is ___
(A) 6 (b) 7 (c) 8 (d) 9
Scoping Rule:
The scope of a variable in C is the block or the region in the program where a variable is
declared, defined, and used. Outside this region, we cannot access the variable and it is
treated as an undeclared identifier.
Example 1
#include<stdio.h>
int main {
{
int a =10 ;
printf(“%d”; a); // will print the 10
}
}
Output : it will print 10
Example 2
#include<stdio.h>
int main() {
{
int a =10 ;
}
printf("%d", a); // will not print 10
return 0 ;
}
#include<stdio.h>
int x = 40;
int main() {
int x = 30;
{
int x = 20;
{
int x = 10;
printf("%d", x);
}
}
return 0 ;
}
Relational Operator
Less than Greater than Greater than Less than equal Exactly equal Not equal to
equal to to
(a) 30 > 40
(b) 30 >= 40
(c) 30 == 40
(d) 40!=30
(e) 40==40
(f) 40!=40
(g) 50 <50
#include<stdio.h>
int x = 40;
int main() {
printf("%d\n", 30>40);
printf("%d\n", 30>=40);
printf("%d\n", 30==40);
printf("%d\n", 30!=40);
printf("%d\n", 40!=30);
printf("%d\n", 40==40);
printf("%d\n", 50>50);
printf("%d\n", 50<=50);
return 0 ;
}
Logical Operator:
1. Logical AND (&&)
2. Logical OR (||)
3. Logical NOT (!)
// output
Logical OR (||)
Example
#include <stdio.h>
int main() {
int a = 20;
int b = 5;
printf("%d", a > 10 || b > 10);
}
Logical NOT (!)
#include <stdio.h>
int main( ) {
int a = 20;
int b = 5;
printf("%d", !(a > 10 ));
}
Question practice
Question 1
What will be the output of the following C code?
#include <stdio.h>
#include<stdio.h>
int main()
{
int x = 1, y = 0, z = 0;
int a = x && y || z+1;
printf("%d", a);
}
A. 6
B. 5
C. 0
D. 1
Question 2
What will be the output of the following C code?
#include <stdio.h>
int main () {
int x = 1, y = 0, z = 5;
int a = x && y && z+1;
printf("%d", a);
}
A. 6
B. 5
C. 0
D. 1
Question 3
#include <stdio.h>
int main () {
int a =(( 5!=10 )==8)&&((6>10)==(10>6));
printf("%d", z);
}
A. 0
B. 1
Precedence of Logical operators
Example 1
#include <stdio.h>
int main () {
int a =(( 5+6!=10 )==8)&&((6*2/4>10)==(10>6));
printf("%d", a);
}
Answer :0
The output of the program is _____
Example 2
#include <stdio.h>
int main () {
int a =(( 5+6!=10 )==8)||((6*2/4>10)==(10>6));
printf("%d", a);
}
The output of the program is _____
Answer :0
Example 3
#include <stdio.h>
int main () {
int a =5+5!=10|| 6+4;
printf("%d", a);
}
Example 4
What is the output of the program?
#include <stdio.h>
int main () {
int a =3 < 6!= 3<6 && 9>11== 9>11;
printf("%d", a);
}
The output of the program ___
Answer 0
int a;
a = 12&& ;
int a;
a = 1|| ;
if the first operand is one then second operand is not evaluated.
int a;
a = 0|| ;
Example 1
What is the output of the program?
#include <stdio.h>
int main () {
int x=5;
int y;
y = ++x;
printf("%d %d", x, y);
}
The output of the program
(A) 5,5 (B) 5,6 (C) 6,5 (D) 6,6
Answer
D
Example2
#include <stdio.h>
int main () {
int x=5;
int y;
y = x++;
printf("%d %d", x, y);
}
(A) 5,5 (B) 5,6 (C) 6,5 (D) 6,6
Answer
C
Example 1
#include <stdio.h>
int main () {
int x=5;
int y=5,z;
z = ++x ||++y ;
printf("%d %d %d", x, y,z);
}
The output of the program
(A) 6,5 ,1 (B) 6,6,1 (C) 5,5,1 (D) 1,1,1
Example 2
#include <stdio.h>
int main () {
int x=5;
int y=5,z;
z = ++x && ++y ;
printf("%d %d %d", x, y,z);
}
The output of the program
(A) 6,5 ,1 (B) 6,6,1 (C) 5,5,1 (D) 1,1,1
Answer
B
Example 3
#include <stdio.h>
int main () {
int x=0;
int y=0,z;
z = ++x || ++y ;
printf("%d %d %d", x, y,z);
}
(A) 1,1 ,0 (B) 1,0,1 (C) 1,1,1 (D) 0,1,1
Answer
B
Example 4
#include <stdio.h>
int main () {
int x=0;
int y=0,z;
z = x++ || ++y ;
printf("%d %d %d", x, y,z);
}
Answer
C
Example 5
#include <stdio.h>
int main () {
int x=0;
int y=0,z;
z = x++ || y++ ;
printf("%d %d %d", x, y,z);
}
# include <stdio.h>
int main () {
int m = 10;
int n, n1;
n = ++m;
n1 = m++;
n--;
--n1;
n - = nl;
printf (“%d”, n) ;
return 0;
}
Key: (0)
Number System
Decimal number system (base 10)
0,1,2,3….9
Binary Number system (Base 2)
0,1
Octal Number System (base 8)
01,2,3…7
Hexadecimal number System (base 16)
0,1,2,3…10 A,B,C,D,E,F
Number base system:
1022 Expand it
#include <stdio.h>
int main () {
int x = 018;
printf("%d", x);
}
(A) 16 (B) Compiler Error
(C) warning (D) 18
Example 2
#include <stdio.h>
int main () {
int x = 017;
printf("%d", x);
}
(A) 17 (B) Compiler Error
(C) warning (D) 15
Answer
D
Hexadecimal
Example 4
What is the output the program
#include <stdio.h>
int main () {
int x = 0x18;
printf("%d", x);
}
(A) 16 (B) 24
(C) warning (D) 18
Answer
B
Example 5
What is the output the program
#include <stdio.h>
int main () {
int x = 0x1A;
printf("%d", x);
}
(A) 26 (B) 100
(C) warning (D) 18
Answer
A
Bit Wise Operator
Binary Conversion process
Binary to Decimal
Bitwise Operators
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
Example 1
What is the output the program
#include <stdio.h>
int main () {
int x = 5, y=17, z
z = x&y;
printf("%d", z);
}
(A) 1 (B) 21
(C) 2 (D) -6
Answer
A
Example 2
What is the output the program
#include <stdio.h>
int main () {
int x = 5, y=17, z
z = x|y;
printf("%d", z);
}
(A) 1 (B) 21
(C) 2 (D) -6
Answer
B
Example 3
What is the output the program
#include <stdio.h>
int main () {
int x = 5, y=17, z
z = x ^ y;
printf("%d", z);
}
(A) 1 (B) 21
(C) 20 (D) -6
Answer
C
Example 4
What is the output the program
#include <stdio.h>
int main () {
int x = 5, z
z = ~x;
printf("%d", z);
}
(A) 1 (B) 21
(C) 20 (D) -6
Answer
D
Example 5
#include<stdio.h>
int main(){
char a = 8;
int k;
k =a<<3;
printf("%d", k);
return 0;
}
(A) 1 (B) 64
(C) 20 (D) -6
Example 5
#include<stdio.h>
int main(){
char a = 64;
int k;
k =a>>3;
printf("%d", k);
return 0;
}
(A) 1 (B) 21
(C) 8 (D) -6