Introduction to C programming

From Wikiversity
Jump to navigation Jump to search

Welcome to Introduction to Programming in C,

Goals[edit | edit source]

The goal of this course is to teach the basics of computer programming and software engineering, as well as the C programming language using free and open source tools available to users on all operating systems.

Prerequisites[edit | edit source]

You must be capable of using a computer (edit files in a text editor and know how to run a program) There's no need to understand compilers yet, that will be covered in the first lesson.

No previous knowledge of programming is required.

Development Timeline[edit | edit source]

This course is under active development. Readability and comments is finished as of 10/29/05. I hope to spend more time on this, and get up to the first homework within 2-3 weeks.

Enrollment[edit | edit source]

The teacher requests that you post your user name and where in the course you are on the Enrollment Page. This is so that he or she can judge how many people are taking the course, and how many office hours he needs to set and assignments he may need to grade. And so he can cry for help if it looks swamped. If you can, please keep your progress up to date.

Feedback[edit | edit source]

This content is very much a work in progress. Feedback on the content and how easily understood it is is greatly appreciated. Please leave those comments on the Talk page, Problems Page, or Forum.

Organization[edit | edit source]

Lessons in this course will come as a combination of reading from the C wikibook and lectures found here. The book will spend most of its time teaching the syntax of the language. The point of the lecture will be to explain the concepts behind the syntax and explain how to use them to write good programs.

In addition to lectures and readings, there are quizzes, homework, analysis, and projects. Quizzes are short series of questions and answers that you can use to check your knowledge. Homework is longer and generally involves a program to be written. Analysis sections actually take a small program apart and show you how it works and why it was written that way. Projects are large programming problems where collaborative effort is encouraged.

A lesson is not meant to cover a single lecture from a real world school. It is meant to cover a single topic. As such, some lessons will be significantly longer than others. Don't force yourself to do every lesson in the same amount of time. Relax and take it at a comfortable pace.

Chat resources[edit | edit source]

Sometimes static web pages just aren't enough. For more help with the material, we have office hours and our wiki forums. The wiki forums allows you to post a question to us, and we'll answer as we can. Feel free to answer a previous poster if you know the answer.

Office hours are held at <INSERT IRC SERVER AND ROOM HERE> on <INSERT DAY/TIME HERE>. You're welcome to stop in the room and see if people are there at any time, but we only promise to be present at the given hours.

Lessons[edit | edit source]

Lesson 1 - Introduction[edit | edit source]

Reading: Software needed and Introduction to Programming in UNIXw:Unix

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

Homework 1- Writing a calculator[edit | edit source]

Analysis 1: Your first program

Homework 1: Writing a calculator

Lesson 6- Debugging[edit | edit source]

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