C Programming in 2021

by Ray Dall

################################################################################
Section 4
################################################################################

You can now write a program that will print something out other than just a number. Time to test yourself. Write a program that will print out your name! Try one that will print out your name, address, email, and telephone number. Do it in a format that might come in handy later:

Name:	Bob Surunkle
ADDR: 	1234 Underwhere Drive
	Nowhere'sville, CO 44678
EM:	bob@noplace.com
PH:	800.456.1212


To line things up neatly, try using the escape symbol \t (escape t).
Just like \n means NEWLINE, \t means TAB.
There are other escape symbols, but these are the most common that you will be likely to use.

/* 
   NAME:	printing.c
   DESCRIPTION:	Teaches about compiler errors, inclusion, header files, and the standard input/output header.
   SYNOPSIS:	./a.out
*/
#include 

int main()		
   {
	printf("Hello World\n");
	printf("Name:\tBob Surunkle\n");
	printf("Address:\t1234 Underwhere Drive\n");
	printf("Nowheresville, CO 44678\n");
	printf("EM:\tbob@noplace.com\n");
	printf("PH:\t800.456.1212\n");
	return(0);
   }



When you are satisfied that you've done that program well, try a couple of others. One that prints out a list of employees, their positions, and annual wages. When you've done that one, try another that prints out an invoice or bill of sale. Get creative. This is YOUR career you are working on. Get skilled at your craft before moving on.

...(hours later)...c

So you call yourself a programmer? A REAL programmer could clear the screen before he prints something out, make a menu, and even add some color/flair to his project. Believe it or not - it isn't all that difficult to do that. You're almost there, why not take the next step?

/* 
   NAME:        printing.c
   DESCRIPTION: Teaches clear, color and special characters...
   SYNOPSIS:    ./a.out
*/
#include 

int main()
   {
        printf("\033[H\033[J"); //      CLEAR THE SCREEN
        printf("Clear the Screen.\n");
        printf("\033[1;34m");   //      TURN TEXT BLUE
        printf("Change the color.\n");
        printf("\033[0m");      //      RESET THE COLOR TO NORMAL
        return(0);
   }



What is this dark magic? Well, Harry, I'm glad you asked. This makes use of something called "escape codes" or "escape sequences".

You've already come across some other escape codes, like /t (tab) and /n (newline). There are actually thousands of them - but you don't have to memorize all of them.

The primary ones are as follows:
\a     Alarm (beep)
\b     Backspace
\f     Form Feed
\n    Newline
\r     Return
\t     Tab
\v    Vertical Tab
\\     Backslash
\'     Single Quote
\"     Double Quote
\?     Question Mark
\0     Null
... and for extended alphabets (think Greek, Cyrillic, or colors - more on this later)
\OOO Octal Number - simply put the number in octal
\x## Hexadecimal Number - x preceding the Hex number
\uXXXX     Unicode - u preceding the number

Colors and stuff are great, but not very interactive, or even useful. In the end, when you go looking for a job, your future employer will be looking for someone who can write a USEFUL program, right?

So let's get started writing a program that is actually USEFUL! How about a simple calculator program? We'll call it: calc1

Wow - that sounds like quite a task. Well, it isn't. You've already written a program that can add, remember? It didn't print the answer out on the screen without help. You had to type "echo $?" [ENTER] after running it to get the answer... but it did work.

Well, we are going to go a bit farther this time. We are going to build a nice, pretty menu, have it ask you for input, and give the result. So hang on - this is going to be a bumpy ride!

Let us start with, well, the beginning! First we need to have a header for the program. In the head of the program, we must have our program description, etc. We also need our inclusions and... if there are any... declarations.

Once again, open nano and hack out some code: (nano calc1.c)
/*
   NAME:        calc1.c
   DESCRIPTION: Teaches definitions, float TYPEs, scanf(), if-then, and maths.
   SYNOPSIS:    ./calc1
*/

#include <stdio.h>

#define PI 3.1415926

int main()
{
printf("\033[H");printf("\033[J"); // clears the screen
printf("%f\n",PI);
return(0);
}



This is the beginning of our program. Before we compile and run it, let us look at the code a little and examine differences we have put into it:

Remember in an earlier section, we mentioned that there are multiple parts to any document. In the case of a letter or a white paper, there are headers, footer (or footnotes) and the main body. In Coding, it is also true that there is the main body of the document, and there may also be headers.

In the header of a C program, we have Inclusions (other programs that may need to be included for the compiler to understand how to deal with a particular function), Declarations and Definitions.

A Declaration tells the program that we are going to be using a specific variable (or even a function) throughout the program, regardless of what function we are in. Declarations must include the TYPE of the variable (or function) that we are declaring. So far the only type of variable or function that we have used is the int, or integer type.

When used with a function, it tells the compiler what TYPE of variable we will be RETURNing.

int main()

This means that we will be returning an integer.

{return (0);}

When we declare an integer, we can also define its original value.

int Volume = 10;

We are not restricted to integers in C. As we alluded to in an earlier secion, there are several different TYPES of variables. One such type is called a float - short for floating poing decimal.

float Deposit = 135.21;

We will be introducing other types later on in the next section. Not all numbers used in life change. The ones we do, we call variables. The ones that don't, we call constants. We can declare variables simply by giving the type, and we may define its value. Constants - we usually handle a bit differently. One such constant is PI - the number we use when calculating circles, etc. Instead of declaring it somewhere in the middle of a function [like main()], we typically declare it very early on (usually in the head of the program) right unde r the includes.

#define PI 3.1415926536

A Definition is like a Declaration, but it deals with CONSTANTS, rather than variables. (Some instructors refer to these as "constant variables", but this is actually an oxymoron, because constants do not vary). Definitions do not include the type. They are introduced with the keyword "#declare".

When we use this method - it is NOT a function, so it doesn't need a type Alternately, we could do the following within a function [like main()]: static float PI = 3.1415925636
or
const float PI = 3.1415925636

somewhere within the program

################################################################################
1 2 3 4 5
################################################################################