|
|
Important Note:
|
This will be explained in the automatic conversion rule next....
|
|
|
int main(int argc, char* argv[] )
{
int i;
short s;
double d;
i = 9827563;
s = i; /* Unsafe conversion, allowed in C !!! */
printf( "i = %d , s = %d \n", i, s );
d = 9827563.444;
i = d; /* Unsafe conversion, allowed in C !!! */
printf( "d = %lf , i = %d \n", d, i );
d = 9827563.444;
s = d; /* Unsafe conversion, allowed in C !!! */
printf( "d = %lf , s = %d \n", d, s );
}
|
Compiler output:
cs255-1@aruba (3822)> gcc -Wconversion casting1.c
casting1.c: In function 'main':
casting1.c:10:8: warning: conversion to 'short int' from 'int' may alter its value [-Wconversion]
s = i; /* Unsafe conversion, allowed in C !!! */
^
casting1.c:15:8: warning: conversion to 'int' from 'double' may alter its value [-Wfloat-conversion]
i = d; /* Unsafe conversion, allowed in C !!! */
^
casting1.c:20:8: warning: conversion to 'short int' from 'double' may alter its value [-Wfloat-conversion]
s = d; /* Unsafe conversion, allowed in C !!! */
^
|
The program will still compile successfully. But at least, you are warned that these conversion may result in errors !!!
How to run the program:
|
|
#include <stdio.h>
int main(int argc, char* argv[] )
{
unsigned long i = 4; // Integer
// i is a number that you can add, subtract, etc
int a[5]; // Array of integers
// a is the LOCATION (address) of the first elem of the array
printf("a = %p\n", a);
printf("i = %lx\n", i);
i = a; // WARNING !! Types are "too different" !!!
// BUT: it will still compile and run
// Note: a "warning" is NOT fatal !!!
printf("i = 0x%lx\n", i);
}
|
Compiler message:
casting2.c: In function 'main':
casting2.c:13:6: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
i = a;
^
|
How to run the program:
|