Home > Articles > Programming > Visual Basic

This chapter is from the book

Expressions

Expressions cover assigning or computing values and use a sequence of operators and operands. The operators and operands are combined in such a way as to obtain the desired results.

There are an unlimited number of ways to build expressions, but there are some basic rules that are fairly easy to follow. Most of these rules in C are similar to those in Visual Basic. C has some "shortcut" operators that make statements shorter, like the ++ operator that can increment a variable.

This section covers the operators, precedence, conversions, and type casts. This gives you the building blocks for expressions.

Operators and Precedence

In this section I discuss operators, their uses, and their precedence. This provides much of the information you need in order to successfully use these operators within expressions.

Unary Operators

The unary operators in C include *, &, sizeof, -, ~, !, ++, and --. You've seen and used the indirection (*) and address-of operators (&) earlier in this chapter in the section on pointers:

  • Indirection operator—This is used to assign a value to the variable that a pointer variable is pointing to.

  • Address-of operator—This operator is used to assign a variable's address to a pointer. These are demonstrated following:

  • int x=10;
    int *px = &x; //address-of operator to assign pointer
    *px = 20; //indirection to assign value
  • Sizeof operator—This operator provides the amount of storage required to store the item in the operand. This is expressed in bytes. Len in Visual Basic roughly equates to this operator.

  • Negation operator (-)—This operator does what you might expect: It returns the negative of the operand. Visual Basic has the same operator.

  • Bitwise not (~)—This operator produces the bitwise complement of the operand. The logical not (!) produces the logical opposite of the operand. Visual Basic uses the NOT keyword for the logical not.

  • Increment (++)and decrement (--)operators—These operators can be used in a couple ways. When used within an expression, it is significant if these operators are used as prefix operators or suffix operators. Basically they increment or decrement the variable they are affixed to. Used creatively they can save code. The code following shows how to use these operators.

    x = 0;
    item[++x] = 1; //item[1]
    x = 0;
    item[x++] = 1; //item[0]

Using the increment operator as a prefix changes the value before the assignment. Using it as a suffix operator increments the value after the assignment.

Binary Operators

The binary operators include *, /, %, +, -, <<, >>, <, >, <=, >=, ==, !, =, &, |, ^, &&, ||, and ,; only some of these have counterparts in Visual Basic. And unlike VB, C also allows a combination of the assignment operator and other binary operators.

The multiplicative operators—the multiplication (*), division (/), and remainder (%) —all have counterparts in Visual Basic. Only the remainder operator is different in VB, the Mod operator is used in VB instead of the %. The multiplication and division operators are the same in C and VB. These three operators work pretty much like you'd expect.

The additive operators plus(+) and minus (-) have direct counterparts in Visual Basic and work just as you'd expect.

The bitwise shift operators shift their operand left(<<) or right (>>) by the number of positions specified. Visual Basic can simulate this by multiplying by a number, but the shift still does things a bit differently than a multiplication does and works across the entire unsigned number. Because of signed numbers, bit shifts in Visual Basic are fairly complex issues.

Logical comparison operators are the same as Visual Basic in many cases, but different enough in a couple of cases to be aggravating. The <, >, <=, and >= operators are all the same as VB and work like you would expect them. The equivalent comparison is different (==) in C than in VB (=). Also the not operator in C (!) is different than VB (NOT). A common way of using the not operator (!) in C has a different counterpart in VB: not equal in C is !=, in VB it is <>.

Bitwise operators AND (&), OR (|), and exclusive OR (^) compare to the Visual Basic keywords AND, OR, and XOR. The problem with the Visual Basic operators is that context decides whether they are bit operators or logical operators and this can produce unexpected results in some cases. In addition you have the fact that there are no unsigned types in Visual Basic which hinder the use of bitwise operators.

Logical operators AND (&&) and OR (||) compare to the Visual Basic operators AND and OR. Again in VB, context determines which operation is performed, logical or bitwise.

The == operator is worth further mention. This is an equal comparison operator. In Visual Basic the equal operator does double duty, both as assignment and comparison. Not so in C. This has both good points and bad points and it can be especially troublesome to a VB programmer.

VB programmers are used to using the equal sign for comparison and so typically make the mistake of trying to use it in C. This is a simple thing, but sometimes habit takes over. Many times misuse of this operator will not produce an error, just an unwanted result.

C operators are summarized in Table 3.2.

Table 3.2 C Operators

Operator

Basic Use

Visual Basic

Unary

 

 

*

Indirection

None

&

Address of

Varptr

sizeof

Size of

Len(x)—sort of

-

Negation

-

~

Bitwise not

NOT

!

Logical not

NOT

++

Increment

x = x + 1

--

Decrement

x = x - 1

Binary

 

 

*

Multiplication

*

/

Division

/

%

Remainder

MOD

+

Addition

+

-

Subtraction

-

<<

Left bit shift

None

>>

Right bit shift

None

<

Less than

<

>

Greater then

>

<=

