📄 王大刚--c语言编程宝典--4_ 变量.htm
字号:
最低的地址对应 <BR>于第一个数组元素, 最高的地址对应最后一个数组元素。数组可以是一维的、也可 <BR>以是多维的。Turbo
C2.0象它高级语方一样也使用数组变量。 <BR>
<P> 一、一维数组 <BR> 一维数组的说明格式是:
<BR> 类型 变量名[长度];
<BR> 类型是指数据类型, 即每一个数组元素的数据类型, 包括整数型、浮点型、字
<BR>符型、指针型以及结构和联合。 <BR> 例如: <BR>
int a[10]; <BR> unsigned long a[20];
<BR> char *s[5]; <BR> char
*f[]; <BR>
<P> 说明: <BR> 1. 数组都是以0作为第一个元素的下标, 因此,
当说明一个int a[16] 的整型 <BR>数组时, 表明该数组有16个元素, a[0]~a[15], 一个元素为一个整型变量。
<BR> 2. 大多数字符串用一维数组表示。数组元素的多少表示字符串长度, 数组名
<BR>表示字符串中第一个字符的地址, 例如在语句char str[ 8] 说明的数组中存入
<BR>"hello"字符串后, str表示第一个字母"h"所在的内存单元地址。str[0] 存放的是 <BR>字母"h"的ASCII码值,
以此类推, str[4]存入的是字母"o"的ASCII码值, str[5]则 <BR>应存放字符串终止符'\0'。
<BR> 3. Turbo C2.0对数组不作边界检查。例如用下面语句说明两个数组
<BR> char str1[5], str2[6]; <BR>
当赋给str1一个字符串"ABCDEFG"时, 只有"ABCDE"被赋给, "E" 将会自动的赋 <BR>给str2, 这点应特别注意。 <BR>
<P> 三、多维数组 <BR> 多维数组的一般说明格式是:
<BR> 类型
数组名[第n维长度][第n-1维长度]......[第1维长度]; <BR>
这种说明方式与BASIC、FORTRAN等语言中多维数组的说明不一样。 <BR> 例如:
<BR> int
m[3][2]; /*定义一个整数型的二维数组*/
<BR> char c[2][2][3];
/*定义一个字符型的三维数组*/ <BR> 数组m[3][2]共有3*2=6个元素, 顺序为:
<BR> m[0][0], m[0][1], m[1][0], m[1][1],
m[2][0], m[2][1]; <BR> 数组c[2][2][3]共有2*2*3=12个元素, 顺序为:
<BR> c[0][0][0], c[0][0][1], c[0][0][2],
<BR> c[0][1][0], c[0][1][1], c[0][1][2],
<BR> c[1][0][0], c[1][0][1], c[1][0][2],
<BR> c[1][1][0], c[1][1][1], c[1][1][2],
<BR> 数组占用的内存空间(即字节数)的计算式为: <BR>
字节数=第1维长度*第2维长度*...*第n维长度*该数组数据类型占用的字节数 <BR>
<P> 4.5 变量的初始化和赋值 <BR>
<P> 一、变量的初始化 <BR>
变量的初始化是指变量在被说明的同时赋给一个初值。Turbo C2.0中外部变量 <BR>和静态全程变量在程序开始处被初始化,
局部变量包括静态局部变量是在进入定义 <BR>它们的函数或复合语句时才作初始化。所有全程变量在没有明确的初始化时将被自 <BR>动清零,
而局部变量和寄存器变量在未赋值前其值是不确定的。 <BR> 对于外部变量和静态变量, 初值必须是常数表达式,
而自动变量和寄存器变量 <BR>可以是任意的表达式, 这个表达式可以包括常数和前面说明过的变量和函数。 <BR>
1. 单个变量的初始化 <BR> 例如: <BR> float
f0, f1=0.2;/*定义全程变量, 在初始化时f0被清零, f1被赋0.2*/ <BR>
main() <BR> {
<BR> static int
i=10, j; /*定义静态局部变量, 初始化时i被赋10, j不确
<BR>&n<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0038)http://www.hjflying.8u8.com/cl/036.htm -->
<HTML><HEAD><TITLE>王大刚-->C语言编程宝典-->S</TITLE>
<META http-equiv=Content-Type content="text/html; charset=GB2312">
<META content="王大刚 C语言编程宝典 S" name=keywords>
<META content="王大刚 - C语言编程宝典 - S" name=description>
<STYLE>#page {
LEFT: 0px; POSITION: absolute; TOP: 0px
}
.tt3 {
FONT: 9pt/12pt "宋体"
}
.tt2 {
FONT: 12pt/15pt "宋体"
}
A {
TEXT-DECORATION: none
}
A:hover {
COLOR: blue; TEXT-DECORATION: underline
}
</STYLE>
<META content="MSHTML 6.00.2600.0" name=GENERATOR></HEAD>
<BODY text=#000000 vLink=#006699 aLink=#9900ff link=#006699 bgColor=#ffffff
leftMargin=3 topMargin=3 marginwidth="3" marginheight="3">
<TABLE cellSpacing=0 cellPadding=10 width="100%" border=0>
<TBODY>
<TR>
<TD class=tt3 vAlign=top width="8%" bgColor=#e0e0e0><STRONG><A
href="http://www.hjflying.8u8.com/cl/037.htm">后一页</A><BR><A
href="http://www.hjflying.8u8.com/cl/035.htm">前一页</A><BR><A
href="http://www.hjflying.8u8.com/cl/index.html">回目录</A><BR><A
href="http://www.hjflying.8u8.com/index.htm">回首页</A><BR></STRONG></TD>
<TD class=tt2 width="84%" bgColor=#f5f8f8>
<CENTER><B><FONT style="FONT-SIZE: 16.5pt" face=楷体_GB2312
color=#ff6666>S</FONT></B></CENTER>
<HR width="94%" color=#ee9b73 SIZE=1>
<P>函数名: sbrk <BR>功 能: 改变数据段空间位置 <BR>用 法: char *sbrk(int incr);
<BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <alloc.h> <BR>
<P>int main(void) <BR>{ <BR> printf("Changing allocation with
sbrk()\n"); <BR> printf("Before sbrk() call: %lu bytes
free\n", <BR> (unsigned long) coreleft()); <BR>
sbrk(1000); <BR> printf(" After sbrk() call: %lu bytes
free\n", <BR> (unsigned long) coreleft()); <BR>
return 0; <BR>} <BR> <BR> <BR>
<P>函数名: scanf <BR>功 能: 执行格式化输入 <BR>用 法: int scanf(char
*format[,argument,...]); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <conio.h> <BR>
<P>int main(void) <BR>{ <BR> char label[20]; <BR>
char name[20]; <BR> int entries = 0; <BR> int
loop, age; <BR> double salary; <BR>
<P> struct Entry_struct <BR> {
<BR> char name[20];
<BR> int age;
<BR> float salary; <BR> }
entry[20]; <BR>
<P>/* Input a label as a string of characters restricting to 20 characters
*/ <BR> printf("\n\nPlease enter a label for the chart: ");
<BR> scanf("%20s", label); <BR>
fflush(stdin); /* flush the input stream in case of bad input */
<BR>
<P>/* Input number of entries as an integer */ <BR>
printf("How many entries will there be? (less than 20) ");
<BR> scanf("%d", &entries); <BR>
fflush(stdin); /* flush the input stream in case of bad input
*/ <BR>
<P>/* input a name restricting input to only letters upper or lower case
*/ <BR> for (loop=0;loop<entries;++loop) <BR> {
<BR> printf("Entry %d\n", loop);
<BR> printf(" Name : ");
<BR> scanf("%[A-Za-z]", entry[loop].name);
<BR> fflush(stdin); /* flush the input
stream in case of bad input */ <BR>
<P>/* input an age as an integer */ <BR>
printf(" Age : ");
<BR> scanf("%d", &entry[loop].age);
<BR> fflush(stdin); /* flush the input
stream in case of bad input */ <BR>
<P>/* input a salary as a float */ <BR>
printf(" Salary : "); <BR> scanf("%f",
&entry[loop].salary); <BR>
fflush(stdin); /* flush the input stream in case of bad input */
<BR> } <BR>
<P>/* Input a name, age and salary as a string, integer, and double */
<BR> printf("\nPlease enter your name, age and salary\n");
<BR> scanf("%20s %d %lf", name, &age, &salary);
<BR> <BR>
<P>/* Print out the data that was input */ <BR>
printf("\n\nTable %s\n",label); <BR> printf("Compiled by
%s age %d $%15.2lf\n", name, age, salary); <BR>
printf("-----------------------------------------------------\n");
<BR> for (loop=0;loop<entries;++loop)
<BR> printf("%4d | %-20s | %5d | %15.2lf\n",
<BR> loop + 1, <BR>
entry[loop].name, <BR> entry[loop].age,
<BR> entry[loop].salary);
<BR>
printf("-----------------------------------------------------\n");
<BR> return 0; <BR>} <BR> <BR> <BR>
<P>函数名: searchpath <BR>功 能: 搜索DOS路径 <BR>用 法: char
*searchpath(char *filename); <BR>程序例: <BR>
<P>#include <stdio.h> <BR>#include <dir.h> <BR>
<P>int main(void) <BR>{ <BR> char *p; <BR>
<P> /* Looks for TLINK and returns a pointer
<BR> to the path */ <BR> p
= searchpath("TLINK.EXE"); <BR> printf("Search for TLINK.EXE :
%s\n", p); <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -