0% found this document useful (0 votes)
28 views12 pages

CP Tutorial - Answers

The document provides answers to questions about output in C programming tutorials. It explains that when a signed char is assigned a value of 128, it takes the value -128 instead because -128 is within the valid range for a signed char (-128 to 127) while 128 is not. It also explains the cyclic nature of data types in C and how assigning a value outside the range wraps around. The document clarifies that there should be no space after the last format specifier in scanf statements. It provides examples to illustrate order of operations and precedence rules for arithmetic operators and unary operators in C.

Uploaded by

star lord
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views12 pages

CP Tutorial - Answers

The document provides answers to questions about output in C programming tutorials. It explains that when a signed char is assigned a value of 128, it takes the value -128 instead because -128 is within the valid range for a signed char (-128 to 127) while 128 is not. It also explains the cyclic nature of data types in C and how assigning a value outside the range wraps around. The document clarifies that there should be no space after the last format specifier in scanf statements. It provides examples to illustrate order of operations and precedence rules for arithmetic operators and unary operators in C.

Uploaded by

star lord
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CP Tutorial: Answers to output questions

Tutorial 2

nn It is accepting three -128


numbers instead of two. Reason: Because signed char is assigned value
nn Because there is a space128, the range is -128 to +127, since assigned
nn /n/n nn/n after last %d in scanf value 128 is outside the range, hence, first
statement. number from negative side of the range i.e. -
128 is assigned to chr, due to cyclic nature of
There should be no C data types. This happens for all the data
space after last %d in types in C when assigned value goes outside
scanf statement, the range.
otherwise it behaves in
odd way. Refer the following link:
https://www.cquestions.com/2009/06/cyclic-
nature-of-data-type-in-c.html

Logic:
We know that computer uses 2’s complement
to represent data. For example, if we have 1
byte, we can represent -128 to 127. If we add
1 to 127 we will get -128. That’s because 127
is 01111111 in binary. And if we add 1 into
01111111, we will get 10000000. 10000000 is
a negative number due to MSB being a sign
bit, sign bit as 1 denotes negative number and
is -128 in 2’s complement form.

Tutorial 3

Compile Error: invalid operands a=0b=-6 -1, 0, 6, 7, 8, 9


to binary % (have 'float' and
'float') a = -3 - - 3; enum is enumerated data type.
Cannot perform modulus on as per precedence and So MON is assigned value -1,
float numbers associativity, unary minus with the next possible value will
3 is first encountered and have a incremented value, so
combined with a minus sign, it TUE became 0, WED is again
becomes plus. assigned value externally as 6,
a = -3 - - 3; next possible values will have
a = -3 + 3; incremented values, so THU
a=0 will be 7, FRI will be 8 and SAT
will be 9.
b = -3 - - ( - 3 ) ;
b = -3 - - ( - 3 ) ;
b = -3 - +3;
again unary plus has higher
precedence.
b = -3 -3 ;
b = -6
1 1, 40, 1 ffffffdf

< has left to right associativity. Step 1: int x=55; here m = 32


Since x < y turns to be TRUE it variable x is declared as an Write 32 in binary.
is replaced by 1. Then 1 < z is integer type and initialized to Since int is of 4 bytes, write
compared and to be TRUE. The '55'. entire number and bitwise not
1 is assigned to i. is to be performed.
Step 2: printf("%d, %d, %d\n",
x<=55, x=40, x>=10); 11111111 11111111 11111111
In printf, the execution of 11011111 = ~m = ~32
expressions is from Right to
Left. Now, format specifier is %x,
that means hexadecimal
here x>=10 returns TRUE number, so ffffffdf.
hence it prints '1'.
If format specifier is %d,
x=40 here x is assigned to 40 answer would be -33. [find
Hence it prints '40'. using 1’s and 2’s
completement method]
x<=55 returns TRUE. hence it
prints '1'. If format specifier is %u,
answer would be 4294967263.
Step 3: Hence the output is "1,
40, 1".
0.000000 512 Nirma5

5.1 is converted to int, i.e. 5 1 << 2 + 3 << 4 printf statement is written


