Introduction to C programming
Warning: This is an out-update course and is being merged into https://en.wikiversity.org/wiki/C_Programming |
Variable and Math
editBoolean logic
editQuiz: Quizes/Boolean Logic
Comments and readability
editReading: C Structure and Style
Lecture: Comments and Readability
Basic IO
editReading: Simple IO
Lecture: BasicIO
Loops
editReading: Understanding and Implementing Loops
Arrays
editLet's look at the syntax for declaring an array.
int examplearray[100]; /* This declares an array */
This would make an integer array with 100 slots (the places in which values of an array are stored). To access a specific part element of the array, you merely put the array name and, in brackets, an index number. This corresponds to a specific element of the array. The one trick is that the first index number, and thus the first element, is zero, and the last is the number of elements minus one. The indices for a 100 element array range from 0 to 99. Be careful not to "walk off the end" of the array by trying to access element 100!
What can you do with this simple knowledge? Let's say you want to store a string, because C has no built-in datatype for strings, you can make an array of characters.
For example: char astring[100];
will allow you to declare a char array of 100 elements, or slots. Then you can receive input into it from the user, and when the user types in a string, it will go in the array, the first character of the string will be at position 0, the second character at position 1, and so forth. It is relatively easy to work with strings in this way because it allows support for any size string you can imagine all stored in a single variable with each element in the string stored in an adjacent location--think about how hard it would be to store nearly arbitrary sized strings using simple variables that only store one value. Since we can write loops that increment integers, it's very easy to scan through a string:
char astring[10]; int i = 0; /* Using scanf isn't really the best way to do this; we'll talk about that in the next tutorial, on strings */
scanf( "%s", astring ); for ( i = 0; i < 10; ++i ) {
if ( astring[i] == 'a' )
{
printf( "You entered an a!\n" );
}
}
Function
editVariable scope in function
editA variable's scope limits where in the program you can access it. That's right, you can't always access every variable. There are 4 types of scope in the C language:
- Global scope. These variables can be accessed anywhere in the program. You should use these sparingly, if your program needs a lot of globals you are probably doing something wrong. You can declare a global variable just by defining it in any file outside of any functions. In any other file you want to use it, you need to use export type variable;
- File scope. These variables can be accessed anywhere in the file its declared. These are a bit better than global variables, but you should still try and minimize them. You can declare one by declaring a variable at global scope with the static keyword.
- Local scope. These variables can be accessed only within the nearest set of {}. Any variable inside a set of {} (also know as inside of a block) are automaticly local scope. Every time you go inside the block you create a new version of the variable. The old value of the variable no longer exists. These are the vast, vast majority of variables.
- Static local scope. These variables are just like local scope, except they keep their value between times you enter a block. You can declare one by creating a local variable with the static keyword. These should be used sparingly, its extremely rare to see one of these.
Sometimes you may end up with two variables in different scopes with the same name (usually 1 file or global and 1 local). In that case, you end up getting the local variable, not the global one.
So, why does scope exist? Well, in a program you frequently want to call several things by the same name in different places. For example, a variable newBalance may exist in a checking program in the code where you withdraw money and where you deposit money. If all variables were global, you'd need two names. With local variables, you can use the same name for two different variables in two different blocks. Allowing you to reuse names like that makes it much easier to use good names. It also stops you from accidentaly accessing variables that other parts of the program may be using, because you unknowingly chose the same name.
Pointer
editvoid type
editvoid type has 0 size and cannot be used as a type. The only time it's used is when doing pointer-type conversions. Note that, confusingly, when a void-pointer is accessed (ie. the value which should be void), a single byte is accessed (if possible). The main reason for not causing an error is probably that a void pointer is used for functions to move data, and constant conversions being written in the code, while not slowing it down, can obfuscate it, plus the void-pointer already stores an actual memory address to access.