Swap Two Numbers in C – Dead Simple, Innit

swap two numbers in c – dead simple, innit

intro

Oi, you! New to C and fancy summat easy? We’re gonna cobble together a lil program to swap two numbers round. It’s nowt flash, but dead useful—like when I was tryna swap seats at the pub and worked out who got the comfier one (not me, gutted). Ace for getting your noggin round C without losing the plot. Let’s have at it!

code bit

Here’s the code—nowt posh, just a quick job I bashed out. Shoved it in a box so it don’t look like a right mess on your blog. take a gander:

#include

int main() { // nearly cocked up with void, ha!
int num1,num2,temp; // temp’s the middleman, innit

printf("gimme first number: ");
scanf("%d",&num1);

printf("gimme second number: ");
scanf("%d" ,&num2); // extra space coz I’m daft

printf("before swap: %d and %d\n",num1,num2);

temp=num1; // stash num1 here for a sec
num1=num2; // num1 gets num2’s spot
num2=temp; // num2 nabs the stash

printf("after swap: %d and %d\n",num1,num2); // forgot ; once, nightmare!

return 0;
}

what ya see

Here’s what pops out when ya run it. I chucked in some numbers—worked like a charm, mate!

gimme first number: 5
gimme second number: 8
before swap: 5 and 8
after swap: 8 and 5

how it works, sorta

Right, let’s have a butcher’s at this like we’re slumped in the boozer with a pint—or a bag of chips, coz I’m starving again. I’ll ramble it out like my scruffy notes from when I was faffing with C. It’s a piece of piss once ya suss it, swear down!

1. that `#include ` gubbins
This is me nicking my tools before we get going. ``—fancy pants “standard input-output”—lets me mess with `printf` and `scanf`. Left it out once, and my code just sat there like a plonker. Total mug move!

2. `int main()` funtion
Gotta have a “main” to start—think of it like the 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 tight. Nearly wrote `void main()`—dumb old me!

3. boxes: `int num1,num2,temp;`
These are my lil buckets. Used `int` coz we’re sticking with whole numbers—like 5 or 8—no mucking about with decimals. `temp` is the extra bucket to help with the swap—forgot it once and it all went pear-shaped!

4. yakking at ya: `printf` and `scanf`
So yeah, `printf("gimme first number: ")` is me barking, “Oi, number, sharpish!” Then `scanf("%d",&num1)` grabs what ya chuck in and stuffs it in `num1`. `%d` is for integers—none of that floaty rubbish—and `&` is like a note saying “bung it here, ya twit!” Do that twice.

5. swapping the buggers
Here’s the magic! First, `temp=num1` stashes num1 safe. Then `num1=num2` shoves num2 into num1’s spot. Finally, `num2=temp` drags the stashed num1 back out. It’s like swapping coats with your mate—ya need a spare peg to hang one on!

6. showing off: `printf`
`printf("before swap: %d and %d\n",num1,num2)` shows the numbers first, then `printf("after swap: %d and %d\n",num1,num2)` flaunts the switcheroo. `%d` slots the numbers in, and `\n` keeps it neat. Forgot a `;` once—CODE JUST DIED, and I was proper raging!

7. `return 0` to scarper
That `return 0;` is me legging it, like, “Ta-ta, we’re done!” It’s a C ritual—means it all went smooth as a greased weasel.

And that’s yer lot! A tidy lil program to swap two numbers. Mess with it—chuck in bonkers numbers, call it “before flip” if ya fancy. First time I did this, I forgot `temp` and ended up with two 8s—total stitch-up! You’re nailing it already, mate—keep at it! 😊