L1.Q1: Write The Output of The Following Program
L1.Q1: Write The Output of The Following Program
#include
#define ABC 20
#define XYZ 10
#define XXX ABC - XYZ
void main()
{
int a;
a = XXX * 10;
printf("%d\n", a);
}
#include
#define calc(a, b) (a * b) / (a - b)
void main()
{
int a = 20, b = 10;
#include
void main()
{
int cnt = 5, a;
do {
a /= cnt;
} while (cnt --);
#include
void main()
{
int a, b, c, abc = 0;
a = b = c = 40;
if (c) {
int abc;
abc = a*b+c;
}
#include
main()
{
int k = 5;
printf("%d\n", k);
}
#include
main()
{
int a = 5;
fn(a, a++);
}
void fn(int a, int b)
{
printf("Fn : a = %d \t b = %d\n", a, b);
}
Answers
L1.A1
Solution for L1.Q1
a = xxx * 10
which is => a = ABC - XYZ * 10
=> a = 20 - 10 * 10
=> a = 20 - 100
=> a = -80
L1.A2
Solution for L1.Q2
L1.A3
Solution for L1.Q3
a /= cnt; /* ie. a /= 0 */
which leads to divide-by-zero error.
L1.A4
Solution for L1.Q4
the result will be c = 40 and abc = 0;
because the scope of the variable 'abc' inside if(c) {.. }
is not valid out side that if (.) { .. }.
L1.A5
Solution for L1.Q5
L1.A6
Solution for L1.Q6
Main : 5 7 Fn : 7 7
Main : 6 6
Fn : 8 7
#include
#define ABC 20
#define XYZ 10
#define XXX ABC - XYZ
void main()
{
int a;
a = XXX * 10;
printf("%d\n", a);
}
#define calc(a, b) (a * b) / (a - b)
void main()
{
int a = 20, b = 10;
#include
void main()
{
int cnt = 5, a;
do {
a /= cnt;
} while (cnt --);
#include
void main()
{
int a, b, c, abc = 0;
a = b = c = 40;
if (c) {
int abc;
abc = a*b+c;
}
#include
main()
{
int k = 5;
printf("%d\n", k);
}
#include
main()
{
int a = 5;
fn(a, a++);
}
Answers
L1.A1
Solution for L1.Q1
a = xxx * 10
which is => a = ABC - XYZ * 10
=> a = 20 - 10 * 10
=> a = 20 - 100
=> a = -80
L1.A2
Solution for L1.Q2
L1.A3
Solution for L1.Q3
a /= cnt; /* ie. a /= 0 */
which leads to divide-by-zero error.
L1.A4
Solution for L1.Q4
L1.A5
Solution for L1.Q5
L1.A6
Solution for L1.Q6
Main : 6 6
Fn : 8 7
L3.Q1 :
L3.Q2 :
L3.Q3 :
L3.Q4 :
- A macro which can swap any type of data (ie. int, char,
float, struct, etc..)
L3.Q5 :
Write a program to delete the entry from the doubly linked
list without saving any of the entries of the list to the
temporary variable.
#include
main()
{
int *a, *savea, i;
#include
a *= a;
strcat(b, "func1 ");
return (fn(a, b));
}
main()
{
abc *f1, *f2;
int res;
static char str[50] = "hello! ";
f1 = func1;
res = f1(10, str);
f1 = func2;
res = f1(res, str);
LX.Q8 :
#include
main()
{
int a=3, b = 5;
LX.Q10 :
Answers
L3.A1
Solution for L3.Q1
#include
len--;
while(i < j) {
*(str+i)^=*(str+len)^=*(str+i)^=*(str+len);
i++; len--;
}
return(str);
}
L3.A2
Solution for L3.Q2
#include
void main()
{
int i;
L3.A3
Solution for L3.Q3
#include
main()
{
int a, b;
L3.A4
Solution for L3.Q4
#include
/* Verification routines */
main()
{
int a=10, b =20;
float e=10.0, f = 20.0;
char *x = "string1", *y = "string2";
typedef struct { int a; char s[20]; } st;
st s1 = {50, "struct1"}, s2 = {100, "struct2"};
swap(a, b, int);
printf("%d %d\n", a, b);
swap(e, f, float );
printf("%f %f\n", e, f);
ptr_swap();
}
ptr_swap()
{
int *a, *b;
float *c, *d;
a = (int *) malloc(sizeof(int));
b = (int *) malloc(sizeof(int));
*a = 10; *b = 20;
c = (float *) malloc(sizeof(float));
d = (float *) malloc(sizeof(float));
*c = 10.01; *d = 20.02;
L3.A5
Solution for L3.Q5
#include
/* Solution */
while ((*head)) {
if ((*head)->next == NULL) tail = head;
if ((*head)->val == val) {
*head = (*head)->next;
}
else head = &(*head)->next;
}
while((*tail)) {
if ((*tail)->val == val) {
*tail = (*tail)->prev;
}
else tail= &(*tail)->prev;
}
}
Link *DL_build();
void DL_print(Link *);
main()
{
int val;
Link *head;
head = DL_build();
DL_print(head);
DL_delete(&head, val);
DL_print(head);
}
Link *DL_build()
{
int val;
Link *head, *prev, *next;
while(1) {
Link *new;
if (val == 0) break;
prev = new;
}
return (head);
}
L3.A6
Solution for L3.Q6
The first value will be 0, the rest of the three values will
not be predictable. Actually it prints the values of the
following location in each step
* savea
* (savea + sizeof(int) * sizeof(int))
etc...
Note: You can verify the above by varing the type of 'savea'
variable to char, double, struct, etc.
LX.A7
Solution for LX.Q7
LX.A8
Solution for LX.Q8
#include
while(head) {
Link *tmp;
tmp = head;
head = head->next;
tmp->next = revlist;
revlist = tmp;
}
return revlist;
}
Link *SL_build();
main()
{
Link *head;
head = SL_build();
head = SL_reverse(head);
printf("\nReversed List\n\n");
while(head) {
printf("%d\t", head->val);
head = head->next;
}
}
Link *SL_build()
{
Link *head, *prev;
while(1) {
Link *new;
int val;
Extend the same logic to this problem. You will get the
output as follows