Variable Control for C Programming - dummies

By Dan Gookin

In C programming, you can mess with the variable in your code by changing it into another type, giving it a new name altogether, or casting a spell upon the variable to meet your needs, benevolent or not.

Typecasting into disbelief

When is a float variable not a float? When it’s typecast into an int, of course. This trick is made possible in C by using the typecast. For example:

(int)debt

In the preceding line, the float variable debt is typecast to an int value. The int in parentheses directs the compiler to treat the value of debt as an integer.

Why would anyone want to do that?

Because sometimes a function requires a specific variable type and that type isn’t available. Rather than convert and juggle several variable types in one program, you can simply typecast a variable into the type you desire. It’s not a common trick, but it’s often necessary, as shown in That’s Not Right.

THAT’S NOT RIGHT

#include <stdio.h>
int main()
{
 int a,b;
 float c;
 printf("Input the first value: ");
 scanf("%d",&a);
 printf("Input the second value: ");
 scanf("%d",&b);
 c = a/b;
 printf("%d/%d = %.2fn",a,b,c);
 return(0);
}

Exercise 1: Type the source code from That’s Not Right into your editor. Build and run.

Here’s a sample run with the input in bold:

Input the first value: 3
Input the second value: 2
3/2 = 1.00

Obviously, it’s wrong that 3 ÷ 2 would somehow work out to 1.50. If the computer says it’s 1.00, the computer must be correct.

Or perhaps the computer is merely confused because in Line 12 of the source code, two int values are divided, and the result is assigned to a float. That doesn’t quite work, however, because dividing an integer by an integer in C yields an integer as the result.

The value 1 is the closest integer value to 1.50. So even though the computer is wrong, it’s doing exactly what it was told to do.

Exercise 2: Modify your source code, changing Line 12 to read

c = (float)a/(float)b;

Save the change. Build and run using the same values as just shown. Here’s the new output:

Input the first value: 3
Input the second value: 2
3/2 = 1.50

Better. That’s because you typecast variables a and b in the equation, temporarily allowing the compiler to treat them as floating-point numbers. Therefore, the result is what it should be.

How to create new things with typedef

You can get into loads of trouble with the typedef keyword. It can cast normal C words and operators from their consistent selves into all sorts of mischief.

THE PERILS OF TYPEDEF

#include <stdio.h>
typedef int stinky;
stinky main()
{
 stinky a = 2;
 printf("Everyone knows that ");
 printf("%d + %d = %dn",a,a,a+a);
 return(0);
}

In The Perils of typedef, the typedef statement at Line 3 defines the word stinky to be the same as the keyword int. Anywhere you can use int in the code, you can use the word stinky instead, as shown on Lines 5 and 7.

Exercise 3: Use the source code from The Perils of typedef to create a new program, demonstrating that a stinky variable type is the same as an int. Build and run.

Granted, the example in The Perils of typedef is rather silly; no serious programmer would set up a real program like that. Where typedef is used most often is in defining structures. The typedef statement helps to reduce the chunkiness of that activity.

Creating a Structure the Traditional Way shows how nested structure operation works, given a knowledge of structures:

CREATING A STRUCTURE THE TRADITIONAL WAY

struct id
{
 char first[20];
 char last[20];
};
struct date
{
 int month;
 int day;
 int year;
};
struct human
{
 struct id name;
 struct date birthday;
};

Using typedef to Define a Structure shows how the declarations take place if you were to typedef the structures:

USING TYPEDEF TO DEFINE A STRUCTURE

typedef struct id
{
 char first[20];
 char last[20];
} personal;
typedef struct date
{
 int month;
 int day;
 int year;
} calendar;
struct human
{
 personal name;
 calendar birthday;
};

In this Listing, the structure id is typedef’d to the name personal. That’s not a variable name; it’s a typedef. It’s the same as saying, “All references to struct id are now the same as the name personal.”

Likewise, the structure date is typedef’d to calendar. Finally, in the declaration of the structure human, the typedef names are used instead of the more complex structure definitions.

A NESTED STRUCTURE

#include <stdio.h>
#include <string.h>
int main()
{
  struct date
  {
    int month;
    int day;
    int year;
  };
  struct human
  {
    char name[45];
    struct date birthday;
  };
  struct human president;
  strcpy(president.name,"George Washington");
  president.birthday.month = 2;
  president.birthday.day = 22;
  president.birthday.year = 1732;
  printf("%s was born on %d/%d/%dn",
      president.name,
      president.birthday.month,
      president.birthday.day,
      president.birthday.year);
  return(0);
}

Exercise 4: Replace the name member in the human structure in A Nested Structure with a nested structure. Name that structure id and have it contain two members, char arrays, first and last, for storing an individual’s first and last names.

If you do everything correctly, the reference to the president’s name will be the variables president.name.first and president.name.last. Be sure to assign values to these variables in your code and display the results.

Exercise 5: Modify the source code from the project you create in Exercise 4 to use typedef, as shown in Using typedef to Define a Structure. Build and run.

It can be argued that using typedef doesn’t make your code any more clear than had you simply used good variable names and well-formatted text. For example, you may not use typedef simply because you don’t want to have to remember what you’ve defined. But you will encounter other code that uses typedef. Don’t let it freak you out.

The true advantage of using typedef with a structure is, possibly, that it saves you from typing the word struct too many times.