Presentation is loading. Please wait.

Presentation is loading. Please wait.

C OMPUTER P ROGRAMMING 1 Introduction to the C Language.

Similar presentations


Presentation on theme: "C OMPUTER P ROGRAMMING 1 Introduction to the C Language."— Presentation transcript:

1 C OMPUTER P ROGRAMMING 1 Introduction to the C Language

2 T HE C P ROGRAMMING L ANGUAGE 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)

3 T HE C P ROGRAMMING L ANGUAGE 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.

4 T HE C P ROGRAMMING L ANGUAGE 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.

5 T HE C P ROGRAMMING L ANGUAGE 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.

6 I/O S TATEMENTS - O UTPUT 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

7 I/O S TATEMENTS - O UTPUT In order to execute the statement, it must be part of a complete program. The complete C program to display the message is: #include 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 will be explained later

8 I/O S TATEMENTS - O UTPUT 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.

9 I/O S TATEMENTS - O UTPUT 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.

10 I/O S TATEMENTS - O UTPUT 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

11 I/O S TATEMENTS - O UTPUT 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

12 I/O S TATEMENTS - O UTPUT The practical for this week is to develop some programs that print to the console.


Download ppt "C OMPUTER P ROGRAMMING 1 Introduction to the C Language."

Similar presentations


Ads by Google