C Lesson 2 – Data types

So last time we did “Hello World!”, a classic start in programming and the introduction I had when I started teaching myself C.

This time we’re going to be looking at the variable types available in C. In C we have access to four main variable types:

  • char
  • int
  • float
  • double

We’ve seen these before in the Java series, so if you’re unfamiliar with what these variables are, feel free to check the Java series out on the blog.

We’ll take a look at declaring some variables now:

As you can see the syntax is very similar to the other languages we’ve looked at thus far, so if you’ve been following along there you’ll have no problem taking to C.

It may seem like we cannot do a great deal with C since there are so few variable types, but C does support arrays, which opens up more possibilities.

Arrays can be declared like so:

Remember to specify the size of the array, and ensure it is big enough to encompass the volumne of information you’re working with.

C also supports the creation of multi-dimensional arrays.

The inclusion of arrays allows you to do much more with your application, since char arrays can act as strings and you can store data with multiple dimensions.

Next we’ll be looking at comments, so that you can add notes to your code.

There are two types of comment in C and we’ve seen them both before in Java.

  • “//” For a single-line comment
  • “/**/” For a multi-line comment

C and C++ are very similar, but C++ supports object-oriented programming. This is because C++ was developed as an object-oriented extension o fthe C language. The C language is by default built into the C++ language, which means that you can regard C++, for all intensive purposes, a superset of C.

There is plenty of information that highlights more of the differences between the languages, such as the C++ has a greater emphasis on type checking. But for now we’ll assume that C++ just builds on C.

Next time

Next time, we’ll be looking at declaring and writing functions, as well as making a recursive function.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Design a site like this with WordPress.com
Get started