Thread overview
newbees printf
May 10, 2003
Florian Lavrard
May 10, 2003
Helmut Leitner
May 11, 2003
Florian Lavrard
May 13, 2003
Florian Lavrard
May 10, 2003
i've a little problem with printf()
 printf("%d",var);
- var could be an int or a char (or other)
 It don't word gook with char if i use "%d", or don't work good with int if
i use "%c"...


Have you got some tips please ?

thanks

Florian




May 10, 2003

Florian Lavrard wrote:
> 
> i've a little problem with printf()
>  printf("%d",var);
> - var could be an int or a char (or other)
>  It don't word gook with char if i use "%d", or don't work good with int if
> i use "%c"...
> 
> Have you got some tips please ?

You are not quite clear about your problems.

If you have
   int i=66;
   char c='A';

you would typically use
   printf("%d",i);
   printf("%c",c);
for printing.

If you want to print the numerical value of the character, you might use
   printf("%d",(int)c);   or even just
   printf("%d",c);

If you want to print the int as character (66=B) you
could do it this way
   printf("%c",i);

But maybe I don't understand your problem. Please describe it.

-- 
Helmut Leitner    leitner@hls.via.at
Graz, Austria   www.hls-software.com
May 11, 2003
Right,  you have not understand, ;-)
( but i'm not sure to use the best english, i'm french)

So let's try again with an exemple :

error is explained just after source code :

template Gcouple(T){
     public class couple{
     private :
          private T fst;

     public :
          this (T passfst){ this.fst = passfst;}
          T getFst() {return fst;}
          void show(){printf("%c",getFst());}
      }
}

void main(){


instance Gcouple(int) intcouple;
intcouple.couple couple1= new intcouple.couple(1);
couple1.show();

printf("\n test COUPLE char \n");
instance Gcouple(char) charcouple;
charcouple.couple couplea= new charcouple.couple('a');
couplea.show();
}


couplea.show() use printf("%c",...); to print a char >>> .It's right
couple1.show() use printf("%c",...); to print a int >>>it's not right

Do you understand my problem well ? I hope.

( because this is just an exemple not the the true need i don't want to cast
int in char ...)




Florian

"Helmut Leitner" <leitner@hls.via.at> a écrit dans le message news: 3EBD1CE8.9A47632@hls.via.at...
>
>
> Florian Lavrard wrote:
> >
> > i've a little problem with printf()
> >  printf("%d",var);
> > - var could be an int or a char (or other)
> >  It don't word gook with char if i use "%d", or don't work good with int
if
> > i use "%c"...
> >
> > Have you got some tips please ?
>
> You are not quite clear about your problems.
>
> If you have
>    int i=66;
>    char c='A';
>
> you would typically use
>    printf("%d",i);
>    printf("%c",c);
> for printing.
>
> If you want to print the numerical value of the character, you might use
>    printf("%d",(int)c);   or even just
>    printf("%d",c);
>
> If you want to print the int as character (66=B) you
> could do it this way
>    printf("%c",i);
>
> But maybe I don't understand your problem. Please describe it.
>
> --
> Helmut Leitner    leitner@hls.via.at
> Graz, Austria   www.hls-software.com


May 11, 2003
"Florian Lavrard" <flavrard@free.fr.nospam.> escribió en el mensaje
news:b9ljqq$26qc$1@digitaldaemon.com...
| Right,  you have not understand, ;-)
| ( but i'm not sure to use the best english, i'm french)
|
| So let's try again with an exemple :
|
| error is explained just after source code :
|
| template Gcouple(T){
|      public class couple{
|      private :
|           private T fst;
|
|      public :
|           this (T passfst){ this.fst = passfst;}
|           T getFst() {return fst;}
|           void show(){printf("%c",getFst());}
|       }
| }
|
| void main(){
|
|
| instance Gcouple(int) intcouple;
| intcouple.couple couple1= new intcouple.couple(1);
| couple1.show();
|
| printf("\n test COUPLE char \n");
| instance Gcouple(char) charcouple;
| charcouple.couple couplea= new charcouple.couple('a');
| couplea.show();
| }
|
|
| couplea.show() use printf("%c",...); to print a char >>> .It's right
| couple1.show() use printf("%c",...); to print a int >>>it's not right
|
| Do you understand my problem well ? I hope.
|
| ( because this is just an exemple not the the true need i don't want to
cast
| int in char ...)
|
|
|
|
| Florian
|

