Program name: add-two-numbers.c
Language features of C contained in "add-two-numbers.c"
values that have names given to them, called "variables"
types of variables
creating the names and giving them values
(called "declaration" of and "assignment"
to the variables)
mathematical operations, written in C similarly to how we write them
on paper
special way to print variables in the middle of a sentence
Function: adds 4 plus 17 and prints 21
Source code:
Explanations:
Names can be given to values. In this program note the names "addend1", "addend2", and "sum". A name stands for an actual value. Because the value it stands for can be changed from time to time, we call it a "variable" which means "something that can change."
In order to have a name you have to say so. In official terminology, in order to have a variable you have to declare it. Line 5 declares the 3 variables, gives their names, establishes them. Beyond line 5 they can be used. As yet, they don't have any (known) value. But lines 6 and 7 take care of that for addend1 and addend2, where the programmer specifies values for them. sum, on the other hand, receives the value that the computer (not the programmer) produces by adding the two addends. C allows us to express the summation operation the same way we do on paper, with a plus sign. Lines 6-8 are all "assignment" statements.
Think of a variable as a container. It can receive contents, and its contents can be changed from time to time. In physical reality, the container is a small portion of memory set aside to hold the values of interest. So a byte of memory might be set aside to hold a number, and could hold it by flipping the bits within the byte to represent that number using the binary number system. So, zero could be represented by this byte: 00000000. One could be represented by 00000001. Four could be represented by 00000100. Five, by 00000101. Two-hundred fifty-five by 11111111. That's great, but it leads us to notice that it can't accommodate anything bigger than 255. And it makes no allowance for fractional numbers, for example 98.6. It doesn't have a way to handle the "point six." If you want numbers of a larger size, or with fractions in them, you need a different type of data. The computer must find some internal way to represent these other values. Therefore, C has "data types" and each variable must be declared to have one. In this program, note in line 5's declaration "int" precedes the names of our three variables. "int" is a data type. I doesn't accommodate fractions, but it can accommodate values up to about 2 billion because it uses more than my example's single byte to hold its value. It uses 4 bytes instead, so has a larger "capacity." int stand for "integer."
Note how you present a variable to the printf( ) function when you wan to print it. The printf in line 9 prints all three of our variables. But when writing the sentence you want it to print, you play a "fill in the blanks" game. You see %i appearing 3 times in the quoted sentence you want printed. Those are like blanks. Following the sentence, we supply what we want to fill each blank with. The first %i will get filled in with the value of addend1, the second with that of addend2, and the third with that of sum. %i is used to express a blank because the date type of all three variable is int. Other data types use other letters than i in this context.
To do:
Key this program in, using a text editor. Save it in a file named "add-two-numbers.c". Then, in order to convert it into form that contains correct Intel machine instructions, thus enabling the computer to run it, bring up the Windows command box and give this command:
bcc32 add-two-numbers.c
Alternatively use a browser to go to a C compiler web site. Cut your program's text from the text editor and paste it into the appropriate place in the web site. Tell the web site to compile (there's generally a button that says "compile" or maybe "run".)
If there are no error messages, a new file named "add-two-numbers.exe" will result. Then give the command:
add-two-numbers.exe
You will see "The sum of 4 and 17 is 21"" appear printed on the monitor.