Global Trend Radar
Web: www.geeksforgeeks.org US web_search 2026-05-06 00:27

C言語の演算子 - GeeksforGeeks

原題: Operators in C - GeeksforGeeks

元記事を開く →

分析結果

カテゴリ
AI
重要度
54
トレンドスコア
18
要約
C言語における演算子は、データの操作や計算を行うための重要な要素です。演算子には、算術演算子、比較演算子、論理演算子、ビット演算子などがあり、それぞれ異なる機能を持っています。これらの演算子を理解することで、プログラムの効率的な記述が可能になります。演算子の使い方や例について詳しく解説しています。
キーワード
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++ Operators in C Last Updated : 22 Apr, 2026 Operators are the basic components of C programming. They are symbols that represent some kind of operation, such as mathematical, relational, bitwise, conditional, or logical computations, which are to be performed on values or variables. The values and variables used with operators are called operands . C #include <stdio.h> int main () { // Expression for getting sum int sum = 10 + 20 ; printf ( "%d" , sum ); return 0 ; } Output 30 Unary, Binary and Ternary Operators On the basis of the number of operands they work on, operators can be classified into three types : Unary Operators: Operators that work on single operand. Example: Increment( ++ ) , Decrement( -- ) Binary Operators: Operators that work on two operands. Example: Addition ( + ), Subtraction( - ) , Multiplication ( * ) Ternary Operators: Operators that work on three operands. Example : Conditional Operator( ? : ) Types of Operators in C C language provides a wide range of built in operators that can be classified into 6 types based on their functionality: Arithmetic Operators The arithmetic operators are used to perform arithmetic/mathematical operations on operands. In C, these include : C #include <stdio.h> int main () { int a = 25 , b = 5 ; // using operators and printing results printf ( "a + b = %d \n " , a + b ); printf ( "a - b = %d \n " , a - b ); printf ( "a * b = %d \n " , a * b ); printf ( "a / b = %d \n " , a / b ); printf ( "a %% b = %d \n " , a % b ); printf ( "+a = %d \n " , + a ); printf ( "-a = %d \n " , - a ); printf ( "a++ = %d \n " , a ++ ); printf ( "a-- = %d \n " , a -- ); return 0 ; } Output a + b = 30 a - b = 20 a * b = 125 a / b = 5 a % b = 0 +a = 25 -a = -25 a++ = 25 a-- = 26 Relational Operators The relational operators in C are used for the comparison of the two operands. All these operators are binary operators that return true or false values as the result of comparison. C #include <stdio.h> int main () { int a = 25 , b = 5 ; // using operators and printing results printf ( "a < b : %d \n " , a < b ); printf ( "a > b : %d \n " , a > b ); printf ( "a <= b: %d \n " , a <= b ); printf ( "a >= b: %d \n " , a >= b ); printf ( "a == b: %d \n " , a == b ); printf ( "a != b : %d \n " , a != b ); return 0 ; } Output a < b : 0 a > b : 1 a <= b: 0 a >= b: 1 a == b: 0 a != b : 1 Here, 0 means false and 1 means true. Logical Operator Logical Operators are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. The result of the operation of a logical operator is a Boolean value either true or false . Symbol Operator Description Syntax && Logical AND Returns true if both the operands are true. a && b || Logical OR Returns true if both or any of the operand is true. a || b ! Logical NOT Returns true if the operand is false. !a C #include <stdio.h> int main () { int a = 25 , b = 5 ; // using operators and printing results printf ( "a && b : %d \n " , a && b ); printf ( "a || b : %d \n " , a || b ); printf ( "!a: %d \n " , ! a ); return 0 ; } Output a && b : 1 a || b : 1 !a: 0 Bitwise Operators The Bitwise operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands. Note: Mathematical operations such as addition, subtraction, multiplication, etc. can be performed at the bit level for faster processing. Symbol Operator Description Syntax & Bitwise AND Performs bit-by-bit AND operation and returns the result. a & b | Bitwise OR Performs bit-by-bit OR operation and returns the result. a | b ^ Bitwise XOR Performs bit-by-bit XOR operation and returns the result. a ^ b ~ Bitwise First Complement Flips all the set and unset bits on the number. ~a << Bitwise Leftshift Shifts bits to the left by a given number of positions; multiplies the number by 2 for each shift. a << b >> Bitwise Rightshift Shifts bits to the right by a given number of positions; divides the number by 2 for each shift. a >> b C #include <stdio.h> int main () { int a = 25 , b = 5 ; // using operators and printing results printf ( "a & b: %d \n " , a & b ); printf ( "a | b: %d \n " , a | b ); printf ( "a ^ b: %d \n " , a ^ b ); printf ( "~a: %d \n " , ~ a ); printf ( "a >> b: %d \n " , a >> b ); printf ( "a << b: %d \n " , a << b ); return 0 ; } Output a & b: 1 a | b: 29 a ^ b: 28 ~a: -26 a >> b: 0 a << b: 800 Assignment Operators Assignment operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise an error. The assignment operators can be combined with some other operators in C to provide multiple operations using single operator. These operators are called compound operators. Symbol Operator Description Syntax = Simple Assignment Assign the value of the right operand to the left operand. a = b += Plus and assign Add the right operand and left operand and assign this value to the left operand. a += b -= Minus and assign Subtract the right operand and left operand and assign this value to the left operand. a -= b *= Multiply and assign Multiply the right operand and left operand and assign this value to the left operand. a *= b /= Divide and assign Divide the left operand with the right operand and assign this value to the left operand. a /= b %= Modulus and assign Assign the remainder in the division of left operand with the right operand to the left operand. a %= b &= AND and assign Performs bitwise AND and assigns this value to the left operand. a &= b |= OR and assign Performs bitwise OR and assigns this value to the left operand. a |= b ^= XOR and assign Performs bitwise XOR and assigns this value to the left operand. a ^= b >>= Rightshift and assign Performs bitwise Rightshift and assign this value to the left operand. a >>= b <<= Leftshift and assign Performs bitwise Leftshift and assign this value to the left operand. a <<= b C #include <stdio.h> int main () { int a = 25 , b = 5 ; // using operators and printing results printf ( "a = b: %d \n " , a = b ); printf ( "a += b: %d \n " , a += b ); printf ( "a -= b: %d \n " , a -= b ); printf ( "a *= b: %d \n " , a *= b ); printf ( "a /= b: %d \n " , a /= b ); printf ( "a %%= b: %d \n " , a %= b ); printf ( "a &= b: %d \n " , a &= b ); printf ( "a |= b: %d \n " , a |= b ); printf ( "a ^= b: %d \n " , a ^= b ); printf ( "a >>= b: %d \n " , a >>= b ); printf ( "a <<= b: %d \n " , a <<= b ); return 0 ; } Output a = b: 5 a += b: 10 a -= b: 5 a *= b: 25 a /= b: 5 a %= b: 0 a &= b: 0 a |= b: 5 a ^= b: 0 a >>= b: 0 a <<= b: 0 Other Operators Apart from the above operators, there are some other operators available in C used to perform some specific tasks. Some of them are discussed here: sizeof Operator sizeof is much used in the C programming language. It is a compile-time unary operator which can be used to compute the size of its operand. The result of sizeof is of the unsigned integral type which is usually denoted by size_t. Basically, the sizeof the operator is used to compute the size of the variable or datatype. Syntax C sizeof ( operand ) Comma Operator ( , ) The comma operator (represented by the token) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type). The comma operator has the lowest precedence of any C operator. It can act as both operator and separator. Syntax C operand1 , operand2 Conditional Operator ( ? : ) The conditional operator is the only ternary operator in C. It is a conditional operator that we can use in place of if..else statements. Syntax C expression1 ? Expression2 : Expression3 ; Here, Expression1 is the condition to be evaluated. If the condition( Expression1 ) is True then we will execute and return the result of Expression2 otherwise if the condition( Expression1 ) is false then we will execute and return the result of Expression3 . dot (.) and arrow (->) Operators Member operators are used to reference individual members of classes, structures, and unions. The dot operator is applied to the actual object. The arrow operator is used with a pointer to an object. Syntax C structure_variable . member ; structure_pointer -> member ; Cast Operators Casting operators convert one data type to another. For example, (int)2.2000 would return 2. A cast is a special operator that forces one data type to be converted into another. Syntax C ( new_type ) operand ; addressof (&) and Dereference (*) Operators Addressof operator & returns the address of a variable and the dereference operator * is used to access the value stored at that address. Example of Other C Operators C // C Program to demonstrate the use of Misc operators #include <stdio.h> int main () { // integer variable int num = 10 ; int * add_of_num = & num ; printf ( "sizeof(num) = %d bytes \n " , sizeof ( num )); printf ( "&num = %p \n " , & num ); printf ( "*add_of_num = %d \n " , * add_of_num ); printf ( "(10 < 5) ? 10 : 20 = %d \n " , ( 10 < 5 ) ? 10 : 20 ); printf ( "(float)num = %f \n " , ( float ) num ); return 0 ; } Output sizeof(num) = 4 bytes &num = 0x7ffdb58c037c *add_of_num = 10 (10 < 5) ? 10 : 20 = 20 (float)num = 10.000000 It is strongly recommended to learn Operator Precedence and Associativity after reading about operators. Comment Article Tags: Article Tags: C Language C Basics C-Operators 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 Functio

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