1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#define FAVORITE_NUMBER 4
const int SECOND_FAVORITE_NUMBER = 7;
#define HELLO "Hello World"
const char* MESSAGE = "Hello ECE 264";
const int IMPORTANT_NUMBERS[] = {1, 2, 3, 4};
// this does not work as you might expect
#define NUMBERS { 1, 2, 3, 4 }
// this is weird but works, though it pretty inefficient
#define NUMBER_COMPOUND_INITIALIZER ((int[]){ 1, 2, 3, 4 })
int main(int argc, char* argv[]) {
/* SECOND_FAVORITE_NUMBER = 10;
constants.c: In function 'main':
constants.c:10:32: error: assignment of read-only variable 'SECOND_FAVORITE_NUMBER'
10 | SECOND_FAVORITE_NUMBER = 10;
| ^
*/
/* FAVORITE_NUMBER = 10;
constants.c: In function 'main':
constants.c:11:25: error: lvalue required as left operand of assignment
11 | FAVORITE_NUMBER = 10;
| ^
*/
printf("My favorite number is %d\n", FAVORITE_NUMBER);
printf("My second favorite number is %d\n", SECOND_FAVORITE_NUMBER);
printf("Message %s\n", HELLO);
printf("Message %s\n", MESSAGE);
printf("First important number %d\n", IMPORTANT_NUMBERS[0]);
// this invalid
//printf("First important number %d\n", NUMBERS[0]);
//this is valid
int numbers[] = NUMBERS;
printf("First important number %d\n", numbers[0]);
printf("First important number %d\n", NUMBER_COMPOUND_INITIALIZER[0]);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2024 Alexander J. Quinn & David Burnett This content is protected and may not be shared, uploaded, or distributed.