446 views
0 votes
0 votes

What is the error in the following code . Please do correct them

#include <stdio.h>
int main () { 
char *x[5] = {"raju","ramu","tinu","ben","ali"};
printf("%s\n",(x[2][0]) );
    return 0;
}

1 Answer

Best answer
3 votes
3 votes
#include <stdio.h>
int main () {
char *x[5] = {"raju","ramu","tinu","ben","ali"};
printf("%s\n",(x[2]) );
    return 0;
}

[0] is not required.

O/P should be "TINU". %s format specifier prints charachters until null character is encountered. And enclosing charachters in double quotes automatically makes it null terminating.
selected by

Related questions

0 votes
0 votes
1 answer
1
2 votes
2 votes
2 answers
3
atulcse asked Jan 15, 2022
694 views
Consider the following programint find (int n) { int a = 1; for (i = 1; i < = n; i ++) for (j = 1; j < = i; j++) for (k = 1; k <= j, k++) a = a + 1; ...
0 votes
0 votes
3 answers
4
ramakrushna asked Dec 23, 2021
744 views
What should be the ans. Isn’t the Function initialization should be outside main function? They are given inside main. If declaration would be outside main then ans sho...