简体   繁体   中英

Confusion about calculator program

int getop(char s[])
{
    int i = 0, c, next;
    /* Skip whitespace */
    while((s[0] = c = getch()) == ' ' || c == '\t')
        ;
    s[1] = '\0';    
    /* Not a number but may contain a unary minus. */
    if(!isdigit(c) && enter code herec != '.' && c != '-')
        return c;
    if(c == '-')
    {
        next = getch();
        if(!isdigit(next) && next != '.')
           return c;
        c = next;
    }
    else
        c = getch();    

    while(isdigit(s[++i] = c)) //HERE
            c = getch();
    if(c == '.')                     /* Collect fraction part. */
        while(isdigit(s[++i] = c = getch()))
                        ;
    s[i] = '\0';
    if(c != EOF)
        ungetch(c);
    return NUMBER;
};

what if there is no blank space or tab than what value will s[0] will initialize .......& what is the use of s[1]='\\0'

what if there is no blank space or tab than what value will s[0] will intialize

The following loop will continue executing until getch() returns a character that's neither a space nor a tab:

while((s[0] = c = getch()) == ' ' || c == '\t')
    ;

what is the use of s[1]='\\0'

It converts s into a C string of length 1, the only character of which has been read by getch() . The '\\0' is the required NUL-terminator.

while((s[0] = c = getch()) == ' ' || c == '\t')

Read character until its not a tab or space.

s[1] = '\0';

Convert char array s to a proper string in C format (all strings in c must be terminated with a null byte, which is represented by '\\0'.

  1. If there is no space or tab, you're stuck with an infinite loop.

  2. s[1]='\\0' is a way of marking the end so functions like strlen() know when to stop reading through c strings. it's called "Null-Terminating" a string: http://chortle.ccsu.edu/assemblytutorial/Chapter-20/ass20_2.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM