Source One Educational Society    
Home | OS Install | MS-Office | Photoshop | SQL | Networking | HTML | Java | Model Papers | FAQ's | Health Tips | News | Jobs | Fun SMS
C - Tutorial
INTRODUCTION
o Getting Started
o C Boot Disk
o What Is An Identifier
o Getting Started C
PROGRAM CONTROL
o While Loop
o Do-While Loop
o For Loop
o If Statement
o Break And Continue
o Switch Statement
o Goto Statement
o Finally, A Meaningful Program
ASSIGNMENT & LOGICAL COMPARES
o Integer Assignment Statements
o Additional Data Types
   
   
 
 
C Language Tutorial
Program Control  

Integer Assignment Statements

 

/* This program will illustrate the assignment statements */
main( )
{
int a,b,c; /* Interger variables for examples */
a = 12;
b = 3;
c = a + b; /* simple addition */
c = a - b; /* simple subtraction */
c = a * b; /* simple multiplication */
c = a / b; /* simple addition */
c = a % b; /* simple modulo (remainder) */
c = 12*a + b/2 - a*b*2/(a*c + b*2);
c = c/4+13*(a + b)/3 - a*b + 2*a*a;
a = a + 1; /* incrementing a variable */
b = b * 5;
a = b = c = 20; /* multiple assignment */
a = b = c = 12*13/4;
}

Three variables are defined for use in the program and the rest of the program is merely a series of illustrations of various assignments. The first two lines of the assignment statements assign numerical values to "a" and "b", and the next four lines illustrate the five basic arithmetic functions and how to use them. The fifth is the modulo operator and gives the remainder if the two variables were divided. It can only be applied to "int" or "char" type variables, and of course"int" extensions such as "long", "short", etc. Following these, there are two lines illustrating how to combine some of the variables in some complex math expressions. All of the above examples should require no comment except to say that none of the equations are meant to be particularly useful except as illustrations.

The next two expressions are perfectly acceptable as given, but we will see later in this chapter that there is another way to write these for more compact code.

This leaves us with the last two lines which may appear to you as being very strange. The C compiler scans the assignment statement from right to left, (which may seem a bit odd since we do not read that way), resulting in a very useful construct, namely the one given here. The compiler finds the value 20, assigns it to "c", then continues to the left finding that the latest result of a calculation should be assigned to "b". Thinking that the latest calculation resulted in a 20, it assigns it to "b" also, and continues the leftward scan assigning the value 20 to "a" also. This is a very useful construct when you are initializing a group of variables. The last statement illustrates that it is possible to actually do some calculations to arrive at the value which will be assigned to all three variables.

The program has no output, so compiling and executing this program will be very uninteresting. Since you have already learned how to display some integer results using the "printf" function, it would be to your advantage to add some output statements to this program to see if the various statements do what you think they should do.

This would be a good time for a preliminary definition of a rule to be followed in C. The data definitions are always given before any executable statements in any program block. This is why the variables are defined first in this program and in any C program. If you try to define a new variable after executing some statements, the compiler will issue an error.

 
 
 
Quick Links
Java FAQ's
XML FAQ's
.Net FAQ's
Javascript FAQ's
SQL Interview Questions
 
 
 
 
Home  |  Aboutus  |  Post Your Content  |  Tell a Friend  |  Contact us  |  Sitemap
© 2009 365edu. All Rights Reserved. Site by 365edu. Best view in 1024x768 resolution.