1,526 views
1 1 vote

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 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
Position:
Show:

Related questions

3 3 votes
2 answers 2 answers
3.5k
3.5k views
srestha asked Nov 3, 2017
3,536 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; }
1 1 vote
1 1 answer
279
279 views
khanshahima4 asked Sep 24
279 views
1. If precedence tells me which operator has priority, and associativity tells me how to handle operators at the same level, then why do I need a separate concept called ...
14 14 votes
1 1 answer
1.3k
1.3k views
GO Classes asked Jul 2
1,345 views
Assume:$\texttt{short = 2 bytes}$, $\texttt{int = 4 bytes}$, $\texttt{char = 1 byte}$$\texttt{int}$ needs $4$-byte alignment and the final structure size is rounded to a ...
18 18 votes
2 2 answers
750
750 views
GO Classes asked Jul 2
750 views
Assume:struct Student { double gpa; }; struct Student alice; struct Student *sptr = &alice;Which of the following correctly assigns $\texttt{4.0}$ to $\texttt{alice.gpa}$...