Академический Документы
Профессиональный Документы
Культура Документы
#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; }
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
SEQUENCE
NAME
MEANING
\a
Alert
Sound a beep
\b
Backspace
\f
Form feed
\n
New line
\r
Carriage
Return
\t
Horizontal tab
\v
Vertical tab
\\
Backslash
Single quote
\?
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))
Prefix increment
is
Postfix increment
is
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
Function
Purpose
abs(x)
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.
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);