edited by
402 views
8 votes
8 votes

What will be the output of the following C program:

#include<stdio.h>
int main()
{
  char* my_words[] = {"Abin", "Mahesh", "Seema", "Palak"};
  char *p;
  p = my_words[3];
  printf("%s, ", p+3);
  printf("%c", *p+3);
}
  1. ak, S
  2. , a
  3. Possible Runtime Error
  4. ak, a
edited by

1 Answer

Best answer
17 votes
17 votes
Here, p is pointing to the fourth element (array index in C starts from 0) in the array of strings my_words which is "Palak".

p+3, will point to the fourth character in "Palak" which is 'a' and "%s" will print all strings from there till "\0" which is "ak".

(All string literals automatically gets a "\0" at end by the C compiler)

*p+3 -- *p returns 'P' and 'P' + 3 will give the character code of the 3rd character after 'P' which is 'S'. and %c will print the character corresponding to this which is 'S'.

PS: A system can use other character codes than ASCII too but in any character code the values of "a-z" must be in order and so the above answer is not implementation/system defined.
selected by
Answer:

Related questions

9 votes
9 votes
1 answer
3
gatecse asked Jul 26, 2020
640 views
What will be the output of the following C program:#include<stdio.h int main() { char* disease = "COVID-19"; (*disease)++; printf("%s",disease); }DPWJE-$20$DOVID-$19$COVI...
8 votes
8 votes
2 answers
4
gatecse asked Jul 26, 2020
717 views
The number of characters (including whitespaces if any) printed by the following C program is#define sum(a, b) #a "+"#b "=%d" #include<stdio.h int main() { printf(sum(6,9...