Week 4 Exam Notes Annotated

Introduction to Operators

Operators

  • memory: and how we can declare characters char: and variables: var = ‘A’


int letterA = 0; ←————- input(done), process, and the output

int letterB = 2;

int result = 0;

^^^ ||

verbs: “the stuff”. word problem ex: “take two ||

numbers and find the average” or when I need ||

to increment or decrement something then we ||

are operating on it. So they all become operands ||

in some form of expressions

^

||

||

In this process there are something we still need to consider which may not conform to the rules if C the language as they do typically in math. Like if its a left or right operator? What do they different operators do?


if you want to know the sum of A + B;

(A and B and now both operands and the ‘+’ is now a binary operator)


Uniairy operation ex: B++;

(which basically means it operates on one operand in the case B)

so for example if our operand aka B’s value being 2 after it executes B’s value would be 3


Assignment operator is = NOT A COMPARISON always going to be on the left bc its a right to left operator


result = A + B; B++;



= (A + B)+(A * B);

The precedent of which is solved first

PEMDAS


a semi colon by itself is called an empty/ NULL statement ;

and theres sometime when we actually need to do that to tell the computer to not do anything in this block of code. and that we will get back to it later


so actually the + operator has a higher assignment than the assignment operator =

Comparison operator ==



Arithmetic to Operators

Order of operation Precedent edition

obvi the typical PEMDAS

1.) ()

2.) unary - so like 2* -x the -x is computed first then multiplied by 2

this is used for negation (unary minus) is

3.) *, /, %

has equal precedence

4.) +-

has equal precedence

5.) left to right

if more than one operation of equal precedence appears then its evaluated left to right


Implicit promotion and demotion
Explicit demotion
#include <stdio.h>
int main() {

    int nbr1 = 10;
    float nbr2 = 2.2;
    double nbr3 = 3.21;
// Implicit promotion and demotion
    int res1 = ((nbr1 + nbr2) + nbr3); 

    //explicit demotion would look like ((nbr1 + (int)nbr2) + (int)nbr3);
    //and so basically here they are still stored as floast and doubles in memory
    //but like your result would print an integer if that is your deired end goal

    // so like i would think simple math 10 plus 2.2 but no the computer cannot simply add a
    // int and  float so the computer changes it and this right here is call promotion
    // so here its going to promote nbr1 (aka 10) into a float s
    // so the answer of this (nbr1 + nbr2) sub expression will be a float
    // then after this we are going to have a float + a double and so the result of that
    // answer will also ve promted to a DOUBLE 

    // BUT WAIT... in our top equation res1 is set to print out as an integer

    printf("%d\n", res1);

    // so here it going toe get DEMOTED (demotion) to a interger
    // so all in all that is basically going to result in truncation
    return 0;
}


PASS LAB

just things i need to review for myself


int %d

float %

double %

char %

string %

long double %

long long %

const - modifer