Anda di halaman 1dari 10

ASSIGNMENT-1

Comma as an operator: The comma operator (represented by the token ,) is


a binary operator that evaluates its first operand and discards the result, it
then evaluates the second operand and returns this value (and type). The
comma operator has the lowest precedence of any C operator, and acts as a
sequence point.

Ex-1

int main()
{
int i=1,2,3;
int j=(1,2,3);
printf("%d\n",i);
printf("%d\n",j);
}
Ans:-

Comma works just as a separator in Ex-11 and we get compilation error in


this program.

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

Since 0.5 has an exact value in binary(.1b) it is stored as 0.5itself.Thus it


returns true.

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

Hello is stored in 6 bytes as Hello/0, where /0 indicates the termination


of the string. Thus first output is 6. Strlen() function counts the number of
characters in a string until it encounters the null character, and since there
are 5 characters before this. Thus it returns 5 and 5 is printed.

Ex-6

#include <stdio.h>
int main()
{
int a;
printf("%d",scanf("%d",&a));
return 0;
}
Ans:- 1

Scanf is a return type function, it returns the number of variables correctly


assigned and stored. Thus, in this case, it returns 1, so long as a correct input
is provided.

Ex-7

#include <stdio.h>
int main()
{
char str[] = {'a','b','c','\0'};
str[0] -= 32;
printf("%s",str);

return 0;
}
Ans:- Abc

Str[0]=a. When str[0]-=32 is executed, the ASCII value of str[0] becomes


97-32=65, which is the ASCII value of A. Thus, str[0] has A stored in it, and
output is 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

The character array is initialized to abcpppp, and is stored as abcpppp\0,


where \0 is the null character. Printf executes the printing of a string until it
encounters the null character, and so, abcpppp is printed.

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()

if statement has to enclose a valid expression in between the parentheses.


As nothing is given within the braces in this case, it is an error.

Ex-12
int main()
{
char a[] = "Hello World";
char* p = &a;
printf("%s", p+2 );
}
Ans:- llo world

p is initialized to point at string a, it automatically points at a[0]. Thus, when


p+2 is given, it points to p[2] which is l. Hence the string starts printing from
l till it reaches null character.

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.

Anda mungkin juga menyukai