2.1 is converted to int, i.e. 2 Precedence of + is more than inside a printf statement with
<<. So, 1 << 5 << 4 %d as format specifier.
5%2 = 1 Now, << has left to right
associativity. %d counts the number of
But format specifier is %f, 1 << 5 = 32 characters in the string
hence, int and float does not 32 << 4 = 512 printed.
match, so answer is 0.000000
Nirma = 5
If format specifier is %d,
answer would be 1. So, string is printed and also
number of characters is
printed.
-2, 3, 1, 1 -2, 2, 0, 1 z=1

Step 1: int i=-3, j=2, k=0, Step 1: int i=-3, j=2, k=0, Step 1: int x=12, y=7, z; here
m; here variable i, j, k, m are m; here variable i, j, k, m are variable x, y and z are declared
declared as an integer type declared as an integer type as an integer and
and variable i, j, k are and variable i, j, k are variable x and y are initialized
initialized to -3, 2, 0 initialized to -3, 2, 0 to 12, 7 respectively.
respectively. respectively.
Step 2: z = x!=4 || y == 2;
Step 2: m = ++i && ++j && ++k; Step 2: m = ++i || ++j && != and == has higher
becomes m = -2 && 3 && 1; ++k; here (++j && ++k;) this precedence than ||.
becomes m = TRUE && code will not get executed
TRUE; (Apply logic of short- because ++i has non-zero So, z = 12!=4 || 7 == 2;
circuit AND). value. (Apply logic of short- then z = (condition true) ||
circuit AND). (condition false); Hence it
Hence this statement becomes returns 1. So the value of z=1.
TRUE. So it returns '1'(one). becomes m = -2 || ++j && ++k;
Step 3: printf("z=%dn",
Hence m=1. becomes m = TRUE || ++j &&
z); Hence the output of the
++k; Hence this statement
program is "z=1".
Step 3: printf("%d, %d, %d, becomes TRUE. So it returns
%d\n", i, j, k, m); In the '1'(one). Hence m=1.
previous step the value of i,j,k
are incremented by '1'(one). Step 3: printf("%d, %d, %d,
%d\n", i, j, k, m); In the
Hence the output is "-2, 3, 1,
previous step the value of
1".
variable 'i' only incremented
by '1'(one). The variable j,k are
not incremented.
Hence the output is "-2, 2, 0,
1".

2, 3, 3 30 Compile Error

Step 1: int x=4, y, z; here Step 1: int k, num=30; here Because ++ or -- cannot be
variable x, y, z are declared as variable k and num are done on enum value.
an integer type and variable x is declared as an integer
initialized to 4. type and
variable num is initialized to
Step 2: y = --x; becomes y =
'30'.
3; because (--x) is pre-
decrement operator. Step 2: k = (num>5 ? (num
<=10 ? 100 : 200): 500); This
Step 3: z = x--; becomes z = 3;. statement does not affect the
In the next step output of the program.
variable x becomes 2, because Because we are going to print
(x--) is post-decrement the variable num in the next
operator. statement.

Step 4: printf("%d, %d, %d\n", Step 3: printf("%d\\n", num); It


x, y, z); Hence it prints "2, 3, 3". prints the value of
variable num '30'

If k is printed, output will be


200. Explain ternary operator.

Tutorial 4

Step 1: int a = 300, b, c; here variable a is x = 3, condition of (x ==3) is satisfied, so 3 is


initialized to '300', variable b and c are declared, printed.
but not initialized. Then, else is written with a semicolon is written,
so printf ( "\n%d", y ) ; is not a part of else
block, it’s a separate statement.
So, 5 is also printed.
Step 2: if(a >= 400) means if(300 >= 400). Hence
this condition will be failed. Hence, the output of the program is
newline
Step 3: c = 200; here variable c is initialized to 3
'200'. 5
Step 4: printf("%d, %d, %d\n", a, b, c); It prints
"300, garbage value, 200". because variable b is
not initialized.

Hence, the output of the program is


300, Garbage, 200
if (x == y) here we are comparing if (3 == 3.0) switch(i) has the variable i it has the value
hence this condition is satisfied. Because we '1'(one).
cannot compare int Then case 1: statements got executed. so, it
and float so the int is converted to float and then prints "Hi". The break; statement make the
compared. Hence it prints “x and y are equal”. program to be exited from switch-case
statement.
Answer : x and y are equal switch-case do not execute any statements
outside these blocks case and default
Hence the output is "Hi".
It will show compile time error. In the very beginning of switch-case
statement default statement is encountered. So,
The keyword continue cannot be used in switch it prints "This is default".
case. It must be used in for or while or do
while loop. If there is any looping statement in In default statement there is
switch case then we can use continue. no break; statement is included. So, it prints
the case 1 statements. "This is case 1".

Then the break; statement is encountered.


Hence the program exits from the switch-case
block.

Hence, the output is


This is default
This is case 1
if(0.7 > a) here a is a float variable and 0.7 is a a is 11 initially, and b is 5.
double constant. The double constant 0.7 is if (a = 5) -> this is not comparison, it is
greater than the float variable a. Hence assignment. So, it assigns value 5 to a.
the if condition is satisfied and it prints 'Hi' So, a will be updated to 5.
Example: if(5) -> if(true) -> b++ is evaluated.
b will be incremented to 6.
#include<stdio.h> So, a = 5 and b = 6
int main()
{ Then, printf("%d %d", ++a, b++);
float a=0.7; ++a will give 6 because of pre increment.
printf("%.10f %.10f\n",0.7, a); b++ will give 6 because of post increment.
return 0;
} The output is 6 6
Output:
0.7000000000 0.6999999881

The output is
“Hi”

These are compiler dependent questions, so will


not be asked. But you should know the concept.
It will be compile time error.

As continue statement is used without any loop.

Tutorial 6

for loop contains semicolon at the end. while(‘1’ < ‘2’) -> here, ASCII value of 1 and ASCII
value of 2 is compared.
So, loop is terminated only when condition goes
false. ASCII of ‘1’ is 49 and ASCII value of ‘2’ is 50.

First time, i is 1, 1 <= 5, condition goes true, prints while(49 < 50) is true and there is no statement
character value of 65 due to statement ->printf that violates this condition
("\n%c", 65)

But there is no statement which increases the Hence, the output of the program is
value of i and hence, the condition never goes false “In while loop” is printed infinite times.
and hence, for loop is never terminated.

Hence, the output of the program is A is printed


infinite times
Step 1: int i = 0; here variable i is an integer type x is initialized to 1, while condition will be true
and initialized to '0'. i.e. 1 == 1,
Step 2: for(; i<=5; i++); variable i=0 is already x=1–1=0
assigned in previous step. The semi-colon at the A newline and then 0 is printed.
end of this for loop tells, "there is no more
statement is inside the loop". Next time in while, condition goes false, as
0 == 1 will return false.
Loop 1: here i=0, the condition in for(; 0<=5; i++)
loop satisfies and then i is incremented by '1'(one) Hence, the output of the program is
Loop 2: here i=1, the condition in for(; 1<=5; i++) newline
loop satisfies and then i is incremented by '1'(one) 0
Loop 3: here i=2, the condition in for(; 2<=5; i++)
loop satisfies and then i is incremented by '1'(one)
Loop 4: here i=3, the condition in for(; 3<=5; i++)
loop satisfies and then i is incremented by '1'(one)
Loop 5: here i=4, the condition in for(; 4<=5; i++)
loop satisfies and then i is incremented by '1'(one)
Loop 6: here i=5, the condition in for(; 5<=5; i++)
loop satisfies and then i is incremented by '1'(one)
Loop 7: here i=6, the condition in for(; 6<=5; i++)
loop fails and then i is not incremented.

Step 3: printf("%d", i); here the value of i is 6.


Hence the output is '6'.
A signed char is defined as 8 bits and will hold -128 main()
to 127. {
First time, x = 0, 0 <= 255, condition is satisfied, short int i = 0;
Ascii value 0 and its corresponding character is for(i<=5 && i>=-1; ++i; i>0)
printed. printf("%hu,", i);
Next time, x is incremented to 1, 1 <=255, }
condition is true. Question is changed and instead of %u, %hu
Ascii value 1 and its corresponding character is is supposed to be written.
printed.
Then, x = 2 and so on. for(i<=5 && i>=-1; ++i; i>0)
When x reaches 128, 128 is not accepted by signed So expression i<=5 && i>=-1 initializes for loop.
char and due to cyclic nature of data type, x will Expression ++i is the loop condition.
become -128. Expression i>0 is the increment expression.
Then ASCII value -128 and its corresponding
character is printed. In, for (i<=5 && i >= -1; ++i; i>0)
Then for -127 and so on. expression i<=5 && i>=-1 evaluates to one.

So, the output is an infinite loop where ASCII value Loop condition always get evaluated to true.
0 to 127, then -128 to -1, then back from 0 to 127 Also, at this point it increases i by one.
and its corresponding characters are printed in an
infinite manner. An increment expression i>0 has no effect on
value of i.so for loop get executed till the limit of
integer (ie. 65535)

Use the concept of cyclic nature of data types.

Hence, the output is 1, 2, 3, ..., 65533, 65534,


65535.
Step 1: Initially the value of variable i is '5'. i = 0, j = 0
Loop 1: while(i-- >= 0) here i = 5, this statement while(0 < 2), condition is true, next statement
becomes while(5-->=0) Hence the while condition is l1: i++; -> i would be incremented to 1.
is satisfied and it prints '4'. (variable 'i' is Then, while(j < 3) i.e. while(0 < 3), condition is
decremented by '1'(one) in previous while true, “Loop is printed”, then it goes back to
condition) statement i++ due to goto l1;
Loop 2: while(i-- >= 0) here i = 4, this statement Again, i is incremented to 2, j value is never
becomes while(4-->=0) Hence the while condition incremented and hence, inner while loop
is satisfied and it prints '3'. (variable 'i' is never breaks and iterates infinitely.
decremented by '1'(one) in previous while
condition) So, the output is “Loop” is printed infinite
times.
Loop 3: while(i-- >= 0) here i = 3, this statement
becomes while(3-->=0) Hence the while condition
is satisfied and it prints '2'. (variable 'i' is
decremented by '1'(one) in previous while
condition)
Loop 4: while(i-- >= 0) here i = 2, this statement
becomes while(2-->=0) Hence the while condition
is satisfied and it prints '1'. (variable 'i' is
decremented by '1'(one) in previous while
condition)
Loop 5: while(i-- >= 0) here i = 1, this statement
becomes while(1-->=0) Hence the while condition
is satisfied and it prints '0'. (variable 'i' is
decremented by '1'(one) in previous while
condition)
Loop 6: while(i-- >= 0) here i = 0, this statement
becomes while(0-->=0) Hence the while condition
is satisfied and it prints '-1'. (variable 'i' is
decremented by '1'(one) in previous while
condition)
Loop 7: while(i-- >= 0) here i = -1, this statement
becomes while(-1-->=0) Hence
the while condition is not satisfied and loop exits.
The output of first while loop is 4,3,2,1,0,-1

Step 2: Then the value of variable i is initialized to


'5' Then it prints a new line character(\n).
See the above Loop 1 to Loop 7 .
The output of second while loop is 4,3,2,1,0,-1

Step 3: The third while loop, while(i-- >= 0) here i =


-1(because the variable 'i' is decremented to '-1'
by previous while loop and it never initialized.).
This statement becomes while(-1-- >= 0) Hence
the while condition is not satisfied and loop exits.

Hence the output of the program is


4,3,2,1,0,-1
4,3,2,1,0,-1
Value of x is -1, -1 <= 10 condition is true, Step 1: for(;;) this statement will generate
if(x < 5) i.e. if(-1 < 5), condition is true, continue is infinite loop.
executed, means it will skip the current iteration. Step 2: printf("%d\n", i++); this statement will
print the value of variable i and increment i by
x is incremented to 0, 0 <= 10, condition is true, 1(one).
if(x < 5) i.e. if(0 < 5), condition is true, continue is Step 3: if(i>10) here, if the variable i value is
executed, means it will skip the current iteration. greater than 10, then the for loop breaks.

x is incremented to 1, 1 <= 10, condition is true, Hence the output of the program is
if(x < 5) i.e. if(1 < 5), condition is true, continue is 1
executed, means it will skip the current iteration. 2
3
x is incremented to 2, 2 <= 10, condition is true, 4
if(x < 5) i.e. if(2 < 5), condition is true, continue is 5
executed, means it will skip the current iteration. 6
7
x is incremented to 3, 3 <= 10, condition is true, 8
if(x < 5) i.e. if(3 < 5), condition is true, continue is 9
executed, means it will skip the current iteration. 10
x is incremented to 4, 4 <= 10, condition is true,
if(x < 5) i.e. if(4 < 5), condition is true, continue is
executed, means it will skip the current iteration.

x is incremented to 5, 5 <= 10, condition is true,


if(x < 5) i.e. if(5 < 5), condition is false, hence, it will
go to else block and break statement is executed.
So, the program comes outside for loop. And
nothing is printed.

Hence the output of the program is


blank. Nothing is printed.

Hence, the output of the program is


\newline
31
13
04
-1 5

Tutorial 7

The output of the program is 3, 2, 15 The output of the program is Garbage value, 1,
2, 3, 4,
Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr
is declared as an integer array with a size of 5 and First time value of i is 0.
it is initialized to while(0 < 5), condition is satisfied.
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 . arr[i] = ++i;
Step 2: int i, j, m; The variable i,j,m are declared as Assignment operator works from right to left.
an integer type. So first ++i will take place.
Step 3: i = ++a[1]; becomes i = ++1; Hence i = ++i will give value as 1 and i is also incremented
2 and a[1] = 2 to 1.
Step 4: j = a[1]++; becomes j = 2++; Hence j = Now, arr[i] will take take value as arr[1] rather
2 and a[1] = 3. than arr[0], as i is incremented.
Step 5: m = a[i++]; becomes m = a[2]; Hence m = So, arr[1] = 1, hence, arr[0] will not be holding
15 and i is incremented by 1(i++ means 2++ so i=3) a valid value and hence, will hold a garbage
Step 6: printf("%d, %d, %d", i, j, m); It prints the value.
value of the variables i, j, m Next time, while(1 < 5), condition goes true.
Hence, the output of the program is 3, 2, 15 Same procedure takes place, ++i will be 2 and I
is incremented to 2, then arr[2] = 2 and so on
till value of i reaches 5, when condition goes
false.

Hence, the output of the program is Garbage


value, 1, 2, 3, 4,
Considering if the array begins at address 65486 Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9,
0}; The array a[3][4] is declared as an integer
Step 1: int arr[] = {12, 14, 15, 23, 45}; The array having the 3 rows and 4 columns
variable arr is declared as an integer array and dimensions.
initialized.
The base address(also the address of the first
Step 2: printf("%u, %u\n", arr, &arr); Here, element) of array is 65472.
The base address of the array is 65486.
=> arr, &arr is pointing to the base address of the a will hold address of first element of first row.
array arr. Therefore, a+1 is pointing to the memory
Hence, the output of the program is 65486, 65486 location of first element of the second row in
array a. Hence 65472 + (4 ints * 2 bytes) =
65480. 2 bytes is taken because in question its
written that consider int is of 2 bytes, others 4
bytes should be taken.

Hence, the output of the program is 65480.

The sizeof function return the given variable. In C, if we initialize an array with fewer
Example: float a=10; sizeof(a) is 4 bytes members, all remaining members are
Step 1: float arr[] = {12.4, 2.3, 4.5, 6.7}; The automatically initialized as 0.
variable arr is declared as an floating point array
and it is initialized with the values. So, since arr[5] = {1} is written, means first
Step 2: printf("%d\n", sizeof(arr)/sizeof(arr[0])); element will be initialized to 1 and rest will be
The variable arr has 4 elements. The size of the taken as 0.
float variable is 4 bytes. So, arr[0] will be 1 and others will be 0.
Hence 4 elements x 4 bytes = 16 bytes Hence the output of the program is 1 0 0 0 0
sizeof(arr[0]) is 4 bytes
Hence 16/4 is 4 bytes
Hence the output of the program is 4
No, C does not perform bound checking, means if It will give compile error.
you try to print the value of array index outside the
range specified, it will not give error, instead will In statement, int a[ ][ ] = {{1,2},{3,4}};
give garbage value.
Arr[5] = {0} is written, that means all elements are Number of columns should be mentioned
initialized to 0. mandatorily as per the syntax rules.
So, arr[0] = 0, arr[1] = 0, arr[2] = 0, arr[3] = 0, arr[4]
= 0.
When, for loop is executed, it runs for i = 5 also, at
arr[5], the value is not assigned and hence it takes
garbage value.

Hence the output of the program is 0 0 0 0 0


garbage value

The array is declared as arr[4] as value of size is 4. int a[2][3] = {1, 2, 3, 4, 5}; is defined.
Now, the array is not initialized with any value, so Means 2 rows and three columns.
it will take all garbage values in array. 1st row will have values 1 2 and 3
2nd row will have values 4 5 and 0 [0 because
Hence, arr[0] will be having garbage value, 6th value is not initialized, so will take 0]
garbage values are always positive values .
Using nested for loop, only array values are
So, arr[0] will be a true value, hence condition is printed.
satisfied and prints “Initialized to ZERO”.
Hence the output of the program is 1 2 3 4 5 0
Hence the output of the program is Initialized to
ZERO

expr = 1 | 2 | 3; Be careful about the scope of i,


| here means bitwise OR. there are two variables named: i, with different
So, perform the bitwise operation on 1, 2 and 3. scope.
001 There are 2 main points to consider while
010 solving this question. Scope of variable i and
011 integer division.
0 1 1 which is equivalent to 3. First for loop will run for i = 0, 2 and 4 as i is
Hence, expr = 3. incremented twice inside loop and resultant
Then, value[expr] i.e value[3] will be from the array will be a = 2, 2, 4, 4, 5, 6, 7, 8 (Loop will
array i.e. at 3rd index, 4.0 is stored. terminate at i = 4)
After that i value is 3 as there is a decrement
Hence the output of the program is 3 4.000000 operation after for loop.
Next for loop is running for j = 7, 6 and 5 and
corresponding i values which is a local variable
inside for loop will be 3 (7/2), 3 (6/2) and 2
(5/2). Array after this for loop will be
a = 2, 2, 3, 2, 5, 6, 7, 8
After the for loop, current i value is 3 and
element at a[3] = 2.
Hence the output of the program is 3 2

Tutorial 8

The for loop will iterate for 26 times, for i = 0 to i = 25. Here, c is a string as character array is defined.
First time, arr[i] i.e. arr[0] = ‘A’ + i = ‘A’ + 0 = 65 + 0 And array c stores string value “A”.
[because 65 is ASCII value of ‘A’].
So, arr[0] = 65. At c[0] which is a character printed with %c, ‘A’ is
Then %d and %c values of arr[i] is printed, which is 65 A stored.
And so on till 26 characters are printed i.e. A to Z along
with corresponding ASCII values. And c string printed with %s, contains “A” only.
Hence, the output is Hence, the output is
65 A newline
66 B A
. A
.
.
89 Y
90 Z

As at the base address or starting of the string “Null” is placed, so while reading array if Null comes it assumes
that this is the end of array, so it terminates here only.

Hence, the output is blank or null. Nothing is printed.


You cannot compare strings by means of == operator. Step 1: char str1[20] = "Hello", str2[20] = "
World"; The variable str1 and str2 is declared as an
If(str1 == str2) will not compare the contents of the array of characters and initialized with value "Hello"
strings, it will compare the addresses of both the and " World" respectively.
strings, which will be different. Step 2: printf("%s\n", strcpy(str2, strcat(str1,
str2)));
If you print - > printf("%u \n", str1); This will give you => strcat(str1, str2)) it append the
address of string. string str2 to str1. The result will be stored in str1.
Hence, the output will be “Unequal”. Therefore str1 contains "Hello World".
=> strcpy(str2, "Hello World") it copies the "Hello
World" to the variable str2.
Hence it prints "Hello World"

