Here we do a closer analysis of some 'C' code
- Select a location on your system (or create such with a file manager).
- Open a terminal window; Navigate to that location.
- Open a text editor; Copy then paste the boxes below into your editor.
- (Arrange the terminal & text editor windows so both are seen side-by-side.)
- Save the first block of code below as 'goc'. Save the second as 'main.c'.
- In the terminal window, make shell script 'goc' executable: "chmod +x goc"
- Invoke the compile script with the line: "./goc pgm2"
- List the files present in the terminal. There should be 3: goc, main.c, pgm2.
- Invoke the newly-created program with './pgm2'
#!/bin/bash
# invoke this script with desired name of executable
# 'C' source code is expected in file 'main.c'
# flag the compiler to provide more error and warning checks
pgmname=$1
gcc main.c -Wall -o $pgmname
/* Here's source for 'pgm2' */
#include <stdio.h>
#define QNTY_GIFTS 7
#define BROWN "Chartreuse"
int main(void) {
printf("\nThe number I'm thinking of is %d\n", 27);
printf("LOOK, Santa left %d presents for ME under the tree.\n", QNTY_GIFTS);
printf("Some day I'd like to buy a %s car.\n\n", BROWN);
// since main() is defined as "int main()", we must return a decimal value
return(0);
}
This code expands the prior 'pgm1' example by adding 2 styles of commenting, and adds the display of both numeric and character string variables in the 'printf()' function. The '#define' directive is introduced. The next article will examine a way to pass data to our executable at run-time. Using '#define' is an efficient way to allow rapid change of some numeric or character string, if that item is used frequently in various locations in multiple 'C' source files. For example, we might "#define MAX_WORDS 32", then later decide a larger value is better: "#define MAX_WORDS 64".