Posted on 19th September 200921 Responses
What is the Assignment Operators

The Assignment Operator
We can use the assignment operator within any valid expression. This is not the case with most computer languages (including Pascal, BASIC, and FORTRAN), which treat the assignment operator as a special case statement. The general form of the assignment operator is
variable_name = expression;
where an expression may be as simple as a single constant or as complex as we require. Frequently in literature on C/C++ and in compiler error messages we will see these two terms: lvalue and rvalue. Simply put, an lvalue is any object that can occur on the left side of an assignment statement. For all practical purposes, “lvalue” means “variable.” The term rvalue refers to expressions on the right side of an assignment and simply means the value of an expression.

Type Conversion in Assignments
When variables of one type are mixed with variables of another type, a type conversion will occur. In an assignment statement, the type conversion rule is easy: The value of the right side (expression side) of the assignment is converted to the type of the left side (target variable), as illustrated here:
int x;
char ch;
float f;
void func(void)
{
ch = x; /* line 1 */
x = f; /* line 2 */
f = ch; /* line 3 */
f = x; /* line 4 */
}
In line 1, the left high-order bits of the integer variable x are lopped off, leaving ch with the lower 8 bits. If x were between 255 and 0, ch and x would have identical values. Otherwise, the value of ch would reflect only the lower-order bits of x. In line 2, x will receive the nonfractional part of f. In line 3, f will convert the 8-bit integer value stored in ch to the same value in the floating-point format. This also happens in line 4, except that f will convert an integer value into floating-point format.
When converting from integers to characters and long integers to integers, the appropriate amount of high-order bits will be removed. In many 16-bit environments, this means that 8 bits will be lost when going from an integer to a character and 16 bits will be lost when going from a long integer to an integer. For 32-bit environments, 24 bits will be lost when converting from an integer to a character and 16 bits will be lost when converting from an integer to a short integer.
Remember that the conversion of an int to a float, ora float to a double, and so on, precision or accuracy. These kinds of conversions only change the form in which the value is represented. In addition, some compilers always treat a char variable as positive, no matter what value it has, when converting it to an int or float. Other compilers treat char variable values greater than 127 as negative numbers when converting. Generally speaking, you should use char variables for characters, and use int s, short int s, or signed char s when needed to avoid possible portability problems.

Multiple Assignments
C/C++ allows you to assign many variables the same value by using multiple assignments in a single statement. For example, this program fragment assigns x, y, and z the value 0:
x = y = z = 0;

Target Type Expression Type Possible Info Loss
signed char char If value > 127, target is negative
char short int High-order 8 bits
char int (16 bits) High-order 8 bits
char int (32 bits) High-order 24 bits
char long int High-order 24 bits
short int int (16 bits) None
short int int (32 bits) High-order 16 bits
int (16 bits) long int High-order 16 bits
int (32 bits) long int None
int float Fractional part and possibly more
float double Precision, result rounded
double long double Precision, result rounded
The Outcome of Common Type Conversions

In professional programs, variables are frequently assigned common values using this method.

Popularity: 80% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • BlinkList
  • blogmarks
  • BlogMemes Fr
  • BlogMemes Sp
  • Blogosphere News
  • blogtercimlap
  • co.mments
  • connotea
  • Current
  • Design Float
  • Diigo
  • DotNetKicks
  • DZone
  • eKudos
  • email
Comments
comment by Aabba
Posted on October 16, 2009 at 10:10 am

Thats really a very nice article…its really helps me lot to learn about Assignment operator.

comment by Ambroziy
Posted on October 19, 2009 at 10:07 am

thank you for this nice article.

comment by trongaphogigo
Posted on October 20, 2009 at 9:59 pm

hi sir, how are you? Its really a superb article on Assignment Operator. I love to read your articles.
Thank you

comment by Pharmg106
Posted on October 27, 2009 at 3:51 am

Very nice site!

comment by Pharme393
Posted on October 27, 2009 at 3:51 am

Very nice site!

comment by Pharmk836
Posted on November 16, 2009 at 2:30 pm

Very nice site!

comment by Pharmf413
Posted on November 27, 2009 at 5:47 am

Very nice site!

comment by GauttyKam
Posted on December 7, 2009 at 9:58 am

This is really a very beautiful article…by this i was really learned lot about assignment operator.

comment by pamqandixf
Posted on December 8, 2009 at 1:55 pm

hello sir, thank you for this very educational article on assignment operator. please explain all operators with example

comment by vroxyrfi
Posted on December 8, 2009 at 4:26 pm

is it possible that we can use assignment operator in .net also…

comment by greprocactcaW
Posted on December 8, 2009 at 6:20 pm

how we can use assignment operator….

comment by hentai
Posted on December 8, 2009 at 8:19 pm

Sir can you please explain the Assignment Operator in easy language??

comment by Talapomilow
Posted on December 9, 2009 at 2:40 am

very nice article on Assignment Operator

comment by pokemon
Posted on December 9, 2009 at 2:52 am

please give some more example of Assignment operator

comment by yaoi
Posted on December 9, 2009 at 10:06 am

except assignment operator how many operator we have…….???

comment by clintongonzes
Posted on December 10, 2009 at 8:26 am

Thats cool. I agree, that was a good post!Dude a friend of mine has been was tring to increase Ejaculation Volume,. He try all natural threaph, they were working, but very slow, so at the end, he try try semen volume pills to increase prodcution, volume and flavour. yes results were great. so my suggestion if u want to Increase Ejaculation Volume

Click here to get Best Sperm pills to Multiple Orgasms

comment by doujin
Posted on December 10, 2009 at 10:30 am

that is a cool article of a very cool site

comment by paxfozygyla
Posted on December 10, 2009 at 10:51 pm

is this the perfect definition of Assignment Operator

comment by maclean
Posted on December 14, 2009 at 12:06 pm

i think dis is not d proper definition of Assignment operator

comment by bedaDuand
Posted on December 14, 2009 at 4:10 pm

how many types of operators we have in C & C++

comment by aretbagbeaupe
Posted on February 14, 2010 at 8:49 am

Hello! Can you tell me how i can register mail at google google http://google.com

Leave a Response