O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Performing Arithmetic

While the principles of arithmetic in C are exactly like those you learned in school, the actual results are more complex because of how C works with the different number types. For this reason, choosing the appropriate data type depends not only on the possible values it might store but also on how it will be used.

The standard operators for performing arithmetic are

  • +, for addition

  • -, for subtraction

  • *, for multiplication

  • /, for division

  • %, for modulus (finding the remainder of a division)

Using these operators, here are some sample calculations:

int a = 3;
int b = 2;
int c;
c = a + b; /* 5 */
c = a - b; /* 1 */
c = b - a; /* -1 */
c = a * b; /* 6 */
c = a / b; /* 1, not 1.5! */
c = a % b; /* 1 */

When you divide one integer ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required