Putting a composite statement in the condition of a for loop

前端 未结 6 1141
一整个雨季
一整个雨季 2021-01-18 18:02

I have a contrived example to demonstrate the request for a specific functionality - I wonder if anyone has a clever trick to do this.

The following is a problem one

6条回答
  •  庸人自扰
    2021-01-18 18:39

    Here's a way that requires a separate initialization pass, but that has a slightly cleaner for loop. Array sepsarray holds the separators you want (in this example, commas so you can see them). The last element of sepsarray is followed by a '\0' that terminates the loop.

    #include 
    
    #define MAXNUM 5
      //print up to this 
    
    void main() {
      char sepsarray[256] = {0}; //256=arbitrary max size
      int i;
      char *seps=sepsarray;
    
      for(i=0;i

    Output:

    0,1,2,3,4,5
    

    Hardly IOCCC material, but hey... :)

提交回复
热议问题