How to put %s within alert

V

Vanitha

Hi All,
Is there anything wrong with this syntax..I am generating javascript
code from "C" language.

unsigned char Buf="Test";
printf("alert(\"%s\");\n\n",Buf);

I am getting error "Unterminated string literal in the javascript
console

alert("Test

but i have terminated this string in my "c" code..?

Thanks
 
M

Martin Honnen

Vanitha wrote:

Is there anything wrong with this syntax..I am generating javascript
code from "C" language.

unsigned char Buf="Test";

Are you sure you wouldn't need
unsigned char *Buf = "Test";
printf("alert(\"%s\");\n\n",Buf);

I am getting error "Unterminated string literal in the javascript
console

alert("Test

but i have terminated this string in my "c" code..?

Hard to tell, if there really is
alert("Test
send to the browser then the script engine is right to flag the syntax
error.
You might want to ask in a C group perhaps as it is more a problem using
C to generate some string in a certain output format than being a
JavaScript problem.
If you have a public URL where the problem occurs then you could post it
here so that we can at least check whether other browsers have the same
problem. Which browser did you test with?
 
L

Lasse Reichstein Nielsen

Is there anything wrong with this syntax..I am generating javascript
code from "C" language.

It's not a Javascript problem, but a C problem.
unsigned char Buf="Test";

Should be
char *Buf = "Test";

As it is, you are assigning a pointer to a char variable. Only fun
can come from that :)
printf("alert(\"%s\");\n\n",Buf);

I am getting error "Unterminated string literal in the javascript
console

alert("Test

but i have terminated this string in my "c" code..?

Yes, otherwise the C compiler would have complained.
My guess is that the pointer is converted to the char value zero.

/L
 
K

kaeli

Hi All,
Is there anything wrong with this syntax..I am generating javascript
code from "C" language.

unsigned char Buf="Test";

Um...
This isn't ANSI C.
An unsigned char is ONE character. Not a string.
Are you missing a pointer (*) or some brackets([]), or is this not ANSI C? If
this is C# or C++, mention that. :)

but i have terminated this string in my "c" code..?

Are you SURE? ;)

--
 
L

Lee

Vanitha said:
Hi All,
Is there anything wrong with this syntax..I am generating javascript
code from "C" language.

unsigned char Buf="Test";
printf("alert(\"%s\");\n\n",Buf);

I am getting error "Unterminated string literal in the javascript
console

alert("Test

but i have terminated this string in my "c" code..?

The alert results look like you've got a newline character in Buf.
You don't show one in your example, but since your example is bad C syntax, I'm
guessing that you've wasted people's time by posting code that's "sorta like"
what you really have.
 
V

Vanitha

Sorry that was a typo error. I am assigning the string to a character
array only..I am using Mozilla browser. I am getting the error message
alert("Test Unterminated string literal in the Javascript console...
But actually i am terminating the string properly in my printf
statement for the alert message


kaeli said:
Hi All,
Is there anything wrong with this syntax..I am generating javascript
code from "C" language.

unsigned char Buf="Test";

Um...
This isn't ANSI C.
An unsigned char is ONE character. Not a string.
Are you missing a pointer (*) or some brackets([]), or is this not ANSI C? If
this is C# or C++, mention that. :)

but i have terminated this string in my "c" code..?

Are you SURE? ;)

--
 
V

vanitha

Thanks for your replies. That solved the problem. It was the newline
char causing the problem.

I was actually copying some string to my buffer based on some
condition...

unsigned char au8RespBuf[40];

if(i32RetCode == SUCCESS)
{
strcpy(au8RespBuf,"Config set successfully\n");
}else
{
//Decode the error code here....and form the correct error string
strcpy(au8RespBuf,"Config Failed\n");
}
Now when i remove the Newline char its working fine.sorry for the
confusion. next time onwards i will post the original code.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,430
Latest member
7dog123

Latest Threads

Top