我偶然发现了这段代码,我想了解什么
args[0][0]-'!'
是什么意思?
else if (args[0][0]-'!' ==0)
{ int x = args[0][1]- '0';
int z = args[0][2]- '0';
if(x>count) //second letter check
{
printf("\nNo Such Command in the history\n");
strcpy(inputBuffer,"Wrong command");
}
else if (z!=-48) //third letter check
{
printf("\nNo Such Command in the history. Enter <=!9 (buffer size is 10 along with current command)\n");
strcpy(inputBuffer,"Wrong command");
}
else
{
if(x==-15)//Checking for '!!',ascii value of '!' is 33.
{ strcpy(inputBuffer,history[0]); // this will be your 10 th(last) command
}
else if(x==0) //Checking for '!0'
{ printf("Enter proper command");
strcpy(inputBuffer,"Wrong command");
}
else if(x>=1) //Checking for '!n', n >=1
{
strcpy(inputBuffer,history[count-x]);
}
}
此代码来自此 github 帐户:https://github.com/deepakavs/Unix-shell-and-history-feature-C/blob/master/shell2.c
最佳答案
'!'
只是一个数值的文本表示,它以指定的编码对感叹号进行编码。 args[0][0]
是数组第一个元素的第一个字符。
那么当 x - y == 0
时?当 x == y
时,将 y
移到另一边,这样代码就等同于 args[0][0] == '!'
.
不过,我看不出有任何实际理由将等价性表示为示例中的减法。
关于c - 我不明白 args[0][0] -'!' 是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40008060/