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
GATE IT 2008 | Question: 49 / GATE Overflow for GATE CSE
edited by
9,601 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,951 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,597 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...
Total PHP MySQL Other RAM
Time (ms) % Time (ms) % File count Time (ms) % Query count Time (ms) % Amount %
Setup 5.2 4% 2.6 2% 72 2.5 2% 2 0.1 0% 569k 39%
Control 18.5 16% 2.3 2% 5 16.6 15% 12 0.0 0% 477k 32%
View 3.9 3% 3.9 3% 12 0.0 0% 0 0.0 0% 35k 2%
Theme 73.5 67% 5.1 4% 15 68.4 62% 3 0.0 0% 363k 25%
Stats 8.4 7% 0.1 0% 0 8.3 7% 1 0.0 0% 0k 0%
Total 109.4 100% 13.9 12% 104 95.9 87% 18 0.0 0% 1447k 100%