I can write char a[] = "hello world"; , but i can not write
char a[];
a = "hello world";
why is that ??

There are several reasons, but probably not from the K & R.

The assignment is wrong for two reasons. It is to an array of no size, and you have to use strcpy, sprintf, or similar function to assign.

Also, char a[] = "hello world" is handled at compile time, where storage is allocated for that string.

There are several reasons, but probably not from the K & R.

The assignment is wrong for two reasons. It is to an array of no size, and you have to use strcpy, sprintf, or similar function to assign.

Also, char a[] = "hello world" is handled at compile time, where storage is allocated for that string.

1)Sir but if char a[]="hello world" is handled at compile time, then why cant
char a[];
strcpy(a,"hello world"); be handled at compile time ?
2)Moreover, i dont understand the part about compile time;
Say int a;
a=5; is also handled at compile time right ?
So if it can work for integers, then why cant it work for character array ?

Thank you for the response sir, Please reply

The storage for your array is handled at compile time. The a=5 is executed at run time. I suggest getting a copy of the original Kernighan and Ritchie book on C. It's just one of the rules. If you'd state why you want a[], that would help.

The storage for your array is handled at compile time. The a=5 is executed at run time. I suggest getting a copy of the original Kernighan and Ritchie book on C. It's just one of the rules. If you'd state why you want a[], that would help.

Thank you very much for the explanation sir. Yes, i m clear with it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.