The Universe is ending. Please logoff now.
   The Universe is ending. Please logoff now. Sunday - 20th July, 2008 - 06:47:28 

Site Menu  
 
Home
Web News
Reviews
Previews
Guides
Case Gallery
Contact Us
About Us
Sponsors
Links
 
     

  Visitors  
   
     

   Guide : Fundamental C Programming (Rev 1.0) »  
 

 

Fundamental C Programming (Rev 1.0) - Input/Output
   
 Date  : Feb 17th, 2001
 Category  : Programming
 Manufacturer   : N/A
 Author  : Jin-Wei Tioh
The standard C library provides quite a few functions for doing input. The scanf function is the most commonly used; others include getchar and gets. Using scanf, let's write a program that will read in two integers (whole numbers), calculate the sum of the values and print the result to the display using printf.

Code :

/* Integer addition program */

#include <stdio.h>

main() {

/* Variable declaration */
int number1, number2, sum;

printf("First integer\t: ");
scanf("%d", &number1);
printf("Second integer\t: ");
scanf("%d", &number2);

sum = number1 + number2;
printf("The sum of %d and %d is %d\n", number1, number2, sum);
return 0;

}

Output :

Since this program involves both input and output, it would definitely look messier than the previous example, which involved only output. Not to worry, let's step through the program line by line. Again, we have comments, the preprocessor directive, main(), { and }. The line :

int number1, number2, sum;

is called a declaration. number1, number2 and sum are the names of variables. A variable is a location in memory where data can be stored for use by a program. To use a variable, it must be declared first. This tells the compiler a few important things : what type of variables we want; what names the variables will have. The declaration in our program tells the program to create three variables, number1, number2 and sum, of type int, which means that these variables will hold integer values, ie. whole numbers. In C, variables must be declared immediately after the left brace that begins the body of main. Most compilers will give a compilation error if you attempt to do otherwise. Several variables of the same type can be declared in one declaration, but we could have also written :

int number1;
int number2;
int sum;

Three separate declarations, one for each variable. However, this isn't necessarily a bright idea, since the single declaration is clearer and more concise. Of course, C programs can deal with other data types as well, such as float (real numbers) and char (characters). We'll only use these in subsequent articles.

A variable name in C is any valid identifier. They may include letters, digits and underscores, so long as they do not start with a digit. According to the ANSI C standard, only the first 31 characters of an identifier are required to be recognized by compilers. Thus :

ali_baba_and_the_fourty_thieves_part_one
ali_baba_and_the_fourty_thieves_part_two

would be exactly the same to the compiler, since part_one and part_two are ignored. C identifiers are also case sensitive - uppercase and lowercase letters are different, so number1 and Number1 would be different identifiers.

Diverging a bit from the program step-through, there are also keywords in C. Not all identifiers may be used freely because some have special meanings. If we used these as identifiers, the compiler would misinterpret our program. The keywords are :

Keywords
auto break case char
const continue default do
double else enum extern
float for goto if
int long register return
short signed sizeof static
struct switch typedef union
unsigned void volatile while

The lines (not in order) :

printf("First integer\t: ");
printf("Second integer\t: ");

print the strings First integer : and Second integer : on the display and make use of the horizontal tab escape sequence to evenly align the colons. They are called prompts because they well, prompt the user to take a specific action. In this case, we want the user to enter integers. However, the action is on the lines (not in order) :

scanf("%d", &number1);
scanf("%d", &number2);

These lines use scanf to obtain a value from the user. Think of scanf as a pizza delivery service. It takes input from the standard input (the keyboard, not the pizza parlor), and "delivers" the data to the computer's memory. But, just like the poor chap in real life who has to know where you live to deliver your pizza, scanf must know where in the computer's memory must it store data. This is where scanf's two arguments come in, ie. the "%d" and &number1. "%d" is the format control string, which specifies the type of data that the user inputs. The %d conversion specifier specifies that the data should be an integer. The % is treated as an escape character in both scanf and printf, just like \. Thus, %d forms an escape sequence (like \n).

The second argument begins with an ampersand (& ) - the address operator - followed by our variable name. &number1 tells scanf the location in memory where the variable number1 resides. So after reading in the data, scanf stores the value for number1 at that location.

When the computer executes the scanf statement, it sits and waits for the user to enter a value. The user responds by typing an integer, then pressing the enter or return key. As described earlier, the computer assigns this number to our variable. The next statement is also known as an assignment statement :

sum = number1 + number2;

It is named as such because it contains the C assignment operator, =. The value of number1 + number2 is calculated and assigned to the variable sum. Correctly, the statement reads "sum gets the value of number1 + number2", and not "sum equals to the value of number1 + number2". In C, the equality operator is ==, but I'll cover this further only in subsequent articles. Finally, the statement :

printf("The sum of %d and %d is %d\n", number1, number2, sum);

uses the printf function to print the literal The sum of and is, with the values of our three variables (number1, number2, sum) in between. In this statement, printf has four arguments, "The sum of %d and %d is %d\n", number1, number2 and sum, separated by commas. The first argument is the format control string, which contains some literal characters as well as three %d conversion specifiers, indicating that three integers will be printed. The second, third and fourth arguments specify the values to be printed. They can either be all variable names, all values or a mix of both. We could have a statement that would print Napster is 2 years old :

printf("Napster is %d years old\n", 2);

Pay attention to two things though. One, the conversion specifier for an integer is the same in both printf and scanf. This is the case for most data types in C. Two, the values/variables are printed out in the sequence that they are specified, ie. if we had written :

printf("The sum of %d and %d is %d\n", sum, number1, number2)

we would have gotten The sum of 11 and 3 is 8.

Calculations can also be performed within printf statements. We can replace these two lines :

sum = number1 + number2;
printf("The sum of %d and %d is %d\n", number1, number2, sum);

with

printf("The sum of %d and %d is %d\n", number1, number2, number1+number2);

in which case, we wouldn't need the third variable sum. You can do things either way, since it is mainly a matter of personal preference.Let's move on to the various arithmetic operators in C.

 Print this article Arithmetic Operations

 E-mail this article

 Discuss this article

 
 
     


Copyright © 2000-2005 BlueSmoke. All rights reserved. Terms, Conditions and Privacy Information.
Site Design by Jin-Wei Tioh

Sitemap