CS1101 Computational Engineering: Introduction To C Programming Language
CS1101 Computational Engineering: Introduction To C Programming Language
CS1101 Computational Engineering: Introduction To C Programming Language
Computational Engineering
Introduction to C Programming
Language
Course Material – SD, SB, PSK, NSN, DK, TAG – CS&E, IIT M 1
The C Programming Language
• An imperative general-purpose programming
language
• Used extensively in the development of UNIX
• Extremely effective and expressive
• Not a “very high level” nor a “big” language
• Has compact syntax, modern control flow and
data structures and a rich a set of operators
• Extensive collections of library functions
}
Statement terminator
a + ((( b c ) d ) % e ) – (f / g )
good practice – use parentheses rather than rely on
precedence rules – better readability
SD, PSK, NSN, DK, TAG – CS&E, IIT M 18
Precedence – Another Example
• Value = a * (b + c) % 5 + x / (3 + p) – r – j
• Evaluation order –
– (b + c) and (3 + p) : due to brackets
– * and % and / have same precedence: a*(b + c) is
evaluated first, then mod 5. Also, x/(3 + p).
– Finally, the additions and subtractions are done from
the left to right.
• Finally, the assignment of the RHS to LHS is
done.
– = is the operator that violates the left to right rule
SD, PSK, NSN, DK, TAG – CS&E, IIT M 19
Relational and Logical Operators
• A logical variable can have two values {true, false} or {1,
0}
• In C: int flag // 0 is false, any non-zero value is true
• Operators:
! unary logical negation operator
< , <= , > , >= comparison operators
= = , != equality and inequality
&& logical AND operator
|| logical OR operator
• logical operators return true/false
• order of evaluation -- as given above
SD, PSK, NSN, DK, TAG – CS&E, IIT M 20
Increment and Decrement Operators
• Unusual operators - prefix or postfix only to
variables
++ adds 1 to its operand
–– subtracts 1 from its operand
• n++ increments n after its use
• ++n increments n before its use
• n = 4; x = n++; y = ++n;
• x is 4, y is 6 and n is 6 after the execution
main() {
printf ("%d %d %d %d\n", 5%2, (-5)%2, 5%(-2), (-5)%(-2)); //1 -1 1 -1
x = y5/(-2),
printf ("%d %d %d %d\n", 5/2, (-5)/2, = z = a(-5)/(-2));
+ b; // 2 -2 -2 2
} x = (y = (z = a + b))