Skip to content

Instantly share code, notes, and snippets.

@christophevg
Created December 9, 2014 11:31
Show Gist options
  • Save christophevg/2b76ed68e038fe36a4b1 to your computer and use it in GitHub Desktop.
Save christophevg/2b76ed68e038fe36a4b1 to your computer and use it in GitHub Desktop.
How to create an array of consecutive numbers in C
#include <stdio.h> // for printf
#define START 123 // initial number to use to start sequence
#define MAX 5 // maximum number of items in the list
int main(void) {
// create list on the stack
int lst[MAX];
// fill list
for(int i=0; i<MAX; i++) {
lst[i] = START + i;
}
// print list
for(int i=0; i<MAX-1; i++) {
printf("%d = %d, ", i, lst[i]);
}
printf("%d = %d\n", MAX-1, lst[MAX-1]);
}
// output:
// 0 = 123, 1 = 124, 2 = 125, 3 = 126, 4 = 127
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment