jackall
09-05-2008, 09:14 AM
My acquaintance with any language or coding is almost imaginary. This following code by an eminent personality used in his book, got on my meager imagination and made me feel again my inadequacies in this ground.
The level of my comprehensibility is ridiculed by these five characters “abc\n”.
#include<stdio.h>
#include<string.h>
#define Length 100
#define password "abc\n"
int main()
{
char buff[Length];
int count=0;
for(;;)
{
printf("Enter password\n");
fgets(&buff[0] , Length ,stdin);
if(strcmp(&buff[0], password))
printf("\nWRONG...\n");
else break;
if(++count>2)
return -1;
}
printf("CORRECT");
}
When prompted, i enter the password abc\n, I get WRONG message. If i remove the \n from #define password “abc”, again i get WRONG message no matter whatever the input is?
Of course the code gives CORRECT response when the string “abc” is entered.
Could i get a little explanation on this (#define password “abc\n”) ?
Thank you...
The level of my comprehensibility is ridiculed by these five characters “abc\n”.
#include<stdio.h>
#include<string.h>
#define Length 100
#define password "abc\n"
int main()
{
char buff[Length];
int count=0;
for(;;)
{
printf("Enter password\n");
fgets(&buff[0] , Length ,stdin);
if(strcmp(&buff[0], password))
printf("\nWRONG...\n");
else break;
if(++count>2)
return -1;
}
printf("CORRECT");
}
When prompted, i enter the password abc\n, I get WRONG message. If i remove the \n from #define password “abc”, again i get WRONG message no matter whatever the input is?
Of course the code gives CORRECT response when the string “abc” is entered.
Could i get a little explanation on this (#define password “abc\n”) ?
Thank you...