Posts

Showing posts with the label Char

C Char Array Initialization

Answer : This is not how you initialize an array, but for: The first declaration: char buf[10] = ""; is equivalent to char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; The second declaration: char buf[10] = " "; is equivalent to char buf[10] = {' ', 0, 0, 0, 0, 0, 0, 0, 0, 0}; The third declaration: char buf[10] = "a"; is equivalent to char buf[10] = {'a', 0, 0, 0, 0, 0, 0, 0, 0, 0}; As you can see, no random content: if there are fewer initializers, the remaining of the array is initialized with 0 . This the case even if the array is declared inside a function. Edit: OP (or an editor) silently changed some of the single quotes in the original question to double quotes at some point after I provided this answer. Your code will result in compiler errors. Your first code fragment: char buf[10] ; buf = '' is doubly illegal. First, in C, there is no such thing as an empty char . You can use double quote...
Answer : Two errors here: first, you're trying to declare arrays[63] for storing 64 elements, as you've probably confused the size of array ( n ) with the maximum possible index value (that's n - 1 ). So it definitely should be litera[64] and liczba[64] . BTW, you have to change this line too - while (i<=64) : otherwise you end up trying to access 65th element. And second, you're trying to fill char value with %s format specifier for scanf, while you should have used %c here. Also, can't help wondering why you declare liczba array as one that stores int s, that initialize it with array of char s. All these '1', '2', etc... literals represent NOT the corresponding digits - but the charcodes for them. I doubt that was your intent.