I have given the program and the output corresponding to it. Also I have added comments so that it will be clear.
All questions that comes with why it is displaying like this for printf statements that are related to float, can be better understood if we know how float is stored internally. I will try to see how float how works exactly and then will try to make a post on this.
Also I did not get the difference between %f, and %lf when I try to print float and double values. I will be happy to hear from someone what is the difference between the two.
Code
#include<stdio.h>
main()
{
// Declaration of Variables
int iNum=86;
char cChar='V';
float fNum=86.1234567890;
double dNum=86.1234567890;
// To print the integer value
printf("Integer - %d\n",iNum);
// If I use %c for a integer, it will print its ASCII equivalent character
printf("Printing Number as Character - %c\n",iNum);
// I dont have any idea why it is displaying 0.
printf("Printing Number as Float - %f\n",iNum);
// I dont have any idea why it is displaying 0.
printf("Printing Number as Double - %lf\n",iNum);
// Displays hex equivalent of a number
printf("Printing Number as Hex - %x\n\n",iNum);
// Displaying a Character
printf("Character - %c\n",cChar);
// If I use %d for a charcter, it will print its ASCII equivalent number
printf("Printing Character as Number - %d\n",cChar);
// I dont have any idea why it is displaying 0.
printf("Printing Character as Float - %f\n",cChar);
// I dont have any idea why it is displaying 0.
printf("Printing Character as Double - %lf\n",cChar);
// Gets the ASCII equivalent(in Hex) of character
printf("Printing Character as Hex - %x\n\n",cChar);
// Displaying Float Variable - Precision is only 6 digits
printf("Float - %f\n",fNum);
// To explain the precision I am going to print 10 digits after decimal
printf("Float - %.10f\n",fNum);
// Displaying Float as a integer
printf("Printing float as integer - %d\n",fNum);
// Check the explanation for Displaying Double as Character
printf("Printing float as character - %c\n",fNum);
// Displaying Float as a Double.
printf("Printing float as double - %lf\n",fNum);
// Displays hex equivalent of a float - May be I have to understand how
// floats are stored internally.
printf("Printing float as hex - %x\n\n",fNum);
// Using double Variable
printf("Double - %lf\n",dNum);
// To explain the precision I am going to print 10 digits after decimal
// Compare this precision with corresponding printf statement given in float.
printf("Double - %.10f\n",dNum);
// May be due to how double is stored internally, Check the Hex equivalent
// of double below.
printf("Printing double as integer - %d\n",dNum);
// Displays 'Y' because it is the ASCII equivalent of HEX Value 59
// since 59 is the LSB, and Char takes only 1 byte as I have explained
// in the previous posts. Same explanation applies for displaying Float as Char
printf("Printing double as character - %c\n",dNum);
// Printing a double value as float.
printf("Printing double as float - %f\n",dNum);
// Double in Hex format
printf("Printing double as hex - %x\n\n",dNum);
// Printing a hex value. If we put 0x before a number, compiler treats it as
// a hex number.
printf("Printing Hex Value directly - %x\n",0x15);
// Printing a hex equivalent of a decimal number
printf("Printing Hex Value of a Number - %x\n",15);
// Hex value with capitalized characters A,B,C,D,E,F
printf("Printing Hex Value of a Number(capitalized) - %X\n\n",15);
// Printing a Octal Value - If we put 0 before a number, compiler treats it as
// a Octal number
printf("Printing Octal Value directly - %o\n",015);
// Printing a octal equivalent of a decimal number
printf("Printing Octal Value of a number - %o\n\n",15);
//Octal to Hex Conversion
printf("Printing Hex value of Octal Value - %x\n",010);
//Hex to Octal Conversion
printf("Printing Octal value of Hex Value - %o\n\n",0x10);
getch();
}
Output
Integer - 86
Printing Number as Character - V
Printing Number as Float - 0.000000
Printing Number as Double - 0.000000
Printing Number as Hex - 56
Character - V
Printing Character as Number - 86
Printing Character as Float - 0.000000
Printing Character as Double - 0.000000
Printing Character as Hex - 56
Float - 86.123459
Float - 86.1234588623
Printing float as integer - -1073741824
Printing float as character -
Printing float as double - 86.123459
Printing float as hex - c0000000
Double - 86.123457
Double - 86.1234567890
Printing double as integer - -1219637671
Printing double as character - Y
Printing double as float - 86.123457
Printing double as hex - b74dce59
Printing Hex Value directly - 15
Printing Hex Value of a Number - f
Printing Hex Value of a Number(capitalized) - F
Printing Octal Value directly - 15
Printing Octal Value of a number - 17
Printing Hex value of Octal Value - 8
Printing Octal value of Hex Value - 20
All questions that comes with why it is displaying like this for printf statements that are related to float, can be better understood if we know how float is stored internally. I will try to see how float how works exactly and then will try to make a post on this.
Also I did not get the difference between %f, and %lf when I try to print float and double values. I will be happy to hear from someone what is the difference between the two.
0 comments:
Post a Comment