C OMPUTER P ROGRAMMING 1 Introduction to the C Language.

12
COMPUTER PROGRAMMING 1 Introduction to the C Language

Transcript of C OMPUTER P ROGRAMMING 1 Introduction to the C Language.

Page 1: C OMPUTER P ROGRAMMING 1 Introduction to the C Language.

COMPUTER PROGRAMMING 1Introduction to the C Language

Page 2: C OMPUTER P ROGRAMMING 1 Introduction to the C Language.

THE C PROGRAMMING LANGUAGE C was invented between 1969 and 1973 by Dennis

Richie for AT&T Bell Labs. Standardised in 1989 as “ANSI C” C is one of the most widely used languages of all time

Designed for cross-platform programming Idea: the same source code can be compiled on different

operating systems / hardware

C is still used for systems programming: Operating Systems (e.g. UNIX) Embedded Systems (e.g. sensor platforms)

Page 3: C OMPUTER P ROGRAMMING 1 Introduction to the C Language.

THE C PROGRAMMING LANGUAGE A C program is made up of a group of statements.

These statements allow us to control the computer.

Using them we can display information on the screen, read information from the keyboard, store information on disk and retrieve it and we can process information in a variety of ways.

In this section we will study the statements that arise in writing programs.

Page 4: C OMPUTER P ROGRAMMING 1 Introduction to the C Language.

THE C PROGRAMMING LANGUAGE We can classify statements as follows:

I/O statements, variable declaration statements, variable manipulation statements (e.g. to do arithmetic)

and conditional statements.

We also look at the use of subprograms, which enable to us to break large programs into meaningful units.

Associated with the different types of statement is a set of special words called reserved words (keywords). Every programming language has its own set of reserved

words. These words have a special meaning in the language and

can only be used for particular purposes.

Page 5: C OMPUTER P ROGRAMMING 1 Introduction to the C Language.

THE C PROGRAMMING LANGUAGE The following are some of the reserved words of the C

language that will be used in this text:

int, float, char, if, while

All C code and variable names will be printed using the Courier font in these notes.

Page 6: C OMPUTER P ROGRAMMING 1 Introduction to the C Language.

I/O STATEMENTS - OUTPUT Output is the term used to describe information that

the processor sends to peripheral devices e.g. to display results on a screen or to store information in a

disk file.

One of the commonest forms of output is that of displaying a message on your screen. In C we use printf() to display output on the screen.

The following printf() statement will display the message

this is my first program

on the screen

printf ( “this is my first program” );

This is a single C program statement

Page 7: C OMPUTER P ROGRAMMING 1 Introduction to the C Language.

I/O STATEMENTS - OUTPUT In order to execute the statement, it must be part of a

complete program. The complete C program to display the message is:

#include <stdio.h>main(){

printf ( “this is my first program“ );}

All C programs start with main() followed by the opening chain bracket {, and finish with a closing chain bracket }

The line #include <stdio.h> will be explained later

Page 8: C OMPUTER P ROGRAMMING 1 Introduction to the C Language.

I/O STATEMENTS - OUTPUT You may have as many statements as you wish

between the chain brackets.

The chain brackets are used to group a collection of statements together. Such a group is called a compound statement. A single statement is called a simple statement.

A compound statement is treated as if it was a simple statement and may be used anywhere that a simple statement may be used.

This is a very important feature of C. Other programming languages (e.g. Fortran, BASIC) at the

time did not provide compound statements, and it is more difficult to write programs in these languages as a result.

Page 9: C OMPUTER P ROGRAMMING 1 Introduction to the C Language.

I/O STATEMENTS - OUTPUT main() and printf() are technically called

functions.

In simple terms, in C, a function is a named group of statements that carry out some task.

The main() function is the one which defines what a C program does when you run it – it carries out the statements contained in main()

The printf() function is pre-defined for you. It displays information on the screen. It is one of a large number of pre-defined functions (library functions) that you may use.

Functions will be discussed later and you will see how to define your own functions.

Page 10: C OMPUTER P ROGRAMMING 1 Introduction to the C Language.

I/O STATEMENTS - OUTPUT Special characters allow you to enter characters

that cannot be expressed directly using a keyboard.

They are denoted by a backslash character ‘\’ (known as the escape character) combined with another character.

For example, the character ‘\n’ (called newline) is used in printf() to cause the next output to go onto a new line.

The following printf() statement illustrate the use of ‘\n’:printf(“one\ntwo\nthree\n“);

produces as output:onetwothree

Page 11: C OMPUTER P ROGRAMMING 1 Introduction to the C Language.

I/O STATEMENTS - OUTPUT It is interesting to note that the following sequence of printf() statements:

printf(“one\n”);printf(“two\n”);printf(“three\n“) ;

produces identical output to the single printf() on the previous slide i.e.

onetwothree

Page 12: C OMPUTER P ROGRAMMING 1 Introduction to the C Language.

I/O STATEMENTS - OUTPUT

The practical for this week is to develop some programs that print to the console.