Mythili Vutukuru
IIT Bombay
Reference: “C How to Program”, Deitel and Deitel, 8th Edition, Chapter 2 (and Appendix C)
Declare variables to store user data
Variable declaration: data type and name
Read integer from standard input
%d specifies format of integer
Define variable sum, assign value
computed using addition operator
Variables are user data stored in program memory
Variable has a data type (say, integer) and name (say, sum)
Users can set values to variables directly in the program, or
get from user via standard input
Users can print values of variables on standard output
CPU instructions read and write values of variables, and
perform operations (e.g., addition) on variables
The statement “sum = integer1 + integer2” is translated
by compiler into multiple CPU instructions
Read “integer1” and “integer2” from memory
Add them up
Write resulting value into memory location of variable “sum”
Every data type in C has a size and range of
values it can represent
Usually depends on underlying machine, but common
case discussed below
For example, integer data type is 4 bytes = 32 bits
Can represent 2^32 values, from -2^31 to 2^31-1, that is,
[-2147483648 , 2147483647]
Permitted ranges of data types defined in
<limits.h>
Data types with more bits (e.g., long int) store a bigger
range of values, and less bits (e.g., short int) store a smaller
range
Unsigned integer type (unsigned int) stores from 0 to 2^32-
1, that is, [0, 4294967295], used when negative values not
needed
What happens if a value larger than the permitted range is
stored in a variable? OVERFLOW BUG!
Example: if one is added to an integer storing the maximum value,
it will lead to wrong results
Can perform several arithmetic operations on variables
Note: integer division: if result of division stored in integer, only
integer part of the answer is retained, the remainder can be obtained
via mod operation
Arithmetic operations applied according to precedence rules
Order of evaluation shown
Concise way of doing arithmetic operation and assigning to variable
These two statements are equivalent:
Concise way to increment/decrement a counter by 1 “c=c+1” same as “c++”
Increment/decrement can be done after using the variable in an expression
(post-increment) or before (pre-increment)
Used to compare the value of variables and take decisions in program
Used to make decisions in program based on whether a condition is
true or not
Declare variables, input from user
If the numbers are equal, print to screen
Check various conditions
Print suitably to screen
Equality (==) or relational operators (<, >, ..) allow for testing
one condition only
Logical operators allow testing multiple conditions at the
same time
Logical AND (&&) is true only if all conditions are true
Logical OR (||) is true if any one of the conditions is true
Logical NOT (!) negates the original condition (achievable using other ways)
C evaluates all equality, relational, logical expressions to 0 or 1
0 indicates false, 1 or any non zero value indicates true
C also has bool data type, takes true or false values (more on this later)
Truth tables shows how an expression evaluates for every
combination of inputs
Precedence of operators seen so far: highest to lowest
Parentheses
Post increment/decrement, pre increment/decrement
Multiplication, division, modulus
Addition, subtraction
Relational / equality operators (comparisons, equal to, not equal to)
Logical AND
Logical OR
Assignment and assignment+arithmetic operators
Syntax error: mistake in syntax or format of code
Example: spelling mistake in variable name or keyword
Example: Missing braces or semicolons
Compiler prints error during compilation, executable not produced
Logic error: program compiles and runs, but wrong result
Example: Using the wrong relational operator (“<“ instead of “>”)
Example: Using assignment (“=“) instead of equal to (“==“)
if(num1 == num2) checks if num1 is equal to num2
if(num1 = num2) is equivalent to num1 = num2; if(num1)
if(num1 = num2) will be true whenever assigned value of num1 > 0
Fatal vs. non-fatal errors: some logic errors crash the
program (fatal), while others let program continue with
wrong results
How to debug your program
Read code carefully, do dry run execution
Use print statements to trace execution
Use debuggers to step through code (advanced, not for now)
How are integers (positive and negative) represented in a
computer?
How are sign and magnitude encoded in 32 bits?
Most significant bit (MSB) indicates sign (0: positive, 1: negative)
For positive integers: 2^31 – 1 numbers represented within 31
bits, MSB is 0
What about negative integers? Two’s complement method
Take complement of number (one’s complement), add one to it
For example, consider a positive integer, say, int value = 13
Why is this a good representation? Adding the positive and negative
values will result in 0 as expected (ignore carry)