-2

If the maximum capacity of character is 256 how to store character array of 1000? is it possible to declare: char s[1000];

tarun Gupta
  • 11
  • 1
  • 2
  • 2
    Yes, it is possible to declare `char s[1000];`. – MarkWeston Sep 24 '17 at 13:15
  • What do you mean by "maximum capacity"? – Binarus Sep 24 '17 at 13:23
  • 3
    A typical `char` may have 1 of 256 values. The number of elements in an _array_ can range `[1...SIZE_MAX)`. – chux - Reinstate Monica Sep 24 '17 at 13:25
  • @tarun Gupta You probably on confusion between limit app architect and memory storage : when you create an static array, is going to storing on the stack in the PE .data/ .rdata /.bss sections of your binary. A dynamically array when you use new, malloc ... will be allocated directly on the heap. –  Sep 24 '17 at 13:25
  • 3
    The number of values that can be stored in a single character has no bearing on how large an array of characters can be. It isn't as though C uses the first byte of a string to store the length of the string. (Some languages, such as Pascal, do use that sort of trick in some implementations.) – Jonathan Leffler Sep 24 '17 at 14:16

3 Answers3

3

Yes, it is certainly possible.

char s[1000];

You can think of 1000 as the "length" of the array and 256 as the "width". You get an array of 1000 chars. Each char is 8 bits (on the machine you're using, at least), and can therefore store 256 distinct values. (And, actually, it would probably be more appropriate to think of the "width" as being 8, not 256.)

Here is your array, with each box representing one char:

   +---+---+---+---+---+---+---+---+-   -+---+
s: |   |   |   |   |   |   |   |   | ... |   |
   +---+---+---+---+---+---+---+---+-   -+---+
     0   1   2   3   4   5   6   7        999

Or here it is showing the individual bits:

   +---+---+---+---+---+---+---+---+-   -+---+
s: |   |   |   |   |   |   |   |   |     |   | 7
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 6
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 5
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 4
   +---+---+---+---+---+---+---+---+-   -+---+    bit
   |   |   |   |   |   |   |   |   | ... |   | 3  number
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 2
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 1
   +---+---+---+---+---+---+---+---+-   -+---+
   |   |   |   |   |   |   |   |   |     |   | 0
   +---+---+---+---+---+---+---+---+-   -+---+
     0   1   2   3   4   5   6   7        999
                array index

Suppose we put a string in the array, either by calling strcpy:

strcpy(s, "Hello!");

or my initializing it when we declare it:

char s[1000] = "Hello!";

By bytes it looks like this:

   +---+---+---+---+---+---+---+---+-   -+---+
s: | H | e | l | l | o | ! |\0 |   | ... |   |
   +---+---+---+---+---+---+---+---+-   -+---+
     0   1   2   3   4   5   6   7        999

Or by bits it looks like this:

   +---+---+---+---+---+---+---+---+-   -+---+
s: | 0 | 0 | 0 | 0 | 0 | 0 | 0 |   |     |   | 7
   +---+---+---+---+---+---+---+---+-   -+---+
   | 1 | 1 | 1 | 1 | 1 | 0 | 0 |   |     |   | 6
   +---+---+---+---+---+---+---+---+-   -+---+
   | 0 | 1 | 1 | 1 | 1 | 1 | 0 |   |     |   | 5
   +---+---+---+---+---+---+---+---+-   -+---+
   | 0 | 0 | 0 | 0 | 0 | 0 | 0 |   |     |   | 4
   +---+---+---+---+---+---+---+---+-   -+---+    bit
   | 1 | 0 | 1 | 1 | 1 | 0 | 0 |   | ... |   | 3  number
   +---+---+---+---+---+---+---+---+-   -+---+
   | 0 | 1 | 1 | 1 | 1 | 0 | 0 |   |     |   | 2
   +---+---+---+---+---+---+---+---+-   -+---+
   | 0 | 0 | 0 | 0 | 1 | 0 | 0 |   |     |   | 1
   +---+---+---+---+---+---+---+---+-   -+---+
   | 0 | 1 | 0 | 0 | 1 | 1 | 0 |   |     |   | 0
   +---+---+---+---+---+---+---+---+-   -+---+
     0   1   2   3   4   5   6   7        999
                array index

And there are 993 spaces in the array left over.

[P.S. to nitpickers: Yes, those are ASCII codes, and no, character encoding is not specified by the C Standard. But I think we can safely assume that those are the codes the questioner would see.]

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • 1
    Nitpicking: how can you be *sure* that the OP is using ASCII and not e.g. UTF-8 ? I guess -from his name or pseudo- he might not be American so is more likely (than US citizens) to use UTF-8 instead of the obsolete ASCII. See http://utf8everywhere.org/ ; in 2017 ASCII is not very common and most computers are using UTF-8 – Basile Starynkevitch Sep 24 '17 at 15:15
  • @BasileStarynkevitch When I wrote that I was worried about EBCDIC nitpickers, not UTF-8 nitpickers. And of course the encoding of the example string I chose is identical in both ASCII and UTF-8. But I suppose I'll have to fix that last sentence... done. – Steve Summit Sep 24 '17 at 17:42
2

The 256 is the number of values in a single char (which is often an 8 bits byte, and 256 = 28).

(caveat, the C11 standard allows wider char-s, e.g. of 32 bits; but this is very uncommon)

A string is an array or a memory zone containing several char-s, and conventionally terminated by a zero byte.

You can have very big strings, notably using C dynamic memory allocation. For instance, on some computers

 char*hugestring = malloc(1000000000);

can succeed. Then you could fill that billion-bytes string. On many computers, that malloc call would fail, and you always need to check the result of malloc, at least by following the above line with

if (!hugestring) { perror("malloc hugestring"); exit(EXIT_FAILURE); };

If you use malloc, don't forget to later call free (you need to have conventions about who is responsible for that); otherwise you have a memory leak. BTW the asprintf, strdup and open_memstream functions are very useful (but not available everywhere) to conveniently build dynamically allocated strings (internally malloc is used by them). Tools like valgrind are helpful to help detecting memory leaks.

You can also have arrays. If they are local variables (a.k.a. automatic variables) they generally sit in the call stack (unless the compiler optimized for them).

For example, using snprintf to safely fill a local buffer (without buffer overflow),

char buffer[100];
snprintf(buffer, sizeof(buffer), "x=%d, name=%s", x, name);

but it is unreasonable to have large call frames, so a local array should generally be less than a few hundred bytes (or perhaps a few thousands of them). The entire call stack is generally limited to one or a few megabytes. Details are system specific.

Be aware of character encoding issues. In 2017 read at least utf8everywhere.org and about Unicode.... so think of char as a byte (since some UTF-8 characters need several bytes, so take several char-s to be represented, hence on my Linux desktop strlen("être") is 5 and sizeof("être") is 6 since the accentuated ê letter is UTF-8 encoded in two bytes). You might use some library like libunistring.

Look also into some C reference.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

You seem to misinterpret what you refer to as "capacity" of a char. char is an 8-bit value, which means it can range anywhere from 0000 0000b () to 1111 1111b (255).

This only refers to one individual value. This means you can write char c = 20;, but not char c = 1000;.

As such, this means that there are 256 different possible values for a single char.


Arrays are a different concept: An array stores multiple values of one specific type - such as an array of characters.

To answer you question: Yes, you can store 1000 char values in an array, like char s[1000] as Steve Summit suggested.

Naturally, if you have 1000 chars, this will mean there will be duplicates (since there are only 256 unique characters possible).

MechMK1
  • 3,278
  • 7
  • 37
  • 55