Вы находитесь на странице: 1из 29

Module 3

allows you to make text


substitutions before compiling
the program.
by convention, all identifiers that
are to be changed by the
preprocessor are written in
capital letters.

#include <stdio.h>
#define MIN 0 /* #defines */
#define MAX 10
#define TRUE 1
#define FALSE 0
int main() { /* beginning of program */
int a;
int okay=FALSE;/*the compiler sees this as int okay=0;*/
while(!okay) {
printf("Input an integer between %d and %d: ", MIN, MAX);
scanf("%d", &a);
if(a>MAX) {
printf("\nToo large.\n"); }
else if(a<MIN) {
printf("\nToo small.\n"); }
else { printf("\nThanks.\n");
okay = TRUE; }
} return 0; }

Libraries C implementations that


contain collections of useful
functions and symbols that may
be accessed by a program.
Note: A C system may expand the number of
operation available by supplying additional
libraries. Each library has a standard
header file whose name ends with the
symbol .h.

Commenting Your Code


You can add comments to your code by enclosing
your remarks within /* and */. However, nested
comments aren't allowed.
A few properties of comments:
They can be used to inform the person viewing
the code what the code does. This is helpful
when you revisit the code at a later date.
The compiler ignores all the comments. Hence,
commenting does not affect the efficiency of
the program.
You can use /* and */ to comment out sections
of code when it comes to finding errors, instead
of deletion.

Here are examples of commented code:


/* Comments spanning several */
/* lines can be commented*/
/* out like this!*/
/* But this is a simpler way of doing it! */
// These are C++
// style comments
// and should NOT
// be used with C!!
/* /* NESTED COMMENTS ARE ILLEGAL!! */ */

Reserved Words
In C, a reserved word is defined as
the word that has special meaning
in C and cannot be used for other
purposes.
Examples: int, void, double, return

/* */ -(slash asterisk) used to enclose a single line


remarks.

-(double quotation) used to display series of


characters, and initializing string constant.
;
-(semicolon) statement separator
,
-(comma) used to separate one variable to
another
=
-(equal sign) used as assignment operator

-(single quotation) used for initializing


character expression
&
-(ampersand) used as address operator
{}
-(open/close braces) denotes the beginning
and end of the program.

Constants identifiers that are having a


constant value all throughout the program
execution.
- also fixed values that may not be altered
by the program.
Examples:
1.
Character constants enclosed between
single quotes. Ex. A, +
2.
Integer constants specified as numbers
without fractional components.
Ex.
5 and -160

Floating constants require the use of


decimal point followed by the numbers
fractional components. Ex. 16.234
String constants set of characters
enclosed by double quotes. Ex. bag
and this is good
Backslash character constants
enclosing all character constants in
single quotes that works for most
printing characters. Ex. g = \t

SEQUENCE

NAME

MEANING

\a

Alert

Sound a beep

\b

Backspace

Backs up one character

\f

Form feed

Starts a new screen of page

\n

New line

Moves to the beginning of the next


line

\r

Carriage
Return

Moves to the beginning of the


current line

\t

Horizontal tab

Moves to the next Tab position

\v

Vertical tab

Moves down a fixed amount

\\

Backslash

Displays an actual backslash

Single quote

Displays an actual single quote

\?

Question mark Displays an actual question mark

Double quote

Displays an actual double quote

#define preprocessor
- allows us to define symbolic names and
constants.
A quick example:
#define PI 3.14159
This statement will translate every occurrence of
PI in the program to 3.14159.
Here is a more complete example:
#define PI 3.14159 main() { int r=10; float cir; cir
= PI * (r*r); }

const keyword
- used to create a read only variable. Once
initialized, the value of the variable cannot be
changed but can be used just like any other
variable.
const syntax:
main()
{ const float pi = 3.14; }
The const keyword is used as a qualifier to the
following data types - int float char double struct.
const int degrees = 360;
const float pi = 3.14;
const char quit = 'q';

Unary Operators
- changes the sign of a value or
variable.
- Unary minus (-) and unary plus (+)
Examples:
2 +-3
((-x) + (+y))

Increment (++) and Decrement (--)


Operators
++
--

increment adds one to a value of the


variable
decrement subtracts one from a value
of the variable

Note: The value of the expression in which ++


or -- operator is used depends on the
position of the operator.

Prefix increment
is

when the ++ is placed


immediately in front of its operand.
the value of the expression is the
variables value after incrementing.

Postfix increment
is

when the expressions value is


the value of the variable before it is
incremented.

Comparison of POSTFIX and PREFIX


Increments
Before
x
y

Increments y = ++x
y=
x++
Prefix: Increment x and then used it.
Postfix: Use x and then increment it.

After

Prefix
x
y
5

Postfix
x
6

main(){
int a=3, b;
clrscr();
b=a++;
printf(b is %d, and a is %d\n, b, a);
b=++a;
printf(Now b is %d, and a is %d\n, b, a);
b=5%--a;
printf(Now b is %d, and a is %d\n, b, a);
printf(Now b is %d, and a is %d\n, ++b, a--);
printf(Now b is %d, and a is %d\n, b, a);

b is 3, and a is 4
Now b is 5, and a
Now b is 1, and a
Now b is 2, and a
Now b is 2, and a

is
is
is
is

5
4
4
3

Prefix increment/decrement when the ++


or is placed immediately in front of its
operand. Meaning the value of the
expression is the variables value after
incrementing or decrementing.
Postfix increment/decrement when the ++
or comes immediately after the
operand. The value of the expression is
the value of the variable before it is
incremented or decremented.

Function

Purpose

abs(x)

returns the absolute value of integer x.


x=abs(-5); x=5
returns the absolute valued of
double.
x=fabs(-5.2);
x=5.2
rounds up or returns the smallest whole
number that is not less than x.
x=ceil(5.2); x=6
rounds down or returns the largest
whole number that is not greater

fabs(x)
type
ceil(x)
floor(x)
than x.

x=floor(5.2); x=5

Function

Purpose

sqrt(x)
returns the non-negative
square of x.
x=sqrt(25); x=5
pow(x)
returns x to the power of y.
x=pow(4,2); x=16
sin(x)
returns the sine of angle x.
cos(x)
returns the cosine of angle x.
tan(x)
returns the tangent of angle x.
log(x)
returns the natural logarithm of x.
log10(x)
returns the base 10 logarithm of x.

Character/String Input Command


getch() allows the user to input a character
and wait for the enter key. Inputted char will
not be echoed but could be stored if
location is specified.
Syntax:
getch();
var_name = getch();
Example: ans = getch();

Character/String Input Command


getche() allows the user to input a
character and there is no need for the enter
key. Inputted char will be echoed but could
be stored if location is specified.
Syntax:
getche();
var_name = getche();
Example: ans = getche();

Character/String Input Command


gets() allows the user to input a sequence
of characters (string).
syntax:
gets(variable_name_of_char_type);
Example:
gets(name);

Output Command
puts writes the string to the standard output device
such as the monitor and positions the cursor to the
next line.
syntax:
puts(string expression);
Example: puts(CIT);

Output Command
putchar writes the single character to
the screen.
syntax:
putchar(var_name);
Example:
putchar(a);

Вам также может понравиться