0
#include <stdio.h>
int main(void){
    char c1[], c2[];
    c1 = "hello";
    c2 = "world";
    printf("%s %s", c1, c2);
    return 0;
}

What exactly makes it that I cannot use 'char c1[], c2[]'? I have some knowledge in Java and I find C syntax to be rather familiar with it but clearly somethings do not work.

Is there also any reason why char is declared as char variable[] instead of char[] variable? It seems to make more sense like this (Java notation)

7
  • I never use c for long time but if I remember array must defined length Commented May 4, 2016 at 4:05
  • you write c[2] = 'x'; and not [2]c = 'x';, so you write char c[3]; and not char[3] c; . Commented May 4, 2016 at 4:11
  • char c1[] - so how many characters do you expect c1 to hold? In C, this makes c1 an array, not a reference to an array like it does in Java. Commented May 4, 2016 at 4:22
  • 1
    Just forget about Java and study a beginner-level book in C. Commented May 4, 2016 at 6:22
  • regarding this kind of expression: char c1[], An array in C, must be either declared with a specific length or initialized with a char string. I.E. either: char c1[20]; or char c1[] = "hello ";. Strings can only be copied char by char or using a function like: strcpy() Only at variable initialization can a char array be set using =. Commented May 7, 2016 at 1:56

2 Answers 2

6

This:

char c1[]

Means "An array of characters with unknown length." You can't create arrays of unknown length, though you can take them as parameters to a function, as in:

void foo(char c1[])

You can also create them if the length is known implicitly, as in:

char c1[] = "hello"; // size is 6, including null terminator

And as for this:

Is there also any reason why char is declared as char variable[] instead of char[] variable?

It has always been like this. I'm sure C programmers feel the Java way doesn't make sense too. Decisions made in language syntax 40 years ago are not usefully debated today.

Sign up to request clarification or add additional context in comments.

4 Comments

But why is it in the case that char c1[] = "hello"; it is able to figure it out on its own whilst if you have more than one character array it suddenly needs the direct length? What if I wanted to input a giant string of text that I didnt know the length for?
Specifically, it says "make me an array of unknown length on the stack". Which makes no sense. In Java it means "make me a pointer to an array on the stack", this is because of Java's concept of 'value type' and 'reference type'.
I dont understand... in the code above if I removed c2 altogether the code works fine... yet I didnt declare the size of the array. Why does this work?
Technically OP's array would gain an implicit size in the first assignment. Hypothetically the compiler could attempt to find a first assignment, so I think a little bit more needs to be explained. "hello" is known at compile time after all.
1

That code precisely speaking is not declarations, it's definitions. And in C, you cannot define arrays leaving the length empty, you can either specify it explicitly:

char c1[6] = "hello";

or you can do it implicitly:

char c1[] = "hello";

If the length of the array is only known in running time, use allocated arrays:

size_t n = <calculate the size needed>;
char *c1 = malloc(n);

But for declarations, you can do external declarations like this:

extern char c1[];

13 Comments

So let us say id want to declare a string array that contains well over 1000 characters, how would I know what to input?
@Wjk just do char c[1000];?
@Wjk If the size is not known at developing time and it is calculated in runtime, you need to use dynamic allocation: size_t n = get_size(); char *c1 = malloc(n);
Or you could simply do char c1[n].
@Lundin Yes, that will also do.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.