|
CS456 - Systems Programming
Spring 2026
|
Displaying ./code/jan28/printnums.c
/*
printnums.c - printing all numbers from 1 to a specified number
Compilation and output:
> gcc -o printnums printnums.c
> ./printnums
Usage ./printnums number
> ./printnums 6
1
2
3
4
5
6
*/
#include <stdio.h>
#include <stdlib.h> //needed for exit and atoi functios
int main(int argc, char **argv){
//printing usage statement if number is not provided
if(argc < 2){
fprintf(stderr, "Usage %s number\n", argv[0]);
exit(-1);
}
// everything stored in argv is a string
// we need to convert it to an integer before we can use it
// atoi accomplishes this objective
int endNum = atoi(argv[1]);
//then use for loop to print from 1 to the specified number
for(int i = 1; i <= endNum; i++)
printf("%d\n", i);
return 0;
}
|