Use of symbolic constants define preprocessor directive define name value All

Use of symbolic constants define preprocessor

This preview shows page 3 - 5 out of 7 pages.

Use of symbolic constants #define preprocessor directive #define name value All occurrences of name in the source file are replaced by value Used to define constants for things such as array sizes and other values to improve program maintainability #include <stdio.h> #define ARRAY_LENGTH 3 int main() { int i, a[ARRAY_LENGTH] = {10, 20, 30}; printf("[%d", a[0]); for (i = 1; i < ARRAY_LENGTH; i++) { printf(", %d", a[i]); } printf("]\n"); return 0; } 3 © 2020 Dept of Computer Science UMD
Image of page 3
Array initialization Supply a list of values in braces, separated by commas: int a[5] = {1, 1, 2, 3, 5}; Occurs when array is first created When this occurs depends on the array's storage class (static initializes to 0) Also means you can't initialize after declaration And you can't initialize with variable expressions Zeroes pad the array when initializer is short Use of an initializer allows size to be omitted Can't omit size otherwise when declaring local variables (parameters are an exception, though) Initializers with excess elements cause compiler errors Example: array.c 4 © 2020 Dept of Computer Science UMD
Image of page 4
Image of page 5

You've reached the end of your free preview.

Want to read all 7 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture