Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Get Certified Upgrade Teachers Spaces
   ❮   
     ❯   

C Tutorial

C HOMEC IntroC Get StartedC SyntaxC OutputC CommentsC VariablesC Data TypesC Type ConversionC ConstantsC OperatorsC BooleansC If...ElseC SwitchC While LoopC For LoopC Break/ContinueC ArraysC StringsC User InputC Memory AddressC Pointers

C Functions

C FunctionsC Function ParametersC ScopeC Function DeclarationC Math FunctionsC Inline FunctionsC RecursionC Function Pointers

C Files

C Create FilesC Write To FilesC Read Files

C Structures

C StructuresC Nested StructuresC Structs & PointersC UnionsC typedefC Struct Padding

C Enums

C Enums

C Memory

C Memory Management

C Errors

C ErrorsC DebuggingC NULLC Error HandlingC Input Validation

C More

C DateC Random NumbersC MacrosC Organize CodeC Storage ClassesC Bitwise OperatorsC Fixed-width Integers

C Projects

C Projects

C Reference

C ReferenceC KeywordsC <stdio.h>C <stdlib.h>C <string.h>C <math.h>C <ctype.h>C <time.h>

C Examples

C ExamplesC Real-Life ExamplesC ExercisesC QuizC CompilerC SyllabusC Study PlanC Interview Q&AC Certificate

CArithmetic Operators


Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations.

OperatorNameDescriptionExampleTry it
+AdditionAdds together two valuesx + yTry it »
-SubtractionSubtracts one value from anotherx - yTry it »
*MultiplicationMultiplies two valuesx * yTry it »
/DivisionDivides one value by anotherx / yTry it »
%ModulusReturns the division remainderx % yTry it »
++IncrementIncreases the value of a variable by 1++xTry it »
--DecrementDecreases the value of a variable by 1--xTry it »

Here is an example using different arithmetic operators in one example:

Example

int x = 10;int y = 3;printf("%d\n", x + y); // 13printf("%d\n", x - y); // 7printf("%d\n", x * y); // 30printf("%d\n", x / y); // 3printf("%d\n", x % y); // 1int z = 5;++z;printf("%d\n", z); // 6--z;printf("%d\n", z); // 5

Try it Yourself »

Note: When dividing two integers in C, the result will also be an integer. For example,10 / 3 gives3. If you want a decimal result, usefloat ordouble values, like10.0 / 3.

Example

int a = 10;int b = 3;printf("%d\n", a / b);   // Integer division, result is 3double c = 10.0;double d = 3.0;printf("%f\n", c / d);   // Decimal division, result is 3.333...

Try it Yourself »


Incrementing and Decrementing

Incrementing and decrementing are very common in programming, especially when working with counters, loops, and arrays (which you will learn more about in later chapters).

The++ operator increases a value by 1, while the-- operator decreases a value by 1:

Example

int x = 5;++x; // Increment x by 1printf("%d\n", x); // 6

Try it Yourself »

Example

int x = 5;--x; // Decrement x by 1printf("%d\n", x); // 4

Try it Yourself »

Sometimes, you might both increment and decrement the same variable. Remember that if you increase a value and later decrease it, it will go up by one and then back down by one - ending up where it started:

Example

int x = 5;++x; // Increment x by 1 (x becomes 6)--x; // Decrement x by 1 (x becomes 5 again)printf("%d\n", x); // 5

Try it Yourself »

Real Life Example: Counting People

Imagine you are building a program to count how many people enter and leave a room. You can use++ to increase the counter when someone enters, and-- to decrease it when someone leaves:

Example

int peopleInRoom = 0;// 3 people enterpeopleInRoom++;peopleInRoom++;peopleInRoom++;printf("%d\n", peopleInRoom); // 3// 1 person leavespeopleInRoom--;printf("%d\n", peopleInRoom); // 2

Try it Yourself »




×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.


[8]ページ先頭

©2009-2025 Movatter.jp