2

I know that C and C++ are different languages.

Code - C

#include <stdio.h>

int main()
{
    printf("%zu",sizeof('a'));
    return 0;
}

Output

4

Code- C++

#include <iostream>
int main()
{
    std::cout<<sizeof('a');
    return 0;
}

Output

1

https://stackoverflow.com/a/14822074/11862989 in this answer user Kerrek SB(438k Rep.) telling about types in C++ nor mentions char neither int but just integral.

is char in C++ is integral type or strict char type ?

Abhishek Mane
  • 619
  • 7
  • 20
  • 3
    A character literal in C is an `int`, but in C++, it's a `char`. – Shawn Nov 19 '21 at 07:40
  • 1
    Yes, `char` is an integral type, in both C and C++. The difference is that a character literal (like `'a'`) has type `char` in C++ (hence size `1`) and type `int` in C (size implementation defined, usually not `1`). – Peter Nov 19 '21 at 07:43
  • 1
    One good reason for the difference: C++ has overloaded methods. For example: `void method( char c );` and `void method( int n );`. Calling `method( 'a' );` would call the `int` method if character constants were `int` values in C++. Because C doesn't have function overloading, that can't happen in C. – Andrew Henle Nov 19 '21 at 09:32

3 Answers3

1

is char in C++ is integral type or strict char type ?

Usage of type_traits lets you know the type:

#include <iostream>
#include <type_traits>
int main()
{
    std::cout << std::is_integral<char>();
}

Output:

1
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
1

is char in C++ is integral type or strict char type ?

Character types, such as char, are integral types in C++.

The type of narrow character constant in C is int, while the type of narrow character literal in C++ is char.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • what is mean by narrow character can you elaborate ? – Abhishek Mane Nov 19 '21 at 09:17
  • @AbhishekMane `'a'` that is character constant. It has type `int` in ***C*** and type `char` in ***C++*** – 0___________ Nov 19 '21 at 09:18
  • `while the type of narrow character literal in C++ is char` what's that mean ? – Abhishek Mane Nov 19 '21 at 09:20
  • @eerorika can you answer my above 2 comments ? – Abhishek Mane Nov 19 '21 at 09:27
  • @AbhishekMane Narrow character literal is for example `'a'`. This is in contrast to a wide character literal such as `L'a'`. Such literal has a different type - a wide character type. All expressions in C++ have a type. The type of `1` is `int`. The type of `malloc(1)` is `void*`. The type of `'a'` is `char`. – eerorika Nov 19 '21 at 09:31
  • @eerorika Then what is the narrow character constant ? – Abhishek Mane Nov 19 '21 at 09:32
  • @AbhishekMane `'a'` for example. C uses different nomenclature. – eerorika Nov 19 '21 at 09:33
  • @eerorika in first comment you said `'a'` is Narrow character literal now you are saying it is narrow character constant isn't both things are contradicting ? – Abhishek Mane Nov 19 '21 at 09:37
  • @AbhishekMane `in first comment you said 'a' is Narrow character litera` That's because we were talking about C++. `now you are saying it is narrow character constant` Like I said, C uses different nomenclature. There is no contradiction. – eerorika Nov 19 '21 at 09:38
  • @eerorika means Narrow character literal and Narrow character constant is same thing but just different names in respective languages, right – Abhishek Mane Nov 19 '21 at 09:42
  • @AbhishekMane Yes. – eerorika Nov 19 '21 at 09:43
  • @eerorika I know it's not appropriate way but no one is giving answer to my question if possible can you answer that please & I am unable understand that concept in detail on my own https://stackoverflow.com/q/70245768/11862989 so please if possible answer this. – Abhishek Mane Dec 07 '21 at 12:03
1

As others mentioned, In C 'a' is char constant and treated as an integer. In C++ it is integral.

Also you can check the difference between char c = 'a' and 'a' in C++ by using RTTI (run time type info) as follows:

#include <iostream>
#include <typeinfo>
using namespace std;
  
int main()
{

    char c = 'a';
  
    // Get the type info using typeid operator
    const type_info& ti2 = typeid('a');
    const type_info& ti3 = typeid(c);
  

    // Check if both types are same
    if (ti2 != ti3)
        cout << "different type" << endl;
    else
        cout << "same type"<< endl;
  
    return 0;
}

The output is : same type.

However, char c = 'a' and 'a' are NOT same in C.

asevindik
  • 131
  • 9