Download presentation
Presentation is loading. Please wait.
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 systems 4 ANSI standardized in order to prevent the fragmentation of the language
2
C Language Elements 4 Preprocessor directives –Commands processed by the preprocessor before compilation –#include (especially for standard header files) #include –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
3
C Language Elements 4 Comments (begin with ‘/*’ end with ‘*/’ ) –/* HELLO THERE! */ 4 Comments have no effect on the program and are ignored by the compiler 4 Be careful to close all comments! 4 Used carefully, comments can help document a program
4
C Language Elements 4 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 4 Every C program contains a function called main –Execution of the program begins with the main function
5
C Language Elements 4 The main function begins with the heading: int main (void) 4 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
6
C Language Elements 4 Example: int my_age, your_age; 4 Executable statements cause operations to be performed
8
C Language Elements 4 Reserved words have special meanings in C and cannot be used for other purposes –Examples: int, void, return 4 Standard identifiers are words defined in the standard libraries and known to C. –They can be used, but shouldn’t be! 4 User-defined identifiers are words that you define in your program
9
C Language Elements 4 Identifiers are used to name memory cells and operations that we define 4 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
10
C Language Elements 4 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.
11
Variable Declarations and Data Types 4 The memory cells which contain a program’s input, intermediate results, and final results are called variables 4 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
12
Variable Declarations and Data Types 4 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; 4 The type must be one of the predefined C data types (greatly simplified explanation!)
13
Variable Declarations and Data Types 4 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
14
Variable Declarations and Data Types 4 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
15
Variable Declarations and Data Types 4 Data type char represents an individual character (not a string!) –Each character is enclosed in apostrophes: ‘A’, ‘b’, ‘ ‘ 4 The values of a data type: ‘a’, 12, 4.0e3 are called literals when used in a program
16
Executable Statements 4 The executable statements follow the declarations in a function. (Why?) 4 These statements implement the algorithm previously developed 4 The statements must be translated into machine language prior to execution 4 We will now examine several common types of statements
17
Executable Statements 4 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
18
Executable Statements 4 Examples of assignment statements: –a = 2.0; –b = ‘c’; –c = a; –a = 2.0 + 7.0 * e; –‘b’ = ‘c’; –j = k/l;
19
Executable Statements 4 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
20
Executable Statements 4 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. 4 The function printf is called with two arguments, a format string and a print list. –printf(“That equals %f kilometers.\n”, kms);
21
Executable Statements 4 The arguments to a function are pieces of information that the function needs in order to be executed 4 The arguments to a function are enclosed in parentheses and separated by commas. 4 In the printf function, for each variable whose value we want to print, we must have a corresponding placeholder in the format string
22
Executable Statements 4 The placeholders for the data types we have seen are given below: –%cchar –%dint –%fdouble (used in printf only) –%lfdouble (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!
23
Executable Statements 4 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”); 4 The printf statement can be used to prompt the user to enter some input
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.