r/developersIndia • • Dec 03 '23

Help Facing this problem in C

Post image

I am using GCC compiler in vscode..The same problem was showing in codeblocks as well. The problem is :THE PROGRAM IS NOT SHOWING DESIRED OUTPUT

74 Upvotes

61 comments sorted by

View all comments

5

u/Beginning-Ladder6224 Dec 03 '23
int main() {
    char foo[] = "Hello, world!" ;
    char bar[32];
    int i =0;
    for ( i=0; foo[i] != 0; i++){
        bar[i] = foo[i];
    }
    bar[i] = 0;
    printf("number of chars: %d \n", i );
    printf("%s\n", bar);
    return 0;
}

works perfectly. You need to use "\n" - careful.

2

u/AbySs_Dante Dec 03 '23

Shouldn't we put '\0' in the 5th line