Less then or equal

<=

>=

Greater than or equal

>=

==

Logical equal

=

!

Not

NOT

=

Equal assignment

=

&

Bitwise AND

AND

|

Bitwise inclusive OR

OR

^

Bitwise exclusive or

XOR

&&

Logical AND

AND

||

Logical OR

OR

,

Sequential evaluation

None

? :

Conditional Evaluator

IIF

Assignment

 

 

=

Simple assignment

=

*=

Multiplication assignment

x = x * y

/=

Division assignment

x = x / y

%=

Remainder assignment

x = x MOD y

+=

Addition assignment

x = x + y

<<=

Left shift assignment

None

>>=

Right shift assignment

None

&=

Bit AND assignment

x = x AND y

|=

Bit OR assignment

x = x OR y

^=

Bit Exclusive OR assignment

x = x XOR y


Precedence

Operators listed at the same level have the same precedence. As in Visual Basic, grouping statement segments within parentheses can alter precedence.

Table 3.3 summarizes operator precedence.

Table 3.3 C Operator Precedence

Symbols

Basic Use

Associativity

[]().-> postfix ++ postfix --

Expression

Left to Right

Prefix ++ prefix—sizeof & * - ~ !

Unary

Right to Left

Type casts

Unary

Right to Left

* / %

Multiplicative

Left to Right

+ -

Additive

Left to Right

<< >>

Bitwise shift

Left to Right

< > <= >=

Relational

Left to Right

== !=

Logical Equality

Left to Right

&

Bitwise AND

Left to Right

^

Bitwise Exclusive OR

Left to Right

|

Bitwise Inclusive OR

Left to Right

&&

Logical-AND

Left to Right

||

Logical-OR

Left to Right

?:

Conditional-expression

Right to Left

= *= /= %= += -= <<= >>= &= ^= |=

Assignment

Right to Left

,

Sequential evaluation

Left to Right


There are subtle problems that can creep into expressions with multiple operators depending on your compiler and the current compilation flags. For example, logical expressions will often "shortcut" the latter part of the expression if the first part is enough to determine the outcome of the logical expression.

Tip

Precedence is difficult to deal with in complex expressions. My advice is to group your expressions so there will be little ambiguity.

Conversions and Type Casts

Conversions and type casts follow a set of rules. These rules involve a couple of definitions that you need to know to understand the conversions involving numeric data:

  • Sign extend—This rule dictates how smaller integral types are converted to larger ones. The number must be represented in a different manner because the bit that denotes the negativity is in a different spot.

  • Preserve low order word or byte—This rule describes how a larger integral type is converted to a smaller one. The high order word or byte will be ignored, effectively it is dropped.

  • Loss of precision—This rule governs what happens when a number is converted to float and the significant digits are larger than 7. It can also happen with a double after 14 digits.

From Signed Integral Types

When converting from signed integral types, the main point to remember in integral conversions is that there is a loss of the upper bytes when going from a larger integral to a smaller one, like a long to a short. If there is information contained in the upper bytes, it will therefore be lost. Converting from a smaller int to a larger one involves no real problems. Converting a signed integer to an unsigned one doesn't change the data as long as they are the same size. It may change the way the bits are interpreted if the signed number is negative, but the in memory representation will remain unchanged as long as they are the same size.

Converting an integral type to a decimal type (float or double) should leave the number unchanged, unless the number is longer than the decimal type's significant digits, in which case a loss of precision will occur. A summary of signed integral conversions is shown in Table 3.4. Also a floating-point representation has gaps in its coverage of all decimal numbers (which is infinite).

Note

Because the long will be converted to a mantissa and an exponent (for example, 1000 becomes 1E+3), you can actually end up with a different number than what you started with. This has nothing to do with loss of precision that results from too few significant digits.

Table 3.4 Signed Integral Conversions

From

To

Method

char

short

Sign-extend

char

long

Sign-extend

char

unsigned char

Preserve pattern; high-order bit loses function as sign bit

char

unsigned short

Sign-extend to short; convert short to unsigned short

char

unsigned long

Sign-extend to long; convert long to unsigned long

char

float

Sign-extend to long; convert long to float

char

double

Sign-extend to long; convert long to double

char

long double

Sign-extend to long; convert long to double

short

char

Preserve low-order byte

short

long

Sign-extend

short

unsigned char

Preserve low-order byte

short

unsigned short

Preserve bit pattern; high-order bit loses function as sign bit

short

unsigned long

Sign-extend to long; convert long to unsigned long

short

float

Sign-extend to long; convert long to float

short

double

Sign-extend to long; convert long to double

short

long double

Sign-extend to long; convert long to double

long

char

Preserve low-order byte

long

short

Preserve low-order word

long

unsigned char

Preserve low-order byte

long

unsigned short

Preserve low-order word

long

unsigned long

Preserve bit pattern; high-order bit loses function as sign bit

long

float

-Represent as float. If long cannot be represented exactly, some precision is lost.

long

