Select Page

This is part two of a three-part introduction to C series. Please read Part One first if you have not done so already and are new to the language.

Variables

Variables are a medium with which to hold storage of some value in a program. Variables are implemented in every language in some way. In C, we type something called a “declaration” in order to create a variable in the following manner:

[sourcecode language=”cpp”]
int x;
[/sourcecode]

Note the keyword int. This is a special word signifying what type of variable is being declared. In this instance, we are declaring an integer variable named x. There are several different types available:

  • int: integer
  • char: character
  • float: floating-point
  • double: double-precision

For the most part, we only care about integers and characters. Integers, on a 32-bit machine, such as Windows 9X and higher, are 32 bits long or 4 bytes. A character is always 8 bits long or 1 byte. However, that’s going a little off track from the subject at hand…

The letter x, in this case, is the name that we have assigned to the variable. Any name is valid to use here as long as the following conditions are met:

  1. Names must start with a letter or an underscore
  2. Names cannot contain spaces
  3. Other than the first character, subsequent characters may be digits as well

So, that means that the following variables are good…

[sourcecode language=”cpp”]
int oneNum;
int _oneNum;
int _1Num;
int one_num;
[/sourcecode]

…and the following are examples of bad variables:

[sourcecode language=”cpp”]
int one Num; // cannot contain a space
int 1Num; // starts with a digit
[/sourcecode]

Variables are useful for putting values into. So, if we wanted to add two numbers, we might write code that looks something like this:

[sourcecode language=”cpp”]
void main()
{
int x = 5;
int y = 4;
int z;

z = x + y;
}
[/sourcecode]

Notice how we are using the equal sign (=) to assign values to the variables. We can assign values either during the declaration, after the declaration, or both. In the example above, the variable z now contains the sum of x and y; or 9, in this case. In order to display the value of a variable, we use the printf() function, just as we did for our first program. However this time, we add something more:

[sourcecode language=”cpp”]
void main()
{
int x = 5;
int y = 4;
int z;

z = x + y;
printf( “The sum is = %d”, z );
}
[/sourcecode]

That’s the same main function we wrote above, but this time we are printing the values of the variable z. In this example, we use %d to signify where variables should be printed. As the printf function parses the string, it comes to a special character — %d. When this combination is found, it takes the variable after the comma (in this case z) and replaces %d with the value of it. There are several of these special characters, some of which are:

  • %d: displays integers
  • %c: displays characters
  • %f: displays floating-points
  • %x: displays hexadecimals

Anyway, the result of the above program should look something like this:

The sum is = 9

Functions

According to K & R: a function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation. With properly designed functions, it is possible to ignore how a job is done; knowing what is done is sufficient. C makes the usage of functions easy, convenient, and efficient; you will often see a short function defined and called only once, just because it clarifies some piece of code.

In the previous article, you wrote your first program. We called it “Hello who?” Believe it or not, you wrote your first function:

[sourcecode language=”cpp”]
void main()
{
printf( “Hello Dan!” );
}
[/sourcecode]

The function name here is “main”. The “main” function has special meaning to the computer. When you run your executable, the “main” function is the starting point.

Let’s look at how to write our own function by using an example. To start, lets say that we want to print “Hello Dan!” from a function. To do this, we add a function above the “main” function like so:

[sourcecode language=”cpp”]
void MyPrintFunction()
{
printf( “Hello Dan!” );
}
[/sourcecode]

Now, in the “main” function, we can get the same result by calling this new function:

[sourcecode language=”cpp”]
void main()
{
MyPrintFunction();
}
[/sourcecode]

This program will now produce the same result that we expect:

 Hello Dan!

The general definition of a function is as follows:

[sourcecode language=”cpp”]
datatype functionname( paramater1, parameter2, …, parameterN )
{
// code to execute
// return datatype
}
[/sourcecode]

There are probably a few questions you are asking yourself now about the datatype and parameter list mentioned above. To start, let’s discuss the datatype. This value can be of any variable data type. What this tells the function is that you would like to “return” a piece of information from the function. For instance, let’s say you want to add two numbers together; you would do something like this:

[sourcecode language=”cpp”]
int Add( int num1, int num2 )
{
return ( num1 + num2 );
}
[/sourcecode]

Pretty easy, huh? No? What’s wrong? Oh! You don’t understand num1 and num2. These are variables, but because they are declared inside of the function header, they are considered to be parameters to the function named “Add”. To use this “Add” function, your “main” function might look like this:

[sourcecode language=”cpp”]
void main()
{
int x = 5;
int y = 4;
int z;

z = Add( x, y );
printf( “%d + %d = %d”, x, y, z );
}

[/sourcecode]

The output to this program would look like this:

5 + 4 = 9

In regards to the “Add” function, what has happened is that the x has gone into the num1 variable and y has gone into the num2 variable. The line, “return ( num1 + num2 );” sums num1 and num2 together, then takes that result and replaces “Add( x, y )” from the “main” function. That’s what the keyword, “return” does. It replaces the corresponding function portion of the calling statement. In this case, “Add( x, y )” is replaced with the integer value of 9 and then assigned to the variable z.

Input

Finally, I just want to throw out a quick example on input. Let’s say that you want to have the user enter a number into your program. To do so, your program might look like this:

[sourcecode language=”cpp”]
void main()
{
int x;

printf( “Please enter a number: ” );
scanf( “%d”, &x );
printf( “nThe number you entered is %d”, x );
}
[/sourcecode]

Assuming the user enters 77 as the input, the output would be:

Please enter a number: 77
The number you entered is 77

Two things to notice here. The first is the scanf() function. Just like printf(), the scanf() function is part of the stdio.h file. It’s purpose is to simply accept input. To do so, the first parameter we use is a string (just like in printf) that contains the special % characters determining what type of input to accept. The subsequent parameters are the variables that you would like to hold the input.

The second thing that you should notice is the ampersand (&). It’s not important to understand the underlying meaning of the ampersand right now. But what you should know is that in order for scanf() to work, you must use it in conjunction with your variables.