Lecture 2
Review
    Variables and data types
    Operators
     Epilogue
                               1
Review: Basics
    • Variable declarations: int i ; float f ;
    • Intialization: char c=’A’; int x=y=10;
    • Operators: +,−,∗,/,%
    • Expressions: int x,y,z; x=y∗2+z∗3;
    • Function: int factorial ( int n); /∗function takes int , returns int
      ∗/
                                                                             2
Lecture 2
    Review
    Variables and data types
    Operators
    Epilogue
                               3
Definitions
  Datatypes:
    • The datatype of an object in memory determines the set
       of values it can have and what operations that can be
      performed on it.
    • C is a weakly typed language. It allows implicit conversions
       as well as forced casting.
  Operators:
   • Operators specify how an object (value/string) can
     be manipulated (e.g.,, numeric vs. string operations).
    • operators can be unary(e.g., -,++),binary (e.g.,
      +,-,*,/),ternary (?:)
                                                                     3
Ternary conditional syntax
condition ? expression_if_true : expression_if_false;
Definitions (contd.)
  Expressions:
    • An expression in a programming language is a
      combination of values, variables, operators, and functions
  Variables:
    • A variable is as named link/reference to a value stored in
       the system’s memory or an expression that can be
      evaluated.
  Consider: int x,y; y=x+2;.
    • x, y are variables
    • y = x + 2 is an expression
    •   + is an operator.
                                                                   4
Variable names
Naming rules:
  • Variable names can contain letters,digits and _
  • Variable names should start with letters.
  • Keywords (e.g., for,while etc.) cannot be used as variable names
  (Keywords in C programming are reserved words with predefined meanings
  that cannot be used as identifiers, such as variable or function names, as
  they serve specific purposes in the language syntax.)
  • Variable names are case sensitive. int x; int X declares two different
      variables.
Pop quiz (correct/incorrect):
  •   int money$owed; (incorrect: cannot contain $)
  •   int total_count (correct)
  •   int score2 (correct)
  •   int 2ndscore (incorrect: must start with a letter)
  •   int long (incorrect: cannot use keyword)
                                                                         5
Data types and sizes
  C has a small family of datatypes.
    • Numeric (int,float,double)
    • Character (char)
    • User defined (struct,union)
                                       6
Numeric data types
                     7
Numeric data types
                     7
Numeric data types
    sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long)
                                                                         7
Constants
  Constants are literal/fixed values assigned to variables or used
  directly in expressions.
     Datatype                   example                 meaning
                                  int i=3;               integer
                                 long l=3;            long integer
      integer
                                int i=0xA;            hexadecimal
                                int i=012;            octal number
                             float pi=3.14159               float
   floating point             float pi=3.141F                float
                   double pi=3.1415926535897932384L      double
                                                                     10
Constants (contd.)
    Datatype         example           meaning
                       ’A’             character
    character        ’\x41’         specified in hex
                     ’\0101’        specified in octal
                 "hello world"        string literal
      string
                 "hello""world"   same as "hello world"
                                                        11
Declarations
  The general format for a declaration is
  type variable-name [=value]                   .
  Examples:
    • char x; /∗ uninitialized ∗/
    • char x=’A’; /∗ intialized to ’ A’∗/
    • char x=’A’,y=’B’; /∗multiple variables
      initialized ∗/
    • char x=y=’Z’;/∗multiple initializations
      ∗/
                                                    12
Lecture 2
    Review
    Variables and data types
    Operators
    Epilogue
                               14
Arithmetic operators
   operator     meaning               examples
                                 x=3+2; /∗constants∗/
      +         addition          y+z;
                                    /∗variables∗/
                                   x+y+2; /∗both∗/
                                  3−2; /∗constants∗/
      -        subtraction
                                      int x=y−z;
                                     /∗variables∗/
                                int y−2−z;  /∗both∗/
                                    x=3∗2; /∗constants∗/
      *       multiplication    int x=y∗z; /∗variables∗/
                                          x∗y∗2;
                               /∗both∗/
                                                           14
Arithmetic operators (contd.)
   operator    meaning                       examples
                               float x=3/2; /∗produces x=1 (int /) ∗/
      /        division      float x=3.0/2 /∗produces x=1.5 (float /) ∗/
                            int x=3.0/2; /∗produces x=1 (int conversion)∗/
               modulus              int x=3%2; /∗produces x=1∗/
      %                          int y=7;int x=y%4; /∗produces 3∗/
              (remainder)
                                                                int y=7;int
                                      x=y%10; /∗produces 7∗/
                                                                         15
Relational Operators
  Relational operators compare two operands to produce a
  ’boolean’ result. In C any non-zero value (1 by convention) is
   considered to be ’true’ and 0 is considered to be false.
    operator          meaning                examples
                                       3>2; /∗evaluates to 1 ∗/
      >           greater than
                                      2.99>3 /∗evaluates to 0 ∗/
              greater than or         3>=3; /∗evaluates to 1 ∗/
      >=
              equal to                2.99>=3 /∗evaluates to 0 ∗/
                                       3<3; /∗evaluates to 0 ∗/
      <       lesser than
                                      ’A’<’B’/∗evaluates to 1∗/
              lesser than or equal     3<=3; /∗evaluates to 1 ∗/
      <=
              to                      3.99<3 /∗evaluates to 0 ∗/
                                                                    16