I don't know what the problem is. I compiled and ran it and I got a smiling face and an 'a', and that's what should be since ascii 1 is a smiling face. Now, if you want to print 1, then you should put %d instead of %c.

————————————————————————— Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.478 / Virus Database: 275 - Release Date: 2003-05-06


May 11, 2003
"Florian Lavrard" <flavrard@free.fr.nospam.> escribió en el mensaje
news:b9ljqq$26qc$1@digitaldaemon.com...
|
| couplea.show() use printf("%c",...); to print a char >>> .It's right
| couple1.show() use printf("%c",...); to print a int >>>it's not right
|

If you want to print 1 and 'a', try this:

void show(){printf("%.*s",toStr(getFst()));}  //1
...
//outside template
import string;

char[] toStr(int i) { return string.toString(i); }  //2
char[] toStr(char c) { //3
 char [] res;
 res ~= c;
 return res;
}

This leads to a bug. I couldn't use toString instead of toStr in //2 and //3 because dmd complained in //1 that toString() doesn't match (int). I know why's that: because dmd looks for toString inside the class. But wouldn't it be desirable if dmd could look outside the class too? I mean, if it doesn't find a function inside the class, then try inside the module, then inside the imports, etc. Could it be possible?

————————————————————————— Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.478 / Virus Database: 275 - Release Date: 2003-05-06


May 11, 2003
"Carlos Santander B." <carlos8294@msn.com> escribió en el mensaje
news:b9lpra$2chc$1@digitaldaemon.com...
| ...
| This leads to a bug. I couldn't use toString instead of toStr in //2 and
//3
| ...

My mistake: there's no bug in the code I posted. Getting there led me to that bug.

————————————————————————— Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.478 / Virus Database: 275 - Release Date: 2003-05-06


May 13, 2003
thanks but i don't want to import string,
something else ?

Can't  i overload printf ? how ?

florian

"Carlos Santander B." <carlos8294@msn.com> a écrit dans le message news: b9lpra$2chc$1@digitaldaemon.com...
> "Florian Lavrard" <flavrard@free.fr.nospam.> escribió en el mensaje
> news:b9ljqq$26qc$1@digitaldaemon.com...
> |
> | couplea.show() use printf("%c",...); to print a char >>> .It's right
> | couple1.show() use printf("%c",...); to print a int >>>it's not right
> |
>
> If you want to print 1 and 'a', try this:
>
> void show(){printf("%.*s",toStr(getFst()));}  //1
> ...
> //outside template
> import string;
>
> char[] toStr(int i) { return string.toString(i); }  //2
> char[] toStr(char c) { //3
>  char [] res;
>  res ~= c;
>  return res;
> }
>
> This leads to a bug. I couldn't use toString instead of toStr in //2 and
//3
> because dmd complained in //1 that toString() doesn't match (int). I know
> why's that: because dmd looks for toString inside the class. But wouldn't
it
> be desirable if dmd could look outside the class too? I mean, if it
doesn't
> find a function inside the class, then try inside the module, then inside the imports, etc. Could it be possible?
>
> -------------------------
> Carlos Santander
>
>
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.478 / Virus Database: 275 - Release Date: 2003-05-06
>
>


May 13, 2003
"Florian Lavrard" <flavrard@free.fr.nospam.> escribió en el mensaje
news:b9q59r$p7o$1@digitaldaemon.com...
| thanks but i don't want to import string,
| something else ?
|
| Can't  i overload printf ? how ?
|
| florian
|

Sure, write your own :D

————————————————————————— Carlos Santander


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.480 / Virus Database: 276 - Release Date: 2003-05-12