Professional Documents
Culture Documents
C 2
C 2
Questions
L2.Q1 : Write the output of this program
#include
main()
{
int *a, *s, i;
s = a = (int *) malloc( 4 * sizeof(int));
for (i=0; i<4; i++) *(a+i) = i * 10;
printf("%d\n", *s++);
printf("%d\n", (*s)++);
printf("%d\n", *s);
printf("%d\n", *++s);
printf("%d\n", ++*s);
}
Solution for L2.Q1
L2.Q2 : Checkout this program result
#include
void fn(int);
static int val = 5;
main()
{
while (val --) fn(val);
printf("%d\n", val);
}
void fn(int val)
{
static int val = 0;
for (; val < 5; val ++) printf("%d\n", val);
}
Solution for L2.Q2
L2.Q3 : Can you predict the output of this program ?
#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;
printf ("Union 2 : %d %s %f\n", x.a, x.b, x.c);
printf ("Union Y : %d %s %f\n", y.a, y.b, y.c);
}
Solution for L2.Q3
L2.Q4 : Print the output of the program
#include
main()
{
struct Data {
int a;
int b;
} y[4] = { 1, 10, 3, 30, 2, 20, 4, 40};
struct Data *x = y;
int i;
for(i=0; i<4; i++) {
x->a = x->b, ++x++->b;
printf("%d %d\t", y[i].a, y[i].b);
}
}
Solution for L2.Q4
L2.Q5 : Write the output of this program
#include
main()
{
typedef struct {
int a;
int b;
int c;
char ch;
int d;
}xyz;
typedef union {
xyz X;
char y[100];
}abc;
printf("sizeof xyz = %d sizeof abc = %d\n",
sizeof(xyz), sizeof(abc));
}
Solution for L2.Q5
L2.Q6 : Find out the error in this code
#include
#include
#define Error(str) printf("Error : %s\n", str); exit(1);
main()
{
int fd;
char str[20] = "Hello! Test me";
if ((fd = open("xx", O_CREAT | O_RDWR)) < 0)
Error("open failed");
if (write(fd, str, strlen(str)) < 0)
Error("Write failed");
if (read(fd, str, strlen(str)) < 0)
Error("read failed");
printf("File read : %s\n", str);
close(fd);
}
Solution for L2.Q6
L2.Q7 : What will be the output of this program ?
#include
main()
{
int *a, i;
a = (int *) malloc(10*sizeof(int));
for (i=0; i<10; i++)
*(a + i) = i * i;
for (i=0; i<10; i++)
printf("%d\t", *a++);
free(a);
}
Solution for L2.Q7
L2.Q8 :
Write a program to calculate number of 1's (bit) in a given
integer number i.e) Number of 1's in the given integer's
equivalent binary representation.
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.
#define Error(str) { printf("Error : %s\n", str); exit(1); }
L2.A7
Solution for L2.Q7
This program will fault (Memory fault/segmentation fault).
Can you predict Why?
Remove the statment 'free(a);' from the program, then
execute the program. It will run. It gives the results
correctly.
What causes 'free(a)' to generate fault?
Just trace the address location of pointer variable 'a'.
The variable 'a' is incremented inside the 'for loop'. Out
side the 'for loop' the variable 'a' will point to 'null'.
When the free() call is made, it will free the data area
from the base_address (which is passed as the argument of
the free call) upto the length of the data allocated
previously. In this case, free() tries to free the length
of 10 *sizeof(int) from the base pointer location passed as
the argument to the free call, which is 'null' in this case.
Thus, it generates memory fault.
L2.A8
Solution for L2.Q8
#include
main(argc, argv)
int argc;
char *argv[];
{
int count = 0, i;
int v = atoi(argv[1]);
for(i=0; i<8*sizeof(int); i++)
if(v &(1<