声明和初始化
float length; /* declares the variable "length"
as a floating number */
length = 20.3; /* initialize "length" by giving
it the value 20.3 */
{
int a = 1; /* a gets the value 1 */
int b = 2; /* b gets the value 2 */
{
int c = a + b; /* a has the value 1, b has the value 2,
c gets the value 3 */
a = 5; /* a gets the value 5 */
int d = a + b; /* a has the value 5, b has the value 2,
d gets the value 7 */
}
}
外部变量
External variables
File 1:
int a = 2; /* declares and initialize a variable a */
File 2:
extern int a; /* explicit declaration of a */
int main(){
printf("%d\n",a); /* a can now be used as a
global variable in File 2 */
}
enum color{
red; /* red is given the value 0 by default */
blue; /* blue gets the value 1 */
green; /* green gets the value 2 */
yellow = 5; /* yellow gets the specified value 5 */
};
int32_t A [3]; /* declares an array of size 3 */
A[0] = 5; /* initialize the three elements
A[1] = 7; of the array. Could also have written
A[2] = 1; int32_t A = {5,7,1}; */
A[2]; /* gets access to the second value of A */
char message [100] = "Hello World!";
/* allocates memory to 100 data characters. Remember to have
enough space for the null character! */
struct data{
int x;
int A[3];
char y;
};
struct data a; /* declares a new variable
of type data */
a.x = 3;
a.A[0]= 1; /* initialize the three member
a.A[1]= 3; variables of a */
a.A[2]= 4;
a.y = 'B';
typedef float sec;
sec time; /*declares a variable time of type sec */
这样我们就可以用sec作为一个变量类型了。
或如下结构体类型,定义了一个类型名为data的类型的变量a.
typedef struct{
int x;
int A[3];
char y;
} data;
data a; /* declares a new variable of type data */
int a; /* BSS */
int b = 3; /* DATA */
int main(){
int i; /* Stack */
char *ptr = malloc(7); /* The char pointer is put on the stack
and the allocated memory is put on
the heap.*/
}