C [x … y] ranged assignment

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question


















  • 1




    with clang -Wgnu-designator
    – Stargateur
    Aug 18 at 1:44














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?







share|improve this question


















  • 1




    with clang -Wgnu-designator
    – Stargateur
    Aug 18 at 1:44












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?







share|improve this question














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?









share|improve this question













share|improve this question




share|improve this question








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












  • 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












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.






share|improve this answer






















  • 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










Your Answer





StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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.






share|improve this answer






















  • 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














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.






share|improve this answer






















  • 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












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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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












 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































這個網誌中的熱門文章

How to combine Bézier curves to a surface?

Mutual Information Always Non-negative

Why am i infinitely getting the same tweet with the Twitter Search API?