size of char datatype and char array in C - GeeksforGeeks (2024)

Improve Article

Save Article

  • Difficulty Level :Easy
  • Last Updated :15 Oct, 2019

Improve Article

Save Article

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 byteSize of char array is: 3 byteInput: ch = 'G', arr[] = {'G', 'F'}Output: Size of char datatype is: 1 byteSize 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 byteSize of char array is: 3 byte

My Personal Notesarrow_drop_up

size of char datatype and char array in C - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 5785

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.