Introduction to C programming

From Wikiversity
Jump to navigation Jump to search

Lessons[edit | edit source]

Lesson 1 - Introduction[edit | edit source]

Reading: Software needed

Lecture: Hello World

Lesson 2 - Variables[edit | edit source]

Reading: Variables, Simple Math, and C types (this section of the page only)

Lecture: Variables

Quiz: Variables

Lesson 3 - Boolean logic and flow control[edit | edit source]

Reading: Flow control Read all of section 1 (on ifs) except the part on bitwise operators. Section 2 will be covered later.

Lecture: Boolean Logic and If

Quiz: Quizes/Boolean Logic

Lesson 4 - Comments and readability[edit | edit source]

Reading: C Structure and Style

Lecture: Comments and Readability

Lesson 5 - Basic IO[edit | edit source]

Reading: Simple IO

Lecture: BasicIO

Lesson 6 Debugging[edit | edit source]

Debugging is searching for mistakes in your program. No one is perfect, we all make mistakes. Despite having programmed for nearly a decade, I made several errors while writing the first few programs for this class. If you're going to program, you're going to need to learn to debug. This class will teach some basic debugging techniques and common mistakes along with the rest of the material. Don't treat this as a minor part of the class - you will generally spend more time debugging a program than you will writing it.

Lesson 7 Loops[edit | edit source]

Reading: Understanding and Implementing Loops

Lesson 8 Arrays[edit | edit source]

Let'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:

<syntaxhighilight lang="c"> 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" );
   }

} </syntaxhighlight>

Homework 2[edit | edit source]

... (breaking up the rest of the class)

Other sources[edit | edit source]

Here's some other sources, both plain text and web, that you may find helpful.

  • Topic:C
  • The C Programming Language - Kernighan and Richie