Before I begin improvising with this tutorial and possibly misinforming everyone reading, I recommend you, the reader, check out this site for its well-crafted C tutorials: http://www.cprogramming.com/tutorial.html. Also, I will try to better structure my tutorials to make them more readable.
Now, onto the topic of variables:
Creating:
In order to create a variable, a type and name must be specified. In this case, let's use the type int since it is the type the main function returns. The term int is an abbreviation for integer which is a whole number (such as 0).
The name can be anything, like a, b, apple, this_is_a_variable, etc. However, note the lack of spaces because, like function names, variable names cannot have spaces in them. Also, variables cannot have the name of existing variables or functions.
The command to create integers called x and y should look something like int x, y; and be placed at the top of a program or function. This means you cannot create a new variable in the middle of a function. This is unfortunately part of the C89 standard and therefore inflexible.
Setting:
To give this variable x a value of 5, simply add the command x = 5; after the variable declaration. Alternatively, the composite command int x = 5, y; works as both a creation of the integer x and an assignment of it's value to 5.
In this case, the equals sign does not mean x must equal 5, rather x is given the value of 5 until changed. By declaring x = 6 after these commands, the value of x is now 6 and the 5 is gone. Variables can also be set by other variables, like the statement y = x; indicates.
Printing:
By printing the value of a variable to the prompt, we ensure that the result of these commands is made visible. To print, the function printf will be used and another special sequence of characters introduced.
To print x, use the command printf("%d", x); after the declaration and assignment of x. The %d in the quotes will not be printed because it indicates a placeholder for a decimal integer. This placeholder is filled by the next parameter after the character string, x.
This will only print the value of x though. To give it a bit more context, the command printf("x: %d\n", x); will print x: 5 in this case. The symbols after x may look a bit messy, but remember that %d is for printing the integer and \n is to start a new line.
Here is code to demonstrate these concepts in action:
#include <stdio.h>
int main() { //declare integers x and y int x = 5, y; //re-assign value of x to 6 x = 6; //display value of x printf("x: %d\n", x); //assign to value of y the value of x y = x; //display value of y printf("y: %d\n", y); //wait for key press, exit getchar(); return 0; } | output in prompt: x: 6 y: 6 |
Remember to set the value of a variable before using it since an uninitialized variable tends to be a random value every time the program runs.
Types:
In addition to integers, there exist other types of variables, all of which hold number values. Here is a table of their various properties:
This table does not cover all data types available under C and may be inaccurate on certain types of computers. To verify that a size in the table is the same for your system or compiler, use the command sizeof with a data type: printf("sizeof int = %d", sizeof(int)); for example should output sizeof int = 4 to the prompt. If there is a discrepancy, the size of a data type tends to correspond to its range of numbers.
The float and double are the real number data types; they store whole numbers as well as numbers with fractions. The ranges provided may be misleading: both are capable of negative and positive numbers, so the smaller range is how close the number can get to zero (before becoming zero) and the larger range is how far away it can get from zero (before becoming + or - infinity).
Now that we have new data types to work with, we must know how to print them. Luckily, the only new special characters for printf are %u for all unsigned types, %d for all signed types, %g for floats, and %lg for doubles.
To demonstrate variable types and printing in action, here is an example which prints variables of the mentioned types:
To find out more about special character sequences for printf, check out the page http://www.dgp.toronto.edu/~ajr/209/notes/printf.html. For more information on data types found in C, read the website http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/CONCEPT/data_types.html.
The next post will discuss operators and arithmetic.
Types:
In addition to integers, there exist other types of variables, all of which hold number values. Here is a table of their various properties:
name | range | size |
int (signed) | -2,147,483,648 to +2,147,483,647 | 4 bytes |
unsigned int | 0 to +4,294,967,296 | 4 bytes |
short (signed) | -32,768 to +32,767 | 2 bytes |
unsigned short | 0 to +65,535 | 2 bytes |
char (signed) | -128 to +127 | 1 byte |
unsigned char | 0 to +255 | 1 byte |
float | 1.1754943508222875079687365372222e-38 to 3.4028234663852885981170418348452e+38 | 4 bytes |
double | 2.2250738585072013830902327173324e-308 to 1.797693134862315708145274237317e+308 | 8 bytes |
This table does not cover all data types available under C and may be inaccurate on certain types of computers. To verify that a size in the table is the same for your system or compiler, use the command sizeof with a data type: printf("sizeof int = %d", sizeof(int)); for example should output sizeof int = 4 to the prompt. If there is a discrepancy, the size of a data type tends to correspond to its range of numbers.
The float and double are the real number data types; they store whole numbers as well as numbers with fractions. The ranges provided may be misleading: both are capable of negative and positive numbers, so the smaller range is how close the number can get to zero (before becoming zero) and the larger range is how far away it can get from zero (before becoming + or - infinity).
Now that we have new data types to work with, we must know how to print them. Luckily, the only new special characters for printf are %u for all unsigned types, %d for all signed types, %g for floats, and %lg for doubles.
To demonstrate variable types and printing in action, here is an example which prints variables of the mentioned types:
#include <stdio.h>
int main() { //declare variables double real_number = -4.5; unsigned int four_billion = 4000000000; float pi = 3.1415927; short neg_thirty_thousand = -30000; //print in different formats printf("real_number = %g\n", real_number); printf("four_billion = %u\n", four_billion); printf("pi = %g\n", pi); printf("neg_thirty_thousand = %d\n", neg_thirty_thousand); printf("\n"); //blank line //print sizes (for signed and unsigned) printf("sizes of data types:\n"); printf("integer: %u\n", sizeof(int)); printf("short int: %u\n", sizeof(short)); printf("character: %u\n", sizeof(char)); //sizeof works for variables and types printf("float: %u\n", sizeof(pi)); printf("double: %u\n", sizeof(double)); getchar(); return 0; } |
output in prompt: real_number = -4.5 four_billion = 4000000000 pi = 3.14159 neg_thirty_thousand = -30000 sizes of data types: integer: 4 short int: 2 character: 1 float: 4 double: 8 |
To find out more about special character sequences for printf, check out the page http://www.dgp.toronto.edu/~ajr/209/notes/printf.html. For more information on data types found in C, read the website http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/CONCEPT/data_types.html.
The next post will discuss operators and arithmetic.
No comments:
Post a Comment