我回答问题stricto sensu,如果 x 和 y 的值未知,这是其他问题
感谢int x = 5, y = 4;这一行,x和y的值是已知的,代码可以简化为
printf("not ok");
gcc 知道,有(我将“ok”替换为“is ok”以不允许使用“not ok”+4 来获得“ok”):
#include <stdio.h>
int main()
{
int x = 5, y = 4;
if(x == 1){
if(y == 1)
printf("is ok");
else
printf("not ok");
}
else if(x == 2){
if((y == 1) || (y == 2))
printf("is ok");
else
printf("not ok");
}
else{
if((y == 1) || (y == 2) || (y == 3))
printf("is ok");
else
printf("not ok");
}
return 0;
}
编译生成汇编器:
pi@raspberrypi:/tmp $ gcc -S -O2 c.c
pi@raspberrypi:/tmp $ more c.s
.arch armv6
.eabi_attribute 28, 1
.eabi_attribute 20, 1
.eabi_attribute 21, 1
.eabi_attribute 23, 3
.eabi_attribute 24, 1
.eabi_attribute 25, 1
.eabi_attribute 26, 2
.eabi_attribute 30, 2
.eabi_attribute 34, 1
.eabi_attribute 18, 4
.file "c.c"
.section .text.startup,"ax",%progbits
.align 2
.global main
.syntax unified
.arm
.fpu vfp
.type main, %function
main:
@ args = 0, pretend = 0, frame = 0
@ frame_needed = 0, uses_anonymous_args = 0
push {r4, lr}
ldr r0, .L3
bl printf
mov r0, #0
pop {r4, pc}
.L4:
.align 2
.L3:
.word .LC0
.size main, .-main
.section .rodata.str1.4,"aMS",%progbits,1
.align 2
.LC0:
.ascii "not ok\000"
.ident "GCC: (Raspbian 6.3.0-18+rpi1+deb9u1) 6.3.0 20170516"
.section .note.GNU-stack,"",%progbits
pi@raspberrypi:/tmp $ grep ok c.s
.ascii "not ok\000"
main 中没有测试或等效项,并且字符串“is ok”不在生成的代码中