C [x ⦠y] ranged assignment
Clash Royale CLAN TAG#URR8PPP
up vote
9
down vote
favorite
I came across some code today which used syntax that in my years of doing C programming I've never seen before.
MWE:
#include<stdio.h>
char *example_array =
[0 ... 5] = "hello world",
[6 ... 10] = "goodbye world"
;
int main(void)
printf("%s, %s.n", example_array[3], example_array[7]);
return 0;
Expected output:
hello world, goodbye world.
It's pretty clear what's going on here in a static context, but I'm curious if this can be used as a convenient shortcut in non-static shortcuts, such as assignments in a loop. Of course, it wouldn't give any performance boost that -funroll-loops
couldn't, but it might make for cleaner code in, say, matrix row assignments or otherwise.
clang
and gcc
give no warnings by default when using this syntax, but I've never seen it documented anywhere. Is this some kind of extension, or is it standard C syntax?
c arrays
add a comment |Â
up vote
9
down vote
favorite
I came across some code today which used syntax that in my years of doing C programming I've never seen before.
MWE:
#include<stdio.h>
char *example_array =
[0 ... 5] = "hello world",
[6 ... 10] = "goodbye world"
;
int main(void)
printf("%s, %s.n", example_array[3], example_array[7]);
return 0;
Expected output:
hello world, goodbye world.
It's pretty clear what's going on here in a static context, but I'm curious if this can be used as a convenient shortcut in non-static shortcuts, such as assignments in a loop. Of course, it wouldn't give any performance boost that -funroll-loops
couldn't, but it might make for cleaner code in, say, matrix row assignments or otherwise.
clang
and gcc
give no warnings by default when using this syntax, but I've never seen it documented anywhere. Is this some kind of extension, or is it standard C syntax?
c arrays
1
with clang-Wgnu-designator
â Stargateur
Aug 18 at 1:44
add a comment |Â
up vote
9
down vote
favorite
up vote
9
down vote
favorite
I came across some code today which used syntax that in my years of doing C programming I've never seen before.
MWE:
#include<stdio.h>
char *example_array =
[0 ... 5] = "hello world",
[6 ... 10] = "goodbye world"
;
int main(void)
printf("%s, %s.n", example_array[3], example_array[7]);
return 0;
Expected output:
hello world, goodbye world.
It's pretty clear what's going on here in a static context, but I'm curious if this can be used as a convenient shortcut in non-static shortcuts, such as assignments in a loop. Of course, it wouldn't give any performance boost that -funroll-loops
couldn't, but it might make for cleaner code in, say, matrix row assignments or otherwise.
clang
and gcc
give no warnings by default when using this syntax, but I've never seen it documented anywhere. Is this some kind of extension, or is it standard C syntax?
c arrays
I came across some code today which used syntax that in my years of doing C programming I've never seen before.
MWE:
#include<stdio.h>
char *example_array =
[0 ... 5] = "hello world",
[6 ... 10] = "goodbye world"
;
int main(void)
printf("%s, %s.n", example_array[3], example_array[7]);
return 0;
Expected output:
hello world, goodbye world.
It's pretty clear what's going on here in a static context, but I'm curious if this can be used as a convenient shortcut in non-static shortcuts, such as assignments in a loop. Of course, it wouldn't give any performance boost that -funroll-loops
couldn't, but it might make for cleaner code in, say, matrix row assignments or otherwise.
clang
and gcc
give no warnings by default when using this syntax, but I've never seen it documented anywhere. Is this some kind of extension, or is it standard C syntax?
c arrays
edited Aug 18 at 1:39
ggorlen
3,1031720
3,1031720
asked Aug 18 at 1:33
PyroAVR
3221414
3221414
1
with clang-Wgnu-designator
â Stargateur
Aug 18 at 1:44
add a comment |Â
1
with clang-Wgnu-designator
â Stargateur
Aug 18 at 1:44
1
1
with clang
-Wgnu-designator
â Stargateur
Aug 18 at 1:44
with clang
-Wgnu-designator
â Stargateur
Aug 18 at 1:44
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
9
down vote
accepted
This is a GNU extension to designated initializers supported by both gcc and clang, which you can read about in the gcc docs. Note that this is only for initializers, not for assignments, which are very different things, despite both using the =
symbol.
This is incorrect, it is part of the C standard since C99.
â Dietrich Epp
Aug 18 at 1:40
6
@DietrichEpp No, the C standard does not allow ranges in designated initializers
â Chris Dodd
Aug 18 at 1:41
Nevermind, I didn't notice that part. Might be a good idea to make it clear what extension you are talking about, then.
â Dietrich Epp
Aug 18 at 1:41
Thanks so much! I didn't realize this was in any spec because I didn't know what to call it - does it have an official name?
â PyroAVR
Aug 18 at 3:12
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
9
down vote
accepted
This is a GNU extension to designated initializers supported by both gcc and clang, which you can read about in the gcc docs. Note that this is only for initializers, not for assignments, which are very different things, despite both using the =
symbol.
This is incorrect, it is part of the C standard since C99.
â Dietrich Epp
Aug 18 at 1:40
6
@DietrichEpp No, the C standard does not allow ranges in designated initializers
â Chris Dodd
Aug 18 at 1:41
Nevermind, I didn't notice that part. Might be a good idea to make it clear what extension you are talking about, then.
â Dietrich Epp
Aug 18 at 1:41
Thanks so much! I didn't realize this was in any spec because I didn't know what to call it - does it have an official name?
â PyroAVR
Aug 18 at 3:12
add a comment |Â
up vote
9
down vote
accepted
This is a GNU extension to designated initializers supported by both gcc and clang, which you can read about in the gcc docs. Note that this is only for initializers, not for assignments, which are very different things, despite both using the =
symbol.
This is incorrect, it is part of the C standard since C99.
â Dietrich Epp
Aug 18 at 1:40
6
@DietrichEpp No, the C standard does not allow ranges in designated initializers
â Chris Dodd
Aug 18 at 1:41
Nevermind, I didn't notice that part. Might be a good idea to make it clear what extension you are talking about, then.
â Dietrich Epp
Aug 18 at 1:41
Thanks so much! I didn't realize this was in any spec because I didn't know what to call it - does it have an official name?
â PyroAVR
Aug 18 at 3:12
add a comment |Â
up vote
9
down vote
accepted
up vote
9
down vote
accepted
This is a GNU extension to designated initializers supported by both gcc and clang, which you can read about in the gcc docs. Note that this is only for initializers, not for assignments, which are very different things, despite both using the =
symbol.
This is a GNU extension to designated initializers supported by both gcc and clang, which you can read about in the gcc docs. Note that this is only for initializers, not for assignments, which are very different things, despite both using the =
symbol.
edited Aug 18 at 1:44
answered Aug 18 at 1:38
Chris Dodd
77.7k576155
77.7k576155
This is incorrect, it is part of the C standard since C99.
â Dietrich Epp
Aug 18 at 1:40
6
@DietrichEpp No, the C standard does not allow ranges in designated initializers
â Chris Dodd
Aug 18 at 1:41
Nevermind, I didn't notice that part. Might be a good idea to make it clear what extension you are talking about, then.
â Dietrich Epp
Aug 18 at 1:41
Thanks so much! I didn't realize this was in any spec because I didn't know what to call it - does it have an official name?
â PyroAVR
Aug 18 at 3:12
add a comment |Â
This is incorrect, it is part of the C standard since C99.
â Dietrich Epp
Aug 18 at 1:40
6
@DietrichEpp No, the C standard does not allow ranges in designated initializers
â Chris Dodd
Aug 18 at 1:41
Nevermind, I didn't notice that part. Might be a good idea to make it clear what extension you are talking about, then.
â Dietrich Epp
Aug 18 at 1:41
Thanks so much! I didn't realize this was in any spec because I didn't know what to call it - does it have an official name?
â PyroAVR
Aug 18 at 3:12
This is incorrect, it is part of the C standard since C99.
â Dietrich Epp
Aug 18 at 1:40
This is incorrect, it is part of the C standard since C99.
â Dietrich Epp
Aug 18 at 1:40
6
6
@DietrichEpp No, the C standard does not allow ranges in designated initializers
â Chris Dodd
Aug 18 at 1:41
@DietrichEpp No, the C standard does not allow ranges in designated initializers
â Chris Dodd
Aug 18 at 1:41
Nevermind, I didn't notice that part. Might be a good idea to make it clear what extension you are talking about, then.
â Dietrich Epp
Aug 18 at 1:41
Nevermind, I didn't notice that part. Might be a good idea to make it clear what extension you are talking about, then.
â Dietrich Epp
Aug 18 at 1:41
Thanks so much! I didn't realize this was in any spec because I didn't know what to call it - does it have an official name?
â PyroAVR
Aug 18 at 3:12
Thanks so much! I didn't realize this was in any spec because I didn't know what to call it - does it have an official name?
â PyroAVR
Aug 18 at 3:12
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51904543%2fc-x-y-ranged-assignment%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
with clang
-Wgnu-designator
â Stargateur
Aug 18 at 1:44