1 / 12

Computer Programming 1

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

chidi
Download Presentation

Computer Programming 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Programming 1 Introduction to the C Language

  2. 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)

  3. 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.

  4. 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.

  5. 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.

  6. 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

  7. 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

  8. 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.

  9. 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.

  10. 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

  11. 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

  12. I/O Statements - Output • The practical for this week is to develop some programs that print to the console.

More Related