size of char datatype and char array in C

Given a char variable and a char array, the task is to write a program to find the size of this char variable and char array in C.

Examples:

Input: ch = 'G', arr[] = {'G', 'F', 'G'}
Output: 
Size of char datatype is: 1 byte
Size of char array is: 3 byte

Input: ch = 'G', arr[] = {'G', 'F'}
Output: 
Size of char datatype is: 1 byte
Size of char array is: 2 byte

Approach:
In the below program, to find the size of the char variable and char array:



  • first, the char variable is defined in charType and the char array in arr.
  • Then, the size of the char variable is calculated using sizeof() operator.
  • Then the size of the char array is find by dividing the size of the complete array by the size of the first variable.

Below is the C program to find the size of the char variable and char array:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to find the size of
// char data type and char array
  
#include <stdio.h>
  
int main()
{
  
    char charType = 'G';
    char arr[] = { 'G', 'F', 'G' };
  
    // Calculate and Print
    // the size of charType
    printf("Size of char datatype is: %ld byte\n",
           sizeof(charType));
  
    // Calculate the size of char array
    size_t size = sizeof(arr) / sizeof(arr[0]);
  
    // Print the size of char array
    printf("Size of char array is: %ld byte",
           size);
  
    return 0;
}

chevron_right


Output:

Size of char datatype is: 1 byte
Size of char array is: 3 byte


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.