2016-10-12 2 views
0

이 코드 블록을 우연히 만났는데 무엇을 이해하고 싶습니다. args[0][0]-'!'을 의미합니까?나는 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

+0

args [0] [0]은 args 배열의 첫 번째 문자열의 첫 번째 문자이므로 '!'에 대한 아스키 코드를 뺍니다. 그것으로부터 – bruceg

+0

그것은 닮았을 것입니다 - ASCII가 무엇인지에 대한 질문입니까? –

+0

첫 번째 인수'argv [0]'은 일반적으로 실행 파일 이름 자체입니다. 그러나 AFAIK'exec **'함수는 어떤 프로그램 인수도 전달하지 않으므로 이러한 사용법이 될 수 있습니다. –

답변

3

argschar**, 또는 다른 말로하면, 문자열의 배열 (문자 배열)한다. 그래서 : 즉

args[0]    // first string in args 
args[0][0]   // first character of first string in args 
args[0][0]-'!'  // value of subtracting the character value of ! from 
        // the first character in the first string in args 
args[0][0]-'!' == 0 // is said difference equal to zero 

, 그것은 검사 경우 ! 문자 args 시작의 첫 번째 문자열.

이 (틀림없이한다)는

args[0][0] == '!' 

뿐만 아니라 같이 다시 작성할 수 있습니다 (하지만이 하나를 사용하지 않음) :

**args == '!' 
3

'!'는 숫자에 불과 텍스트 표현입니다 느낌표를 지정된 인코딩으로 인코딩하는 값입니다. args[0][0]은 배열의 첫 번째 요소의 첫 번째 문자입니다.

그래서 x - y == 0? 코드가 args[0][0] == '!'과 같도록 x == y 일 때 다른 쪽에서 y을 움직입니다.

예제에서 빼기로 등가를 표현할 실질적인 이유는 없습니다.

관련 문제