Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Standard Size of character (\'a\') in C/C++ on Linux
In C, every character including 'a' is stored using a specific size in memory. On most systems including Linux, the size of a character is 1 byte. This means that any character (like 'a') occupies 1 byte (8 bits) of memory.
To determine how much memory is used by the character 'a', we can use the sizeof() operator, which returns the size in bytes of a variable or data type.
Syntax
sizeof(expression) sizeof(datatype)
Method 1: Using sizeof with Character Literal
This approach checks the size of the character literal 'a' using the sizeof() operator −
#include <stdio.h>
int main() {
printf("Size of character 'a' = %zu byte\n", sizeof('a'));
return 0;
}
Size of character 'a' = 1 byte
Method 2: Using sizeof with char Variable
Here, we declare a variable of type char and then use sizeof() to determine its size −
#include <stdio.h>
int main() {
char ch = 'a';
printf("Size of char variable 'ch' = %zu byte\n", sizeof(ch));
return 0;
}
Size of char variable 'ch' = 1 byte
Method 3: Using sizeof with Pointer to char
When we declare a pointer to a character and check its size, it shows the size of the pointer itself, not the character −
#include <stdio.h>
int main() {
char *ptr;
printf("Size of char pointer = %zu byte(s)\n", sizeof(ptr));
printf("Size of char data type = %zu byte\n", sizeof(char));
return 0;
}
Size of char pointer = 8 byte(s) Size of char data type = 1 byte
Method 4: Checking ASCII Value and Size
We can also check the ASCII value of a character while confirming its size. The ASCII value of 'a' is 97 −
#include <stdio.h>
int main() {
char ch = 'a';
printf("Character: %c\n", ch);
printf("ASCII Value: %d\n", ch);
printf("Size: %zu byte\n", sizeof(ch));
return 0;
}
Character: a ASCII Value: 97 Size: 1 byte
Key Points
- The
chardata type in C is guaranteed to be exactly 1 byte on all systems. - Use
%zuformat specifier forsizeof()results to ensure portability. - Pointer size depends on the system architecture (8 bytes on 64-bit systems).
Conclusion
In C, the character 'a' and all other characters consistently occupy 1 byte of memory. The sizeof() operator is the standard way to verify memory usage of any data type or variable.
