Deprecated: Implicit conversion from float-string "1650653977.982" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1650653977.982" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1650653977.982" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1650653977.982" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1650653977.982" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1710066026.319" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1710066026.319" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1710066026.319" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1710066026.319" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1710066026.319" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1710066118.089" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1710066118.089" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1710066118.089" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1710066118.089" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1710066118.089" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1620657558.450" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1620657558.450" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1620657558.450" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1620657558.450" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1620657558.450" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1531878716.964" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1531878716.964" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1531878716.964" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1531878716.964" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1531878716.964" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1548127842.169" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1548127842.169" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1548127842.169" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1548127842.169" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1548127842.169" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1595319522.659" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1595319522.659" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1595319522.659" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1595319522.659" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1595319522.659" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594

Deprecated: Implicit conversion from float-string "1666148979.639" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 796

Deprecated: Implicit conversion from float-string "1666148979.639" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 801

Deprecated: Implicit conversion from float-string "1666148979.639" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 802

Deprecated: Implicit conversion from float-string "1666148979.639" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 803

Deprecated: Implicit conversion from float-string "1666148979.639" to int loses precision in /var/www/html/qadb/qa-include/app/format.php on line 594
Programming in C: GATE IT 2008 | Question: 49
edited by
9,603 views
29 votes
29 votes

What is the output printed by the following C code?

# include <stdio.h>
int main ()
{
    char a [6] = "world";
    int i, j;
    for (i = 0, j = 5; i < j; a [i++] = a [j--]);
    printf ("%s\n", a);
}
  1. dlrow
  2. Null string
  3. dlrld
  4. worow
edited by

4 Answers

Best answer
42 votes
42 votes

Char $a[6] = \begin{array}{|l|l|l|l|l|l|} \hline \text{w} &  \text{o} &  \text{r} &  \text{l} &  \text{d} &  \text{\0}\\\hline \end{array}$ 

After the loop executes for the first time, 

$a[0] = a[5]$

$a[0] =$`\0`

Next two more iterations of the loop till $i < j$ condition becomes false, are not important for the output as the first position is '\0';

printf(‘’%s’’, $a$);

printf function for format specifier '%s' prints the characters from the corresponding parameter (which should be an address) until "\0" occurs. Here, first character at $a$ is "\0" and hence it will print NOTHING. 

So, option (B).                  

edited by
22 votes
22 votes

char a[6] = "WORLD"  which be stored like this 

W O R L D \0

Key of this question : there is semicolon (;) after the for loop  , so printf statement will only execute when control will come out of the loop or loop condition will be false.

Iteration 1 : i=0,j=5; 0<5 (condition is true ) ;

                  a[0++] = j[5--] 

/o o r l d \0

 ( here , R.H.S will execute first and j[5--](=null) will be assigned to a[0++] .and then j=5 will be decremented once, bcz post decrement here , same with a[0++] . i will be incremented by one after this statement . )

iteration 2: i=1,j=4 ; 1<4 ( condition is true )

                   a[1++] = j[4--]

\o d r l d \o

iteration 3 :  i=2,j=3; 2<3 (condition is true)

                      a[2++]=j[3--]

\o d l l d \o

iteration 4 : i=3,j=2; 3<2 (condition false )

                    so control will go to the printf statement , and the command inside printf statement is saying print the string present on this address of a  (base address of array) upto null.

but , here first string is null at a[0] . so control will not move further and the output will be null .

so option b is the correct answer.

edited by
1 votes
1 votes

$a[6]=w|o|r|l|d|\setminus0$

$int\ i,j;$

$for(i=0,j=5;i<j;a[i++]=a[j--])$ $;$

$0<5 //condition\ True$

Because of the semi-colon it will not execute the statement instead it will do increment/decrement.

$a[0]=a[5]$

$i=1,j=4$

$1<4 //condition\ True$

$a[1]=a[4]$

$i=2,j=3$

$2<3 //condition\ True$

$a[2]=a[3]$

$i=3,j=2$

$3<2 //condition\ False$

$printf("\%s\setminus n",a)$

Correct Answer (B): Null string

1 votes
1 votes
We can consider like this

for(i=0, j=5 ; i<j ; i++, j--)

{

a[i] = a[j];

}

Therefore a[0] = null char which means end of string. Hence Ans will be Null String.

Thank you for reading.
Answer:

Related questions

10.5k
views
2 answers
42 votes
Ishrat Jahan asked Oct 28, 2014
10,488 views
C program is given below:# include <stdio.h int main () { int i, j; char a [3] = {{'a', 'b', 'c'}, {'d', 'e', 'f'}}; char b [3] ; char *p = *b; for (i = 0; i < 2; i++)...
15.0k
views
3 answers
64 votes
Ishrat Jahan asked Oct 28, 2014
14,954 views
Consider the C program given below. What does it print?#include <stdio.h int main () { int i, j; int a [8] = {1, 2, 3, 4, 5, 6, 7, 8}; for(i = 0; i < 3; i++) { a[i] = a[i...
10.6k
views
4 answers
23 votes
Ishrat Jahan asked Oct 28, 2014
10,598 views
Consider the C program below. What does it print?# include <stdio.h # define swapl (a, b) tmp = a; a = b; b = tmp void swap2 ( int a, int b) { int tmp; tmp = a; a = b; b ...
5.7k
views
2 answers
12 votes
Ishrat Jahan asked Oct 27, 2014
5,659 views
Match the programming paradigms and languages given in the following table. Paradigms Languages(I)Imperative(a)Prolog(II)Object Oriented(b)Lisp(III)Functional(c)C, Fortra...