C Programming/Variables
Objective edit
|
Lesson editIntroduction editThe most fundamental concept in C (and most other programming languages) is the variable. A variable is like a container. You can store things in it for later use, particularly numbers. The concept of a variable is borrowed from mathematics. A statement such as x = 1 // This is a variable statement in Math, not in C programming stores the value 1 in the variable x. In C, it is much the same. To assign a value to a variable, you can write: x = 1;
But before you can do this, you must declare a variable. You declare a variable by specifying it's data type and name, like so: data_type variable_name;
The programmer may combine declaration and assignment, which is known as initialization. Here is the variable x being declared and initialized: int x = 1;
Every variable has a name. Whenever you want to use a variable, you specify what variable you want to use by using its name. The set of rules, ways and syntax we used to name a variable, as well as other programming terminology like function, source files,... is known as naming convention. In C, a variable name can be any combination of letters, digits, and underscores (the _ character) you want, so long as the first character is not a digit. Picking a good name makes your program much easier to read and understand. A good variable name describes exactly what the data it holds is. For example, if a variable stores the current temperature, you can name it current_temperature. If it stores the score of a video game , name it player_score. However, putting the full words for a variable, like current_temperature, can make the variable's name to be be very long when the situation involved with this variable is complicated. That's why we are also expected to use abbreviation in this case. For example, current_temperature could be abbreviated to cur_tem. However, you should also be careful with abbreviation, as it might save you a bit of typing, but it can remove clarity from your code in some ways. You should really only use abbreviations if there is only 1 possible interpretation for it, and if its obvious what that interpretation is by the context of the program. For example if using the abbreviated word temp, does it mean temperature or temporary ? Things can even get more confuse when your variable need to stored the temporary temperature, should it be temp_tem ? In those situations, it's recommended to use the full names, its not that many more keys. Variable naming convention provides one more guideline with the syntax to name the variables. It involves with the use of capitalized and uncapitalized letters, as well as the underscore symbol. This results in three naming conventions: under_score, camelCase, and PascalCase. A few examples of underscore: current_temperature, temp_variable, this_is_a_long_name,... A few examples of the camelCase: currentTemperature, tempVariable, thisIsALongName,... A few examples of the PascalCase: CurrentTemperature, TempVariable, ThisIsALongName,... All those ways are fine, it's just a matter of preference/readability and also depends on the coding standard or the naming convention expected in your project. Data Types and Qualifiers editData types are the way in which memory is abstracted away from the programmer. It allows us to store memory in a particular way so that code can be portable, and save us from constantly worrying about data sizes. For example, if data were just stored as a number of bits, what happens if you move to an (excedingly rare) 9-bit-byte machine? Or one where by default chars are 16-bit? Data types stop a programmer having to be concerned about these things. C is strongly typed, which means that variables must be explicitly assigned a data type. Data types indicate the type of data a variable can hold. When a variable is defined, a memory location will be assigned to the newly defined variable and it will also define the type of data that memory location will hold. C has 4 basic data types:
In addition to basic data types C also defines certain qualifiers to these data types. Qualifiers are used to make variable declaration more specific to variable uses. Qualifiers available in the C language are:
Using these qualifiers, basic data types can be flavoured in many ways as shown in the table below. Note that the values given are acceptable minimum magnitudes defined by the C Standard - each implementation will define values greater or equal in magnitude.
Note: a Standard conforming compiler can ignore these qualifiers; nevertheless, it must issue a message when an attempt is made to alter an object declared as const. Storage Classes editStorage classes define the longevity and scope of variables and functions. There are two types of storage classes: automatic and static. There are several storage class specifiers:
Storage classes can be remembered easily by remembering mnemonic RASE, i.e., register, auto, static, and extern. Example: the arithmetic mean of two numbers editThe following source code is compiled with GCC: #include <stdio.h>
void main(void)
{
int a, b;
float avg; // data type
printf("Enter the a:");
scanf("%d", &a);
printf("Enter the b:");
scanf("%d", &b);
avg = (a + b) / 2; // expression
printf("%f", avg);
getch(); // getchar() may work instead
return 0;
}
Expressions editTo manipulate the variable, ‘a’, declared and defined in the previous section, an expression is needed. By definition, an expression, in C, is an interpreted combination of values, variables, operators or functions. There are a number of operators available including addition, ‘+’, subtraction, ‘-‘, division ‘/’, and multiplication ‘*’. In an expression, the variable name on the left side of the assignment operator represents the area of memory that stores interpreted results. Variable and constants on the right side of the assignment operator are interpreted to determine a result prior to assignment. Note these definitions and declarations: int a;
int b;
a = 0;
b = 8;
What follows is a statement which manipulates storage in memory (an expression becomes a statement when it is followed by a semicolon): a = b + 24;
In this statement, the constant ‘24’, is added to the value stored in the variable ‘b’. The result of that calculation, then, is assigned to a memory location, symbolically represented by the variable ‘a’. After the interpretation of the statement, the variable 'a' is assigned the value 32. Typecasting editC has the concept of data types: int a;
float b;
When you assign b to a: a = b;
An int is converted to a float implicitly. However, (potentially) dangerous conversions are forbidden and stopped by the compiler: /* * means pointer, you will learn them later.
Assigning between pointers of different types is dangerous unless their layout is compatible */
int* x;
float* y;
y = x;
Converting a float* to an int* is dangerous and will be blocked by the compiler. If you must perform this operation, use the following typecast: y = (float*) x;
A type inside a pair of () is to force change x to a float* so that it can be assigned to y. If the compiler doesn't know how to convert, the result is unspecified. One can use casts to circumvent the type system of C. Casts are dangerous, but sometimes needed. External resources edit |
Assignments edit
|