i am a beginner to coding and learning c language as my first step. when i use string function the program returns with a value without showing output. using minGW as compiler
i tried to add string.h header folder string from below follows code
''''
#include <stdio.h>
#include <conio.h>
int main()
{
    /*
    int strlen(string);
    */
    char name = { 's','i','l','a','m','\0' };
    int length;
    length = strlen(name);
    printf("the length of %s is %d\n", name, length);
    getch();
    return 0;
}
'''
code ends here
expected to to print length of the char name but it crashes as in build log "Process terminated with status -1073741819 " in build messages warning: passing argument 1 of 'strlen' makes pointer from integer without a cast [-Wint-conversion]| note: expected 'const char *' but argument is of type 'char'|
thanking you for looking into
You declare name as a char yet you treat it like an array. To declare name as a char array, use:
char name[] = { 's','i','l','a','m','\0' };
Also since you reference the function strlen(), you must add the header file:
#include <string.h>
Enjoy:
#include <stdio.h>
#include <string.h>
int main()
{
    // single char can't store that string
    // so we have to declare an array of chars
    char name[] = {'s', 'i', 'l', 'a', 'm', '\0'}; 
    // also we can use string literal syntax:
    // char name[] = "silam";
    int length = strlen(name);
    printf("the length of %s is %d\n", name, length);
    getc(stdin);
    // i don't have <conio.h> so i call 'getc' instead of 'getch'
    return 0;
}
 Alex Python
 Alex PythonUser contributions licensed under CC BY-SA 3.0