Intermediate Programming

with C

 

Table of Contents

Introduction

Basics

Variables

Input and Output

Boolean Expressions and Branching

Loops

Functions

Files

Arrays and Pointers

 

C Programming Final Test

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Chapter 2

BASICS OF C PROGRAMMING

 

Keywords in C

C has a relatively small set of keywords. They are:

 auto break case char const continue default
do double else enum extern float for
goto if int long register return short
signed sizeof static struct switch typedef union
unsigned void while        

Identifiers in C

C has its own rules for defining identifiers. They are:

 1. Identifers may use all alphanumeric characters and the Underscore character.
 2. Identifers must start with a letter
3. Identifiers in C are case sensitive. A variable named "LIST" is different from a variable named "list"



The MAIN function

C programs are made up completely of functions. All programs, including the simplest ones, must have at least one function called the "Main Function". The format for a function is:

 

<return type> <funciton name> (<arguments>) { function body }

 

The main function is declared like this:

void main(void) { }

The word "void" tells the compiler that this function returns no value. The word "main" is the name of the function. Every C program must have a function called "main." The "void" inside the parentheses means that the function requires no arguments. The body of the function goes inside the brackets.

 

Including Files

All C compilers come pre-packaged with a set of LIBRARY FUNCTIONS. These functions are functions that perform common tasks such as printing output, and getting input. To include these functions in your program you must use a statement that tells the compiler how to compile the program. This is called a COMPILER DIRECTIVE. .

Compiler directives always start with a "#" sign. The "#include" directive tells the compiler to include a library with the program. Actually it includes a HEADER FILE which contains declarations for the library. In our first program we will include the standard input/output header file that comes with the compiler. To do that we type:

 

#include <stdio.h>

 

The C compiler has a special directory where all its header files are stored. The "< >" brackets tell the compiler that the "stdio.h" file is located in this directory. The following is a very simple C program.


#include <stdio.h>

void main(void) { 

 /* This is a comment. anything in here is 
    ignored by the compiler */

  puts("This is a sample string");

}


The program starts by including the standard input/output header file. The next line is the heading for the main function. After the heading comes a comment. Comments are enclosed in between "/*" and "*/". The last statement is an output statement. The "puts()" function prints strings to the screen. The "puts( )" function is declared in the "stdio.h" file.