Global Trend Radar
Web: www.geeksforgeeks.org US web_search 2026-05-01 01:53

C言語におけるインクリメントおよびデクリメント演算子

原題: Increment and Decrement Operators in C - GeeksforGeeks

元記事を開く →

分析結果

カテゴリ
AI
重要度
54
トレンドスコア
18
要約
C言語では、インクリメント(++)およびデクリメント(--)演算子を使用して変数の値を1増加または1減少させることができます。これらの演算子は、前置形式(++i, --i)と後置形式(i++, i--)があり、使用する位置によって結果が異なります。前置形式は変数の値を変更した後にその値を返し、後置形式は変更前の値を返します。これにより、プログラムの制御フローや計算において柔軟性が増します。
キーワード
Increment and Decrement Operators in C - GeeksforGeeks Courses Tutorials Interview Prep C Tutorial Interview Questions Examples Quizzes Projects Cheatsheet File Handling Multithreading Memory Layout DSA in C C++ Increment and Decrement Operators in C Last Updated : 21 May, 2025 The increment ( ++ ) and decrement ( -- ) operators in C are unary operators for incrementing and decrementing the numeric values by 1, respectively . They are one of the most frequently used operators in programming for looping, array traversal, pointer arithmetic, and many more. Increment Operator in C The increment operator ( ++ ) is used to increment the value of a variable in an expression by 1. It can be used on variables of the numeric type, such as integer, float, character, pointers, etc. The Increment Operator in C can be used in two ways, either as a prefix (pre-increment) or a postfix (post-increment). Syntax: C // AS PREFIX ++ m // AS POSTFIX m ++ where m is variable. Both pre-increment and post-increment increase the value of the variable but there is a little difference in how they work. 1. Pre-Increment In pre-increment, the increment operator is used as the prefix. Also known as prefix increment, the value is incremented first according to the precedence and then the less priority operations are done. C result = ++ var1 ; The above expression can be expanded as C var = var + 1 ; result = var ; 2. Post-Increment In post-increment, the increment operator is used as the suffix of the operand. The increment operation is performed after all the other operations are done. It is also known as postfix increment. C result = var1 ++ ; The above expression is equivalent C result = var ; var = var + 1 ; Example of Increment Operator C #include <stdio.h> void preincrement ( int x ) { // PREFIX printf ( "X = %d \n " , x ); int prefix = ++ x ; printf ( "Prefix Increment: %d \n " , prefix ); } void postincrement ( int x ) { // POSTFIX printf ( "X = %d \n " , x ); int postfix = x ++ ; printf ( "Postfix Increment: %d" , postfix ); } int main () { int x = 5 ; preincrement ( x ); postincrement ( x ); return 0 ; } Output X = 5 Prefix Increment: 6 X = 5 Postfix Increment: 5 As we can see in postfix, the value is incremented after the assignment operator is done. Note: The post-increment has higher precedence that pre-increment as it is postfix operator while pre-increment comes in unary operator category. Decrement Operator in C The decrement operator is used to decrement the value of a variable in an expression. In the Pre-Decrement, the value is first decremented and then used inside the expression. Whereas in the Post-Decrement, the value is first used inside the expression and then decremented. Syntax Just like the increment operator, the decrement operator can also be used in two ways: C // AS PREFIX -- m // AS POSTFIX m -- where m is variable. 1. Pre-Decrement Operator The pre-decrement operator decreases the value of the variable immediately when encountered. It is also known as prefix decrement as the decrement operator is used as the prefix of the operand. C result = -- m ; which can be expanded to C m = m - 1 ; result = m ; 2. Post-Decrement Operator The post-decrement happens when the decrement operator is used as the suffix of the variable. In this case, the decrement operation is performed after all the other operators are evaluated. C result = m -- ; The above expression can be expanded as C result = m ; m = m -1 ; Example of Decrement Operator C #include <stdio.h> void predecrement ( int x ) { // PREFIX printf ( "X = %d \n " , x ); int prefix = -- x ; printf ( "Prefix Decrement: %d \n " , prefix ); } void postdecrement ( int x ) { // POSTFIX printf ( "X = %d \n " , x ); int postfix = x ++ ; printf ( "Postfix Decrement: %d" , postfix ); } // Driver code int main () { int x = 5 ; predecrement ( x ); postdecrement ( x ); return 0 ; } Output X = 5 Prefix Decrement: 4 X = 5 Postfix Decrement: 5 Increment vs Decrement The following table lists the major differences between increment and decrement operators in C : Increment Operator Decrement Operator Increment Operator adds 1 to the operand. Decrement Operator subtracts 1 from the operand. The Postfix increment operator means the expression is evaluated first using the original value of the variable and then the variable is incremented(increased). The Postfix decrement operator means the expression is evaluated first using the original value of the variable and then the variable is decremented(decreased). Prefix increment operator means the variable is incremented first and then the expression is evaluated using the new value of the variable. Prefix decrement operator means the variable is decremented first and then the expression is evaluated using the new value of the variable. Generally, we use this in decision-making and looping. This is also used in decision-making and looping. Comment Article Tags: Article Tags: Technical Scripter Difference Between Programming Language C Language Technical Scripter 2019 + 1 More Explore Basics Introduction 4 min read Identifiers 3 min read Keywords 1 min read Variables 4 min read Data Types 4 min read Operators 8 min read Conditional Statements 5 min read Loops 6 min read Functions 4 min read Arrays & Strings Arrays 4 min read Strings 5 min read Pointers and Structures Pointers 7 min read Function Pointer 5 min read Unions 3 min read Enumeration 4 min read Structure Alignment, Padding & Data Packing 8 min read Memory Management Memory Layout 5 min read Dynamic Memory Allocation 7 min read Memory Leak 2 min read File & Error Handling File Handling 11 min read Read/Write Structures 3 min read Error Handling 7 min read Using goto for Exception Handling in C 2 min read File Error Handling 4 min read Advanced Concepts Variadic Functions 4 min read Signals in C 5 min read Socket Programming 8 min read _Generics Keyword 3 min read Multithreading 7 min read Courses DSA Self 2 min read

類似記事(ベクトル近傍)