ESC111 Tutorial 4
ESC111 Tutorial 4
their own variable names without interfering, they use the same
#include <stdio.h>
int swap(int a, int b) {
int t = a;
a = b;
b = t;
printf("%d %d\n", a, b);
return 0;
}
int main() {
int a = 3;
int b = 10;
swap(b, a);
printf("%d %d\n", a, b);
return 0;
}
Output:
3 10
3 10
For first 3, 10- arguments are passed in opposite order(a and b are switched twice, once while
passing argument and once inside swap function before swapping. )
The value of a and b does not change inside main.
Ans is the variable we currently are checking is the GCD. It is set to the smaller of the two
numbers a and b as this is the highest possible factor or GCD to start checking with. The while
loop should contain ans>=1. The if statement should be a % ans == 0 && b % ans == 0,
within which we break out of the loop, and return ans outside.
Q3. Identify if the below snippets have any errors or not. If any,
correct the minimum possible code lines else write the output:
(a)
#include <stdio.h>
void introduction()
{
printf("Hi\n");
printf("I am Mr. C\n");
}
int main()
{
introduction();
return 0;
}
(b)
#include <stdio.h>
int main()
{
float m, n;
//Enter some number for finding square
scanf("%f", &m);
n = square(m);
printf("\nSquare of the given number %f is %f", m, n);
}
void square(float x)
{
float p;
p = x * x;
return p;
}
int global_var = 5;
void function() {
static int static_var = 5;
int local_var = 5;
static_var++;
local_var++;
global_var++;
}
int main() {
function();
function();
function();
return 0;
}
(a)
#include <stdio.h>
int main()
{
printf("This is %d", main);
return 0;
}
This prints the address of the main function. NOTE: We don’t call the
function(notice the absence of parentheses).
(b)
#include <stdio.h>
int main()
{
printf("This is %d", main());
return 0;
}
This errors out due to calling the main() function infinitely many times.
Q6. Describe the output/behaviour of the following programs.
Give suitable reasons for the same(Using macros).
#include <stdio.h>
#define X 3
int main(){
if(!X)
printf("esc_quiz");
else
printf("esc_endsem");
return 0;
}
// Function Call
CountingEvenOdd(arr, n);
}