Website by Joshua Bleier

Module 1 - Programming Basics
Variables & Data Types

Se far we've only used the output portion of the standard input/ouput library, stdio.h. In the SCANF directory of the week1 folder you decompressed from week1.tar, we see the input function from that library; namely, scanf.

Scanf works almost exactly like printf; it takes a string as the first argument, but the string only contains the format identifier; it must match the data type of the variable we'll use to store the value input by the user. The variable that we're using is the next argument to scanf, with an addition; we place an ampersand & in front of that variable, so that the value input by the user goes into the right place (We'll discuss the ampersand at length when we get to the pointers module).

Compile and run sumi.c and productf.c from the SCANF directory, inputting the values of your choice when requested, and note the results.

Now, let's think a little bit about what would happen if we violate one of the rigorous syntax rules that govern the construction of C programs.

  • What would happen if we construct a syntactically incorrect C program? Specifically, if we left off a semi-colon ;? Edit sumi.c by removing a semi-colon, and then try to compile it. What happens? Did you guess correctly?
  • Let's think about something a bit more subtle; a syntactically correct, but grammatically incorrect program. For instance, if we declared a floating point variable, put tried to print it out using an integer format identifier, what would happen?

If we violate syntax rules, we get an error during compilation, and it won't generate an executable file. However, if the syntax is correct, but we mess up on the grammar (Like a data type/format identifier mismatch), the program will compile, but something will go wrong during program execution.

These kind of bugs are subtle in nature, and often much more difficult to detect. Keep this in mind when you write programs; specifically, read and reread your program, going over it line by line, making no assumptions, and you'll likely save yourself significant time with reduced debugging.

To help commit the important concepts we covered in this module, take a look at the tutorials we mentioned in the previous Basic Concepts Module.

  • Go back to About.com's C Tutorials; specifically, take a look at the one covering Variables.
  • As you many have guessed, HowStuffWorks also provides a tutorials on variables; take a look at that one as well.
  • It should be clear by now that what we're doing here is not unique; C tutorials abound on the net. Follow along as we continue through our C programming modules with the tutorial of your choice, either one of the above, or one you find from a Google (or Yahoo or AskJeeves) search on C Programming Basics, (or the Google Directory for C Tutorials we mentioned in the last Module).

And if you notice a discrepancy between one of those tutorials and our modules, or if you find a method of instruction you find particularly compelling, please do not hesitate to bring it up to Dr. Impelluso or Joshua Bleier, the designers of these modules.

We're always looking for ways to improve this course, and your input is an importnat part of making that happen.

Move on the the next Module (Loops) ->