• edited by
22,277 views
60 60 votes

Consider the following C program segment:

char p[20]; int i;
char* s = "string";
int length = strlen(s);
for(i = 0; i < length; i++)
    p[i] = s[length-i];
printf("%s", p);

The output of the program is:

  1. gnirts
  2. string
  3. gnirt
  4. no output is printed

4 Answers

Best answer
76 76 votes

Here,

$p[0] = s[length] = $ '\0'; //compiler puts a '\0' at the end of all string literals

Now, for any string function in C, it checks till the first '\0' to identify the end of string. So, since the first char is '\0', printf %s, will print empty string. If we use printf("%s", p+1); we will get option (C) with some possible garbage until some memory location happens to contain "\0". For the given code, answer is (D).

• edited by
9 9 votes
string\0
s[0]s[1]s[2]s[3]s[4]s[5]s[6]

note: string literals always ends with a null character (\0)

 

s is a pointer to string literal and it is pointing to the first element of "string" (ie s)

length of the string is 6 so strlen(s) will return 6 and  int length will be 6 

since length is 6 the for loop will run from 0 to 5 

 

for(i = 0; i < length; i++)
    p[i] = s[length-i];
this will become 
for(i = 0; i < 6; i++)
    p[i] = s[6-i];
 
for i=0 --> p[0] = s[6-0] =s[6] --> s[6] is a null character (\0) so we will add it in our char array p at p[0]
for i=1 --> p[1] = s[6-1] =s[5] --> s[5] is g and we will add it at p[1]
for i=2 --> p[2] = s[6-2] =s[4] --> s[4]=n
for i=3 --> p[3] = s[6-3] =s[3] --> s[3]=i
for i=4 --> p[4] = s[6-4] =s[2] --> s[2]=r
for i=5 --> p[5] = s[6-5] =s[1] --> s[1]=t
for i=6 --> the condition i<6 fails
 
so the array p will look like 
\0gnirt
p[0]p[1]p[2]p[3]p[4]p[5]
 
printf("%s", p); here the %s is used to print a string and 
it prints from the first element of the string untill it finds the null character 
Here the first element is null (\0) itself so it will print nothing. Hence the correct ans 

D. no output is printed

 

 

 

8 8 votes
no output
• edited by
1 1 vote
DeclarationIs '\0' added automatically?Valid C String?Internal Storage
c char s[] = "hello";YesYesh e l l o \0
c char *p = "hello";YesYesh e l l o \0
c char s[10] = "abc";YesYesa b c \0 0 0 ...
c char s[] = {'h','e','l','l','o'};NoNoh e l l o
c char s[] = {'h','e','l','l','o','\0'};Already given manuallyYesh e l l o \0
c char s[5] = "hello";No space for '\0'Noh e l l o
c char s[6] = "hello";YesYesh e l l o \0

Quick Memory Trick--->

Syntax'\0' Added?
Using " " (double quotes)Yes
Using { } manuallyNo (unless you add it)
Answer:
Position:
Show:

Related questions

49 49 votes
4 answers 4 answers
21.4k
21.4k views
Kathleen asked Sep 18, 2014
21,377 views
Consider the following C function:int f(int n) { static int i = 1; if(n >= 5) return n; n = n+i; i++; return f(n); }The value returned by $f(1)$ is:$5$$6$$7$$8$
38 38 votes
3 answers 3 answers
13.7k
13.7k views
Kathleen asked Sep 18, 2014
13,739 views
The goal of structured programming is to:have well indented programsbe able to infer the flow of control from the compiled codebe able to infer the flow of control from t...
35 35 votes
5 answers 5 answers
20.2k
20.2k views
Kathleen asked Sep 18, 2014
20,228 views
Consider the following functionvoid swap(int a, int b) { int temp; temp = a; a = b; b = temp; }In order to exchange the values of two variables $x$ and $y$.call $swap(x, ...
39 39 votes
5 answers 5 answers
11.9k
11.9k views
Kathleen asked Sep 18, 2014
11,862 views
Choose the best matching between the programming styles in Group 1 and their characteristics in Group 2.$$\begin{array}{|ll|ll|}\hline \rlap{\textbf{Group 1}} & & \rlap{...