jarlaxle
jarlaxle

Reputation: 17

how to print 'space character' with the double defined in c?

double d,e,f;

I have 3 variables and my program needs space character in some lines.I try everything but cant find.

d='space character' 

This is what i need.

if( ...) in here d,e and f calculates number

else{
        o="no";
        w="";
        d= ' ' ;
        e =' ';
        f=' ';
        quest="";

        }


        fprintf(bb,"%-10s%-13s%-13.2lf\t%-13.2lf\t%-13.2lf\t%-3s\n", o, w, d,e,f,quest);    

        printf( "%-10s%-13s%-13.2f\t%-13.2f\t%-13.2f\t%-3s\n\n", o, w, d,e,f,quest ); 

Upvotes: 0

Views: 9989

Answers (3)

Gangadhar
Gangadhar

Reputation: 10516

if d value equals to 32 will print space.

double d;

if (somecondition)
{
   d=32; 
   printf("%c==%d==%f",(char)d,(int) d,d ); //cast to char while printing
}

You should not put ' '(space) into double.

when ever you want, store 32 into d and use cast.


Based on your edit, you can do like this

if(...)
{
   //do calculations and print and print statements inside if
  fprintf(bb,"%-10s%-13s%-13.2lf\t%-13.2lf\t%-13.2lf\t%-3s\n", o, w, d,e,f,quest); 
}
else
{
  //modify fprintf and printf statements, do not modify values
  fprintf(bb,"%-10s%-13s%- \t%-13.2lf\t%-13.2lf\t%-3s\n", o, w,   e,f,quest); 
                        //^^ here use space.do not use this     ^^ argument d 

}

Upvotes: 1

Lundin
Lundin

Reputation: 213809

In the C language, all characters exist as small integers (usually 1 byte large). It never makes any sense to store characters in variables used for floating point calculation.

Instead, built up an output format string in the desired format. The most obvious would be

printf("%f %f %f", d, e, f);

or alternatively, sprintf in case you need the string for other purposes than printing it on the screen.

EDIT : if the output of space should depend on the values of the doubles, then you should do like this:

signed char ascii_value = ' ';

if ( fabs(my_double - (double)ascii_value) <= epsilon * fabs(my_double) )
{
  print(ascii_value);
}

And if you have absolutely no idea what the above means or why I did it, you probably shouldn't be doing floating any point programming, not before reading this.

Upvotes: 0

haccks
haccks

Reputation: 106012

Declare d as char.

 char d;
 ...

 if(......)
    d = ' ';

Upvotes: 0

Related Questions