Hence, the output is “Angel”.


Hence, the output is 0.
Note that the sizeof() operator would return size of The program reverses the statement entered by the
array. The size of the array is written during its user.
declaration. i.e. char str[20].
Hence, the output is 20 If input is “Hi Student”.
The output will be “tnedutD iH”.

Iterate through the for loop on your own.


It will give compile error. Let us consider below line inside the for loop

str = "Kanpur"; -> this kind of string assignment is p[i] = s[length — i];
not allowed. Hence, it gives error. For i = 0, p[i] will be s[6 — 0] and s[6] is ‘\0’
So p[0] becomes ‘\0’.
The statement str = "Kanpur"; generates an error.
We have to use strcpy function to copy a string. It doesn’t matter what comes in p[1], p[2]….. as p[0]
will not change for i > 0. Nothing is printed if we print
To remove error we have to change this a string with first character ‘\0’
statement str = "Kanpur"; to strcpy(str, "Kanpur");
Hence, the output is blank or null. Nothing is printed.
The program prints the string "anpur".
if(printf(“%s”,a)) counts the total characters in the
string a, since a is initialized to “\0”, hence, the total
characters will be 0.

if(0) will be false. So, it will go in else block.

Hence, the output is


null -> null character is printed here, so it will be
blank
The string is empty
newline

You might also like