i want to be able to print out multiple values from an array without having to re-write the char name and array number multiple times, is this possible? example...
char letters[] = {'a','b','c','d'};
printf("%c%c%c%c", letters[0], letters[1], letters[2]);i want to make that code cleaner and easier to use/understand. I thought i'd be able to use
printf("%c", letters[0][1][2]);or seperated by commas, but that doesn't work either. Any suggestions?
How to print a char array in C through printf? - Stack Overflow
C: Print char array as string
Printing Char Arrays in C - Stack Overflow
How to print the content of char array?
Videos
You declare an array of characters like so:
char foo[size];
You seem to have it mixed up with char *, which is a pointer to a character. You could say
char *bar = foo;
which would make bar point to the contents of foo. (Or, actually, to the first character of foo.)
To then print the contents of the array, you can do one of the following:
// either print directly from foo:
printf("%s", foo);
// or print through bar:
printf("%s", bar);
Note, however, that C performs no initialization of the contents of variables, so unless you specifically set the contents to something, you'll get garbage. In addition, if that garbage doesn't happen to contain a \0; that is, a char with value 0, it will keep on outputting past the end of the array.
Your array is not initialized, and also you have an array of pointers, instead of an array of char's. It should be char* array = (char*)malloc(sizeof(char)*size);, if you want an array of char's. Now you have a pointer to the first element of the array.
The code posted is incorrect: a_static and b_static should be defined as arrays.
There are two ways to correct the code:
you can add null terminators to make these arrays proper C strings:
#include <stdio.h> int main(void) { char a_static[] = { 'q', 'w', 'e', 'r', '\0' }; char b_static[] = { 'a', 's', 'd', 'f', '\0' }; printf("value of a_static: %s\n", a_static); printf("value of b_static: %s\n", b_static); return 0; }Alternately,
printfcan print the contents of an array that is not null terminated using the precision field:#include <stdio.h> int main(void) { char a_static[] = { 'q', 'w', 'e', 'r' }; char b_static[] = { 'a', 's', 'd', 'f' }; printf("value of a_static: %.4s\n", a_static); printf("value of b_static: %.*s\n", (int)sizeof(b_static), b_static); return 0; }The precision given after the
.specifies the maximum number of characters to output from the string. It can be given as a decimal number or as*and provided as anintargument before thecharpointer.
This results in segmentation fault. ? because of the below statement
char a_static = {'q', 'w', 'e', 'r'};
a_static should be char array to hold multiple characters. make it like
char a_static[] = {'q', 'w', 'e', 'r','\0'}; /* add null terminator at end of array */
Similarly for b_static
char b_static[] = {'a', 's', 'd', 'f','\0'};
Don't you need to end your strings with \0 in C? ) For example:
char array[randSize + 1];
for (i=0; i < randSize; i++)
array[i] = 'x';
array[i] = '\0';
(updated this because you indeed probably wanted to get a useful part of string of randSize length).
printf("%s | ", bar);
The %s conversion requires a pointer to a 0-terminated character array, but your array is not 0-terminated. Thus the behaviour is undefined, but what usually happens is that printf continues reading and printing characters until it finds a 0 byte or reaches forbidden memory, causing a segmentation fault, whichever happens first.
You got to understand behavior behind your program. So when you load your program it is given N bytes in memory and that N bytes gets reused many times and not erased. So in first instance your program have loaded some data into spot where s[0] would later reside, where as in second something was loaded where s[2] as well. That is why you are getting different output in these 2 cases.
So to sum it up: Your array is not initiated to 0 or unless you do it your self, it is given memory that have been previously used by the same program. To do it as it was pointed before you have to do this:
char s[10] = " ";
....
One more thing I see is you were not expecting space before a, in C/C++/Java array indexes start at 0. So if you do:
char s[4];
s[1] = 'a';
s[2] = 'b';
s[3] = '\0';
print ("%s", s);
you would probably get:
@ab
@ comes up because there was nothing written by you as programmer in memory spot where s[0] resides.
Note that every time you have string in C you have to terminate it with '\0' character.
The problem is that you didn't initialise your array. In order to get that output you can do it in this way:
char s[10];
for(int i=0;i<10;i++)
{
s[i] = ' ';
}
s[1]='a';
s[6]='4';
for(int i=0;i<10;i++)
printf("%c",s[i]);
But still, the memory address for each letter in this address is different.
Memory address is different but as its array of characters they are sequential. When you pass address of first element and use %s, printf will print all characters starting from given address until it finds '\0'.
Incase of arrays, the base address (i.e. address of the array) is the address of the 1st element in the array. Also the array name acts as a pointer.
Consider a row of houses (each is an element in the array). To identify the row, you only need the 1st house address.You know each house is followed by the next (sequential).Getting the address of the 1st house, will also give you the address of the row.
Incase of string literals(character arrays defined at declaration), they are automatically
appended by \0.
printf prints using the format specifier and the address provided. Since, you use %s
it prints from the 1st address (incrementing the pointer using arithmetic) until '\0'
When you use this line.
printf("%s\n", s[0]);
The compiler should print some warning about mismatch of the format string %s and the corresponding argument, s[0].
The type of s[0] is char, not char*.
What's your intention?
If you want to print just one character, use:
printf("%c\n", s[0]);
If you want to print the entire array of chracters, use:
printf("%s\n", s);
You need to replace below line
printf("%s\n", s[0]);
with
printf("%c\n", s[0]);
to print 1 character.
To print all characters 1 by 1, use a loop.