This question already has answers here:
What does 1[d=b] mean?
(3 answers)
Closed 5 years ago.
I came across a code as in below
#include <stdlib.h>
int main(){
char a[]="0123456789";
printf("%s\n",a+6[a]-2[a]);
return 0;
}
Output
456789
How does the calculation of a+6[a]-2[a] happens in printf?
Why giving just 6[a] in printf doesn't work?
printf("%s\n",6[a]);
Well, a statement like
a+6[a]-2[a]
can be re-written as
&(a[ a[6] - a[2] ])
which is simply,
use the value of a[6] (type, int) as the index in the first case
use the value of a[2] (type int) as the RHS.
The result, is a pointer, is passed to printf() as an argument to %s conversion specification.
Related
This question already has answers here:
Is scanf("%d%d", &x, &x) well defined?
(3 answers)
Closed 2 years ago.
#include <stdio.h>
int main(void)
{
char b[5];
scanf("%4s%4s", b, b);
//My input: "qwer<Enter>sgsh<Enter>"
printf("%s", b);
//Output: sgsh
}
C99:
Between the previous and next sequence point an object shall have its stored value
modified at most once by the evaluation of an expression.
In this case, I am modifying the value of b twice. Isn't it undefined behavior?
From this scanf reference:
There is a sequence point after the action of each conversion specifier; this permits storing multiple fields in the same "sink" variable.
So what you're doing is defined and should work well.
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 3 years ago.
Consider the following code:
#include <stdio.h>
int main(void){
char *p="A for Apple";
char q[15]="B for Ball";
printf("x=%c y=%c\n",*(p+10)-2,*(&q[4]-2));
printf("u=%c v=%c w=%d\n",p[8],*q+8,8[p]-q[8]);
return 0;
}
Output:
x=c y=f
u=p v=J
w=4
Problem, I am having here is determining how w=4 was evaluated.
What does 8[p] means?
a[x] is shorthand for *(a + x). Consequently, a[x] is equivalent to the expression x[a]. (same holds for 8[p] and p[8] of course ;))
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 8 years ago.
Can anyone help me out, I am not getting how gcc is compiling the below statement and printing its output.:-
printf("%d",7["sunderban"]);
C allows to access array's elements in two ways (see Accessing arrays by index[array] in C and C++ and the answers) :
int v[5];
// 1)
v[2] = 33;
// 2)
2[v] = 44;
So, what happens in your case is that you access the 8th element of the string, and it is interpret as int by the printf.
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 9 years ago.
I am using Ubuntu 12.04lts with the GCC compiler. This program gives the result 10. Could you anybody please describe why this program gives the result like this?
#include <stdio.h>
void main(void)
{
int arr[1] = {10};
printf("\n%d\n\n", 0[arr]);
}
arr[0] gets internally expanded to *(arr+0). Similarly 0[arr] gets expanded to *(0+arr) which points to the same thing. Hence you see 10.
In general for an array or a pointer a, a[b] always means *(a+b) where a is the starting address of the array or pointer and b is the offset. Thus, a[b] and b[a] are equivalent.
below line means arr is int type array and it has size 1 and it is initialized with 10 ie index 0 has 10
int arr[1] = {10};
then next line printf statement printing the value of arr at index 0.
printf("\n%d\n\n",0[arr]);
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 8 years ago.
#include <stdio.h>
int main()
{
int a=3, b = 6;
printf(&a["Hi!Hello! %s\n"], &b["Mnnit/Softathalon"]);
printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"],
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
return 0;
}
output:
Hello! Softathalon
That is C !
Why is this the output? Can anyone explain different format specifier in it?
For any array T arr[N], the expression arr[i] is equivalent to *(arr + i).
Because the addition is commutative in the latter expression, you can also write this as *(i + arr), and hence as i[arr].
In particular, arr[3] and 3[arr] denote the same thing.
It's one of those "curiously funny things you can do in C", but it should go without saying that serious code should never actually use such a construction.