Professional Documents
Culture Documents
Questions
*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
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
L1.A4
Solution for L1.Q4
L1.A5
Solution for L1.Q5
L1.A6
Solution for L1.Q6
Main : 5 7 Fn : 7 7
Main : 6 6
Fn : 8 7
Questions
*L2.Q1 : Write the output of this program
#include
main()
{
int *a, *s, i;
printf("%d\n", *s++);
printf("%d\n", (*s)++);
printf("%d\n", *s);
printf("%d\n", *++s);
printf("%d\n", ++*s);
}
#include
void fn(int);
main()
{
while (val --) fn(val);
printf("%d\n", val);
}
#include
main()
{
typedef union {
int a;
char b[10];
float c;
} Union;
Union x, y = { 100 };
x.a = 50;
strcpy (x.b, "hello");
x.c = 21.50;
#include
main()
{
struct Data {
int a;
int b;
} y[4] = { 1, 10, 3, 30, 2, 20, 4, 40};
struct Data *x = y;
int i;
#include
main()
{
typedef struct {
int a;
int b;
int c;
char ch;
int d;
}xyz;
typedef union {
xyz X;
char y[100];
}abc;
#include
#include
main()
{
int fd;
char str[20] = "Hello! Test me";
#include
main()
{
int *a, i;
a = (int *) malloc(10*sizeof(int));
free(a);
}
Answers
L2.A1
Solution for L2.Q1
L2.A2
Solution for L2.Q2
L2.A3
Solution for L2.Q3
L2.A5
Solution for L2.Q5
L2.A6
Solution for L2.Q6
Just try to execute this file as such. You can find out
that it will exit immediately. Do you know why?
With this hint, we can trace out the error. If you look
into the macro 'Error', you can easily identify that there
are two separete statements without brases '{ ..}'. That is
the problem. So, it exits after the calling open(). The
macro should be put inside the brases like this.
L2.A7
Solution for L2.Q7
L2.A8
Solution for L2.Q8
#include
main(argc, argv)
int argc;
char *argv[];
{
int count = 0, i;
int v = atoi(argv[1]);
Questions
L3.Q1 :
L3.Q3 :
L3.Q4 :
- A macro which can swap any type of data (ie. int, char,
float, struct, etc..)
L3.Q5 :
#include
main()
{
int *a, *savea, i;
savea = a = (int *) malloc(4 * sizeof(int));
#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);
#include
main()
{
int a=3, b = 5;
LX.Q10 :
Answers
L3.A1
Solution for L3.Q1
#include
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
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;
new = (Link *) malloc(sizeof(Link));
new->val = val;
new->prev = prev;
new->next = next;
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.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;
LX.A9
Solution for LX.Q9
Extend the same logic to this problem. You will get the
output as follows