Here we do a closer analysis of some 'C' code

 

  1. Select a location on your system (or create such with a file manager).
  2. Open a terminal window;  Navigate to that location.
  3. Open a text editor; Copy then paste the boxes below into your editor.
  4. (Arrange the terminal & text editor windows so both are seen side-by-side.)
  5. Save the first block of code below as 'goc'.  Save the second as 'main.c'.
  6. In the terminal window, make shell script 'goc' executable:  "chmod +x goc"
  7. Invoke the compile script with the line:   "./goc pgm2"
  8. List the files present in the terminal.  There should be 3:  goc, main.c, pgm2.
  9. 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

 

(right-click to download)

/* 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".