I am little confuse about output and error shown by compilation of below programs.
Case 1 In this case I have not fixed the size of array of variable of char. But assign a value during declaration and it works easily. And I have read that we can't use assignment operator in case of array. But here it works. Don't know how.
#include <stdio.h>
int main()
{
char str[] = "programming"; //dynamic size of array
printf("%s", str);
/*It works. Array size is dynamic as well as array bounded variable with char data type is not assignable but it works here. Don't know how? */
}
Case 2 This case is similar as above but value is not assigned during declration of variable and It's not work.
#include <stdio.h>
int main()
{
char str[]; //dynamic size of array
str = "programming";
printf("%s", str);
/* It's not work. Error shown that " definition of variable with array type needs an explicit size or an initializer" */
}
Case 3; This is the real error about I have learned that array type char is not assignable.
#include<stdio.h>
int main()
{
char str[50];//fixed array size
str="programming";
printf("%s",str);
//It's not working.
//compilor output is " array type 'char [50]' is not assignable"
/*I understand it. It's correct error. but I don't understand error in other case.*/
}
Case 4; in this case I have assigned a value to variable with array but It doesn't work.
#include <stdio.h>
int main()
{
char str[50];
str[11] = "programming";
printf("%s", str);
//It's not work.
/*Error by compilor is "assigning to 'char' from incompatible type 'const char [12]'
str[11]="programming" */
}
chararrays, but they may not be assigned to arrays. If you want to copy a string to an array, usestrcpy.