# [ CPS 125 / Teaching Assistant ] Variables

Please note that this post is for the students in the course in CPS 125 (CPS125 : Digital Computation and Programming), the reason for making these posts are 1) aid the students in the course, 2) to improve my communication skills via writing.

Disclaimer: I was not instructed to write these posts, additionally, I won’t provide any materials directly related to solutions for the assignments.

Introduction

Congrats for enrolling in CPS 125! My name is Jae, currently, a first-year masters student at Ryerson, supervised by Dr. Neil D, Bruce. (doing something related to machine learning/optimization).

I took this course 4? years ago, and I loved it. I hope these posts will help you on your journey on learning C.

I am a firm believer that the best way to learn to programme is from doing it yourself, in this post, I would provide screenshots of how each programming is running. But I am not going to give you the source code, you can’t learn to how to program from copy and paste!

To make our life easier, we can use an online C compiler, so we can directly code online! There are hundreds of those websites, I’ll just link one here.

Let's get right to it, we can print stuff out from our program directly by using the printf() function.

Next, we can declare variables, in very general term variables, are the things you use inside your program. (these variables can be an integer like 3 or 4. Float like 3.76 or 7.23. Strings “Like This” or a character like ‘a’).

Let's see them in action using the printf function we just learned above.

When we want to print those variables, we should use a special term such as “%d” or “%f”.

A good summary of what type of variables we can print for each letter we use can be seen above.

We can see that the integer can be printed as such.

When we try to print out a floating point number using the “%d” character, we can see that the printed number is not 9.0! To avoid those errors make sure to use the correct letter for each type of variable you wish to print.

When we try to use “%s”, we notice that it does not even print out the number 9.0.

So now we declared some variables, let's do something with them, such as adding or subtracting.

Just like that we can add, subtract, divide, and even multiply our original number 9 to get some new numbers. Now let's print some characters and strings.

One interesting thing to note here is the fact that we declared a string using the ‘char’ term, but we added [100] term to denote that this sentence can have a length up to 100. (100 characters make up a sentence.).

Now using the scanf function we can give specific input to a program, but while doing this make sure you know the type of the variable.

As seen above, when I inputted the word “sentence” to the variable type integer we get some random answer 0.

Hope this short article helps your understanding of variables in C, make sure to practice on your own!

Reference

1. (2019). Cps125.scs.ryerson.ca. Retrieved 20 January 2019, from http://cps125.scs.ryerson.ca/slides/02-C.pdf
2. A printf format reference page (cheat sheet) | alvinalexander.com. (2019). Alvinalexander.com. Retrieved 20 January 2019, from https://alvinalexander.com/programming/printf-format-cheat-sheet

https://jaedukseo.me I love to make my own notes my guy, let's get LIT with KNOWLEDGE in my GARAGE

## More from Jae Duk Seo

https://jaedukseo.me I love to make my own notes my guy, let's get LIT with KNOWLEDGE in my GARAGE