Code:#include <stdio.h>
#include <conio.h>
main(void)
{
char mess[80];
int i;
while(1){
printf("Mensaje:");
gets(mess);
if(mess=="salir") break;
}
return 0;
}
Printable View
Code:#include <stdio.h>
#include <conio.h>
main(void)
{
char mess[80];
int i;
while(1){
printf("Mensaje:");
gets(mess);
if(mess=="salir") break;
}
return 0;
}
>if(mess=="salir") break;
Is wrong. You are comparing the addresses and not the contents at those addresses. Try this instead:
Never use gets() [unless you are confident about no buffer overflow]. To learn why, search the forums.Code:if(strcmp(mess, "salir") == 0) break;
isn't main suppose to be int main?
What do you mean i'm compairing the addresses??
if(mess=="salir") is equal to if(&mess[0]=="salir")?.
Please explain!!
isn't main suppose to be int main?
--------
no, because all names and variables is all with the "label" int before them.
It's standard in the compilers understanding of the language.
It only if you need something else like char, float, double etc, that you need to call the varibles with char, float etc.
you cannot compare strings like that you have to use strcmp.
>It's standard in the compilers understanding of the language.
Not anymore. The new C standard removed the implicit int feature, so you'd best get into the habit of putting int in front of your identifiers before the new standard is fully adopted into compilers.
>What do you mean i'm compairing the addresses??
The name of an array is a pointer to the address of its first element. Likewise, a string literal is a pointer to the memory location where it begins. So
if(mess=="salir")
is equivalent to
if(0x1FA==0x2AC) /* These addresses are fake by the way */
Since the addresses are not the same, the test will fail. That's why you need to use strcmp, which compares the actual values of both strings instead of the addresses.
-Prelude
Please do not confuse people with such posts .Quote:
Originally posted by Gugge
isn't main suppose to be int main?
--------
no, because all names and variables is all with the "label" int before them.
It's standard in the compilers understanding of the language.
It only if you need something else like char, float, double etc, that you need to call the varibles with char, float etc.