1 / 23

C Programming Language

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. C Language Elements. Preprocessor directives

gtheriot
Download Presentation

C Programming Language

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

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

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

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

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

  6. C Language Elements • Example: int my_age, your_age; • Executable statements cause operations to be performed

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

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

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

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

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

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

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

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

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

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

  17. Executable Statements • Examples of assignment statements: • a = 2.0; • b = ‘c’; • c = a; • a = 2.0 + 7.0 * e; • ‘b’ = ‘c’; • j = k/l;

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

  19. 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);

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

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

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

More Related