Revision as of 18:24, 13 May 2024 by Admin (Created page with "# How would you assign the value 3.14 to a variable called pi? # Is it possible to assign an ''int'' to a ''double''? ## Is the reverse possible?")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
ABy Admin
May 13'24

Exercise

  1. How would you assign the value 3.14 to a variable called pi?
  2. Is it possible to assign an int to a double?
    1. Is the reverse possible?
ABy Admin
May 13'24
  • The standard way of assigning 3.14 to pi is:
double pi;
pi = 3.14;
    • Since pi is a constant, good programming convention dictates to make it unchangeable during runtime. Extra credit if you use one of the following two lines:
const float pi = 3.14;
#define pi 3.14
  • Yes, for example :
int a = 67;
double b;
b = a;
  • Yes, but a cast is necessary and the double is truncated:
double a=89;
int b;
b = (int) a;

References

Wikibooks contributors. "C Programming/Exercise solutions". Wikibooks. Wikibooks. Retrieved 13 May 2024.

00