c programming language 4 developed in 1972 by dennis ritchie at at&t bell laboratories 4 used to...

23
C Programming Language Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories Used to rewrite the UNIX operating system Widely used on UNIX systems ANSI standardized in order to prevent the fragmentation of the language

Post on 20-Dec-2015

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

C Programming Language

Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories

Used to rewrite the UNIX operating system Widely used on UNIX systems ANSI standardized in order to prevent the

fragmentation of the language

Page 2: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

C Language Elements

Preprocessor directives– Commands processed by the preprocessor before

compilation– #include (especially for standard header files)

• #include <stdio.h>

– The text of the included file is replaces the #include– #define (especially for constants)

• #define PI 3.14

– Every instance of (e.g.) PI is replaced with 3.14

Page 3: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

C Language Elements

Comments (begin with ‘/*’ end with ‘*/’ )– /* HELLO THERE! */

Comments have no effect on the program and are ignored by the compiler

Be careful to close all comments! Used carefully, comments can help

document a program

Page 4: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

C Language Elements

A program is divided into one or more functions– This makes a program easier to understand (we can

ignore the details of a function)– We can also reuse someone else’s functions

Every C program contains a function called main – Execution of the program begins with the main

function

Page 5: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

C Language Elements

The main function begins with the heading: int main (void)

The remainder of the function is called the body of the function– It is enclosed in braces {, }– The body consists of declarations and executable

statements– Declarations names memory cells needed by the function

and tells what kind of data will be stored in each cell

Page 6: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

C Language Elements

Example: int my_age, your_age; Executable statements cause operations to

be performed

Page 7: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

intmain(void){

printf(“Hello world\n”);return(0);

}

Page 8: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

C Language Elements

Reserved words have special meanings in C and cannot be used for other purposes– Examples: int, void, return

Standard identifiers are words defined in the standard libraries and known to C.– They can be used, but shouldn’t be!

User-defined identifiers are words that you define in your program

Page 9: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

C Language Elements

Identifiers are used to name memory cells and operations that we define

Identifiers must respect the following three rules– An identifier must consist only of letters, digits,

and underscores– An identifier cannot begin with a digit– A C reserved word cannot be used as an identifier

Page 10: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

C Language Elements

Identifiers– The case of the letters is significant in C! (cat

and CAT are two different identifiers)– Choose meaningful names for your identifiers.

The people who maintain your programs will be eternally grateful!

– 1. Unless there is a good reason, don’t use the names of standard identifiers

– 2. There are no good reasons! Don’t do it.

Page 11: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

Variable Declarations and Data Types The memory cells which contain a

program’s input, intermediate results, and final results are called variables

Variable declarations tell the C compiler the names of all of the variables which will be used in a program and the kinds of values they will store

Page 12: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

Variable Declarations and Data Types The form of a variable declaration is: type

variable_name;– If we have more than one variable of the same

type, we can give the type just once and separate the variables with commas: type var1, var2, var3;

The type must be one of the predefined C data types (greatly simplified explanation!)

Page 13: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

Variable Declarations and Data Types Data types are a set of values and a set of

operations on those values– The set of integers is one such set of values.

What are the operations defined on this set?– Among the predefined data types of C are char, double, and int which represent characters, real numbers, and integers

– The range of data type int is at least -32767 through 32767

Page 14: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

Variable Declarations and Data Types Values of type double have both an integral

and a fractional part, separated by a decimal point: 3.141, 0.1111, etc.– We can also represent real numbers using

scientific notation: 5.001e3 or 5.001E3 which means

– Either one of these representations can be used in a C program

Page 15: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

Variable Declarations and Data Types Data type char represents an individual

character (not a string!)– Each character is enclosed in apostrophes: ‘A’,

‘b’, ‘ ‘

The values of a data type: ‘a’, 12, 4.0e3 are called literals when used in a program

Page 16: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

Executable Statements

The executable statements follow the declarations in a function. (Why?)

These statements implement the algorithm previously developed

The statements must be translated into machine language prior to execution

We will now examine several common types of statements

Page 17: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

Executable Statements

Assignment statements are perhaps the most common of all statements. They assign a value to a variable (a named memory cell)– The syntax for an assignment statement is:

variable = expression;– Note that an assignment statement will always

have a variable on the left hand side, never a literal

Page 18: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

Executable Statements

Examples of assignment statements:– a = 2.0;– b = ‘c’;– c = a;– a = 2.0 + 7.0 * e;– ‘b’ = ‘c’;– j = k/l;

Page 19: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

Executable Statements

Input/Output operations are performed by special-purpose functions in C– scanf is an input operation

– printf is an output operation

– These functions are part of the standard i/o library

– In order to use them we must have the following statement in our program: #include <stdio.h>

Page 20: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

Executable Statements

In order to activate a function like printf or scanf we use a statement called a function call– This statement causes the executable statements

contained in the function to be executed.

The function printf is called with two arguments, a format string and a print list.– printf(“That equals %f kilometers.\n”, kms);

Page 21: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

Executable Statements

The arguments to a function are pieces of information that the function needs in order to be executed

The arguments to a function are enclosed in parentheses and separated by commas.

In the printf function, for each variable whose value we want to print, we must have a corresponding placeholder in the format string

Page 22: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

Executable Statements

The placeholders for the data types we have seen are given below:– %c char

– %d int

– %f double (used in printf only)

– %lf double (used in scanf only)

– Example:• printf(“%c %d %f That’s all!\n”, ch, real, integer);• Note that the ordering of the placeholders is important!

Page 23: C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX

Executable Statements

The \n pair of symbols is called an escape sequence (used to represent a non-printing character) and causes the cursor to go to the next line in the output– Example: printf(\nline two\nline three\n”);

The printf statement can be used to prompt the user to enter some input