Let’s “C” How This Goes Again – Part 2

January 30, 2017 | Views: 5187

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

Estimated reading time: 9 minutes

Hello, fellow Cybrarians! Way back in December…umm…*checks own page* 12th – yeah I knew that – December 12th I wrote an article for C programming, an intro to C called ‘Let’s “C” How this Goes: Programming Introduction to C’.

Recap from the previous article: Skip to new content

I had hoped that an expert would flex those muscular brains of theirs and share that knowledge but you have little ‘ol guppy me, so here is part 2 of the ‘Let’s C how this Goes…” articles. I don’t claim to be the guru or ‘Yoda’ expert on this, just fair warning. However, I will do my best to help you learn the C programming language a bit more in depth. This one is a bit longer so I apologize for that in advance. By the way, I’m sure most of you know what a guppy is but for those of you who don’t it’s an itty bitty fish which can look cute and pretty but if eaten is more like that small snack bite, like eating a chip or two and there you go. So enough fish ‘n chips…or else we all might drop what we are reading and doing and go eat something, jeesh.

So just to recap, in the intro, we had talked about the basics such as the codes involved for your first ever C program in which we printed the statement “Hello World!.” Yeah, that was original wasn’t it? Just about everyone does that, but for simplicity’s sake we went along with that boring statement. We talked about the knick-knacks involved like brackets and parenthesis and semicolons and such. Just look at the intro article, don’t be lazy now…come on. Keep in mind that the beginning, though it can be tweaked, is pretty much the same as well as the end. It’s the middle part of the program which determines what you’re doing. For example are you writing a simple program which simple says (prints) ‘Hi how are you?’ or is it a small game or a calculator or what? Like saying are you creating Superman man or the bystander taking pictures of Superman as he flies away? See? Simple to complex, it’s up to you and you’re only limited by your imagination and the limitations of the programming language you are using.

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.php

That 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.

So here’s your 3rd program;

# 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 ;

    printf(“Your magical numbers added together is: %dn” ,sum ) ;

   

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! ^_^

Share with Friends
FacebookTwitterLinkedInEmail
Use Cybytes and
Tip the Author!
Join
Share with Friends
FacebookTwitterLinkedInEmail
Ready to share your knowledge and expertise?
21 Comments
  1. Just in case you copy and paste, then compile to receive this error:

    program.c:57: error: stray ‘342’ in program

    It turns out that you need to replace your double quotes in each line.
    If you look closely at what is copied from the website, you will see that the double quotation marks that surround the string literally are not the neutral (vertical) ones.
    Replace your left and right double quotes and then it works, it works!
    Yes I said that twice 🙂

  2. Good post…looking fa dis

  3. nice Nice… very informative to start with.students like me are learning this stuff.

  4. nice Nice… very informative to start with.

  5. Very Nice… very informative to start with.

Page 2 of 2«12
Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Support Cybrary

Donate Here to Get This Month's Donor Badge

 

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel