آموزش زبان ز قسمت 6:
an integer variable. You can declare any number of variables of the same type with a single statement. For example:
int a, b, c;
declares three integers: a, b and c. You have to declare all the variables that you want to use at the start of the program. Later you will discover that exactly where you declare a variable makes a difference, but for now you should put variable declarations after the opening curly bracket of the main program.
Here is an example program that includes some of the concepts outlined above. It includes a slightly more advanced use of the printf function which will covered in detail in the next part of this course:
/*
/*
Program#int.c
Another simple program
using int and printf
*/
#include
main()
{
int a,b,average;
a=10;
b=6;
average = ( a+b ) / 2 ;
printf("Here ");
printf("is ");
printf("the ");
printf("answer... ");
printf("\n");
printf("%d.",average);
}
[program]
More On Initialising Variables
You can assign an initial value to a variable when you declare it. For example:
int i=1;
sets the int variable to one as soon as it's created. This is just the same as:
int i;
i=l;
but the compiler may be able to speed up the operation if you initialise the variable as part of its declaration. Don't assume that an uninitialised variable has a sensible value stored in it. Some C compilers store 0 in newly created numeric variables but nothing in the C language compels them to do so.
Summary
Variable names:
-
should be lowercase for local variables
-
should be UPPERCASE for symbolic constants (to be discussed later)
-
only the first 31 characters of a variables name are significant
-
must begin with a letter or _ (under score) character
Input and Output Functions
Objectives
Having read this section you should have a clearer idea of one of C's:
-
input functions, called scanf
-
output functions, called printf
On The Run
Even with arithmetic you can't do very much other than write programs that are the equivalent of a pocket calculator. The real break through comes when you can read values into variables as the program runs. Notice the important words here: "as the program runs". You can already store values in variables using assignment. That is:
a=100;
stores 100 in the variable a each time you run the program, no matter what you do. Without some sort of input command every program would produce exactly the same result every time it was run. This would certainly make debugging easy! But in practice, of course, we need programs to do different jobs each time they are run. There are a number of different C input commands, the most useful of which is the scanf command. To read a single integer value into the variable called a you would use:
scanf("%d",&a);
For the moment don't worry about what the %d or the &a means - concentrate on the difference between this and:
a=100;
When the program reaches the scanf statement it pauses to give the user time to type something on the keyboard and continues only when users press
The final missing piece in the jigsaw is using the printf function, the one we have already used to print "Hello World", to print the value currently being stored in a variable. To display the value stored in the variable a you would use:
printf("The value stored in a is %d",a);
The %d, both in the case of scanf and printf, simply lets the compiler know that the value being read in, or printed out, is a decimal integer - that is, a few digits but no decimal point.
Note: the scanf function does not prompt for an input. You should get in the habit of always using a printf function, informing the user of the program what they should type, before a scanf function.
Input and Output Functions in More Detail
One of the advantages of C is that essentially it is a small language. This means that you can write a complete description of the language in a few pages. It doesn't have many keywords or data types for that matter. What makes C so powerful is the way that these low-level facilities can be put together to make higher level facilities.
The only problem with this is that C programmers have a tendency to reinvent the wheel each time they want to go for a ride. It is also possible to write C programs in a variety of styles which depend on the particular tricks and devices that a programmer chooses to use. Even after writing C for a long time you will still find the occasionally construction which makes you think, "I never thought of that!" or, "what is that doing?"
One attempt to make C a more uniform language is the provision of standard libraries of functions that perform common tasks. We say standard but until the ANSI committee actually produced a standard there was, and still is, some variation in what the standard libraries contained and exactly how the functions worked. Having said that we had better rush in quickly with the reassurance that in practice the situation isn't that bad and most of the functions that are used