Consider the code:
main()
{
char s[8] = “Exforsys”;
}
The above code is not allowed in string handling concept. This is because strings are represented in C programming language as arrays and therefore assigning as above arrays is not allowed.
One can make use of strcpy() which is a string handling function present in string.h header file for this purpose. So the above code can be written as
#include
main()
{
char s[];
strcpy(s, ”Exforsys”);
}
So one must not assign strings which are represented as arrays by using operator ‘=’. Instead C programmers must use strcpy() function for this purpose.
bairaviuthra wrote on Friday, 22 June 2007 01:11:58:
Why not? we can define values to a char array in C.
Consider and invoke the below ex:
#includemain()
{
char s1[10] = "Hello";
printf("ns1[1]==%cn",s1[1]);//prints e
printf("ns1==%cn",s1);//may print some junk / no values
}
Why is assigning to string as given below not allowed?
Consider the code:
The above code is not allowed in string handling concept. This is because strings are represented in C programming language as arrays and therefore assigning as above arrays is not allowed.
One can make use of strcpy() which is a string handling function present in string.h header file for this purpose. So the above code can be written as
So one must not assign strings which are represented as arrays by using operator ‘=’. Instead C programmers must use strcpy() function for this purpose.
bairaviuthra wrote on Friday, 22 June 2007 01:11:58:
Why not? we can define values to a char array in C.
Consider and invoke the below ex:
#include
main()
{
char s1[10] = "Hello";
printf("ns1[1]==%cn",s1[1]);//prints e
printf("ns1==%cn",s1);//may print some junk / no values
}
Profile Answers by GeekAdmin Questions by GeekAdmin
Questions by GeekAdmin answers by GeekAdmin