# Constants

• 02-13-2011
GokhanK
Constants
I am trying to fully understand details about constants.

1. What is a multibyte character and what is a wide character?

What is the difference between 'A' and L'A'

2. The book I have says:

By default, the compiler fits a numeric constant into the smallest compatible data type that will hold it. Therefore, assuming 16-bit integers, 10 is int by default, but 103,000 is a long int.

What should I understand from that. When does the compiler fit the numeric constant, won't I always define the data type?

3. When should I use suffixes F,L and U?
• 02-13-2011
CommonTater
Quote:

Originally Posted by GokhanK
I am trying to fully understand details about constants.

1. What is a multibyte character and what is a wide character?

Multibyte characters are usually a single byte... ASCII with extensions for accent marks.
Wide characters, also called Unicode are usually 2 bytes, allowing up to 65.535 different characters.

Quote:

What is the difference between 'A' and L'A'
'A' is a single byte character.
L'A' is a unicode character.

(The same appies to strings, btw)

Quote:

2. The book I have says:
By default, the compiler fits a numeric constant into the smallest compatible data type that will hold it. Therefore, assuming 16-bit integers, 10 is int by default, but 103,000 is a long int.

What should I understand from that. When does the compiler fit the numeric constant, won't I always define the data type?
Basically you should always define types, most compilers will error off if you don't.
What you should take from this is that smaller numbers fit into larger variables, but not the other way around.

Code:

``` int x;  char y;  y = 100;  x = y;      // no problem  x = 1000;  y =  x;      // will cause an overflow.```

Quote:

3. When should I use suffixes F,L and U?
[/quote]

I use them as very convenient typecasts for literal numbers double x = 1000f; etc.
• 02-13-2011
GokhanK
Code:

```float x; x=123.12```
Code:

```float x; x=123.12f```
What is the difference between them? I have defined x as a float, then why should I specify it as a float again?

And can you give me an example of a wide character?

Edit: I saw later

Code:

```double x; x=123.12f```
Does it mean that:

At this step store it as a float but x can overflow float later (or could overflow float before)
• 02-13-2011
CommonTater
Quote:

Originally Posted by GokhanK
Code:

```float x; x=123.12```
Code:

```float x; x=123.12f```
What is the difference between them? I have defined x as a float, then why should I specify it as a float again?

In your example, none... anything with decimal points *has* to be a float or a double since integers are whole numbers only. But consider...
Code:

```float x; int y; y = 100; x = y+ 25f;```
Quote:

And can you give me an example of a wide character?
Whew... easier asked than answered... you need to open some unicode files in a hex editor and see what's going on... if it's english language, every other byte will be a 0.

Quote:

Edit: I saw later

Code:

```double x; x=123.12f```
Does it mean that:

At this step store it as a float but x can overflow float later (or could overflow float before)
No it doesn't. Overflows are BAD, we spend an inordinate amount of time avoiding them in C.
• 02-13-2011
GokhanK
Code:

```#include <stdio.h> main() { float x; int y; y = 100; x = y+ 25f; return 0; }```
It doesn't work in CodeBlocks

It says: Invalid suffix f on integer constant
• 02-13-2011
CommonTater
Quote:

Originally Posted by GokhanK
Code:

```#include <stdio.h> main() { float x; int y; y = 100; x = y+ 25f; return 0; }```
It doesn't work in CodeBlocks

It says: Invalid suffix f on integer constant

It was only an example... to demonstrate the concept.
Cut and paste coding is just as BAD as overflows.

Also... the correct form of main is int main (void) or int main (int argc, char *argv[])... and it always returns an integer value, usually 0.
• 02-14-2011
GokhanK
Quote:

Originally Posted by CommonTater
It was only an example... to demonstrate the concept.
Cut and paste coding is just as BAD as overflows.

Also... the correct form of main is int main (void) or int main (int argc, char *argv[])... and it always returns an integer value, usually 0.

I still wonder the difference between;

Code:

```float a; a=100```
and

Code:

```float a; a=100f```
Do I need to place f to specify the variable a is a float, not integer. Isn't it enough to define it as float? If I don't put f will it store it differently from a float?
• 02-14-2011
laserlight
Quote:

Originally Posted by GokhanK
Do I need to place f to specify the variable a is a float, not integer. Isn't it enough to define it as float? If I don't put f will it store it differently from a float?

The f suffix is used to denote that the constant (as in literal) is a float, e.g., 100.0 is a double constant whereas 100.0f is a float constant. It does not denote that the variable is a float.
• 02-14-2011
GokhanK
Code:

```#include <stdio.h> main() { float x; int y; y = 100; x = y+ 25.0f; return 0; }```
If I write it like this it works. I think f cannot be used as 25f or 100f, they have to be like 25.0f or 100.00f.

Then I don't know why to use these suffixes still.
• 02-15-2011
Bayint Naung
Code:

`#define MY_FLOATING_CONST      (1.0f)`
If you just use 1.0 the type will be double, sure you can use cast ((float)1.0) also.