140 likes | 305 Views
Computer Programming 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
E N D
Computer Programming 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)
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.
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.
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.
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
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
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.
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.
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: one two three
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. one two three
I/O Statements - Output • The practical for this week is to develop some programs that print to the console.