C Programming _ Extra Notes (Part-03)
C Programming _ Extra Notes (Part-03)
Example 1
#include <stdio.h>
int main(){
printf("%d", printf("ABCD"));
return 0;
}
Answer
ABCD4
Example 2
#include <stdio.h>
int main(){
printf("\n%d", printf("ABCD"));
return 0;
}
Answer
ABCD
4
Example 3
#include <stdio.h>
int main(){
int a=1000;
printf("%d",printf("\n%d",a));
return 0;
}
Output of the program is _____
Answer
10005
Side effect
X = Y has no side effect
X = Y++;
This expression is having some side effect
Sequence Point:
At certain specified points in the execution sequence called sequence points, all side effects of
previous evaluations shall be complete and no side effects of subsequent evaluations shall have
taken place.
Undefined behaviours
Between two sequence points a variable must not be modified more than once. In an expression
statement, the next sequence point is usually at the terminating semicolon, and the previous
sequence point is at the end of the previous statement. An expression may also contain
intermediate sequence points.
From the above sentence the following expressions invoke Undefined Behaviour.
i++ * ++i; // i is modified more than once
i = ++i // same as above
++i = 2; // same as above
i = ++i +1 // same as above
Ternary Operator:
?
v variable = Expression1 ? Expression2 : Expression3;
variable = Expression1 ? Expression2 : Expression3;
Example 1
#include <stdio.h>
int main()
{
int a;
a = 10>7?10:20;
printf("%d", a);
return 0;
}
Answer
10
Example 2
#include <stdio.h>
int main()
{
int a;
a = 10?10:20;
printf("%d", a);
return 0;
}
Answer
10
Example 3
#include<stdio.h>
int main(){
int x=3, y=4, z=4;
printf("%d", (z>=y>=x?100:200));
return 0 ;
}
(a) 100 (b) 200 (c) 0 (d) 1
Answer
B
if-else statements:
Decision making structures require that the programmer specify one or more conditions
to be evaluated or tested by the program, along with a statement or statements to be
executed if the condition is determined to be true, and optionally, other statements to be
executed if the condition is determined to be false. Following is the general form of a
typical decision-making structure found in most of the programming languages:
C programming language assumes any non-zero and non-null values
as true, and if it is either zero or null, then it is assumed
as false value. C programming language provides following types
of decision making statements.
Syntax
int main(){
s1;
s2;
s3;
s4;
int main(){
s1;
if(condition){
s2;
s3;
}
s4;
}
Statement S1 and S2 will execute if the condition is TRUE
int main(){
s1;
if(condition/expression){
s2;
s3;
}
s4;
Example 1
#include <stdio.h>
int main (){
int a = 10
if( a < 20) {
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
Example 2
#include <stdio.h>
int main (){
int a = 10
if(a+2) {
printf("Namaskar" );
}
printf("Dosto");
return 0;
}
What is output
HelloWorld
Example 3
#include <stdio.h>
int main (){
int a = 10
if(a-10) {
printf("Hello" );
}
printf("World");
return 0;
}
Example 3
#include <stdio.h>
int main (){
int a = 10
if(-a) {
printf("Hello" );
}
printf("World");
return 0;
}
What is the output of the program ____
HelloWorld
Example 4
#include <stdio.h>
int main (){
if(printf(“”) {
printf("Hello" );
}
printf("World");
return 0;
}
What is the output of the program ____
HelloWorld
Example 5
#include <stdio.h>
int main (){
if(printf("")){
printf("Hello" );
}
printf("World");
return 0;
}
What is the output of the program ____
HelloWorld
Example 6
#include <stdio.h>
int main (){
if(!6){
printf("Hello");
}
printf("World");
return 0;
}
What is the output of the program ____
World
Example 7
#include <stdio.h>
int main (){
if(3.14){
printf("Hello");
}
printf("World");
return 0;
}
What is the output of the program ____
HelloWorld
Example 8
#include <stdio.h>
int main (){
i=0;
if(i++){
printf("Hello");
}
printf("World");
return 0;
}
What is the output of the program ____
World
Example 9
#include <stdio.h>
int main (){
i=0;
if(i++){
printf("Hello");
}
printf("World");
return 0;
}
What is the output of the program ____
World
Example 10
#include <stdio.h>
int main (){
i=0;
if(i++);{
printf("Hello");
}
printf("World");
return 0;
}
What is the output of the program ____
HelloWorld
Example 11
#include <stdio.h>
int main (){
i=0;
if(i++){
printf("Hello");
}
printf("World");
return 0;
}
If-else statement
int main(){
s1;
if(condition/expression){
s2;
s3;
}
else {
s4;
s5;
}
S6;
Example 10
#include <stdio.h>
int main (){
i=1;
if(i+2-3){
printf("Hello friends");
}
else
printf("mai bhi hu");
return 0;
}
Example 11
#include <stdio.h>
int main (){
int i=1;
if(i++){
printf("Hello friends");
}
else;
printf("\tmai bhi hu");
return 0;
}
Programming assignment
• Program to Check Even or Odd
• #include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
return 0;
}
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
#include<stdio.h>
int main ()
{
int num1, num2;
num1=12,num2=13;
if (num1 == num2)
printf("both are equal");
else if (num1 > num2)
printf("%d is greater", num1);
else
printf("%d is greater", num2);
return 0;
}
Question 1
Decide the grade based on the marks
If marks <50 then Grade is F
if marks >=50 <60 then Grade is D
if marks >=60 <70 then Grade is C
if marks >=70 <80 then Grade is B
if marks >=80 <90 then Grade is A
if marks >=90 then Grade is A+
program
#include<stdio.h>
int main()
{
int marks;
printf("Enter your marks ");
scanf("%d",&marks);
if(marks<0 || marks>100)
{
printf("Wrong Entry");
}
else if(marks<50)
{
printf("Grade F");
}
else if(marks>=50 && marks<60)
{
printf("Grade D");
}
else if(marks>=60 && marks<70)
{
printf("Grade C");
}
else if(marks>=70 && marks<80)
{
printf("Grade B");
}
else if(marks>=80 && marks<90)
{
printf("Grade A");
}
else
{
printf("Grade A+");
}
return 0;
}
Question 2
Largest of 3 Integer a,b,c using if and else
#include <stdio.h>
int main()
{
int a, b, c;
else
printf("%d is the largest number.", c);
return 0;
}
#include <stdio.h>
int main()
{
// variable declaration
int n1 = 5, n2 = 10, n3 = 15, max;
return 0;
}
#include <stdio.h>
int main (){
int a=0;
if(a++);
{
a = a+10;
a = a-5 ;
}
printf("%d", a);
return 0;
}
Swich Statement
Switch statements:
A switch statement allows a to be tested for equality against a list of values. Each value is
called a case, and the variable being switched on is checked for each case.
The switch-case statement is a multi-way decision statement. Unlike the multiple decision
statement that can be created using if-else, the switch statement evaluates the conditional
expression and tests it against numerous constant values. The branch corresponding to the value
that the expression matches is taken during execution.
Syntax
The syntax for a switch statement in C++ is as follows:
switch(expression){
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional
Break statements
Switch case statements are used to execute only specific case statements based on the switch
expression. If we do not use break statement at the end of each case, program will execute all
consecutive case statements until it finds next break statement or till the end of switch case
block.
#include <stdio.h>
int main(){
int a = 5;
switch(a){
case 4: printf("%d",5);
case 5: printf("%d",5);
case 6: printf("%d",6);
}
}
#include <stdio.h>
int main(){
switch(13/4){
case 3: printf("%d",4); break ;
case 4: printf("%d",2); break ;
case 5: printf("%d",5); break ;
}
}
}
#include <stdio.h>
int main(){
switch(13/4.0){
case 4: printf("%d",4); break ;
case 3: printf("%d",2); break ;
case 5: printf("%d",5); break ;
}
}
#include <stdio.h>
}
int main(){
switch(printf(“ABCD”){
case 4: printf("%d",4); break ;
case 3: printf("%d",2); break ;
case 5: printf("%d",5); break ;
}
}
Output
ABCD4
#include <stdio.h>
int main(){
switch('c'){
case 'a': printf("%d",4); break ;
case 'b': printf("%d",2); break ;
case 'c': printf("%d",5); break ;
}
}
Output:____________________
Position of default does not matter
#include <stdio.h>
int main(){
switch('d'){
case 'a': printf("%d",4); break ;
case 'b': printf("%d",2); break ;
default: printf("None");
case 'c': printf("%d",5); break ;
}
}
Output ______________________
Duplicate case labels are not allowed
#include <stdio.h>
int main(){
switch(4){
case 2: printf("%d",4); break ;
case 1+1: printf("%d",2); break ;
default: printf("None");
}
}
Output:____________
#include <stdio.h>
int main(){
int a=2;
switch(4){
case 2: printf("%d",4); break ;
case a: printf("%d",2); break ;
default: printf("None");
}
}
FOR LOOP
for (expe1;expr2;expr3)
expr1 is initialization
expr2 is condition checking
expr3 is increment or decrement
Note :
Iteration no 6: condition check 6 <= 5; statement is False, block will not execute.
Decrement loop
for (i=5;i>=5; i--){
printf(“I am a good Student”);
}
Condition < and >
for (i=1;i<5; i++){
printf(“I am a good Student”);
}
Question number of times loop executed.
for (i=1;i>5; i++){
printf(“I am a good Student”);
}
Answer: 0 times
Question number of times loop executed.
for (i=1;i<=10; i++){
printf(“I am a good Student”);
}
Answer 10 times
Skipping initialization
int i = 1;
for(;i<=10;i++){
printf(“I am a good Student”);
}
Skip iteration:
int i = 1;
for(;i<=10;){
printf(“I am a good Student”);
}
int i = 1;
for(;; i++){
printf(“I am a good Student”);
}
If condition is empty then it’s always evaluated true.
True means infinite loop
}
This leads to infinite loop
Answer is infinite
int i;
for (i=1;i <= n;i=i+2){
printf(“I am a good Student”);
}
n
The number of times the loop executed in n is even
2
n
The number of times the loop executed in n is even
2
Multiplication factor in increment
int i;
for (i=2;i <= n;i=i*2){
printf(“I am a good Student”);
}
The number of times the loop executed is log n
Question
The number of times loop executed is
#include<stdio.h>
int main(){
int i,count=0;
for (i=1; i<=100; ){
printf("\nI am a good Student");
i = i+3;
}
return 0 ;
}
(A) 33
(B) 34
(C) 100
(D) 10
Answer
B
Sum of n natural Number
What is the output of the program
#include<stdio.h>
int main(){
int i,sum=0;
for (i=1; i<=100;i++){
sum= sum+i;
}
printf("%d",sum);
return 0 ;
}
(A) 100
(B) 5050
(C) 10100
(D) 10000
Wrong progra
What is the output of the program
#include<stdio.h>
int main(){
int i,j,sum=0;
for (i=1; i<=10;i++){
sum= sum+j*j;
j=j+1;
}
printf("%d",sum);
return 0 ;
}
(A) 385
(B) 55
(C) 0100
(D) 10000
Answer
A
Nested loop/ if statement
#include <stdio.h>
int main() {
int i = 10;
printf("Enter the number");
scanf("%d",&i);
if (i > 10) {
// First if statement
if (i < 15)
printf("i is smaller than 15\n");
// Nested - if statement Will only be executed if statement
above is true
if (i < 13)
printf("i is smaller than 13 too\n");
else
printf("i is greater than 13");
}
return 0;
}
#include <stdio.h>
int main() {
int i,j;
for(i=1;i<5;i++){
for(j=1;j<5;j++){
printf("*");
}
printf("\n");
}
return 0;
}
Output
*****
*****
*****
*****
Mathematical aspect:
n + n + n + n + n + ... + n = n2
Second example
#include <stdio.h>
int main() {
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=i;j++){
printf("*");
}
printf("\n");
}
return 0;
}
Output
*
**
***
****
*****
n(n + 1)
Mathematical Aspect
2
Third Pattern
#include <stdio.h>
int main() {
int i,j;
for(i=1;i<=5;i++){
for(j=i;j<=5;j++){
printf("*");
}
printf("\n");
}
return 0;
}
Output
*****
****
***
**
*
Mathematical Aspect
n(n + 1)
2
Question:
Some programming
1, Find exponentiation xn
2. find factorial of a number
3. find number is prime or not
4. print 10 Fibonacci sequence
Answer 1
#include <stdio.h>
int main() {
int x = 2,n=7;
int i;
int power =1;
for(i=1;i<=n;i++){
power = power*x;
}
printf("%d",power);
return 0;
}
Answer 2
#include <stdio.h>
int main() {
int n=5;
int i;
int factorial=1;
for(i=2;i<=n;i++){
factorial = factorial * i;
}
printf("%d",factorial);
return 0;
}
Answer 3
#include <stdio.h>
int main() {
int n=101;
int i;
int flag=0;
for(i=2;i<n;i++){
if(n%i==0){
flag=1;
break;
}
}
if (flag ==0)
printf("Number is prime");
else
printf("Number is not prime");
return 0;
}
Answer 4
Do it by your self
while loop
The while loop loops through a block of code as long as a specified condition
is true:
while (condition) {
// code block to be executed
}
Example
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
}
int i = 0; for(i=0;i<5;i++){
Question:
GATE 2014
int count = 0;
while (num) {
count++;
num>>= 1;
return (count);
Solution:
Ans – 9
Question consider the following program
#include <stdio.h>
int main() {
int i=10;
for (; i<=100; i++);{
printf("\nI am a good Student");
}
printf("%d",i);
return 0 ;
}
The value of I is x and number of print statement is y then value of x+y is
Answer
102
int main() {
int i=2+4%6+9/10;
while (i<10){
i++;
return 0 ;
Answer
4
GATE 2024 set-1
• Simple Loops
• Nested Loops
• Infinite Loops
• Switch case
Example
#include <stdio.h>
int main(){
printf("break in for loop\n");
for (int i = 1; i < 5; i++) {
if (i == 5) {
break;
}
else {
printf("%d ", i);
}
}
printf("\nbreak in while loop\n");
int i = 1;
while (i < 20) {
if (i == 5)
break;
else
printf("%d ", i);
i++;
}
return 0;
}
Output
break in for loop
1 2 3 4
break in while loop
1 2 3 4
Example 2
Consider the following program
#include <stdio.h>
for(int i=1;i<=3;i++){
for(int j=1;j<=20;j++){
printf("I am good student\n");
if(i==2) break;
}
}
return 0;
}
Example 1
Consider the following program
#include<stdio.h>
int main () {
if (i == 5||i==6) {
continue;
printf("%d\n", i);
return 0;
}
Answer : 8
Do while loop
C.8
The do…while in C is a loop statement used to repeat some part of the code till the given condition is
fulfilled. It is a form of an exit-controlled or post-tested loop where the test condition is checked after
executing the body of the loop. Due to this, the statements in the do…while loop will always be executed
at least once no matter what the condition is.
d. 11
Syntax of do…while Loop in C
do {
// body of do-while loop
} while (condition);
Example 1
#include <stdio.h>
int main(){
int i = 0;
do {
printf("I am good Student\n");
i++;
} while (i < 3);
return 0;
}