How to initialize char array in C

2 Answers

0 votes
#include <stdio.h>

void printCharArray(char *p, size_t len) {
    for (size_t i = 0; i < len; ++i) {
        printf("%c ", p[i]);
    }
    printf("\n");
}

int main(){
    char arr[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'e'};

    printCharArray(arr, sizeof arr);

    return 0;
}




/*
run:

a b c d e f g e

*/

 



answered May 5, 2021 by avibootz
0 votes
#include <stdio.h>
 
void printCharArray(char *p, size_t len) {
    for (size_t i = 0; i < len - 1; ++i) {
        printf("%c ", p[i]);
    }
    printf("\n");
}
 
int main(){
    char arr[] = "c programming";

    printCharArray(arr, sizeof arr);
 
    return 0;
}
 
 
 
 
 
/*
run:
 
c   p r o g r a m m i n g 
 
*/

 



answered May 5, 2021 by avibootz
edited Dec 25, 2021 by avibootz

Related questions

1 answer 123 views
2 answers 165 views
165 views asked May 5, 2021 by avibootz
1 answer 176 views
1 answer 190 views
1 answer 182 views
182 views asked Aug 23, 2016 by avibootz
1 answer 80 views
1 answer 118 views
...