본문 바로가기

Study Meeting/빡공단 30기

[Day 8] Operator 1: Assignment, Relational, Arithmetic, Incremental Operators in C Language

In this class, I learned about the operators that enable calculate or compare the variables.
Here's four types of operators that easily classified to explan their functions.

1. Assignment operator
    - 'A = B' means that value 'B' should be assigned to variable 'A'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int a;
    a = 123;
    printf('%d', a);
    
    return 0;
}

 

 

2. Relational operator
    - Comparing the relationship between left and the right and results showed whether the operator is true or false.
    - Examples: 'A > B', 'A >= B', 'A! = B'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int a;
    int b;
    
    a = 123;
    b = 456;
    
    printf('%d \n', a > b);
    printf('%d \n', a >= b);
    printf('%d \n', a == b);
    printf('%d \n', a != b);
    
    return 0;
}

 

3. Arithmetic operator
    - Examples: 'A + B', 'A - B', 'A * B', 'A / B', 'A % B'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int a;
    
    a = 123 + 1;
    printf('%d \n', a);
    
    a = 123 - 2;
    printf('%d \n', a);
    
    a = 123 * 3;
    printf('%d \n', a);
    
    a = 123 / 10;
    printf('%d \n', a);
    
    a = 123 % 7;
    printf('%d \n', a);
    
    return 0;
}

 

4. Incremental operator
    - Examples: 'A ++' means 'A = A + 1 and 'A --' means 'A = A - 1'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int a;
    
    a = 1;
    printf('%d \n', a);
    
    a = a + 1;
    printf('%d \n', a);
    
    a++;
    pritnf('%d \n', a);
    
    a--;
    printf('%d \n', a);
    
    return 0;
}

 

5. Compound substitution operator
    - Examples: 'A += B' means 'A = A + B' and 'A -= B' means 'A = A - B'

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    
    int a;
    
    a = 1;
    printf('%d \n', a);
    
    a = a + 2;
    printf('%d \n', a);
    
    a += 2;
    printf('%d \n', a);
    
    return 0;
}