double

-Represent as double. If long cannot be represented exactly as a double, some precision is lost.

long

long double

-Represent as double. If long cannot be represented exactly as a double, some precision is lost.


From Unsigned Integral Types

The concerns with unsigned types are the same as with signed integral types. Again, going from unsigned to signed may change the way the bits are interpreted, but in memory it will still be the same if the types are the same size. A summary of unsigned integral conversions is shown in Table 3.5.

Table 3.5 Unsigned Integral Conversions

From

To

Method

unsigned char

char

Preserve bit pattern; high-order bit becomes sign bit

unsigned char

short

Zero-extend

unsigned char

long

Zero-extend

unsigned char

unsigned short

Zero-extend

unsigned char

unsigned long

Zero-extend

unsigned char

float

Convert to long; convert long to float

unsigned char

double

Convert to long; convert long to double

unsigned char

long double

Convert to long; convert long to double

unsigned short

char

Preserve low-order byte

unsigned short

short

Preserve bit pattern; high-order bit becomes sign bit

unsigned short

long

Zero-extend

unsigned short

unsigned char

Preserve low-order byte

unsigned short

unsigned long

Zero-extend

unsigned short

float

Convert to long; convert long to float

unsigned short

double

Convert to long; convert long to double

unsigned short

long double

Convert to long; convert long to double

unsigned long

char

Preserve low-order byte

unsigned long

short

Preserve low-order word

unsigned long

long

Preserve bit pattern; high-order bit becomes sign bit

unsigned long

unsigned char

Preserve low-order byte

unsigned long

unsigned short

Preserve low-order word

unsigned long

float

Convert to long; convert long to float

unsigned long

double

Convert directly to double

unsigned long

long double

Convert to long; convert long to double


From Floating Point Types

Floating point conversions can result in loss of precision or can even be undefined if, for example, the float value is converted to a short and the float value is out of the short's range. A summary of floating point conversions is shown in Table 3.6.

Table 3.6 Floating Point Conversions

From

To

Method

Float

char

Convert to long; convert long to char

Float

short

Convert to long; convert long to short

Float

Llong

Truncate at decimal point. If result is too large to be represented as long, result is undefined.

Float

unsigned short

Convert to long; convert long to unsigned short

Float

unsigned long

Convert to long; convert long to unsigned long

Float

double

Change internal representation

Float

long double

Change internal representation

Double

char

Convert to float; convert float to char

Double

short

Convert to float; convert float to short

Double

long

-Truncate at decimal point. If result is too large to be represented as long, result is undefined.

Double

unsigned short

Convert to long; convert long to unsigned short

Double

unsigned long

Convert to long; convert long to unsigned long

double

float

Represent as a float. If double value cannot be represented exactly as float, loss of precision occurs. If value is too large to be represented as float, the result is undefined.

long double

char

Convert to float; convert float to char

long double

short

Convert to float; convert float to short

long double

long

Truncate at decimal point. If result is too large to be represented as long, result is undefined.

long double

unsigned short

Convert to long; convert long to unsigned short

long double

unsigned long

Convert to long; convert long to unsigned long

long double

float

Represent as a float. If double value cannot be represented exactly as float, loss of precision occurs. If value is too large to be represented as float, the result is undefined.

long double

double

The long double value is treated as double.


To and From Pointers

Pointers can be converted to integral types and back to pointers. Here you simply need to be aware of the size of the pointer and the size of the integral, which may be determined by the memory-addressing model of your operating system (for example, 32 bit for WIN32).

Generally speaking pointers can be converted without loss of data or any consequences. However there can be some alignment issues with some types of pointers, as well as other issues with the data.

Note

The biggest issue in converting a pointer has to do with accessing the data after the pointer is converted. Usually this is why you convert the pointer, so you can access the data. However, there is no protection in C if you want to convert a pointer to a double to a pointer to a long and then try to use the data like it contains long data. This gives erroneous results, but you can do it.

Other Types

Because an enum is an integer, conversions for it work the same as for an integer. For the Microsoft compiler, an integer is the same as a long.

Void types have no value and cannot be converted—only void pointers may be converted to other pointer types with no loss of information.

Type Casts

Type casts are the way you change one variable type to another. Obviously some type casts are allowable and others are not. The conversion rules you saw in the previous sections apply to type casts as well as implicit conversions. The code following shows a few examples of type casts.

//type casts
//int to short
int i=2;
short sval = (short) i;
//long to pointer – common in handling lParam of SendMessage
char *p;
p = (char *)lParam;

Table 3.7 summarizes the allowable type casts.

Table 3.7 Type Cast Conversions

Destination Types

Potential Sources

Integral types

Any integer type or floating-point type, or pointer to an object

Floating-point

Any arithmetic type

A pointer to an object, or (void *)

Any integer type, (void *), a pointer to an object, or a function pointer

Function pointer

Any integral type, a pointer to an object, or a function pointer

A structure, union, or array

None

Void type

Any type


InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020