edited by
27,872 views
70 votes
70 votes

Consider the following C program.

#include<stdio.h>
#include<string.h>
int main() {
    char* c="GATECSIT2017";
    char* p=c;
    printf("%d", (int)strlen(c+2[p]-6[p]-1));
    return 0;
}

The output of the program is _______

edited by

13 Answers

0 votes
0 votes

2 is the answer.

0 votes
0 votes
This is a C program that uses pointer arithmetic to calculate the length of a string.

The string "GATECSIT2017" is stored in a character pointer variable c. A pointer variable p is also defined and it is assigned the value of the pointer c.

In the printf statement, the expression (c+2[p]-6[p]-1) is used to calculate the length of the string.

The expression c+2[p] is equivalent to c+2[c], which means that it is referencing the 3rd character of the string (index 2) which is 'T'

The expression 6[p] is equivalent to 6[c], which means that it is referencing the 7th character of the string (index 6) which is 'I'

So, the expression c+2[p]-6[p]-1 is equivalent to 'T'-'I'-1 which evaluates to -9.

The printf statement prints the value of this expression as an integer, which is -9.

It is important to note that this code will not give the correct length of the string, as the strlen() function is not used in the program, and the expression used to calculate the length is not correct and it will always return -9.
Answer:

Related questions

30 votes
30 votes
11 answers
1
Madhav asked Feb 14, 2017
9,655 views
Consider the following function implemented in C:void printxy(int x, int y) { int *ptr; x=0; ptr=&x; y=*ptr; *ptr=1; printf(“%d, %d”, x, y); }The output of invoking $...
25 votes
25 votes
7 answers
2
Madhav asked Feb 14, 2017
11,918 views
Consider the following C program.#include<stdio.h int main () { int m=10; int n, n1; n=++m; n1=m++; n ; n1; n-=n1; printf(“%d”, n); return 0; }The output of the prog...
26 votes
26 votes
3 answers
4
khushtak asked Feb 14, 2017
5,819 views
Match the following:$$\begin{array}{|ll|ll|}\hline P. & \text{static char var ;} & \text{i.} & \text{Sequence of memory locations to store addresses} \\\hline Q. & \text...