convert celsius to fahrenheit in c – dead simple, innit
intro
Oi, you lot! New to C and fancy summat dead easy? We’re gonna knock up a lil program to switch Celsius to Fahrenheit—ya know, that temp thing where us Brits freeze and the Yanks sweat. It’s nowt fancy but proper useful—like when I was tryna figure out if 30°C was hot enough to skip the pub (it was). Ace for getting your head round C without a right faff. Let’s have a bash!
code bit
Here’s the code—nowt posh, just a quick job I slung together. Shoved it in a box so it don’t look like a total mess on your blog. have a butcher’s:
int main() { // nearly did void, doh!
float celsius,fahrenheit; // float coz decimals, innit
printf("gimme temp in celsius: ");
scanf("%f",&celsius);
fahrenheit=(celsius*9/5)+32; // the magic formula, mate
printf("%.2f°C is %.2f°F\n",celsius,fahrenheit); // forgot ; once, ugh!
return 0;
}
what ya get
Here’s what it chucks out when ya run it. I lobbed in a temp—worked bang on, mate!
25.00°C is 77.00°F
how it works, kinda
Right, let’s have a natter bout this like we’re sat at the chippy with a greasy bag—or a pint, coz I’m proper parched. I’ll spill it like my tatty notes from when I was faffing with C. It’s a piece of piss once ya twig it, honest!
1. that `#include
This is me nicking my toolkit before we start. `
2. `int main()` funtion
Gotta have a “main” to kick off—it’s like the front door to my local. `int` means I’m chucking a number out at the end (that `0` says “sorted, guv!”), and them curly `{}` lads keep it all snug. Nearly wrote `void main()`—old me was a right numpty!
3. boxes: `float celsius,fahrenheit;`
These are my lil buckets. Went with `float` coz temps can have decimals—like 25.5 or 77.2—not just boring whole numbers. `celsius` for the input, `fahrenheit` for the answer. Simples!
4. chatting at ya: `printf` and `scanf`
So yeah, `printf("gimme temp in celsius: ")` is me hollering, “Oi, temp, now!” Then `scanf("%f",&celsius)` grabs what ya chuck in and shoves it in `celsius`. `%f` is for floats—none of that integer rubbish—and `&` is like a post-it going “bung it here, ya twit!”
5. the magic bit: `fahrenheit=(celsius*9/5)+32;`
Here’s the guts! `fahrenheit=(celsius*9/5)+32` is the formula—multiply by 9, divide by 5, add 32. That’s how ya flip Celsius to Fahrenheit. Like 25 turns into 77—proper balmy! No faffing about, just maths!
6. showing off: `printf`
Then `printf("%.2f°C is %.2f°F\n",celsius,fahrenheit)` blurts it out. `%.2f` keeps it to two decimals—nice and tidy, like 77.00 not some daft 77.00001 mess. Forgot a `;` once—CODE WOULDN’T BUDGE, and I was proper fuming!
7. `return 0` to leg it
That `return 0;` is me scarpering, like, “Ta-ta, we’re done!” It’s a C thing—means it all went smooth as a greased weasel.
And that’s yer lot! A cracking lil program to flip Celsius to Fahrenheit. Mess with it—chuck in daft temps, tweak it to say “hot stuff” if ya fancy. First time I ran this, I put in 0 and got 32—bloody freezing, eh! You’re a champ already, mate—keep at it! 😊