sprintf question

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 
4,408件の閲覧回数
bluehacker
Contributor III

hi all!

 

I am use codewarrior for microcontroller special edition V6.2 and for coldire v7.1.AND find one question about sprintf function. It seem not support "%C" format arguments. I write the code:

uint8 c; 

sprintf(buf,"char you send is:%c\N",c); 

uart_putstr( buf);

I got "char you send is:   "

but if I change"%c"to"%d",I got correct ascii code of the c.

ラベル(1)
0 件の賞賛
返信
1 解決策
2,038件の閲覧回数
CrasyCat
Specialist III

Hello

 

I did perform some tests on CodeWarior for MCU V6.2.

 

In fact in order to be able to print characters in a sprintf or printf call  you need to rebuild your MSL library with

   #define LIBDEF_PRINTF_CHAR  1

in C/C++ Preprocessor panel.

 

Open the project {Install}\ColdFire_Support\msl\MSL_C\MSL_ColdFire\Project\MSL_C.CF.v1.mcp  in CodeWarrior

Check which library you are linking with your application.

Select the Corresponding Build target

Add the following to the C/C++ Preprocessor panel

   #define LIBDEF_PRINTF_CHAR  1

rebuild the library

rebuild the project.

 

There might be something similar on V7.x.

 

CrasyCat

元の投稿で解決策を見る

0 件の賞賛
返信
7 返答(返信)
2,038件の閲覧回数
Lundin
Senior Contributor IV
The C language is case-sensitive.

%C gives a year, according to the C standard, as opposed to %c which prints a character.
\N is not a valid escape sequence in the C language. I believe using one that isn't valid is undefined behavior.
0 件の賞賛
返信
2,038件の閲覧回数
bluehacker
Contributor III

thanks!

I press wrong character here. the code is use %c and \n not %C and\N

it still don't work

 

0 件の賞賛
返信
2,038件の閲覧回数
CompilerGuru
NXP Employee
NXP Employee

Note that the difference in between %c and %d is not the size of the printed output, its the formatting used.

With a printf("%d", '0') the ASCII code of the '0; character is printed, "48".

With a printf("%c", '0') the character '0' is literally, say as string "0"

 

Note that there is no way to pass a character (or short) to function with a open parameter declaration like printf (and sprintf), using"%d" for a character argument is the correct way if you want to print the numerical value.

I personally try to avoid printfw style functions because of the lack of argument type checking, but I know they are convenient and sometimes the best thing available. When using them I always explicitly cast the argument to the expected type to make sure the types match also when later on some variable types get changed elsewhere.

E.g. char c = 12; printf("%d", (int)c);

 

Daniel

0 件の賞賛
返信
2,038件の閲覧回数
bluehacker
Contributor III

I know the difference between %c and %d. BUT the following code don't work:

uint8  c=48;

sprintf(buf,"%c",c);

uart_putstr(buf);

 

if I change to  sprintf(buf,"%d",c);

I got correct '48' in hyperterminal of microsoft windows 

0 件の賞賛
返信
2,038件の閲覧回数
CompilerGuru
NXP Employee
NXP Employee

What do you get with the %c? And what do you expect to receive?

I would expect to get a "0" for %c.

 

Daniel

0 件の賞賛
返信
2,038件の閲覧回数
bluehacker
Contributor III

I just test again, my code is:

 

char ch=0;

char buf[50];

ch='a';

 

sprintf(buf,"%c",ch); 

uart_putstr(0,buf); 

 

//the uart_putstr function code:

 

 

/********************************************************************/

/*

 * Wait for space in the UART Tx FIFO and then send a character

 */ 

void uart_putchar (uint8 channel, char ch)

{

    /* Wait until space is available in the FIFO */

    while (!(MCF_UART_USR(channel) & MCF_UART_USR_TXRDY)) 

    {

    

    };

 

    /* Send the character */

    MCF_UART_UTB(channel) = (uint8)ch;

}

 

/*********************

* send a string using poll mode

*******************/

void uart_putstr(uint8 channel, char *str)

{

while(*str!=0)

{

uart_putchar(channel,*str++);

}

}

 

 

I expect to get 'a' display in hyperterminal. but in fact , I get nothing, hyperterminal don't recieve anything.

 

but  if i change to  sprintf(buf,"%d",ch); I get correct '97' in hyperternimal.

 

 

0 件の賞賛
返信
2,039件の閲覧回数
CrasyCat
Specialist III

Hello

 

I did perform some tests on CodeWarior for MCU V6.2.

 

In fact in order to be able to print characters in a sprintf or printf call  you need to rebuild your MSL library with

   #define LIBDEF_PRINTF_CHAR  1

in C/C++ Preprocessor panel.

 

Open the project {Install}\ColdFire_Support\msl\MSL_C\MSL_ColdFire\Project\MSL_C.CF.v1.mcp  in CodeWarrior

Check which library you are linking with your application.

Select the Corresponding Build target

Add the following to the C/C++ Preprocessor panel

   #define LIBDEF_PRINTF_CHAR  1

rebuild the library

rebuild the project.

 

There might be something similar on V7.x.

 

CrasyCat

0 件の賞賛
返信