-1

As opposed to C++, In C, a character literal is implemented as to be always of type int.


But why we have then the type of char for holding a character value?


In the question Why are C character literals ints instead of chars?,

it is discussed, why character literals are of type int in C. But this is not what my question is about.

Inside the question If character constants are of type `int', why are they assigned to variables of type `char`? then it is going a little more into the deep with the question, why we actually assign character literals to variables of type char if they are of type int, but the provided answers left the concern, why we need the type of char in general.


My Questions are now:

  • Why we have the type of char if any character literals are always of int type?
  • Isn´t the type of char redundant then?
  • What is the purpose of type char, if it is seemingly redundant?
  • 1
    How are you going to declare an array of characters if the type char will be absent? – Vlad from Moscow Dec 23 '19 at 15:04
  • @VladfromMoscow The concept of the whole `char` type seems incorrect to me now. For me, the language had been needed to be defined different at this topic, to give a modifier or anything like that, to flag to the compiler that the context, if needed, is in regards to literal characters instead of to the usual integral values, by using `int` type. – RobertS supports Monica Cellio Dec 23 '19 at 15:10
  • Detail: "As opposed to C++, In C, a character constant ....".. C does not define _character literal_. In C, a _literal_ can have its address taken. – chux - Reinstate Monica Dec 23 '19 at 15:38
  • @chux-ReinstateMonica Ok, with the definition of *character literal* which is not made in the C standard, I understand. But I´ve thought literals are only hardcoded values. How they can have addresses? – RobertS supports Monica Cellio Dec 23 '19 at 15:56
  • 1
    The `char` type and a literal char constant are 2 separate entities, which dimensions are defined per the standard as the smallest memory unit available on the system, for the first, and as the standard `int` type for the second. Any tentative to make an equivalence between a `char` type and a char literal, i.e. `'abc'`, isn't formally correct. – Frankie_C Dec 23 '19 at 15:57
  • 1
    A char can have different dimension on different machines, 8, 16 or even 32bits, and an assignment between a character literal and a `char` variable imply a type conversion from `int` to `char`. – Frankie_C Dec 23 '19 at 16:03
  • 1
    [re](https://stackoverflow.com/questions/59457398/why-do-we-have-the-type-of-char-in-c-if-a-character-literal-is-always-of-type-i?noredirect=1#comment105097298_59457398) In C, a _literal_ exists at some memory location, so it has an address. Research _string literal_, _compound literal_. A constant like `'A'` need not exist at an address - e.g. could be a value in the executable code. – chux - Reinstate Monica Dec 23 '19 at 17:33
  • "The concept of the whole char type seems incorrect to me now. " -- Millions of programmers think otherwise ... when beginning programmers ask a question, it's best for them to sit back and listen for a while before forming their own conclusions. "For me, the language had been needed to be defined different at this topic ..." -- It's hard to make sense of this and the rest of that sentence. – Jim Balter Dec 24 '19 at 00:03

3 Answers3

3

Just be cause a character constant in C source code has type int doesn't mean that the type char has no use.

The type char occupies 1 byte of space. So you can use it anyplace where the values are in the range of a char, which includes ASCII characters. You can read and write those characters from either the console or a file as single byte entities. The fact that a character constant in source code has a different type doesn't change that.

Using char in an array also means you're using less memory than if you had an array of int, which can be useful in situations where space is at a premium. This is especially true if you're using it as a binary format to store data on disk or send it over a network.

A char * can also be used to access the individual bytes of any object if you need to see how that object is represented.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • ...but note that when a char is passed as an argument to a function, it is first turned into an int, probably because there is no machine that can place a single byte on the stack. – Paul Ogilvie Dec 23 '19 at 15:25
  • 2
    @PaulOgilvie From an ABI perspective yes, however from a *language* perspective a variable of type `char` passed to a function that expecting an argument of type `char` will not be converted. – dbush Dec 23 '19 at 15:27
  • 1
    @PaulOgilvie This is not true. Most 8-bit microcontrollers can place single bytes on stack and/or pass them as arguments using registers. – th33lf Dec 23 '19 at 15:41
  • @PaulOgilvie "note that when a char is passed as an argument to a function, it is first turned into an int" -- that is only true if there's no prototype. If there's a prototype, then `char` may or may not take as many bytes on the stack as an `int`, depending on the calling sequence definition. "probably because there is no machine that can place a single byte on the stack" -- no, that isn't true and has nothing to do with why `char` and `short` are converted to `int` in the absence of a prototype. (And dbush's comment makes no sense.) – Jim Balter Dec 23 '19 at 23:53
  • @dbush Is there a difference between character constants and ASCII characters? I´ve thought they would be the same. – RobertS supports Monica Cellio Dec 24 '19 at 18:28
  • @RobertS-ReinstateMonica ASCII is a specific character encoding. For example, ASCII maps the character `'A'` to the value 65, while EBCDIC maps `'A'` to 193. Character constants in the C language map to whatever encoding the running system uses. – dbush Dec 24 '19 at 18:44
2

The type char allows to address each byte (the smallest addressable unit of a CPU). So for example it allows to specify a memory extent of any number of bytes for example for using in memcpy or memmove.

Also how to declare a character array without the type char?

If you declare it as an integer array when there will be redundant allocated memory.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Why do we have the type of char in C, if a character literal is always of type int?

char, unsigned, char, signed char are the minimal size object. Character literal constants are type int for simplicity of the language and no strong need otherwise. (C++ choose a different path - computers could handle more complex things 20 years later.) There are no integer constants narrower than int.

Isn't the whole type of char in C redundant?
Why we have the type of char if any character literals are always of int type?
Isn't the type of char redundant then?

No. Object sizes benefit with a variety of sizes, constants less so.

What is the purpose of type char, if it is seemingly redundant?

Concerning int and constants, char is not redundant. Concerning signed char, unsigned char, char is redundant and reflects a compromise of early implementations of char as unsigned or signed. This allows char to be signed (which is symmetric other integer type lacking signed or unsigned as conceptually characters are usually thought as.


Code can form a compound literal of type char if a "char` literal" is needed.

char a = (char){'B'};
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256