This is a LinuxChix course page. This and the other courses pages are yet to be ported to the new LinuxChix website. Please feel free to browse the course in the meantime.

We appreciate your patience.

LinuxChix(tm)

Variables in C

Online references:

Scope of variables

Kevin posted about scope of variables. A variable's scope is the part of the program in which using the variable name makes sense. In C, a variable can be declared at the start of any block eg

{
	int b;
	/* more code here */
}

and you can refer to it within that block but not outside it:

{
	int b;
	b = 7; /* will put 7 in the piece of memory called b */
}
b = 8; 	/* will probably cause a compile error, because outside its block b
	isn't defined */

Mary found some example code demonstrating scopes.

Kevin wrote about his understanding of static scoping in C.

Why so many types?

This discussion depends on a tiny bit of knowledge of how assembly code works, see this very brief overview and section 4 of the x86 assembly FAQ.

However, it is simply important here to know that when you compile a C program it is translated into CPU-specific machine code. When we talk about instruction sets below, we mean the assembly instructions that your code was translated into, not the C code itself.

a bit is a single value, set to 0 or 1. A byte is eight bits and can store every number between 0 and 255, using binary notation.

Val asked if anyone was interested in why there are so many C types (int, char, float and so on.

Julie wrote that C needs to run on all sorts of processors with all byte sizes.

Mary wrote that:

The basic model of a computer processer is that values are loaded from memory into registers, which are pieces of memory directly accessible to the CPU. The CPU can not manipulate values from memory, it can only access the registers.

There are a very small number of registers, somewhere in the range of ten to fifty depending on your processor model (and they're much faster to access than RAM too). There are two types of register for storing data - integer registers, and floating point registers.

Now, different processors have different size registers. Some have 16 bit registers (they can store 2^16 different numbers), some have 32 bit registers (they can store 2^32 different numbers) and some have 64 bits, or 8 bits. This is why C integers are not meant to be a specific size.

The reason for this is that there are some differences between performing arithmetic on integer (whole number) data and floating point (real number data, stored to a limited number of significant figures) arithmetic and the processer does integer arithmetic differently from floating point arithmetic. So C makes this distinction too.

Julie noted that this is only true of RISC (reduced instruction set) architectures, and that CISC (complex instruction set) architectures can manipulate values in memory. She also wrote that there are many architectures that use registers that aren't even multiples of 8, such as 24, 36 or 48 bit registers. Floating point is generally 16 or 32 bits, and doubles twice the size of that.

The point is: we have all these different types so that no matter which machine you are working on, you can declare something to be an int, and know that you are getting a single register worth of memory.

Val explained though that sometimes hardware drivers will need to copy a value from the hardware which is exactly 8 bits, in which case header files that define the size of 8 bits in terms of shorts, ints or longs will need to be used.

© LinuxChix 2000-2006 unless otherwise specified Hosting provided by Intel and OSU Open Source Lab.