# GATE2017-2-55

10.9k views

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
3
0
what was actual que . does this contain both printf line .......
0
Actual one had strlen wala .
0
i will just explain this wait for 5 min .....
1
"(int)" is it use for type casting???
0
Acc to me, the value at address 110 is 1 not 7 ,how you have taken 7 which is at 111
0

now solve again

2[p]=∗(2+p)
6[p]=∗(6+p)

char c[]="GATECSIT2017";
char *p=c;
printf("%d",strlen(c+2[p]-6[p]-1)); 

$2[p] = *(2+p) = p$
$6[p] = *(6+p) = p$
$c + 2[p] - 6[p] -1 = c + 'T' - 'I' - 1 = c + 11 - 1 = c + 10$ (In any character coding all alphabet letters are assigned consecutive int values as per C)

printf will print $2$ which is the length of "$17$".

edited by
0

sir i believe the question was a numerical answer type and had only one output which was to print just 2,

i remember that there was only one

printf statement which was

printf("%d",(int)strlen(c+2[p]-6[p]-1));

kindly change the question as it can be misleading

0
leave it for now. When the answer response comes, all the questions will be renetered. It should be coming next week.
0
why it is tycasted?
0
How do i 17 lenth ans is 2 Pls Explain ?
0
strangely counting position of alphabet number worked out here c is c=g (7th position in alphabet) , similarly T=20th , I is 11th (7+20-11-1) :D , do you think it will work for every case ?
5
Strlen returns unsigned values ... thats y type cast is needed ...
0

If we type cast and value of strlen is negative then what happens ?

ex : strlen(-2); // output 2

(int)strlen(-2); // what is the output ?

0
In the strlen why we are not considering null as last character? It means I want to say that strlen should return length as 3. Please correct me if I am wrong
4
in strlen implementation while loop works till not equal to '\0'. So strlen will return the length of string excluded with null character hence in this case length will be 2.

http://clc-wiki.net/wiki/C_standard_library:string.h:strlen
0
Nice explanation
0
i didnt understand, anyone please explain how 11 and 1 value corresponds to T & I ?
0
understood I hope now you can understood easily

0
how you start "c" address from 100. what need to do "110 %5"??? kindly plz explain...
0
you can assume any starting address for array . ANswer would be same .
0
0
tysm
3
%s=17 here bcz of

Since address is now 110. So from 110 it will print whole string i.e 17
0
@Anup

can we index arrays like 2[p] ???

Any reference.
4

Yes @ Rishabh Gupta 2

2[p] Simply means p

0
How u took size of char as 1?? I mean they didn't mentioned 1 or 2 bytes??
0

generally, character is stored in terms of consecutive bytes.

@G Shaheena

0

@Anup patel

%d is given not %s in the question?? Hope it might help.......

1
1010 means from 10 th position consider complete string?
Look at 2[c] - 6[p] this will be evaluate as ( T -  I)

the difference in ascii walue of T and I will be 11.
So it will boil down to c + 11-1

means c+10  and the address for stringlength will become like strlength("17');

That is 2
0
how c+10 become "17"?? kindly eplain
0
@pankajbelwal

(ascii value of c) + 10 (make sure ascii value of 'c' is relative to the value choosen in 't' and 'i' in previous steps )
9
Ascii value of C is not used here, C is the starting address of the array and C+10 is the address of the 11th element of the Array, now strlen starts counting from the address passed to it as argument and stops when it finds '\0'(null- by default added at the end of any array of characters- string) but while counting array length strlen does not add extra 1 for this null character, so starting  from C+10, strlen finds that C+12 is null('\0), hence stops and returns length as 2
0
in ASCII table all alphabets are listed in consecutive locations, so you can assume  a is  1 , then b is 2 , c is 3 , d is 4, e is 5, f is 6, g is 7  .......so on till z=26,

now given in question C +10, where C gives the value of starting address of the array , i,e value of G which is 7

so C+7 turns out to be 17 , i hope this helps
0

@Sourav Basu @Arjun  @Shaik Masthan (sorry for disturbing you )

sir the result is c+10 which (on considering c = 1000) gives 1010.

then shouldn't it be strlen(1010)   why are we taking element at that address as dereferencing operator is not used *(c+10) would give "17"  ...pls tell me where i am getting wrong.

1

@dharmesh7

read first concept of strlen function before attempting the question

https://www.geeksforgeeks.org/strlen-function-in-c/

0
@dharmesh7

same doubt please let know if u get it.

thanks

Hope this helps! output is 2 0

when pointer subtraction is directly allowed then why here we are subtracting ASCII value why not address?

2
2[p] or 6[p] are not pointers, those are elements

## Related questions

1
4k 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 program is ______
Consider the following snippet of a C program. Assume that swap $(\&x, \&y)$ exchanges the content of $x$ and $y$: int main () { int array[] = {3, 5, 1, 4, 6, 2}; int done =0; int i; while (done==0) { done =1; for (i=0; i<=4; i++) { if (array[i] < array[i+1] ... if (array[i] > array[i-1]) { swap(&array[i], &array[i-1]); done =0; } } } printf( %d , array); } The output of the program is _______
Two transactions $T_1$ and $T_2$ are given as $T_1:r_1(X)w_1(X)r_1(Y)w_1(Y)$ $T_2:r_2(Y)w_2(Y)r_2(Z)w_2(Z)$ where $r_i(V)$ denotes a $\textit{read}$ operation by transaction $T_i$ on a variable $V$ and $w_i(V)$ denotes a $\textit{write}$ operation by transaction $T_i$ on a variable $V$. The total number of conflict serializable schedules that can be formed by $T_1$ and $T_2$ is ______
If the characteristic polynomial of a 3 $\times$ 3 matrix $M$ over $\mathbb{R}$ (the set of real numbers) is $\lambda^3 – 4 \lambda^2 + a \lambda +30, \quad a \in \mathbb{R}$, and one eigenvalue of $M$ is 2, then the largest among the absolute values of the eigenvalues of $M$ is _______