Assignment 2(Trace output) Sem-II
Assignment
2 ( Trace Output)
1) What
will be output
of the following program?
#include<stdio.h>
int main(){
int x;
x=10,20,30;
printf("%d",x);
return 0;
}
Explanation:
Precedence table:
|
Operator |
Precedence |
Associative |
|
= |
More than , |
Right to left |
|
, |
Least |
Left to right |
Since assignment
operator (=) has more precedence than comma
operator .So = operator will be evaluated first than comma operator.
In the following expression
x = 10, 20, 30
First 10 will be assigned
to x then comma operator will be evaluated.
2)
#include<stdio.h>
int
main()
{
char
arr[11]="The African Queen";
printf("%s",arr);
return
0;
}
Explanation:
Size
of any character array cannot be less than the number of characters in any
string which it has assigned. Size of an array can be equal (excluding null
character) or greater than but never less than.
3)
#include<stdio.h>
int
main()
{
int
a=2,b=7,c=10;
c=a==b;
printf("%d",c);
return 0;
}
Explanation:
==
is relational operator which returns only two values.
0:
If a == b is false
1:
If a == b is true
Since
a=2
b=7
So,
a == b is false hence c=0
4)
#include<stdio.h>
int main()
{
int
max-val=100;
int
min-val=10;
int
avg-val;
avg-val
= max-val + min-val / 2;
printf("%d",avg-val);
return
0;
}
Explanation:
Compilation error
We cannot
use special character – in the variable name.
5) #include<stdio.h>
int main(){
char arr[20]="MysticRiver";
printf("%d",sizeof(arr));
return 0;
}
Output-20
6) #include <stdio.h>
int main() {
char
*s="hello";
char *p=s;
printf("%c\t%c",*(p+3),s[1]);
return 0;
}
Output –l e
7) #include <stdio.h>
void main()
{
char
nm[]={'A', 'N', 'S', 'I', 0, 'C', '\0'};
int x=0;
while(nm[x]!=
'\0')
printf("%c",nm
[x++]);
}
Output-ANSI
8) #include<stdio.h>
#include<string.h>
int main()
{
char str1[20]="Hello", str2[20]="World";
printf("%s\n",strcpy(str2,strcat(str1, str2)));
return 0;
}
Output-
HelloWorld
9) struct
student
{
char *name;
};
struct student s[2]
void main()
{
s[0].name = "alan";
s[1]=s[0];
printf("%s%s",s[0].name,s[1].name);
s[1].name = "turing";
printf("%s%s",s[0].name,s[1].name);
}
Output-alanalanalanturing
10) #include<stdio.h>
struct student
{
};
void main()
{
struct student s[2];
printf("%d",sizeof(s));
}
Output-0
11) #include
<stdio.h>
int main ( )
{
static char name [] =“programming”;
char *s ;
s =&name[6]-4;
while(*s)
printf ("%c",*s++);
}
Output-
ogramming
12) #include
<stdio.h>
#define Test(x) (x * x)
int main ( )
{
int a, b = 3;
a=Test(b+3);
printf (“%d - %d”, a, b);
}
Output-
15 - 3
13) struct
employee
{
char ename [20];
int empno;
} el,* ptr,e[20];
int main ( )
{
printf("\n%d",sizeof(el));
printf("\n%d",sizeof(ptr));
printf("\n%d”,sizeof(el));
}
Output-
24
8
24
Comments
Post a Comment