K
karthikbalaguru
Hi,
I came across a strange behaviour of 'For Loop'.
Here, to make it clear, I have listed the
strange behaviour in 'Strange_1' and the modified one
that gives the desired output as 'Strange_2'.
In 'Strange_1', the strlen(p1) gives the correct value of
10, but the for loop does not execute for 10 times.
It comes out of 'for loop' after 5th time . But, it was
supposed to come after 10 times.
Strange_1 :
----------------
#include<stdio.h>
int main(void)
{
int i,count=0;
char *p1="abcdefghij";
char *p2="alcmenfoip";
printf("len %d \n",strlen(p1));
for(i=0;i<strlen(p1);i++)
{
if(*p1++ == *p2++)
{
count+=5;
}
else
{
count-=3;
}
}
printf("i = %d count = %d\n",i,count);
}
output -
----------
len 10
i = 5 count = 9
I made a small modification and got the desired output.
But, i am not sure how this 'strlen' is affecting the 'for loop'.
Is the method of calling 'strlen' in 'for statement' an
'Undefined Behaviour' ?
I have listed the changes made under the name 'Strange_2'.
Here, i added a new temporary variable 't' and assigned
the strlen(p1) to it. I replaced the strlen(p1) with 't' and it
worked fine.
Strange_2 :
----------------
#include<stdio.h>
int main(void)
{
int i,count=0;
char *p1="abcdefghij";
char *p2="alcmenfoip";
int t;
t = strlen(p1);
printf("len %d \n",strlen(p1));
for(i=0;i<t;i++)
{
if(*p1++ == *p2++)
{
count+=5;
}
else
{
count-=3;
}
}
printf("i = %d count = %d\n",i,count);
}
output -
------------
len 10
i = 10 count = 2
Any ideas ?
Thx in advans,
Karthik Balaguru
I came across a strange behaviour of 'For Loop'.
Here, to make it clear, I have listed the
strange behaviour in 'Strange_1' and the modified one
that gives the desired output as 'Strange_2'.
In 'Strange_1', the strlen(p1) gives the correct value of
10, but the for loop does not execute for 10 times.
It comes out of 'for loop' after 5th time . But, it was
supposed to come after 10 times.
Strange_1 :
----------------
#include<stdio.h>
int main(void)
{
int i,count=0;
char *p1="abcdefghij";
char *p2="alcmenfoip";
printf("len %d \n",strlen(p1));
for(i=0;i<strlen(p1);i++)
{
if(*p1++ == *p2++)
{
count+=5;
}
else
{
count-=3;
}
}
printf("i = %d count = %d\n",i,count);
}
output -
----------
len 10
i = 5 count = 9
I made a small modification and got the desired output.
But, i am not sure how this 'strlen' is affecting the 'for loop'.
Is the method of calling 'strlen' in 'for statement' an
'Undefined Behaviour' ?
I have listed the changes made under the name 'Strange_2'.
Here, i added a new temporary variable 't' and assigned
the strlen(p1) to it. I replaced the strlen(p1) with 't' and it
worked fine.
Strange_2 :
----------------
#include<stdio.h>
int main(void)
{
int i,count=0;
char *p1="abcdefghij";
char *p2="alcmenfoip";
int t;
t = strlen(p1);
printf("len %d \n",strlen(p1));
for(i=0;i<t;i++)
{
if(*p1++ == *p2++)
{
count+=5;
}
else
{
count-=3;
}
}
printf("i = %d count = %d\n",i,count);
}
output -
------------
len 10
i = 10 count = 2
Any ideas ?
Thx in advans,
Karthik Balaguru