Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a question about linking C and JavaScript languages. I have a variable like 'X' in my C program. I could use html, CSS and JavaScript inside my C code therefore I can show whatever I want inside a browser. Well there is my problem! I have a variable like 'X' in my C code and I want to show its value in my browser page. That needs (as I understood) to make a variable with same value in JavaScript but I don't know how to do that! Is there anyone who knows that? Or is there a better way to do my purpose? I will thank whoever is capable to answer!

What I have tried:

I have a question about linking C and JavaScript languages. I have a variable like 'X' in my C program. I could use html, CSS and JavaScript inside my C code therefore I can show whatever I want inside a browser. Well there is my problem! I have a variable like 'X' in my C code and I want to show its value in my browser page. That needs (as I understood) to make a variable with same value in JavaScript but I don't know how to do that! Is there anyone who knows that? Or is there a better way to do my purpose? I will thank whoever is capable to answer!
Posted
Updated 19-Jul-22 16:49pm
Comments
OriginalGriff 16-Jul-22 1:38am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 16-Jul-22 7:59am    
Unless you can generate the web page in the C program, and pass that to the server, I do not think it is possible. There is no direct connection between a C program and a web page. If, however, you already have some code that does this then please add the details to your question.
Shao Voon Wong 17-Jul-22 0:47am    
You can read my article on how to call C/C++ from Javascript or vice versa.
https://www.codeproject.com/Articles/5160967/Bring-Your-Cplusplus-Code-to-the-Web

1 solution

Here is an example to directly answer your question:

Your question,"Importing C language variable to javascript".

I am not certain that I understand your explaination of the need, so I am attempting to answer the posted question itself directly.


C, or C++ code
C++
const std::string SomeFile = "xyz.txt";
std::ifstream SomeStreamOfTheFile(SomeFile, std::ios_base::binary);

Would be converted to JavaScript as
JavaScript
<script language="JavaScript">

var nl = getNewLine()

function getNewLine() {
	var agent = navigator.userAgent

	if (agent.indexOf("Win") >= 0)
		return "\r\n"
	else
		if (agent.indexOf("Mac") >= 0)
			return "\r"

 	return "\r"

}

pagecode = 'const std::string SomeFile = "xyz.txt";
std::ifstream SomeStreamOfTheFile(SomeFile, std::ios_base::binary);
'

document.write(pagecode);

</script>


I noticed that there seems to be many thousands of people that read posts to this site and NEVER upvote ANY of them. I read some nice posts here and I try to upvote them if I like them. SHAME on you people that give no credit to at least an attempt to usefully answer a question. Shame on you. Those people are the definition of lurkers sucking the life from this site.

I got a -16 Authority Answer Downvoted for my answer. At least I tried. It looks like someone is so afraid of my answering questions that they piss their pants pissing a -16 at me. Maybe I should answer more. They did not even say who they were or even say what they found technically wrong with my answer. This is getting fun. Maybe I should look for more questions that I can offer (in my opinion) a userful answer.

Thank you.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900