---
# System prepended metadata

title: 一個陣列宣告的範例

---

# 一個陣列宣告的範例

```c
#include <stdio.h>

int main() {
    int x, y;

    scanf("%d %d", &x, &y);

    int aa = 1;
    int bb = 2;
    int b[x];
    int h[y];
    int c = 3;
    
    scanf("%d", &c);
    int i;
    for(i=0; i<x; i++) {
        b[i] = i;
    }
    for(i=0; i<y; i++) {
        h[i] = y-i; 
    }
    

    printf("%d %d %d\n", b[x-1], b[x-2], b[x-3]);
    printf("%d %d %d\n", h[y-1], h[y-2], h[y-3]);
    printf("%d\n", c);


    return 0;
}
```

## 編譯

```shell=
riscv64-unknown-elf-gcc -O2 -S test.c
```

## 對應的Assembly

```
	.file	"test.c"
	.option nopic
	.text
	.section	.text.startup,"ax",@progbits
	.align	2
	.globl	main
	.type	main, @function
main:
	addi	sp,sp,-48
	sd	ra,40(sp)
	sd	s0,32(sp)
	sd	s1,24(sp)
	addi	s0,sp,48
	sd	s2,16(sp)
	lui	a0,%hi(.LC0)
	addi	a2,s0,-40
	addi	a1,s0,-44
	addi	a0,a0,%lo(.LC0)
	call	scanf
	lw	a4,-44(s0)
	lw	a5,-40(s0)
	lui	a0,%hi(.LC1)
	slli	a4,a4,2
	addi	a4,a4,15
	slli	a5,a5,2
	andi	a4,a4,-16
	addi	a5,a5,15
	sub	sp,sp,a4
	andi	a5,a5,-16
	mv	s2,sp
	addi	a1,s0,-36
	sub	sp,sp,a5
	addi	a0,a0,%lo(.LC1)
	li	a5,3
	sw	a5,-36(s0)
	call	scanf
	lw	a3,-44(s0)
	mv	s1,sp
	blez	a3,.L2
	mv	a4,s2
	li	a5,0
.L3:
	sw	a5,0(a4)
	addiw	a5,a5,1
	addi	a4,a4,4
	bne	a5,a3,.L3
.L2:
	lw	a5,-40(s0)
	blez	a5,.L4
	sext.w	a5,a5
	mv	a4,s1
.L5:
	sw	a5,0(a4)
	addiw	a5,a5,-1
	addi	a4,a4,4
	bnez	a5,.L5
.L4:
	slli	a3,a3,2
	add	a5,s2,a3
	lw	a3,-12(a5)
	lw	a2,-8(a5)
	lw	a1,-4(a5)
	lui	s2,%hi(.LC2)
	addi	a0,s2,%lo(.LC2)
	call	printf
	lw	a5,-40(s0)
	addi	a0,s2,%lo(.LC2)
	slli	a5,a5,2
	add	a5,s1,a5
	lw	a1,-4(a5)
	lw	a3,-12(a5)
	lw	a2,-8(a5)
	call	printf
	lw	a1,-36(s0)
	lui	a0,%hi(.LC3)
	addi	a0,a0,%lo(.LC3)
	call	printf
	addi	sp,s0,-48
	ld	ra,40(sp)
	li	a0,0
	ld	s0,32(sp)
	ld	s1,24(sp)
	ld	s2,16(sp)
	addi	sp,sp,48
	jr	ra
	.size	main, .-main
	.section	.rodata.str1.8,"aMS",@progbits,1
	.align	3
.LC0:
	.string	"%d %d"
	.zero	2
.LC1:
	.string	"%d"
	.zero	5
.LC2:
	.string	"%d %d %d\n"
	.zero	6
.LC3:
	.string	"%d\n"
	.ident	"GCC: (GNU) 8.2.0"
```