# Let's "C" How This Goes Again - Part 2

By: jnox

January 30, 2017

The good stuff:

Let’s get on with it, I’ll assume you were a good student and took a look or at least skimmed the intro article. When you print a statement (you’re basically telling the computer to display the dang thing) you can print it in two lines or all on one line as we did in the intro. If you want to get fancy and cute, fine but don’t ask me to change the font color or size…that’s a different programming language. Don’t jump ahead of yourself, let’s focus on these baby steps before swimming with sharks and making your brain hate you for jumping around and confusing it. A new line is simply ‘n’, that simple right? Ok, go now and conquer the world…I’m kidding. Let me show you that in one line of code, just like the first example but don’t get worried on me now. So, we had simply used this; printf(“Hello World!”); but we want more than just one line then we use that n like so; printf(“Hellon World!n”); and there you have it. Go ahead and try it!Here’s what the full program would look like;

#include <stdio.h>

int main()

{

printf(“HellonWorld!n”);

return 0;

}

That would give the return (or the result) like this:   Hello

World!

You can try it at this online compiler: https://www.tutorialspoint.com/compile_c_online.phpThat was too simple though, so let’s add two integers (numbers) together. Don’t panic though, there will be more lines and the ‘scanf’ code will be introduced to you in 3…2...1 and now. The scanf code, basically in this program you’re saying “hey you! Yeah you, see these two numbers? Good, well I want you to do something with them.” Let’s say we use the code ‘sum’ which is used to perform the adding part. So we say “hey you take these two numbers and add them for me…oh and show me what it is too.” In nerdy tech talk you are saying, compute the sum of these values and print the result using printf. Oh and before using the numbers you have to tell it what to add, in this case whichever number is inputted (or typed in) by the user (in this case us for now). I have another food example; yes I’m a bit hungry as I’m typing this at the moment so bear with me. Basically, if you want a cheese sandwich you have to say, use two pieces of bread and this cheese for the middle now put them all together and present it to me…then you gobble it up. So what does that all look like? Well lets type out another program, YAY your second program (if you don’t count that one separate lines bit above ‘n’).Ok, so how did we start it off? Hey no peaking. Meh you looked, whatever, after time you won’t need to look the more you type it out, remember me saying practice makes perfect? Ok I didn’t say that exactly but you know what I mean. And no don’t call it a hashtag, before hashtags were popular thisà# was called a pound sign symbol and no I’m not a granny…don’t believe me look it up little guppy. For the longest time, when I was a wee bit little grasshopper I thought it was called a pounce sign, as in the cat pounced. Yeah don’t ask. I have no idea why I thought that, hehe, any who…onward with the lesson. Before we start adding numbers and scanning and all that fancy-ness lets also get introduced to what represents the integers (or numbers in the C program). What is that you may ask? Well ‘%d’ is used for integers. But why the percent sign? The ‘%’ is a format specifier, that’s basically fancy techy talk for ‘what the hell do you want? Numbers? Letter? What?.’ See not that hard. Don’t get too hung up on the why that symbol or why this symbol, for now just remember that ‘%d’ is integer (or numbers). Let’s keep things simple for now. Ok now we can apply what we know to see it in action.Here’s an example of it;

# include <stdio.h>

/* this is the function main which gets this party started…I mean program */

int main()

{

int integer1 ;   /* The statement or line of code stating that the variable input is an integer */

printf("Enter your magical number " ) ;   /* Letting the user know to give us that number */

scanf("%d" , &integer1 );  /* This function takes the data, number inputted, and stores it in what we declared above, in this case integer---like saying ‘ok, let me hold onto this for you’ or ‘Gimme that’ */

printf("Your magical number is : %dn" , integer1 ); /* Displays output, that number taken that was inputted in scanf is being displayed---like saying ‘Here take it back’ */

return 0;

}

Before I go on you are probably wondering what’s with all the red comments, right? Ok, calm down…breathe…don’t panic. Any comment in a program made between the /*….*/ will be ignored by the compiler and not included or factored in when compiling the program like the ones just typed out above. The comments made between the /* and  */ can run on multiple lines but if you use ‘ // ’ with a sentence after it then it can only run on one line. Like so, //This is a one lined comment. I only displayed it in red to make it easier for you to read. If you are working or plan to work for someone or others that might look at the code you wrote, it can be a good practice to write comments not only for yourself to remember but anyone else who has to take over can follow along with what you were doing too, the ‘Be kind, and rewind’ type of policy.  Ok, I think you understand, you seem like a smart cookie.So the program typed out above with all the cute red lines will ask you for a number and display back to you whichever number you first entered. It’s like throwing the ball and getting the same ball back. This time my example wasn’t food, my tummy is happy. In the line where we typed ‘int integer1’ you can call integer1 anything you like really, but simplicity’s sake I used the boring term integer so that it’s better understood what we are doing. You could even call it magical1 if you wanted to or bat or cat, you get the picture. Try it out here, just copy and paste it in as is to see; https://www.tutorialspoint.com/compile_c_online.php .

Now let’s take it a step further, get a little fancy and add two integers together. So what do we do? Simple, it is…don’t look so worried, we add another variable and declare it as well as the sum for simplicity’s sake, like I said we could call it ‘z’ or ‘integer3’ or whatever strikes your fancy but keep KISS in mind. KISS= Keep It Simple Stupid, I’m not saying you’re stupid I’m saying to keep it that simple…so please don’t try to misinterpret that. Now we could scanf two numbers in the same statement or in separate statements but I don’t want to confuse you so we will write it all out on its own then I will show you the on one line bit.

# include <stdio.h>

int main()

{

int integer1 ;

int integer2 ;   /* This is for our second input */

int sum;   /* This is for the addition of the two integers */

printf("Enter your first magical numbern " ) ;

scanf("%d" , &integer1 );

printf("Enter your second magical numbern " ) ;

scanf("%d" , &integer2 );

sum = integer1 + integer2 ;

return 0;

}

Now if you wanted to shorten that up a bit but still add two integers together while also giving you the result, here’s one way of doing so.This does the same thing but formatted differently;

# include <stdio.h>

int main()

{

int num1;

int num2;

printf("Enter two integersn" ) ;

scanf("%d %d" ,&num1,&num2);

int result = num1+num2;

printf("Entered value is : %d and %d and their sum is %dn" ,num1,num2,result );

return 0;

}

Instead of num1, num2, and result you could instead replace them with something even shorter like x, y, and z. So that’s all, for now, folks. I hope you’re still sane even after all that. You may not realize this but you have learned more than you know. Now go forth and practice typing them out and try tweaking them a bit. Try challenging yourself by perhaps adding a third integer to be added. Okay, now you have your homework.  Go be a good student and work on it. Good luck in your coding and remember that every little space and comma counts. See you around fellow Cybrarians! *Ghost hug* you can’t feel it but it’s there! ^_^