
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Standard Size of character (\\\'a\\\') in C/C++ on Linux
In C/C++, every character including 'a' is stored using a specific size in memory. Most of the systems including Linux, the size of a character is 1 byte. This means that any character (like a) can occupy 1 byte(8 bits of memory).
To determine how much memory is used by the character 'a', we can use the sizeof() operator. So, it returns the size in bytes of a variable or data type.
Following are the list of different ways to check the Standard Size in C/C++.
- Using sizeof with character literal
- Using sizeof with char variable
- Using sizeof with pointer to char
- Checking ASCII value and size
Using sizeof with character literal
This approach is used to check the size of the character literal 'a' using sizeof() function.
Syntax
Following is the syntax to sizeof with character literal:
sizeof('a')
Example
In this example, we check that the size of the character literal 'a' in memory is 1 byte and displaying that as the output.
#include<iostream> using namespace std; int main() { cout<< "Size of character 'a' = "<< sizeof('a')<< " byte" << endl; return 0; }
Following is the output to the above program:
Size of character 'a' = 1 byte
Using sizeof with char variable
Here, we declare a variable of type char and then use sizeof(). This is the correct way to check size of a character.
Syntax
Following is the syntax to sizeof with char variable:
sizeof(charVariable)
Example
In the following example, we determine and display that the size of the 'char' variable 'ch' is 1 byte.
#include<iostream> using namespace std; int main() { char ch = 'a'; cout<<"Size of char variable 'ch' = "<< sizeof(ch)<<" byte"<< endl; return 0; }
Following is the output to the above program:
Size of char variable 'ch' = 1 byte
Using sizeof with pointer to char
We declare a pointer to a character and check its size. This shows the size of the pointer, not the character itself.
Syntax
Following is the syntax to sizeof with pointer to char:
char *ptr; sizeof(ptr)
Example
In this example, we calculate and display the memory size of a char pointer in C++.
#include<iostream> using namespace std; int main() { char *ptr; cout<<"Size of char pointer = "<< sizeof(ptr)<< "byte(s)"<< endl; return 0; }
Following is the output to the above program:
Size of char pointer = 8 byte(s)
Checking ASCII value and size
Here, we check the ASCII value of the character and confirms its size like ASCII of 'a' is 97.
Syntax
Following is the syntax of checking ASCII value and size:
int(ch) sizeof(ch)
Example
In this example, we assign a character to demonstrate the character, ASCII value, and the size in memory (1 byte).
#include<iostream> using namespace std; int main() { char ch = 'a'; cout << "Character: "<< ch<< endl; cout << "ASCII Value: "<<int(ch)<<endl; cout << "Size: "<<sizeof(ch)<<"byte"<<endl; return 0; }
Following is the output to the above program:
Character: a ASCII Value: 97 Size: 1 byte