C - Write program to output result of sizeof and strlen for the same char array

Requirements

Write program to output result of sizeof and strlen for the same char array

Demo

#include <stdio.h>
#include <string.h>

int main()//from   w ww.  j  a  v a2  s .  co  m
{
    char string[] = "this is a test?";

    printf("The string \"%s\" has a size of %u,\n",
            string,sizeof(string));
    printf("and a length of %ld.\n",strlen(string));
    return(0);
}

Result

When you create an array, the program allocates space in memory to hold the array's values.

The allocation is based on the size of each element in the array.

So a char array of 15 items (including the \0, or NULL) occupies 15 bytes of storage, but the length of the string is still only 14 characters (bytes).

Related Exercise