746 views
1 votes
1 votes

What will be output

int main()
{
  char str[10]="GATE 2016";
  int length=strlen(str);
  str[length]='\0';
  for(i=0;str[i];i++)
    printf("%c",str[i]);
  return 0;
}

1 Answer

Best answer
4 votes
4 votes

Strlen returns the size of the string = 9 .

In the loop, it is printing one be one all the characters having the condition str[i] TRUE.

In C/C++, value 0 is false and any other value is TRUE. Also, Null character '\0' is guaranteed to have the value '0' (in the byte for '\0', 00000000 will be stored) in any character encoding that is used in the system as per C standard. So, the loop terminates at str[i] == ' \0 ' .

Output will be GATE 2016

PS: Statement 9 - str[length] = '\0' is unnecessary as in C all string literals are automatically given '\0' at end by the compiler. So, "GATE 2016" will be stored in memory as 'G','A','T','E',' ','2','0','1','6','\0'.

selected by

Related questions

2 votes
2 votes
2 answers
1
srestha asked Nov 3, 2017
1,830 views
#include<stdio.h int fun(int a,int b) { if(b==0) return 0; if(b%2==0) return fun(a+a,b/2); return fun(a+a,b/2)+a; } int main() { printf("%d",fun(9,11)); return 0; }
0 votes
0 votes
1 answer
2
Debargha Mitra Roy asked Apr 16
78 views
#include <stdio.h int main() { int a[3] = {1, 3, 5, 7, 9, 11}; int *ptr = a[0]; ptr += sizeof(int); printf("%d", *ptr); return 0; }(Assume size of int to be $2$ bytes.)T...
0 votes
0 votes
2 answers
4
Debargha Mitra Roy asked Apr 10
121 views
What is the output of the below code?#include <stdio.h void main() { static int var = 5; printf("%d ", var ); if (var) main(); }a. 1 2 3 4 5b. 1c. 5 4 3 2 1d. Error