C-programming for newbees!

By McCodEMaN

Part 5








A variable name...

A variable name in the c language is any valid identifier, but the name can not begin with a digit and blankspace is not allowed.

Please note!
-If you like you can use underscore _
-A is not the same as: a , C is Case sensitive.
-Variable declaration must be made direcctly after the left brace, (before any executable statements)!


                                                   Our second C-Program



This program uses the function scanf to obtain two integers typed in by a user, it computes the sum and displays
the result on our screen by using standard input/output library function printf.

Here is the code:

-------------->8--------------------------->8--------------------------->8---------------------
 

                           /* C -exercise two */

                           #include <stdio.h>
 
                           main ()
                           {
                               int integer1, integer2, sum;

                          printf("Enter first integer\n");
                          scanf("%d", &integer1);
                          printf("Enter second integer\n");
                          scanf("%d", &integer2);
                          sum = integer1 + integer2;
                          printf("sum is %d\n", sum);

                          return 0;
                           }



---------------->8------------------------->8---------------------------->8---------------------



 

Line.1

This line is a comment.

Line.2

Next line is: #include <stdio.h>
This line is a directive to the C preprocessor!
Lines that begins with # are processed by the preprocessor before the program compilates. This line
tells the preprocessor to include all of the contents in a header file.

So, what's so good about this?...well, think about it..it's great!
Now we can use the same code in alot of different programs. We can also paste in ready made code made by someone
else, which is what we have done in this case.


Ok, let's explain everything in line.2!

#              Means that this line is for the preprocessor only, not the compilator.

include    Tells the preprocessor to include...

                Then we have made a blankspace. This is of great Importance!

<stdio.h>This is the name of the headerfile and it stands for: STandarD In-Out, all headerfiles has the ending .h



Line.3

We leave this line empty!



Line.4

main ()         To find out more about this function read Part 4



Line.5

{                  To find out more about this function read Part 4



Line.6

int iteger1, integer2, sum;

This is a declaration and iteger1, integer2, and sum are the names of variables!
In this case the declaration specifies that the variables are of type int, so that means that the variables
will hold integer values.
The C language has been developed in order to to help programmers and that's best done by protecting him from his
own mistakes. This is the reason that some things has to be declarated immediatly!
Deep inside the computer you'll find nothing but different types of integers, or data. When programming you'll use
several different integers and it's easy to mix them up, so the compilator demands that you in advance states which you
want to use and that's what this line is all about!



Line.7

printf("Enter first integer\n");

This statement prints Enter first integer on your screen. This is a prompt message because it tells the user to
make a specific action.
The \n is an escape sequence, for more information on this read lesson.4!



Line.8

scanf("%d", &integer1);

scanf is used to obtian a value from the user. This scanf has two arguments, %d and &integer1.




                  Explanation of the argument:    %d


                       The % is in this case treated as an escape character by scanf and d stands for decimal integer, %d conversion specifier indicates
                       that the data should be an integer. When combining %d we get an escape sequence.



                  Explanation of the argument:    &integer1


                       The second argument of scanf starts with & and is called address operator, in C followed by a variable name.
                       & combined with the variable name tells scanf where in memory the variable integer1 are to be stored, the computer
                       then saves the value for integer1 at that location.
                       When the computer executes this scanf it waits for the user to enter a value for this variable, the user should response by
                       typing in a value and followed by "ENTER" for transmission.
                       When the computer has received this value it assigns the value to variable: integer1.




Line.9

printf("Enter second integer\n"); types Enter second integer on your screen.




Line.10

scanf("%d", integer2); receives a value from the user.




Line.11

sum = integer1 + integer2;

At this line is the sum of integer1 and integer2 calculated, the result is assigned to the variable sum , this is made by assignement operator =
The two operators   =   and   +   is called binary operators, in this case is  =  the operator and the two operands are:

"sum" and "integer1 + integer2"




Line.12

printf("sum is %d\n", sum);

printf is used to print sum is on your screen followed by the numerical value of the variable sum.
This prinf has two arguments: sum is %d\n and sum, the first argument is the format control string and that string contains some
literal characters to be printed at the screen and the conversion specifier %d.
The second argument specifies the value that should be printed.

Please note! The conversion specifier for a integer is the same in both scanf and printf.




Line.13

return 0;

This line sends back the value zero to the operating system environment in which the program is to be executed, that indicates
to the operating system that the program was executed safely.




Line.14

}

The right brace indicates that we reached the end of main.








*****END OF PART FIVE*****







This C-Programming Lesson is written by McCodEMaN ŠTRES2000. All Rights Reserved.