Relational Operators
  Testing equality is one of the most commonly used relational
            operator      meaning           examples
                                       3==3; /∗evaluates to 1 ∗/
               ==        equal to
  operator.                           ’A’==’a’/∗evaluates to 0 ∗/
                                        3!=3; /∗evaluates to 0 ∗/
                !=     not equal to
                                       2.99!=3 /∗evaluates to 1 ∗/
  Gotchas:
   • Not
      e
      that
      the
      "=="
      equ                                                            17
      ality
Logical operators
   operator   meaning                    examples
                          ((9/3)==3) && (2∗3==6); /∗evaluates to 1 ∗/
      &&        AND
                           (’A’==’a’) && (3==3) /∗evaluates to 0 ∗/
                            2==3 || ’A’==’A’; /∗evaluates to 1 ∗/
       ||        OR
                               2.99>=3 || 0 /∗evaluates to 0 ∗/
                                 !(3==3); /∗evaluates to 0 ∗/
       !        NOT
                                !(2.99>=3) /∗evaluates to 1 ∗/
  Short circuit: The evaluation of an expression is discontinued if
  the value of a conditional expression can be determined early.
  Be careful of any side effects in the code.
  Examples:
    • (3==3) || (( c=getchar())==’y’). The second expression is not
       evaluated.
    • (0) && ((x=x+1)>0) . The second expression is not
      evaluated.
                                                                        18
Increment and decrement operators
  Increment and decrement are common arithmetic operation. C
   provides two short cuts for the same.
  Postfix
    • x++ is a short cut for x=x+1
    • x−− is a short cut for x=x−1
    • y=x++ is a short cut for y=x;x=x+1. x is evaluated before it is
      incremented.
    • y=x−− is a short cut for y=x;x=x−1. x is evaluated before it
      is decremented.
                                                                        19
Increment and decrement operators
  Prefix:
    • ++x   is a short cut for x=x+1
    • −−x is a short cut for x=x−1
    • y=++x is a short cut for x=x+1;y=x;. x is evaluate after it is
       incremented.
    • y=−−x is a short cut for x=x−1;y=x;. x is evaluate after it is
       decremented.
                                                                       20
Assignment Operators
  Another common expression type found while programming in
  C is of the type var = var (op) expr
    • x=x+1
    • x=x∗10
    • x=x/2
  C provides compact assignment operators that can be used
  instead.
    • x+=1 /∗is the same as x=x+1∗/
    • x−=1 /∗is the same as x=x−1∗/
    • x∗=10 /∗is the same as x=x∗10 ∗/
    • x/=2 /∗ is the same as x=x/2
    • x%=2 /∗is the same as x=x%2
                                                              22
Conditional Expression
  1)
  if (condition) {
     // Execution if the condition is true
  }
  2)
  if (condition) {
     // Execution if the condition is true
  } else {
     // Execution if the condition is false
  }
  3)
  if (condition1) {
     // Execution if condition1 is true
  } else if (condition2) {
     // Execution if condition2 is true
  } else {
     // Execution if neither condition1 nor condition2 are true
  }
                                                                  23
Conditional Expression
  switch (expression) {
    case constant1:
       // code to be executed if expression matches constant1
       break;
    case constant2:
       // code to be executed if expression matches constant2
       break;
    // additional cases
    default:
       // code to be executed if expression doesn't match any constant
  }
  Basing on the value of the evaluated expression, the control jumps to the
  corresponding case label. If none of the case values match the
  expression, the default case (if present) will be executed. Each case
  block should end with a break statement to exit the switch statement.
                                                                              23
Loops --- FOR
     for (initialization; condition; update) {
       // code to be executed
     }
                                                 23
#include <stdio.h>
int main() {
    printf("Even numbers from 1 to 20:\n");
    for (int i = 1; i <= 20; i++) {
 if (i % 2 == 0) {
          printf("%d ", i);
       }
    }
    printf("\n");
    return 0;
}
                                              23
Loops --- WHILE
     while (condition) {
       // code to be executed
     }
                                23
23
Loops --- WHILE - DO
                  do {
                    // code to be executed
                  } while (condition);
   Unlike the while loop, the do-while loop guarantees that the
   code block will be executed at least once, as the condition is
   checked after the execution of the block.
                                                                    23
#include <stdio.h>
int main() {
   int i = 1; // Initialization
  do {
     printf("%d\n", i);
     i++; // Increment
  } while (i <= 5); //
Condition
    return 0;
}
                                  23
   #include <stdio.h>
    int sumDigits(int number) {
      int sum = 0;
      while (number > 0) {
         sum += number % 10;
         number /= 10;
      }
      return sum;
    }
    int main() {
      int number;
      scanf("%d", &number);
      printf("The sum of digits of %d is %d\n", number,
    sumDigits(number));
      return 0;
    }
#include <stdio.h>
int sumDigits(int number);
int main () {
                    int number;
                    scanf("%d", &number);
                    printf("The sum of digits of %d is %d\n", number,
sumDigits(number));
                    return 0;
}
int sumDigits(int number) {
                    int sum = 0;
                    while (number > 0) {
                                       sum += number % 10;
                                       number /= 10;
                    }
                    return sum;
}
"This is only an initialization; we can learn more while
                  programming more."