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:
// 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; } |
Output:
Size of char datatype is: 1 byte Size of char array is: 3 byte
Contact Us