AlldaRage
AlldaRage

Reputation: 121

Parameters in for loop, missing first one?

I don't understand the for loop here. Could someone explain it to me? Normally. This one is missing a first statement and the second one says something different.

/* strcmp: return <0 if s<t, 0 if s==t, >0 if s>t */
int strcmp(char *s, char *t)
{
    for ( ; *s == *t; s++, t++)
        if (*s == '\0')
            return 0;
    return *s - *t;
}

Upvotes: 2

Views: 2080

Answers (6)

Michas
Michas

Reputation: 9428

The for(exp1; exp2; exp3) /* Body */ statement is, more or less, shorthand for this:

exp1;
while (exp2) {
    /* Body */
    exp3;
}

Upvotes: 0

user3629249
user3629249

Reputation: 16540

regarding the 'for' loop the first parameter (an optional parameter) (still needs the ';' separator) (in this case, the variables of interest, 's' and 't', are already initialized and available via the passed in parameters to the function, so the initialization parameter is omitted)

The second parameter is a conditional statement While that condition is 'true', the 'for' loop will continue to be executed. (in this case, while the character pointed to by 's' is the same as the character pointed to by 't')

The third parameter (an optional parameter) (in this case, consisting of two statements separated by a comma ',': the two statements step the pointers 's' and 't' to point to the next char in their respective character arrays.

Upvotes: 0

Kun Ling
Kun Ling

Reputation: 2219

According to C standard $6.8.5.3. For the following statement:

 `for(clause_1;expression-2;expession-3) statement` 

Both clause-1 and expression-3 can be omitted, and expression-2 could be replaced by an non-zero constant.

Compiler's action

To answer your question fundamentally, let's look into how compiler handle your code.

For the for loop stmt in your code

From a compiler's aspect, the loop will finally be transformed into instruction sequence end with condition backward jump instruction as bellow.

   ...
   INST1
   INST2
   COND_JMP LABEL2
LABEL1:
   INST3
   INST4
   INST5
   COND_JMP LABEL1

LABEL2:
   ...

for's clause-1 will be put before the LABEL1, such as INST1 and INST2, therefore it is OK to omit such code. Since developer could also put such operations before the for statement.

expression 3 will be put into the loop body (instruction sequence between LABEL1 and COND_JMP LABEL1), therefore epression 3 code could also be put into the loop body directly in c code, that is why expression 3 could also be omitted.

expression 2 will be the condition used by the COND_JMP instruction, therefore if omitted directly, that means compiler will not insert COND_JMP outside the loop body, this will make for statement meaningless, that is why expression 2 could only be replaced by non-zero constant. When replace by non-zero constant that means always true in C, and then the loop will be an infinite loop.

for the comma of your for stmt expression-3

According to C standard 6.5.17, For he comma expression as following.

  expression, expression-2, expression-3..., expression-last

Here the expression could be an normal expression, or an assignment expression.

For such comma expressions, compiler will generate code which will evaluate all these expressions, and return the type and value of last one as the comma exression's return type and value.

Upvotes: 5

cosmos
cosmos

Reputation: 2303

Any (for) loop has three statements.

  1. Initialization

  2. Condition

  3. Increment

Any of the fields above may be left blank based on requirement

for ( ; *s == *t; s++, t++)

Here

  1. Initialization : There is no initialization

  2. Condition : *s == *t (Till characters of the two strings are equal)

  3. Increment : s++, t++ Increment pointers of both strings.

Upvotes: 1

haccks
haccks

Reputation: 106012

Syntax of for loop is

for ( expression1 ; expression2 ; expression3 ) statement  

Both expression1 and expression3 can be omitted. You can also omit expression2 to make it an infinite loop.

Upvotes: 1

Daniel Daranas
Daniel Daranas

Reputation: 22624

The initialization statement is empty. This means "no initialization action".

Upvotes: 3

Related Questions