Professional Documents
Culture Documents
Lecture 5
Docsity.com
C Program Basics
C vs. C++
C is a subset of C++. All of features in C are
contained in C++
C++ adds more libraries with functions for object
oriented programming
C++ also adds more keywords and some added
features.
Docsity.com
double
else
enum
extern
float
for
friend
goto
if
inline
int
long
new
operator
private
protected
public
register
return
short
signed
sizeof
static
struct
switch
template
this
throw
try
typedef
union
unsigned
virtual
void
volatile
while
Docsity.com
Program Structure in C
EACH complete C program is composed of:
Comment statements
Pre-processor directives
Declaration statements
One or more functions
Executable statements
Docsity.com
Program Structure in C
EACH complete C program is composed of:
Comment statements
Pre-processor directives
Comment statements
Declaration statements
Comment statements
One or more functions
Comment statements
Executable statements
Comment statements
Docsity.com
Comment Statements
Formal Comments:
/* Comment .. */
Pre-Processor Directives
#include -- header files for library functions
Example:
#include <stdio.h>
Note Space
Declarations
Declarations tell the compiler what variable
names will be used and what type of data each
can handle (store).
Example declarations:
int a, b, c ;
float r, p, q ;
double x, y, z ;
char m, n ;
Docsity.com
Data Types
Integer variables:
int a, b ;
float c, d ;
Docsity.com
Executable Statements
Simple
Declaring variables
int temp ;
char a ;
Assigning Values
temp = 5 ; temp is assigned the value of 5
Arithmetic Operators
*
/
%
multiply
+
add
divide
subtract
remainder, where:
x = 13 % 5 ; /* x will be equal to 3 */
=
=
=
=
=
2 * 3 - 9 + 8 % 7;
6 - 9 + 8 % 7;
6 - 9 + 1;
-3 + 1;
-2;
Docsity.com
Docsity.com
Assignment Operators
Operator:
=
+=
=
/=
*=
%=
Example:
Meaning:
x=5;
x += 5 ;
x = 5 ;
x /= 5 ;
x *= 5 ;
x %= 5;
x=5;
x=x+5;
x=x5;
x=x/5;
x=x*5;
x= x%5;
Docsity.com
Assignment Operators
Example of assignment operators:
int a = 4, b = 2, c = 36 ;
a += b ;
/* This adds b to a, a = ?
c /= a + b ;
now? */
*/
/* What is value of c
Docsity.com
Assignment Operators
Example of assignment operators:
int a = 4, b = 2, c = 36 ;
a += b ;
/* This adds b to a, a = ? */
[ Answer: a = a + b, so a = 4 + 2 or a = 6 ]
c /= a + b ;
/* What is value of c now? */
Docsity.com
Assignment Operators
Example of assignment operators:
int a = 4, b = 2, c = 36 ;
a += b ;
/* This adds b to a, a = ? */
[ Answer: a = a + b, so a = 4 + 2 or a = 6 ]
c /= a + b ;
/* What is value of c now?
*/
[ Answer: c = c / (a + b), and a = 6 now, Docsity.com
Increment/Decrement Operators
Operator:
count++ ;
++count ;
count-- ;
--count ;
Meaning:
count = count + 1 ;
count = count + 1 ;
count = count - 1 ;
count = count - 1 ;
When?
After use
Before use
After use
Before use
Docsity.com
Increment/Decrement Operators
Examples of increment and decrement
operators:
int a = 4, b = 2, c;
c = ++a + b-- ;
/* What are the values of a, b, c now?
*/
c = b-- - ++a ;
/* What are the values of a, b, c now?
*/
Docsity.com
Increment/Decrement Operators
Examples of increment and decrement
operators:
int a = 4, b = 2, c;
c = ++a + b-- ;
/* What are the values of a, b, c now? */
(Answers: a = 5, b = 1, c = 7)
c = b-- - ++a ;
/* What are the values of a, b, c now? */
Docsity.com
Increment/Decrement Operators
Examples of increment and decrement
operators:
int a = 4, b = 2, c;
c = ++a + b-- ;
/* What are the values of a, b, c now? */
(Answers: a = 5, b = 1, c = 7)
c = b-- - ++a ;
/* What are the values of a, b, c now? */
(Answers: a = 6, b = 0, c = -5)
Docsity.com
Relational Operators
Operator:
<
>
<=
>=
==
!=
Meaning:
Less Than
Greater Than
Less Than or Equal To
Greater Than or Equal To
Exactly Equal To
Not Equal To
Docsity.com
Relational Operators
Used for asking questions like:
Is x bigger than 10?
In C, the value of 1 stands for true and 0
stands for false. But C will recognize any non
zero value as true.
NOTE:
Logical Operators
! (not)
Ex: a != b is true if a and b are not equal
&& (and)
Ex: 5<6 && 7>4 is true, but
5>6 && 7>4 is not true (i.e., false)
|| (or)
Ex: 5>6 || 7>4 is true
5<6 || 7<4 is also true
Docsity.com
Exponentiation Operations
Exponentiation is not written as x**2 or x^2
C does not have an exponentiation operator.
You can use the math function pow (a, b)
which raises a to the b power. You must put a
#include <math.h> in your source code and
must also use the -lm switch in your compile
command when on the SGI UNIX system.
Ex:
Skeleton Program
/************************************
*****/
/* Name: Brutus Buckeye
*/
/* Seat No. 0, Instr: W. Hayes
*/
/* Program progname/assignment */
/************************************
*****/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
Docsity.com