You are on page 1of 8

I

- 2011/ 1 -

1 -

1 ( ):
#include <stdio.h>
#include <stdlib.h>
#define NUMROWS 20
#define NUMCOLS 30
void main( void )
{
int i,j;
int pixels=0;
for (i=0; i<NUMROWS; i++)
for (j=0; j<NUMCOLS; j++);
pixels++;
printf("\npixels = %d\n", pixels);
return;
}


, , ,
.
20x30=600
.
,
1.
,
.
14 (pixels++) 600 .
- ;
, () for .
- . ,
, .

I
- 2011/ 1 -

2 ( )
int minval(int *A, int n) {
int currmin;
for (int i=0; i<n; i++)
if (A[i] < currmin)
currmin = A[i];
return currmin;
}

currmin,
if (A[i] < currmin) .
(. warning) uninitialized local variable 'currmin' used,
,
A[i] currmin .
The variable 'currmin' is being used without being defined.
3 ( )
int minval(int *A, int n) {
int currmin = INT_MAX;
for (int i=0; i<n; i++)
if (A[i] > currmin)
currmin = A[i];
return currmin;
}

A[i] currmin
<, /.

minval :
void main( void )
{
int A[5] = {12, 25, 3, 9, 8};
_ASSERTE(minval(A, 5)==3);
return;
}


minval(A, 5)==3, minval
INT_MAX
currmin .

I
- 2011/ 1 -

4 ( )
switch (i) {
case 1:
do_something(1);
case 2:
do_something(2);
case 3:
do_something(1);
case 4:
do_something(4);
default:
break;
}

break;
break;
break;
break;

( 1
, ). ,

.
.
, do_something(3),

.
5 ( )
if (foo = 5)
foo == 7;

.
(if (foo = 5)) (.
true), (foo == 7) .
C, .

foo 5, foo == 7.
, 5,
. , foo 7,
5, .
6 ( )
#define Random(n) rand()%n
val = Random(j-i+1);

I
- 2011/ 1 -

Random 0~n. ,
n , .
, :
val = rand()%j-i+1;

val = rand()%(j-i+1);

.
, :
_ASSERTE(val>=0 && val<(j-i+1));

I
- 2011/ 1 -

C
C .
, ,
.
.
.
(. main)
,
. ,
.
,
.

# - .
:

#define

#undef
#include
#ifdef
#ifndef
#if
#else
#endif

;

;
,
;
,
;
,
;
,

#if/#ifdef/#ifndef

#if/#ifdef/#ifndef

Tabela 1

:
,

.

I
- 2011/ 1 -

( )


#define < >.
:
#define <> < >
<> <
>. #define
(. space) .
, C .
, :
,

.

:
#define PI 3.1415926
, .
,
.

.
:
#define <>(<f1>,<f2>,<f3>..<fn>) < >
, .
f1,f2,f3..fn .

, . .

I
- 2011/ 1 -

:
#define CUBE(x) ((x)*(x)*(x))
:
volume=CUBE(side);

volume =((side)*(side)*(side));

, ,
. . :
#define PI 3.1415926
#define SQUARE(x) ((x)*(x))
#define CIRCLE_AREA(r) (PI*SQUARE(r))
(
) , ,
. :
#define MIN(m,n) ((m)<(n)?(m):(n))
x = MIN(10,MIN(7,11));

:
#undef <>

.


#include

I
- 2011/ 1 -

. ,
/
.
,
/ .
, ,
, .
:
#include < >
, , <>,
.

.
#ifdef / #ifndef < >
, . #else
.
#endif .

#if < >
#else #elif < >
. #endif
.
#if < 1>
1;
2;
#elif < 2>
3;
4;
#else
5;
#endif