How To: Security-Oriented C Tutorial 0x02 - Variables && Data Types

Security-Oriented C Tutorial 0x02 - Variables && Data Types

Hello again, reader! In this post, we are going to cover some data types and how to declare variables. It shouldn't be too hard so just sit back, relax, grab some popcorn and enjoy the ride (while you still can!).

What Is a Variable?

For those who do not know, a variable is basically a container which holds some content like a cup which holds liquids. Why do we need variables? Well, variables are a convenient way of storing information and have a symbolic meaning, like in mathematics, e.g. x = 3 where x is equal to 3, or x contains the value 3. By doing this, we can use x where x "stands in place" of 3. Also, as the name suggests, we can change the value of variables as we go which is nice.

In C, variables are more than just basic containers. There are many types of variables and are not all the same just like cups may be used to hold water or bowls are used to hold food. Of course, some variables can be used to hold more than one type of content, like bowls can also be used to hold water.

Data Types

As previously stated, variables come in different types. Let's list some here. Note: These are all 32-bit sizes. 64-bit sizes may be different.

  • int - refers to an integer (whole number) and holds numbers like 10 or 42. Ints are 4 bytes long and can hold numbers from -2^31 to 2^31-1.
  • char - refers to a character and holds characters like 'c' or 'z'. Chars are 1 byte long and can hold values from -2^7 to 2^7-1 (-128 to 127).
  • void - refers to no type or an arbitrary type.
  • short - refers to an integer and holds numbers like 10 and 42. Shorts are 2 bytes in size and can hold numbers from 2^15 to 2^15-1 (32768 to 32767).
  • float - refers to a floating point number (decimal point included) of single precision and holds numbers such as 3.14 and 2.71. Floats are 4 bytes in size and goes up to 6 decimal digits.
  • double - refers to a floating point number of double precision and holds numbers such as 3.14 and 2.71. Doubles are 8 bytes in size and goes up to 15 decimal digits.
  • long - refers to an integer and holds numbers such as 10 and 42. Longs are 4 bytes in size and can hold numbers from 2^31 to 2^31-1.

These are not all of the data types. If you wish to go further and play with more, you can do some research.

Signed or Unsigned?

Some data types can be either "signed" or "unsigned". This means that they can either hold negative and positive values or just positive values. Usually by default, data types are signed and is not required to be specified, however, if you wish for a data type to be unsigned, you must explicitly state it. Unsigned values are able to hold two times the size of their signed counterparts and I will explain why and how this works in the next tutorial.

Example Code

So let's learn how to create some variables.

To declare a variable, you must first specify the data type followed by the variable name. Let's write up some code.

As you can see, each variable has a data type and a name. You can declare a variable by only typing the data type followed by the variable name as in the variable "num" then set it to a value using the "=" operator. Another way you can declare and set a value in one line is by simply combining the two as you can see in the variable "c".

Note: The double forward slashes represent the commenting of an entire line.

Style Note: For more legible code, please use variable names which describe its purpose, for example, here I used "num" to represent "number". Sometimes, you can get away with unclear names such as "c" which can represent "character", otherwise, try not to be too confusing like using random single characters or other random names.

Compiling and Running

Again, we compile using gcc and then run our program.

Result are as expected.

Printf - Format and Print Data

Once again, we use printf but this time it looks different! Yes, printf can be used to print data such as variables, but what's with the percentage characters and the extra parameter?

printf can be used to format and print data. On line 7, what you see in the first parameter "%d" means that print the string %d, but %d represents the formatting of a corresponding parameter, num. The %d tells printf that we want to format the variable num as an integer. Similarly on line 12, %c represents the character format and we say to printf, "Hey, format the variable c so that it prints as a character."

But what happens if we use %d on our "c" variable and %c on our "num" variable? Would that work?

Conclusion

So that's it for this tutorial. Play around with some variables and data types and the printf function and stay tuned for more tutorials! Until next time.

dtm.

Just updated your iPhone? You'll find new emoji, enhanced security, podcast transcripts, Apple Cash virtual numbers, and other useful features. There are even new additions hidden within Safari. Find out what's new and changed on your iPhone with the iOS 17.4 update.

3 Comments

But what happens if we use %d on our "c" variable and %c on our "num" variable? Would that work?

Answer is. no :( never trust the lama

%c returns ASCII value of num
If
Num=0;
Then %c returns 49-ASCII value

Share Your Thoughts

  • Hot
  • Latest