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 Underwear 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 Underwear 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.

...

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);
   }



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. We will be introducing another type later on in this section.

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

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