Created
December 9, 2014 11:31
-
-
Save christophevg/2b76ed68e038fe36a4b1 to your computer and use it in GitHub Desktop.
How to create an array of consecutive numbers in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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