output:Code:#include <stdio.h>
int main() {
char* string="ape";
int i=0;
for(;string[i]!='\0';i++) {
if (string[++i]=='\0') printf("here");
}
}
herehere
Can someone explain what's going on here? Thanks.
Printable View
output:Code:#include <stdio.h>
int main() {
char* string="ape";
int i=0;
for(;string[i]!='\0';i++) {
if (string[++i]=='\0') printf("here");
}
}
herehere
Can someone explain what's going on here? Thanks.
changing the value of i is not a good idea, since the loop does increment i as well and that will result in a problem: an out of bounds array access.Code:if (string[++i]=='\0') printf("here");
Just walk around the loop a few times.
The behaviour is undefined as soon as i reaches 4 and string[i] is accessed. Which will happen, since i is being incremented twice for each execution of the loop body.
string is pointer its not an array..so in string storing only the starting address of "appe". so it will not get null at the end..
string[i]=*(string+i);
in this string contains the starting address, so starting address+i and taking value at that address it may be garbage value..that's why loop is infinity..i think if anything wrong forgive me..
Lets talk about lines 5 and 6 for a minute... this is a very broken way to initialize a for loop. I've seen it multiple times over the last few weeks from various people... so it's likely someone out there is teaching people to do this... and it's WRONG
Here's why it's wrong...
As you can see from the loops this should print 25 stars, right? In fact it prints 5 ... why?Code:#include <stdio.h>
int main (void)
{
int x = 0;
int y = 0;
for(;x < 5; x++)
for (; y<5; y++)
printf("* ");
return 0;
}
Because the 2nd through 5th times through the x loop, y is already 5 and the inner loop doesn't run.
You must ALWAYS initialize a for loop inside the for() statement, that is why they made it that way.
Yes it gets the null at the end.
Type this up and try it...
Code:#include <stdio.h>
int main (void)
{
char *test = "This is a string";
printf("%s",test);
return 0;
}
printf() stops at the null character. If it wasn't there your screen would fill with garbage... so yes literal strings are nul terminated.
yes i will do..string[i] works?? is it gave any garbage value..in the above output will be this is a string..
i tried the about program and the output is just "here"....may be you have ran the program twice....if i am wrong sorry!Code:#include<stdio.h>
#include<conio.h>
void main()
{
int *string="ape";int i;
for(i=0;string[i]!='\0';i++)
{
if(string[++i]=='\0'
printf("here");
}
getch();
}
Welcome to the forum, RamPrasath! :cool:
He's brand new, already posting code between the code tags!
Sweet! ;)
Not quite the case, Ram. As I noted in my previous post, the behaviour of the code is undefined, because it is running off the end of the array named string. The word "undefined" has a particular meaning in the C standard, and essentially (in this case) means the code is allowed to do anything according to the standard. Practically, that often means the results can vary between compilers (printing "here" once, multiple times, crashing after printing "here" once, etc).
This is one of those cases where results of running a program are inconclusive.
I think i is a global variable. When i =0 , the loop does nothing.Code:
#include <stdio.h>
int main() {
char* string="ape";
int i=0;
for(;string[i]!='\0';i++) {
if (string[++i]=='\0') printf("here");
}
}
I is local to the function, declared in the line directly above the for() statment.
But, As I already explained... you should not initialize a for() statement that way, ever... for() provides a built in initializer step, use it. If you don't there will be situations where this bad practice will cause you trouble...
Looks like someone, somewhere grew up on C99/C++ and then fell foul of a non-C99 compiler, where you have to have the declaration at the beginning of the function, and decided the best method was to move the whole initialisation.
I have to agree that it's horrendously dangerous. If you want to do that, just move the *type* declaration, not the initialisation.
But then, they're also incrementing the loop variable from inside for loops without proper thought too.Code:int x;
for(x = 0; x < 5; x++)
........
To the OP: Your program "works" but only by chance depending on the compiler / system you run it on and even what time of day or what program you last run (if there's something in memory after your string or not). That's not good programming practice and means that although your program works FOR YOU, it doesn't necessarily work for anyone else (even if they have a near-identical computer to yourself).
A program that small can be stepped through on paper quite simply and overflows like this avoided. Avoid this one while it's simple and the later ones where it takes out the machine or stops 100,000 lines of code from working won't be a problem. Ignore the problem and you'll never be able to write a program that reliably runs on anyone else's computer.