Académique Documents
Professionnel Documents
Culture Documents
Ex-1
int main()
{
int i=1,2,3;
int j=(1,2,3);
printf("%d\n",i);
printf("%d\n",j);
}
Ans:-
Ex-2
int main()
{
int i;
i=1,2,3;
int j=(1,2,3);
printf("%d\n",i);
printf("%d\n",j);
}
Ans:- Comma works as an operator in Ex-2. Precedence of comma operator
is least in operator precedence table. So the assignment operator takes
precedence over comma and the expression a = 1, 2, 3 becomes equivalent
to (a = 1), 2, 3. That is why we get output as 1 and for the statement int
j=(1,2,3); brackets are used so comma operator is executed first and we get
the output as 3.
Ex-3
int main()
{
float g=0.7;
if(g==0.7)
printf("YES");
else
printf("NO");
}
Ans:- NO
Internally, 0.7 is stored as 0.69999, because there is no match for 0.7. When
it is compared with a constant float 0.7 false is returned.
Ex-4
int main()
{
float g=0.5;
if(g==0.5)
printf("YES");
else
printf("NO");
}
Ans:- YES
Ex-5
#include <stdio.h>
#include <string.h>
int main()
{
char string[] = "Hello";
printf("%u %u",sizeof(string),strlen(string));
return 0;
}
Ans:- 6 5
Ex-6
#include <stdio.h>
int main()
{
int a;
printf("%d",scanf("%d",&a));
return 0;
}
Ans:- 1
Ex-7
#include <stdio.h>
int main()
{
char str[] = {'a','b','c','\0'};
str[0] -= 32;
printf("%s",str);
return 0;
}
Ans:- Abc
Ex-8
#include <stdio.h>
int main()
{
unsigned int a = 5;
if(a > -1)
printf("5 is > -1\n");
return 0;
}
Ans:- No output.
Unsigned int has its range starting from 0. Thus, if we try to compare it with
any value less than zero, the expression is invalid, and thus, a false value is
returned and the printf statement is not executed.
Ex-9
#include <stdio.h>
int main()
{
char buff[255] = "abcpppp";
printf("%s",buff);
return 0;
}
Ans:- abcpppp
Ex-10
#include <stdio.h>
int main()
{
char buff[255] = "\0";
printf("%s", buff);
return 0;
}
Ans:- No output.
Since printf prints a string until null character is encountered, and there are
no characters before \0 in buff, there is no output.
Ex-11
#include <stdio.h>
int main()
{
if()
{
printf("Hello ");
}
else
{
printf("World\n");
}
return 0;
}
Ans:- error: expected expression before ) token
if()
Ex-12
int main()
{
char a[] = "Hello World";
char* p = &a;
printf("%s", p+2 );
}
Ans:- llo world
Ex-13
int main()
{
{
char a = 5;
int b = 5;
if(a == b)
printf("char and int compared equal\n"); }
{
int a = 5;
long int b = 5;
if(a == b)
printf("int and long compared equal\n");
}
{
float a = 5.0;
double b = 5.0;
if(a == b)
printf("float and double compared equal\n");
}
{
float a = 5.2;
double b = 5.2;
if(a == b)
printf("float and double again compared equal\n");
}
{
float a = 5.2;
if(a == 5.2)
printf("float compared equal with constant\n");
}
{
double a = 5.2;
if(a == 5.2)
printf("double compared equal with constant\n");
}
return 0;
}
Ans:-
char and int compared equal when a and b are compared, the ASCII value
of a is compared with integer value of b. Since both are equal to 5, the
statement in if condition is printed.
int and long compared equal Both int and long int values are stored as 5.
Thus, when they are compared, true value is returned and statement is printed
float and double compared equal Internally, both are stored as 5.0. Thus,
when they are both compared, the expression returns true
5.2 does not have an exact binary equivalent when stored as a float. Thus,
when it is compared with constant float 5.2, it returns false.
double compared equal with constant Internally, all doubles are stored as
their correct values, regardless of whether they have a binary equivalent or
not. Thus, when compared with constant 5.2, a true value is returned.
Ex-14
int main()
{
char a= 398;
printf("%d",a);
printf("%c",a);
}
Ans:- -114
398 is out of index range of unsigned char. Thus, the assignment circles back
to negative numbers and -114 is assigned.
Ex-15
int main()
{
//int a= -234;
int a=234567891234567789;
printf("%u\n",a);
printf("%d",a);
}
Ans:-Warning message indicating overflow condition
415217261
415217261
The range for signed int is -65535 to 65535. The given number is out of range.
Hence there is an overflow issue. The assignment circles back to negative
numbers and thus output is a negative number.