t`s`w`c`c`l,s`w`c`c`d`d`x`s`t`l`g`z'h`d`p`y,h`s`z`w`m`b`n`y`w`y`l`n`y`w`l`p`y

莆田市实行改革开放以来,在经济、政治、文化等方面都取行了显著的成就。为展示我市的发展风采,九年级(2)班同学决定组织一次调查探究讨论活动。李强将自己调查情况列成下表王大妈家( W: H4 M) R
`( H8 R3 B 衣. d$ \% X* [3 G
D 食1 ^% Z6 P% C0 [* L% R8 j- G 住9 ]# A: N2 `+ M+ g' \: I: \ 行& g8 J- T" S: R 收入(年)% R' \4 V1 L- ^ 1978年前5 F! K# h% N3 I; b 只够换洗0 N& h4 h3 X5 f, c( N7 G6 L( T 定量供应! d$ g- h9 D1 h7 F0 G6 g 土房3 U+ K0 _
Y; U 自行车- T7 I9 d3 F% M0 W 约1000元- D% V+ L1 `, I4 S" ]; N 1990年8 F* R1 V( M* h* P/ c 新颖多样6 L1 N5 F/ Q% A5 R$ _, `. T5 ` 量多质好2 H/ \' h: H8 ^7 S% Q4 g4 ?0 j 砖房9 O3 ^6 N5 Q+ d$ P$ C; S 摩托车! B" S$ L" C
Y 约15000元( S6 O2 `( ]$ h 2011年" _$ ^! i5 f. ]2 X 追求时尚! G' D* L& c8 K+ h! ` 讲究营养% P: [: H* \( c& N, Z3 D 楼房! @: C6 I, j9 C; P5 [' U 小汽车9 T5 [8 [: R7 N( `: I) j5 V6 [
^& V! T9 `2 B' P7 c" @: _# H3 c7 N (1)同学们也像李强一样,收集到很多“莆田改革开放30年成就”的资料,请你列举搜集资料的方法。(两种即可)(2分)(2)在讨论会上同学们涉及到“衣、食、住、行话变化”的话题,请你就李强同学的调查表,用最简洁的语言概括图表所反映的问题。(2分)(3)运用课文第一单元的知识,为家乡莆田日后更好更快发展献计献策。(至少二条)(4分)(4)同学们对莆田未来的发展充满了希望,在讨论会即将结束之际,请你为家乡莆田的发展写一句祝福语。(2分)(5)简要谈谈参加本次活动后,你有何感想?(4分)
在线咨询您好,告诉我您想学什么,15分钟为您匹配优质老师哦马上咨询
搜索你想学的科目、老师试试搜索吉安
在线咨询您好,告诉我您想学什么,15分钟为您匹配优质老师哦马上咨询&&&分类:莆田市实行改革开放以来,在经济、政治、文化等方面都取行了显著的成就。为展示我市的发展风采,九年级(2)班同学决定组织一次调查探究讨论活动。李强将自己调查情况列成下表王大妈家( W: H4 M) R
`( H8 R3 B 衣. d$ \% X* [3 G
D 食1 ^% Z6 P% C0 [* L% R8 j- G 住9 ]# A: N2 `+ M+ g' \: I: \ 行& g8 J- T" S: R 收入(年)% R' \4 V1 L- ^ 1978年前5 F! K# h% N3 I; b 只够换洗0 N& h4 h3 X5 f, c( N7 G6 L( T 定量供应! d$ g- h9 D1 h7 F0 G6 g 土房3 U+ K0 _
Y; U 自行车- T7 I9 d3 F% M0 W 约1000元- D% V+ L1 `, I4 S" ]; N 1990年8 F* R1 V( M* h* P/ c 新颖多样6 L1 N5 F/ Q% A5 R$ _, `. T5 ` 量多质好2 H/ \' h: H8 ^7 S% Q4 g4 ?0 j 砖房9 O3 ^6 N5 Q+ d$ P$ C; S 摩托车! B" S$ L" C
Y 约15000元( S6 O2 `( ]$ h 2011年" _$ ^! i5 f. ]2 X 追求时尚! G' D* L& c8 K+ h! ` 讲究营养% P: [: H* \( c& N, Z3 D 楼房! @: C6 I, j9 C; P5 [' U 小汽车9 T5 [8 [: R7 N( `: I) j5 V6 [
^& V! T9 `2 B' P7 c" @: _# H3 c7 N (1)同学们也像李强一样,收集到很多“莆田改革开放30年成就”的资料,请你列举搜集资料的方法。(两种即可)(2分)(2)在讨论会上同学们涉及到“衣、食、住、行话变化”的话题,请你就李强同学的调查表,用最简洁的语言概括图表所反映的问题。(2分)(3)运用课文第一单元的知识,为家乡莆田日后更好更快发展献计献策。(至少二条)(4分)(4)同学们对莆田未来的发展充满了希望,在讨论会即将结束之际,请你为家乡莆田的发展写一句祝福语。(2分)(5)简要谈谈参加本次活动后,你有何感想?(4分)
莆田市实行改革开放以来,在经济、政治、文化等方面都取行了显著的成就。为展示我市的发展风采,九年级(2)班同学决定组织一次调查探究讨论活动。李强将自己调查情况列成下表王大妈家( W: H4 M) R
`( H8 R3 B 衣. d$ \% X* [3 G
D 食1 ^% Z6 P% C0 [* L% R8 j- G 住9 ]# A: N2 `+ M+ g' \: I: \ 行& g8 J- T" S: R 收入(年)% R' \4 V1 L- ^ 1978年前5 F! K# h% N3 I; b 只够换洗0 N& h4 h3 X5 f, c( N7 G6 L( T 定量供应! d$ g- h9 D1 h7 F0 G6 g 土房3 U+ K0 _
Y; U 自行车- T7 I9 d3 F% M0 W 约1000元- D% V+ L1 `, I4 S" ]; N 1990年8 F* R1 V( M* h* P/ c 新颖多样6 L1 N5 F/ Q% A5 R$ _, `. T5 ` 量多质好2 H/ \' h: H8 ^7 S% Q4 g4 ?0 j 砖房9 O3 ^6 N5 Q+ d$ P$ C; S 摩托车! B" S$ L" C
Y 约15000元( S6 O2 `( ]$ h 2011年" _$ ^! i5 f. ]2 X 追求时尚! G' D* L& c8 K+ h! ` 讲究营养% P: [: H* \( c& N, Z3 D 楼房! @: C6 I, j9 C; P5 [' U 小汽车9 T5 [8 [: R7 N( `: I) j5 V6 [ 100000元5 ^& V! T9 `2 B' P7 c" @: _# H3 c7 N (1)同学们也像李强一样,收集到很多“莆田改革开放30年成就”的资料,请你列举搜集资料的方法。(两种即可)(2分)(2)在讨论会上同学们涉及到“衣、食、住、行话变化”的话题,请你就李强同学的调查表,用最简洁的语言概括图表所反映的问题。(2分)(3)运用课文第一单元的知识,为家乡莆田日后更好更快发展献计献策。(至少二条)(4分)(4)同学们对莆田未来的发展充满了希望,在讨论会即将结束之际,请你为家乡莆田的发展写一句祝福语。(2分)(5)简要谈谈参加本次活动后,你有何感想?(4分)
科目:难易度:最佳答案(2)(2分)改革开放以来,我国人民生活水平不断提高。(3)(4分)以经济建设为中心;继续深化改革开放;大力发展民营经济(开放题,言之有理即可)(4)(2分)(开放题,言之有理即可)如:莆田明天更美好;坚持改革开放,推动港城崛起等。(5)(4分)(开放题,言之有理即可)可参考:锻炼了自己搜集资料、分析归纳、语言表达的能力,丰富了热爱家乡的情感,增强自己报效家乡、建设家乡的社会责任感。解析(1)网上搜集、图书室查阅外还有问卷调查法、访谈法、观察法(2)注意1978年时间点,代表改革开放后的变化,产品质量的变化代表生活水平的变化。(3)莆田日后更好更快发展涉及政治、经济、文化、生态、社会这五个方面,但是第一单元只有政治与经济两个方面的知识点。从经济角度看:完善我国的基本经济制度,完善我国的分配制度,改革开放政治角度:完善我国根本政治制度与基本政治制度,坚持民族平等、团结、共同繁荣的民族原则,坚持一国两制,坚持党的基本路线等都是能够解放生产力促进经济的发展。(4)略知识点:&&基础试题拔高试题热门知识点最新试题
关注我们官方微信关于跟谁学服务支持帮助中心佛山市禅城区伯锦机电
类型:经销商
联系人:刘先生
本网采购热线:5
联系我时,请说明是在中国化工仪器网上看到的,谢谢
主营产品:低压电器,工业自动化配件,电线器材,光电开关,行程开关,计数器,编码器,接触器
主营业务:我公司是台湾嘉义富祥在大陆的分公司,多年来专业经营台湾、日本、韩国、欧美各国进口 近接开关、光电开关、光纤管、继电器、计数器、译码器、固态继电器、PLC、温控器、变频器、比例控制器、马达、离合器、电磁阀、脚踏开关、仪表、微动开关、按钮....等广泛应用于工业自动化领域。
该商家其它产品
其它商家同类产品Brian W. Kernighan: Programming in C: A Tutorial
in C:  A Tutorial
Bell Laboratories, Murray Hill, N. J.
Disclaimer:
This ``tutorial'' is presented as a historical document,
not as a tutorial.  Although it has lost little of its didactic
value, it describes a language that C compilers today do no longer understand:
the C of 1974, four years before
published the first edition
of ``The C Programming Language''.
Table of Contents:
-- getchar, putchar, printf
of Variables
: Who Knows About What
1.  .
C is a computer language available on the GCOS and UNIX operating
systems at Murray Hill and (in preliminary form) on OS/360 at
Holmdel.  C
and simply it has decent
control flow facilities so your code can be
page, without labels or GOTO's; it lets you write code that is compact
it encourages modularity and good
and it provides good data-structuring facilities.
memorandum
is a tutorial to make learning C as painless as
possible.  The first part concentrates on the central features
C; the second part discusses those parts of the language which are
useful (usually for getting more efficient and smaller code) but which
new user.  This is not
a reference manual. 
Details and special cases will be skipped ruthlessly,
will be made to cover every language feature. 
of presentation is hopefully pedagogical instead of
logical.  Users
the full story should consult the &C
Reference Manual& by D. M.
which should be read
for details anyway. 
Runtime support is described in
you will have to read one of these to
learn how to compile and run a C program.
We will assume that
creating files, text editing, and the like in the operating system you
run on, and that you have programmed in
some language before.
printf(&hello, world&);
A C program consists of one
functions,
the functions and subroutines of a Fortran program or the
procedures of PL/I, and perhaps some external data
definitions. 
is such a function,
and in fact all C programs must have
main. 
program begins
first statement of
main. 
other functions to perform its job,
some coming
from the same program, and others from libraries.
One method of
communicating
arguments. 
The parentheses following the function name surround the argument
here main is a
no arguments, indicated
( ).  The {} enclose the statements of the
function.  Individual statements end with a semicolon
but are otherwise free-format.
printf is a library function which will format and
output on the terminal (unless some other destination is specified). 
case it prints
hello, world
A function is invoked by naming it, followed by
of arguments
in parentheses.  There is no CALL statement as in Fortran or PL/I.
3.  A ; V Types and Type Declarations
and prints their sum.
int a, b, c,
sum = a + b +
printf(&sum is %d&, sum);
Arithmetic and the assignment statements are
semicolons) or PL/I.  The format of C
programs is quite
free.  We
several statements on a line
can split a statement among several lines if it
seems desirable.  The split may
be between
the operators or
variables,
but not in the middle of a name or operator.  As
a matter of style, spaces, tabs, and newlines should
enhance readability.
C has four fundamental types of variables:
int  integer (PDP-11: 16 H6070: 36 IBM360: 32 bits)
char  one byte character (PDP-11, IBM360: 8 H6070: 9 bits)
float  single-precision floating point
double  double-precision floating point
There are also arrays and structures of these
that return them,
all of which we will meet
All variables in a C program must be declared, although this
done implicitly by context.  Declarations must
precede executable statements.  The declaration
int a, b, c,
declares a, b, c, and sum
to be integers.
Variable names have one to eight characters, chosen from
0-9, and _,
and start with a non-digit. 
Stylistically, it's much better to use only
functions and
the first six characters.  (Function and external
by various assemblers, some of which are
limited in the size and case
of identifiers
can handle.)
Furthermore, keywords and library functions may only be recognized
in one case.
have already seen decimal integer constants
-- 1, 2, and 3. 
Since C is often used for system programming
and bit-manipulation,
language.  In C, any number that begins with 0 (zero!) is an
integer (and
hence can't have any 8's or 9's in it). 
Thus 0777 is an octal constant, with decimal value 511.
A ``character'' is one
inherently
machine-dependent
concept).  Most often this is expressed as a character constant,
which is one character
single quotes.  However,
be any quantity that fits in a byte, as in flags below:
char quest, newline,
quest = '?';
newline = '\n';
flags = 077;
The sequence `\n'
character'',
when printed,
skips the terminal to the beginning of the next
line.  Notice that `\n' represents only a single character. 
several other ``escapes'' like `\n'
for representing hard-to-get
or invisible characters, such
`\b' for backspace,
`\0' for end of file, and `\\' for the backslash itself.
float and double constants are discussed
-- getchar, putchar, printf
c = getchar( );
putchar(c);
are the basic
I/O library functions in C.
getchar fetches one character
each time it is called,
returns that
the function.  When it reaches the end
thereafter
represented by `\0' (ascii NUL,
which has value zero).  We will see
very shortly.
putchar puts one character out on the standard output
the terminal) each time it is called.  So the program above reads one
out.  By itself,
this isn't very
interesting, but observe that if we put a loop around this,
test for end of
file, we have a complete program for copying one file
to another.
printf is a more complicated
function for producing formatted
output.  We will talk about only the simplest use of it.  Basically,
printf uses its first argument as formatting information, and
any successive arguments as variables to be output.  Thus
printf (&hello, world\n&);
simplest use.  The
is printed
out.  No formatting information, no variables, so the string is dumped
verbatim.  The
newline is necessary to put this out on a
line by itself.  (The construction
&hello, world\n&
is really an array of chars.  .)
More complicated, if sum is 6,
printf (&sum is %d\n&, sum);
Within the first argument of printf, the
characters
signify that the next argument in the argument list is to be
printed as a base 10 number.
Other useful formatting commands are
single character,
out an entire string, and ``%o''
to print a number
of decimal (no leading
zero). 
For example,
printf (&What is the value of %d in octal?&, n);
printf (&%s! %d decimal is %o octal\n&, &Right&, n, n);
What is the value of 511 in octal?
Right! 511
is 777 octal
Notice that there is no newline at the end of the first output
line. 
Successive
to printf (and/or putchar,
for that matter) simply
put out characters. 
are printed unless
them.  Similarly,
on input, characters are read one at a time as you
them.  Each line is generally terminated
by a newline
(\n), but there is otherwise no concept of record.
6.  ;
compound statements
The basic conditional-testing statement in C is the
statement:
c = getchar( );
if( c == '?' )
printf(&why did you type a question mark?\n&);
The simplest form of if is
if (expression) statement
expression
enclosed in
parentheses.  It is followed
statement. 
expression
is evaluated, and if its value is non-zero, the
executed. 
There's an optional , to be described soon.
The character sequence `=='
in C; here is
the complete set:
equal to (.EQ. to Fortraners)
not equal to
greater than
greater than or equal to
less than or equal to
``expression
expression''
is 1 if the
relation is true, and 0 if false.  Don't forget that the equality test
a single `='
causes an assignment, not a test, and
invariably leads to disaster.
Tests can be combined with the operators `&&' (AND),
`||' (OR), and `!' (NOT).  For example, we can
test whether a character is blank or tab or newline with
if( c==' ' || c=='\t' || c=='\n' ) ...
guarantees that `&&' and `||' are evaluated left to right --
we shall soon see cases where this matters.
statement part of an if can
arbitrarily complicated
by enclosing a set of statements in {}.  As a simple example,
suppose we want to ensure that a is bigger than b,
as part of a sort routine.  The interchange of a
and b takes three statements in C, grouped together by {}:
if (a & b) {
statement, you can use any compound statement,
which is just a number
of simple or compound ones enclosed in
{}.  There is
the } of a compound statement, but there is a semicolon after
non-compound statement inside the {}.
ability to replace
statements
complex ones
will is one feature that makes
Fortran.  Logic
the exchange in the previous
example) which
would require several GOTO's and labels in Fortran can and
without any, using compound statements.
7.  ; Assignment within an E Null Statement
mechanism in C is the while
statement. 
Here's a program that copies its input to its output a character at
time.  Remember that `\0' marks the end
while( (c=getchar( )) != '\0' )
putchar(c);
The while statement is a loop, whose general form is
while (expression) statement
Its meaning is
(a)  evaluate the expression
(b)  if its value is true (i.e., not zero)
do the statement, and go back to (a)
Because the expression is tested
is executed,
the statement
desirable.  As
statement,
expression
and the statement can both be arbitrarily complicated,
although we
haven't seen that yet.  Our example gets the
character,
to c, and then tests if it's a `\0''.  If it is
statement part of the
while is executed,
printing the
character.  The while
repeats.  When the input character is finally a `\0',
the while terminates, and so does main.
assignment
c = getchar( )
within an expression.  This is a handy notational shortcut
code.  (In fact it is often the only way to
write the code cleanly.  As
without using an assignment inside an expression.)  It works
because an assignment statement has
just as any other
expression does.
Its value is the value of the right hand side. 
This also implies that we can use multiple assignments like
x = y = z = 0;
Evaluation goes from right to left.
By the way, the extra
parentheses
assignment statement
the conditional were really necessary: if we had said
c = getchar( ) != '\0'
would be set to 0 or 1 depending on whether the
character fetched
was an end of file or not.  This
parentheses
assignment
is evaluated
relational
`!='.  When
doubt, or even if not,
parenthesize.
putchar(c)
we could also copy the input to the output
by nesting the calls to getchar
and putchar:
while( putchar(getchar( )) != '\0' ) ;
is being repeated?
or technically,
statement,
because all the work is really done within the test
while.  This
because the final `\0' is copied to the output
we decide to stop.
The arithmetic operators are the usual `+',
(truncating
division if the operands are both int),
remainder or mod operator `%':
sets x to the remainder after a is divided by b
(i.e., a mod b).  The results
are machine dependent unless
a and b are both positive.
In arithmetic, char variables can
treated like
variables.  Arithmetic
characters
is quite legal,
and often makes sense:
c = c + 'A' - 'a';
converts a single lower case ascii character stored in
upper case,
of the fact that corresponding ascii
letters are a fixed distance apart.  The rule governing
this arithmetic is that all chars
converted to int
before the arithmetic is done.  Beware
conversion may
involve sign-extension if
the leftmost bit of a
might be negative. 
(This doesn't happen with genuine characters on any current machine.)
So to convert a file into lower case:
while( (c=getchar( )) != '\0' )
if( 'A'&=c && c&='Z' )
putchar(c+'a'-'A');
putchar(c);
Characters have different
machines.  Further,
this code won't work on an IBM machine,
because the
ebcdic alphabet are not contiguous.
9.  ;
Conditional Expressions
just used an else after an
general form of if is
if (expression) statement1 else statement2
the else part is optional, but often useful. 
canonical example sets x to the minimum of a
if (a & b)
Observe that there's a semicolon after x=a.
often more concise. 
It is called the ``conditional expression'' because it
is a conditional
anywhere an expression can.  The value of
is a if a is less than b; it
otherwise.  In
general, the form
expr1 ? expr2 : expr3
``evaluate
expr1.  If it is not zero,
the value of the whole thing is expr2; otherwise the value
is expr3.''
To set x to the minimum of a and
x = (a&b ? a : b);
The parentheses aren't necessary because `?:'
`=', but safety first.
step further,
we could write the
lower-case program as
while( (c=getchar( )) != '\0' )
putchar( ('A'&=c && c&='Z') ? c-'A'+'a' : c );
If's and else's can be used
one of several ways and then rejoins, a common programming structure,
in this way:
else if(...)
else if(...)
The conditions are tested in order, and exactly o
the first one whose if is satisfied,
or the one for the
else.  When this block is finished, the next
statement executed is the one after the last else. 
the ``default'' case, omit
the last else.
For example,
to count letters, digits and others
a file, we
could write
int let, dig, other,
let = dig = other = 0;
while( (c=getchar( )) != '\0' )
if( ('A'&=c && c&='Z') || ('a'&=c &&
else if( '0'&=c && c&='9' ) ++
printf(&%d letters, %d digits, %d others\n&, let, dig, other);
The `++' operator means ``increment by 1''; we will
to it in the
next section.
and Decrement Operators
interesting
operators,
(increment)
(decrement).  Suppose we want to count the lines in a file.
while( (c=getchar( )) != '\0' )
if( c == '\n' )
printf(&%d lines\n&, n);
++n is equivalent to n=n+1 but clearer,
particularly when
complicated
expression.  `++'
and `--' can be applied only to
int's and char's (and
which we haven't
got to yet).
The unusual feature of `++' and `--' is that
either before or after a variable.  The value of ++k
is the value of k after it has been
incremented.  The
value of k++ is k
is incremented.  Suppose k
is 5.  Then
increments k to 6 and then sets x to
value, i.e., to 6.  But
first sets x
increments
6.  The
incrementing effect
++k and k++ is
the same, but their values are respectively 5 and 6. 
We shall soon see examples where both of these uses are important.
as in Fortran or PL/I,
make arrays
whose elements are basic types.  Thus we can
10 integers with the declaration
int x[10];
The square bracke parentheses are
function references.  Array indexes begin at zero, so the
elements of x are
x[0], x[1], x[2], ..., x[9]
If an array has n elements, the largest subscript is
Multiple-dimension arrays are provided,
dimensions.  The declaration and use look like
int name[10] [20];
n = name[i+j] [1] + name[k] [2];
Subscripts can be
expressions. 
Multi-dimension
(opposite to Fortran),
so the rightmost
subs name has 10 rows and 20 columns.
Here is a program which reads a line,
and prints its length (excluding the newline at the end).
char line[100];
while( (c=getchar( )) != '\n' ) {
if( n & 100 )
printf(&length = %d\n&, n);
As a more complicated problem,
suppose we
still storing the first 100
characters of each line.  Try it
exercise before looking
the solution:
int n, char line[100];
while( (c=getchar( )) != '\0' )
if( c == '\n' ) {
printf(&%d0, n);
if( n & 100 ) line[n] =
12.  ; Strings
Text is usually kept as an array of characters,
line[ ] in the example above.  By convention in C,
the last character
array should be a `\0' because
programs that
manipulate
arrays expect
it.  For
printf uses the `\0' to detect
the end of a character
array when printing it out with a `%s'.
copy a character array s
into another t like this:
while( (t[i]=s[i]) != '\0' )
of the time we have to put in our own `\0' at
if we want to print the line with printf,
necessary.  This code prints the character count before the line:
char line[100];
while( (line[n++]=getchar( )) != '\n' );
line[n] = '\0';
printf(&%d:\t%s&, n, line);
Here we increment n in the subscript
the previous
used. 
The character is read,
line[n], and only then n is incremented.
There is one place and one place only where C
construction
&stuff between double quotes&
The compiler puts a `\0' at
automatically.  Text enclosed
properties
precisely those of an (initialized) array of characters.
The for statement is a somewhat generalized
initialization
increment parts of a loop into a single
statement along with the
test.  The
general form of the
for( initialization; expression; increment )
The meaning is exactly
initialization;
while( expression ) {
increment;
following code does the same
for( i=0; (t[i]=s[i]) != '\0'; i++ );
This slightly more ornate example adds up the elements of an array:
for( i=0; i&n; i++)
sum = sum + array[i];
In the for statement,
the initialization
but the semicolon has to be there.  The increment is also
optional.  It is not followed by
semicolon. 
second clause,
the test, works the same way as in the while:
expression
do another
otherwise get
on with the next statement.  As with the while,
the for loop may be done zero times.  If the expression is left out,
it is taken to be always true, so
for( ; ; ) ...
while( 1 ) ...
are both infinite loops.
might ask why we use a for since it's so much
like a while. 
(You might also ask why we use a while because...) 
  The for is
preferable
sometimes eliminates the need for compound
statements,
a two-dimensional array:
for( i=0; i&n; i++ )
for( j=0; j&m; j++ )
array[i][j] = 0;
Suppose we want,
as part of a
occurrences of the ascii characters in some input text.  Let us also
map illegal characters (those with value&127
pile. 
presumably
part of the program,
practice dictates making
a separate function.  Here is one way:
int hist[129];
128 legal chars + 1 illegal group*/
count(hist, 128);
/* count the letters into hist */
printf( ... );
/* com use them */
/* anywhere blanks, tabs or newlines could appear */
count(buf, size)
int size, buf[ ]; {
for( i=0; i&= i++ )
set buf to zero */
(c=getchar(
)) != '\0' ) {
/* read til eof */
if( c & size || c & 0 )
/* fix illegal input */
have already seen many examples of calling a function,
concentrate
on how to define one.  Since count
has two arguments, we need to declare
them, as shown, giving their types,
and in the
case of buf, the fact that it is an array.  The
declarations of
arguments go between the argument
list and the opening `{'.  There
is no need to specify the size of the array buf,
for it is defined outside of count.
go back to the calling
routine.  In fact, we could have omitted it, since a return is implied
at the end of a function.
if we wanted count to
the number of characters read?  The return statement
allows for this too:
nchar = 0;
while( (c=getchar( )) != '\0' ) {
if( c & size || c & 0 )
return(nchar);
Any expression can appear within the parentheses.  Here is a
to compute the minimum of two integers:
return( a & b ? a : b );
To copy a character array, we could write the function
strcopy(s1, s2)
/* copies s1 to s2 */
char s1[ ], s2[ ]; {
for( i = 0; (s2[i] = s1[i]) != '\0'; i++ );
As is often the case, all the work is done by the assignment statement
embedded in the test part of the for.  Again, the declarations of the
arguments s1
to strcopy.  (In the , we will see a more efficient way to do a string copy.)
There is a subtlety in
unsuspecting Fortran programmer.  Simple variables (not arrays) are
passed in C by ``call by
copy of its arguments,
and doesn't know their
addresses.  This makes it
impossible to change the value of
of the actual input arguments.
are two ways out of this dilemma.  One is to make special
arrangements to pass to the function the address of a variable instead
of its value.  The
the variable
external variable, which is known to each function by its name.  We
will discuss both
possibilities in the next few sections.
each x is local to its own routine -- the x
in f is unrelated to
g.  (Local variables are also called
``automatic''.) 
Furthermore each local variable in a routine appears
disappears
when the function is
exited.  Local variables have no memory from one call to the next and
must be explicitly initialized upon each entry.  (There is a
static storage class for making local var
won't discuss it.)
opposed to local variables,
are defined
functions,
and are (potentially) available to
functions.  External
storage always remains in
existence.  To make
variables external we have to define them external to
functions,
and, wherever we want to use them, make a declaration.
extern int nchar, hist[ ];
count( ) {
extern int nchar, hist[ ];
hist[129];
/* space for histogram */
/* character count */
Roughly speaking,
any function
an external
variable must contain an extern declaration for it.  The declaration
is the same as others, except for the added keyword
extern. 
Furthermore,
there must
somewhere be a definition
variables external to
all functions.
External variable
they are set
not explicitly initialized.  In its simplest form, initialization is
a constant) after the
definition:
This is discussed further in a .
ends our discussion of what might
the central
core of C.  You now have enough to write quite substantial C programs,
it would probably be a good idea if you
paused long enough to do
so.  The rest
tutorial will describe some more ornate
constructions, useful but not essential.
C is the address of something.  It is a rare
case indeed
care what the specific address itself
pointers are a quite common way to get at the contents
something. 
The unary operator `&' is used to produce the address of an object, if
it has one.  Thus
puts the address of a into b. 
print it or pass it to some other routine,
because we haven't given
b the right kind of declaration. 
is indeed a pointer
to an integer, we're in good shape:
int a, *b,
contains the address of a and `c = *b' means
the value in
b as an address,
as a pointer. 
The effect is that
back the contents
a, albeit rather indirectly. 
(It's always the case that `*&x' is the same as x if
x has an address.)
efficiently along arrays.  In fact, in the implementation of an array,
name represents
the array, so you can't use it on the left side of an expression. 
(You can't
address of something by assigning to it.) 
char x[100];
y is of type pointer to character (although it doesn't yet
point anywhere).  We can make y point to an element
of x by either of
y = &x[0];
Since x is the address of x[0] this is
legal and consistent.
Now `*y' gives x[0].  More importantly,
gives x[1]
gives x[i]
and the sequence
y = &x[0];
leaves y pointing at x[1].
Let's use pointers in a function length
computes how
is.  Remember
by convention all
arrays are terminated with
`\0'.  (And
this program will blow up inevitably.)  The old way:
char s[ ]; {
for( n=0; s[n] != '\0'; )
return(n);
Rewriting with pointers gives
char *s; {
for( n=0; *s != '\0'; s++ )
return(n);
now see why we have to say
points to -- if we're to increment it with s++ we have to
the right amount.
is more efficient (this
almost always
true) but even more compact is
for( n=0; *s++ != '\0'; n++ );
increments
the pointer
get the next character next time around.  As you can see,
as we make things more
efficient, we
also make them less clear. 
But `*s++' is an idiom so common that you have to know it.
here's our function
that copies a character array s to another t.
strcopy(s,t)
char *s, *t; {
while(*t++ = *s++);
see the code this way.  (You
have a space after the `=': see .)
declarations
char s[ ];
equivalent
unspecified size of that type, are the same thing.
If this all seems mysterious,
nature.  You
complicated.
Look back at the .  We
string names as arguments,
then proceeded to clobber
both of them by
incrementation.  So
the original strings in the function that called strcopy?
As we said before, C is a ``call by
language: when
make a function call like f(x),
the value of x
its address.  So
no way to alter
from inside
f.  If x
is an array (char x[10]) this isn't a
address anyway,
and you're not trying to
just what it addresses. 
This is why strcopy works as it does. 
And it's convenient not to have to
worry about making temporary
copies of the input arguments.
But what if x is a scalar and you do want to change it? 
you have to pass the address of x to
and then use it
a pointer.  Thus
interchange two integers,
flip(x, y)
int *x, *y; {
temp = *x;
and to call flip, we have to pass the addresses of the variables:
flip (&a, &b);
18.  ;
Program Arguments
a C program is called,
the arguments on
command line
are made available to the main program as an argument count argc
argv containing
the arguments. 
Manipulating these arguments is one of
of pointers (``pointer
pointer to ...'').  By
convention,
(in argv[0]) is
the command name itself.
Here is a program that simply echoes its arguments.
main(argc, argv)
for( i=1; i & i++ )
Step by step:
main is called with two arguments,
argument count
of arguments.  argv is a
pointer to an array, whose
individual elements are pointers to arrays
characters. 
The zeroth
name of the command itself,
so we start to
print with the
first argument, until
printed them all.  Each
argv[i] is a character array, so we use a `%s' in the printf.
You will sometimes see the declaration of argv
written as
char *argv[ ];
which is equivalent.  But we can't
char argv[
], because
dimensions
are variable and there would be no way to figure out
how big the array is.
argv.  A common
convention
programs is that if the first argument is `-', it
indicates a flag of some sort.  For example, suppose we want a program
to be callable as
prog -abc arg1 arg2 ...
if it is present,
followed by any combination of a, b, and c.
main(argc, argv)
aflag = bflag = cflag
if( argc & 1 && argv[1][0] == '-' ) {
for( i=1; (c=argv[1][i]) != '\0'; i++ )
if( c=='a' )
else if( c=='b' )
else if( c=='c' )
printf(&%c?\n&, c);
are several
this code. 
a real need for the left-to-right evaluation
at argv[1]
unless we know it's there.  Second, the statements
let us march along the argument list by one position,
we can skip
over the flag argument as if
independent
was a flag
argument.  This only works because argv is a
be incremented.
19.  ;
B Continue
switch statement can be used to replace the
we used in the last example.  When the tests are
like this:
if( c == 'a' ) ...
else if( c == 'b' ) ...
else if( c == 'c' ) ...
testing a value against a series of
constants,
switch statement
clearer and usually gives better code.  Use it like this:
switch( c ) {
printf(&%c?\n&, c);
The case statements
gets done if none of the other cases
satisfied.  (A
default is
if it isn't there,
the cases match, you just
fall out the bottom.)
The break statement in this
new.  It
because the
cases are just labels,
and after you do one of them, you
fall through to the next
escape.  This is a mixed blessing.  On the positive side,
you can have multiple cases on
we might want to
both upper and lower
But what if we just want to get out after doing case
out of a case of the switch with a label
and a goto,
but this is really ugly.  The break
statement lets
us exit without either goto or label.
switch( c ) {
/* the break statements get us here directly */
The break statement also works in for
an immediate exit from the loop.
The continue
and while's;
iteration of the loop to be started. 
This means it goes to the increment
test part of the while.  We could have used
a continue in our example to get on with the next iteration of the
for, but it seems
break instead.
use of structures is to lump together
collections
disparate variable types,
so they can conveniently be
unit.  For example, if
a compiler
assembler,
might need for
identifier
information
source line
(an integer),
information
character,
(another integer).
make a structure out of this quite
easily.  We first
the structure will look like,
after that we can actually reserve
in the same statement or separately.  The simplest thing
is to define it and allocate storage all at once:
sym to be a structure with
type and usage
are members of the structure.  The way we
refer to any
particular
the structure is
structure-name . member
sym.type = 077;
if( sym.usage == 0 ) ...
while( sym.id[j++] ) ...
Although the names of structure members
there can't be another id or usage in some
other structure.
much.  The
advantages of
structures start to come when we have arrays of structures, or when we
want to pass complicated
between functions. 
Suppose we wanted to make a symbol table for up to 100
identifiers.  We could
extend our definitions like
id[100][10];
line[100];
type[100];
usage[100];
structure lets us rearrange this
spread-out
information
all the data about a single identifer is collected into one lump:
} sym[100];
makes sym an
element has the
specified shape.  Now we can refer to members as
sym[i].usage++; /* increment usage of i-th identifier */
for( j=0; sym[i].id[j++] != '\0'; ) ...
Thus to print a list of all identifiers
together with their line number,
for( i=0; i& i++ )
if( sym[i].usage == 0 )
printf(&%d\t%s\n&, sym[i].line, sym[i].id);
Suppose we now want to write
lookup(name)
tell us if name already exists in sym,
by giving its index,
that it doesn't, by returning a -1.  We
can't pass
a structure to a
we have to either define it externally,
pointer to it.  Let's
try the first way first.
/* current length of symbol table */
} sym[100];
/* symbol table */
if( (index = lookup(newname)) &= 0 )
sym[index].usage++;
/* already there ... */
install(newname, newline, newtype);
char *s; {
extern struct {
for( i=0; i& i++ )
if( compar(s, sym[i].id) & 0 )
return(i);
return(-1);
compar(s1,s2)
return 1 if s1==s2, 0 otherwise */
char *s1, *s2; {
while( *s1++ == *s2 )
if( *s2++ == '\0' )
return(1);
return(0);
declaration
in lookup isn't
external definition precedes its use in the same source
shall see in a moment.
Now what if we want to use pointers?
} sym[100], *
psym = &sym[0]; /* or p = */
This makes psym a pointer
symbol table), then initializes it to point to the first element
that we added something after the word struct:
called symtag.  This puts a name on our structure definition
repeating the definition. 
not necessary but useful.  In fact we could have said
... structure definition
which wouldn't have assigned any storage at all, and then said
which would define the array and the pointer.  This could be
condensed further, to
sym[100], *
member of a structure by a
pointer is like this:
ptr -& structure-member
in that context.  ptr
is a pointer to
member. 
expression
ptr-&structure-member refers to the indicated member
of the pointed-to structure.  Thus we have constructions like:
psym-&type = 1;
psym-&id[0] = 'a';
and so on.
complicated
expressions,
parentheses
make it clear who goes with what.  For example,
struct { int x, *y; } *p;
increments x
so does this!
increments p before getting x
*p-&y++ uses y as a pointer, then increments it
so does this
uses y as a pointer, then increments p
The way to remember these is that -&,
tightly.  An
expression involving one of these is treated as a
unit.  p-&x, a[i],
names exactly as abc is.
If p is a pointer to a structure,
any arithmetic
the actual size of the structure.  For instance,
p++ increments p by the correct amount to get
array of structures.  But don't assume that the size of a structure is
the sum of the sizes of
alignments
different sized objects, there may be ``holes'' in a structure.
theory.  Here
this time with
struct symtag {
} sym[100];
struct symtag *lookup( );
struct symtag *
if( (psym = lookup(newname)) )
/* non-zero pointer */
psym -& usage++;
/* means already
install(newname, newline, newtype);
struct symtag *lookup(s)
char *s; {
struct symtag *p;
for( p= p & &sym[nsym]; p++ )
if( compar(s, p-&id) & 0)
return(p);
return(0);
The function compar doesn't
In main we test the pointer returned by
the fact that a pointer is by definition never zero when
it really points at
something.  The
other pointer manipulations are
The only complexity is the set of lines like
struct symtag *lookup( );
to an area that we will t
question of function
types.  So
returned integers (or characters, which are
same).  What
do we do when the function returns something else, like a pointer to a
structure?  The
is that any
doesn't return an
int has to say explicitly what it does return.  The
information
(which can make the name hard to
int *g( ) { ... }
struct symtag *lookup(s) char *s; { ... }
character,
and lookup returns a pointer to a structure
that looks
symtag.  And if we're going to use one
these functions,
to make a declaration where we use it, as we did in main
Notice the parallelism between the declarations
struct symtag *lookup( );
struct symtag *
In effect,
this says that lookup( ) and psym
used the same
way - as a pointer to a structure -- even though one is a
the other is a function.
of Variables
initialized
at compile time by
following its name with an initializing value when it is defined.  The
initializing
whose value is known at
compile time, like a constant.
/* &0& could be any constant */
/* p now points to y[1] */
An external array can be initialized by following
list of initializations enclosed in braces:
{0,1,2,3};
/* makes x[i] = i */
{0,1,2,3};
/* makes y big enough for 4 values */
&syntax error\n&;
/* braces unnecessary here */
char *keyword[ ]{
&continue&,
This last one is very useful -- it makes keyword an array
with a zero at the end so we can identify the
last element easily.  A simple lookup
finds a match or encounters a zero keyword pointer:
lookup(str)
/* search for str in keyword[ ] */
int i,j,r;
for( i=0; keyword[i] != 0; i++) {
for( j=0; (r=keyword[i][j]) == str[j] && r != '\0'; j++ );
if( r == str[j] )
return(i);
return(-1);
structures can
initialized.
source text of
previously
compiled routines
from libraries.  How
do we arrange that data gets passed from one routine to
another?  We
seen how to
use function
let us talk about external data.  Warning: the words
declaration
definition
used preci don't treat them as
the same thing.
A major shortcut exists for making extern declarations.  If the
definition
of a variable appears before its use in some function,
no extern declaration is needed within
the function.  Thus, if a file
f1( ) { ... }
f2( ) { ... foo = 1; ... }
f3( ) { ... if ( foo ) ... }
no declaration of foo is needed
the external definition of foo appears before them. 
But if f1 wants to
use foo, it has to contain
declaration
This is true
foo it has to use an extern declaration
(If somewhere there
extern declaration
for something,
must also eventually be an external definition of it, or you'll get an
``undefined symbol'' message.)
There are some
declarations
definitions if you use multiple source files.  To avoid them, first,
define and initialize
entire set of files:
You can get away with multiple external definitions on UNIX,
on GCOS, so don't ask for trouble.  Multiple
initializations
everywhere.  Second,
of any file that
contains functions needing a variable
definition
other file,
an extern declaration, outside of any function:
f1( ) { ... }
discussed shortly,
the external declarations
then stick them into each
of the source files making up
the program.
23.  , #include
C provides a very limited macro facility.  You can say
#define name
and thereafter anywhere ``name'' appears
``something''
will be substituted.  This is particularly useful in parametering
the sizes of arrays:
#define ARRAYSIZE
arr[ARRAYSIZE];
while( i++ & ARRAYSIZE )...
can alter the entire program by
the define)
or in setting up mysterious constants:
#define SET
#define INTERRUPT
/* interrupt bit */
#define ENABLED 04
if( x & (SET | INTERRUPT | ENABLED) ) ...
Now we have meaningful
mysterious constants. 
mysterious
operators `&' (AND) and `|' (OR) will be covered in
excellent practice
programs without any
literal constants except in #define statements.
#define.  First, there's
no semicolon at the end of a #
all the text from the
comments) is
``something''.  When it's put into the text, blanks are placed around
it.  Good
style typically makes the name in the #
this makes parameters more visible.  Definitions affect
after they occur, and only within the file in which they occur. 
Defines can't be nested.  Last, if there is a #define in a file,
of the file must be a `#',
to signal the
preprocessor that definitions exist.
#include.  To include one
file in your source at compilation time, say
#include &filename&
This is useful for putting a lot of heavily used data definitions
#define statements at the beginning of a file to be compiled. 
a file containing
begin with a `#'.  And #include can't be nested -- an included
file can't contain
another #include.
C has several
bit-operations.
For example,
x = x & 0177;
forms the bit-wise AND of x and 0177, effectively
retaining only the
last seven bits of x.  Other operators are
inclusive OR
(circumflex) exclusive OR
(tilde) 1's complement
logical NOT
left shift (as in x&&2)
right shift
(arithmetic on PDP-11;
logical on H6070,
An unusual feature of C is that the normal binary operators
etc.  can be combined with the assignment operator
`=' to form new
assignment
operators.  For example,
uses the assignment operator `=-' to decrement x by 10, and
forms the AND of x and
0177. 
This convention is a useful
notational
shortcut, particularly if x is a complicated
expression.  The classic example is summing an array:
for( sum=i=0; i&n; i++ )
sum =+ array[i];
critical!  For
sets x to -10, while
subtracts 10 from x.  When no space is present,
10.  This
experience of most programmers.  In particular, watch out for things
both of which are almost certainly not what you wanted.  Newer
various compilers are courteous enough to warn you about
the ambiguity.
expression
are evaluated
assignment
order of evaluation should be
watched carefully:
x = x&&y |
means ``shift x left y places, then OR with z, and store
in x.'' But
means ``shift x left by y|z places'', which is
different.
We've skipped over
the treatment
here will be hasty.  C has single and double precision numbers (where
the precision depends on
machine at hand).  For example,
float avg, y[10];
sum = 0.0;
for( i=0; i&n; i++ )
sum =+ y[i];
avg = sum/n;
forms the sum and average of the array y.
All floating arithmetic is done
precision.  Mixed
if an arithmetic operator in an expression
has both operands int or char, the
arithmetic
if one operand is int or char and the
other is float or double,
are converted
to double.  Thus if i and j are int and x is
converts i and j to float
does i/j integer, then converts
Type conversion may b for instance,
converts x to integer (truncating toward
to floating point.
Floating constants are just like those
except that the exponent letter is `e' instead of `E'.  Thus:
pi = 3.14159;
large = 1.;
printf will format floating point numbers: ``%w.df'' in the
corresponding
variable in a field w
digits wide, with d decimal places. 
An e instead of an f will produce
exponential notation.
27.  Horrors!
and labels
a goto statement and labels,
branch about
the way you
to.  But most of the time goto's
needed.  (How
have we used up
point?) 
be more clearly expressed by for/while, if/else,
and compound
statements.
with some legitimacy is in a
program which
contains a long loop, where a while(1) would
be too extended. 
Then you might write
Another use is to implement a break out of more than one level
while.  goto's can only
same function.
am indebted to a veritable host of readers
made valuable
criticisms
tutorial.  They ranged
experience
through several
implementors
C compilers
C language designer
himself. 
of opinion that
no one is satisfied (including me); comments and suggestions are still
welcome, so
some future version might be improved.
References
C is an extension of B, which was
M. Ritchie
UNIX implementation are the work of
D. M. Ritchie. 
The GCOS version
by A. Snyder and B. A. Barres, and completed
Johnson and M. E. Lesk. 
The IBM version is primarily
to T. G. Peterson, with the assistance
of M. E. Lesk.
D. M. Ritchie, C Reference Manual.  Bell
Jan. 1974.
Barres, The
Library. Bell Labs,
Jan. 1974.
UNIX Programmer's Manual.
5th Edition, Bell Labs, 1974.
C. Johnson & B.
Kernighan,
The Programming Language B.
Computer Science
Labs, 1972.}

我要回帖

更多关于 海尔l39b3500w 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信