Fifth Class
Fifth Class
Fifth Class
expressions
Oyebola Akande (Ph.D)
Computer Science Department
Babcock University
Libraries in C standard library
< stdio.h> -> defining I/O routines
< ctype.h> -> defining character manipulation routines
< string.h> -> defining string manipulation routines
< math.h> -> defining mathematical routines
< stdlib.h> -> defining number conversion, storage allocation and similar tasks
< stdarg.h> -> defining libraries to handle routines with variable numbers of
arguments
< time.h> -> defining time-manipulation routines
< assert.h> -> defining diagnostic routines
< setjmp.h> -> defining non-local function calls
< signal.h> -> defining signal handlers
< limits.h> -> defining constants of the int type
< float.h> -> defining constants of the float type
C OPERATORS
Arithmetic operators
C provides five (5) arithmetic operators
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)
The arithmetic operators are binary operators because
they operate on two operands.
C OPERATORS contd
C provides the remainder operator, %, which
yields the remainder after integer division.
The remainder operator is an integer operator that
can be used only with integer operands.
The expression x % y yields the remainder after x
is divided by y.
Thus, 7 % 4 yields 3 and 17 % 5 yields 2.
C OPERATORS contd
Relational operators
C provides six relational operators:
Operator Explanation
== (Equal to – equality)
!= (Not equal to – inequality)
> (Greater than)
< (Less than)
>= (Greater than or equal to)
<= (Less than or equal to)
C OPERATORS contd
Assignment operators
=
The syntax is:
Operand = operand
Examples
int gross_pay = 0;
int tax = 0;
int net_pay = 0;
gross_pay = 12000;
Post-increment (var++)
Decrement operator
--
Pre-decrement (-- var)
Post-decrement (var--)
^ Bitwise Left-to-right
exclusive OR
| Bitwise OR Left-to-right
&& Logical AND Left-to-right
|| Logical OR Left-to-right
?: Conditional Right-to-left
operator
= Assignment Right-to-left
+= -= Addition
assignment and
subtraction
assignment
/= *= Division
assignment and
C operators, arranged in order of precedence
isodd=x%2==1?1:0;
if ( x%2==1)
isodd=1
else
isodd=0
Precedence and Order of Evaluation
• ++,–,(cast), sizeof have the highest priority•
*,/,% have higher priority than +,-
==,!= have higher priority than &&,||
assignment operators have very low priority
Use () generously to avoid ambiguities or side effects associated with
precendence of operators.
y=x∗3+2 /∗same as y=(x∗3)+2∗/
x!=0 && y==0 /∗same as (x!=0) && (y==0)∗/
d= c>=’0’&& c<=’9’/∗same as d=(c>=’0’) && (c<=’9’) ∗/
Functions in string library (string.h)
char *strcpy(s,ct) -> copy ct into s, including ``\0''; return s
char *strncpy(s,ct,n) -> copy ncharcater of ct into s, return s
char *strncat(s,ct) -> concatenate ct to end of s; return s
char *strncat(s,ct,n) -> concatenate n character of ct to end of s, terminate with
``\0''; return s
int strcmp(cs,ct) -> compare cs and ct; return 0 if cs=ct, <0 if cs0 if cs>ct
char *strchr(cs,c) -> return pointer to first occurence of c in cs or NULL if not
encountered
size_t strlen(cs) -> return length of cs
(s and t are char*, cs and ct are const char*, c is an char converted to type int,
and n is an int.)
Using Constant
Character constant
A character constant is a single character that is written
in single quotes.
Example: