Lecture 2 - Introduction to Data

Up to Main Page
Back to Previous Lecture
Forward to Next Lecture

This topic introduces you to the type of data that computers use. Most of the example programs will be using integer numbers, but many of them could be done with floating point numbers as well.

Second Program - Add Two Numbers

This little program add.c, simply adds two numbers together and displays the result. While somewhat interesting, it is unlikely that you would actually use such a program, when all you really needed was a calculator. :^)

Get An Integer from the User

The program getint illustrates how to get an integer value from the user. Everything that the user enters comes in as a character string. Similarly, everything you display will be a character string as well. When displaying a message, the printf function converts numbers to character strings. When getting information from the user, I recommend that you use the gets (get string) function to read whatever the user entered, and then convert that string to a number using either the atoi or the atof function.

In case you were wondering, atoi stands for "ASCII to Integer". Likewise, atof stands for "ASCII to Float".

ASCII is the code that most computers use to represent characters. Since everything inside the computer is a number, there must be numbers to represent the various characters. You can examine this code in the Appendix of the text book.

Too see what is required to get a floating point value from the user, look at the program getfloat.c.

Getting Numbers from User and Displaying Sum

The program addnums.c shows you how to get two numbers from the user, add them together and display their sum. A floating point version is also available in addfloat.c.

Lining Up Numbers

This program add4.c will read in four numbers from the user and display them so that they all line up. As an added feature, this program will display the total of the numbers that were entered.

Getting Information From the User

The program gets.c shows you how to get character string information from the user. It also shows you how to look at the first character of the string that the user entered.

Getting Information From User

The program getinfo.c shows you how to get various information from the user. In this program, we get their first and last name, as well as their age. This program uses the gets() and atoi() functions, which is they way I recommend.

The program scanf.c shows you how to do the same thing using scanf, but I caution you to not get in the habit of using scanf. It might seem that the gets() and atoi() is harder and more complicated, but not when you consider the problems you can get into with scanf.

Up to Main Page
Back to Previous Lecture
Forward to Next Lecture