why would it not allow me to store multiple chars in the same varible if it can S is defined.. say 20? |
Perhaps you misunderstand what an array is. When you create an array like this
char var2[S];
you get an amount of space that will fit S
char.
To put something in the first space, use var2[0]
If you then put something else in the
exact same space, of course you write over what was originally there. If you understand why this doesn't work:
1 2 3
|
int x;
x=4;
x = 5; // Hey, where did the 4 go? Why can't I put two different int values into one variable?
|
then you understand why this also doesn't work
1 2 3 4 5
|
scanf( "%c", &var);
var2[S-1]=var;
printf("%c", var2[S-1]);
scanf( "%c", &var);
printf("%c", var2[S-1]); // Hey, where did the first value go? Why can't I put two different values into one variable?
|
If you want to store another char in the array, put it into a different space. For example,
1 2
|
var[0] = 'a'; // Store one value in the first space...
var[1] = 'b'; // and store another value in a different space
|
but now I have no idea on how to list the arrays. |
What arrays? There is only one array. You made it like this:
char var[S];
var is an array (just one array). It is the exact same thing as S char variables all next to each other in memory.
What do you mean "list the arrays"? Do you mean you want to output the values? You can do that like this:
1 2 3 4 5
|
printf("%s", var[0]);
printf("%s", var[1]);
printf("%s", var[2]);
printf("%s", var[3]);
printf("%s", var[4]);
|
I see that you already understand how to make a
for loop. Perhaps you'd like to use a
for loop.
1 2 3 4
|
for(i=0; i<5;i++)
{
printf("%s", var[i]);
}
|