Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.

Similar presentations


Presentation on theme: "Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost."— Presentation transcript:

1 Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost 30 years. A language with many high quality numerical libraries available. Available for nearly all computers and operating systems. Excellent for specialty supercomputers. All the above.

2 Chapter 1 slides2 Who created C? Brian Kernighan and Dennis Ritchie at AT&T Bell Labs. Simultaneous development with the UNIX (now primarily Linux) operating system. Millions and millions and millions of lines of code have been written in C. C libraries have evolved over time and are generally of very high quality.

3 Chapter 1 slides3 C compilers Are readily available Some are free: –GNU project, Free Software Foundation Some are everywhere, but cheap –Microsoft Visual, … –Borland … –Metrowerx

4 Chapter 1 slides4 Structure of a C program Program execution begins in a special function called main. C is a block-structured language. The fundamental unit of source is the statement. Statements are usually terminated by a ; Blocks of code (multiple source code statements) are enclosed in braces { }

5 Chapter 1 slides5 The vocabulary of C The C programming language has about 63 reserved words. There are reserved punctuation marks: { } [ ] ( ) ; + - = * / are the ones we will use at first Variables must have names. Variables and constants must have types. Syntax (and semantics) matter!

6 Chapter 1 slides6 A simple example /* This is our first program. */ int main(void) { printf(“Hello, World”); }

7 Chapter 1 slides7 A simple example /* This is our first program. */ int main(void) Execution starts here { printf(“Hello, World”); }

8 Chapter 1 slides8 A simple example /* This is our first program. */ int main(void )Execution starts here { printf(“Hello, World”);Here is the type }

9 Chapter 1 slides9 A simple example /* This is our first program. */ int main(void ) Execution starts here { printf(“Hello, World”);Here is the type } Here is a function

10 Chapter 1 slides10 A simple example /* This is our first program. */ int main(void ) Execution starts here { printf(“Hello, World”);Here is the type } Here is the function’s argument Here is a function

11 Chapter 1 slides11 A simple example /* This is our first program. */ int main(void ) Execution starts here { printf(“Hello, World”);Here is the type } Here is the function’s argument Here is a function The argument is a character string enclosed by quotes

12 Chapter 1 slides12 The program can be improved Use the library function by including the header file stdio.h Comment the program to make is clear what it does and who wrote it.

13 Chapter 1 slides13 The improved program /* This is a comment for the Hello, World program written by A. B. Cee on 1/5/08. */ #include int main(void) { printf(“Hello, World”); }

14 Chapter 1 slides14 Note the matching delimiters /* This is a comment for the Hello, World program written by A. B. Cee on 1/5/08. */ #include int main() { printf(“Hello, World”); }

15 Chapter 1 slides15 Some compilers require more information about main /* This is a comment for the Hello, World program written by A. B. Cee on 1/5/08. */ #include int main( ) { printf(“Hello, World”); }

16 Chapter 1 slides16 Some compilers require more information about main /* This is a comment for the Hello, World program written by A. B. Cee on 1/5/08. */ #include int main( int argc, char *argv[] ) { printf(“Hello, World”); }

17 Chapter 1 slides17 What does the #include mean? This is an instruction to the pre-processor. In this example, code for the linkage to the standard I/O functions is included. We will see other pre-processor directives later.

18 Chapter 1 slides18 What is a compiler? Software to take source code and analyze it for syntax and the language’s semantics. Output of the compiler can be object code, which is machine readable. If the source code has errors, error messages are produced, instead.

19 Chapter 1 slides19 What happens next? If the compiler accepts the source code as being correct, the linker links the object code with the object code of all functions used. If the linker finds all necessary object code, it loads them together and creates an executable file. If not, error.

20 Chapter 1 slides20 What if there are errors? Who fixes the errors? The programmer. Once all the errors are fixed, is the program done? No. It must be tested carefully and the output compared to the correct results. How? This iterative process is the heart of the course.

21 Chapter 1 slides21

22 Chapter 1 slides22 Elementary Data Types in C int (for variables that consist of a single integer) char (for variables that consist of a single character) float (for floating point numbers) double (for double precision floating point numbers)

23 Chapter 1 slides23 How is data stored? The primary unit is the byte, or 8 bits. A bit can take only the values 0 or 1.

24 Chapter 1 slides24 Relative sizes of data types TypeNumber of bytes Short2 char1 int4 unsigned4 long (int)8 float4 double8

25 Chapter 1 slides25 Compilers need size information int main(void) {int i, j; char c, d; float e, f; double g, h; } How many bytes?

26 Chapter 1 slides26 Compilers need size information int main(void) {int i, j; char c, d; float e, f; double g, h; } How many bytes? 4 + 4 + 1 + 1 + 4 + 4 + 8 + 8

27 Chapter 1 slides27 How to read bytes in memory A byte consists of 8 bits, each of which is either 0 or 1. Characters need a special encoding (ASCII is the most common) Ints can be positive or negative Floats also can be positive or negative, and have decimal places

28 Chapter 1 slides28 How to read bytes in memory A byte consists of 8 bits, each of which is either 0 or 1. Characters need a special encoding (ASCII is the most common) Ints can be positive or negative Floats also can be positive or negative, and have decimal places Can’t tell how what a byte means unless we know the organization (like deciphering a code)

29 Chapter 1 slides29 Output in C: printf printf prints information according to controls and lists of what is to be printed. The controls are given as strings with specifications for printing The specifications control the output by telling what format to use: –int, char, float, double, etc

30 Chapter 1 slides30 printf specifications Printing control statements must be enclosed in double quotes. Any reference to the printing of an expression must have a description of how it is to be printed. The percent sign (%) signifies the format of the expression to be printed in a conversion specification; everything between the quotes except conversion specifications or escape sequences will be printed as written.

31 Chapter 1 slides31 printf specifications, cont. %d means that the expression is to be printed as a base 10 integer %c means that the expression is to be printed as a single character %f means that the expression is to be printed as a floating point number A %lf means that the expression is to be printed as a double precision-floating point number. %s means that the expression is to be printed as a string of characters.

32 Chapter 1 slides32 printf specifications, cont. %o means that the expression is to be printed as a base 8 (octal) integer %x means that the expression is to be printed as a base 16 (hexadecimal) integer %Lf means that the expression is to be printed as a "long" double-precision floating point number

33 Chapter 1 slides33 #include int main(void) { int i= 10; char c = 'Y'; float f; float h = 3.14159; double d, mint = 6.023E3; printf("%d\n",i); printf("\t\t%c\n",c); printf("\t\t%c\n",'Y'); printf("\t\t\t\t%7.2f\n",f); }

34 Chapter 1 slides34 Output 10 Y -107374176.00

35 Chapter 1 slides35 #include int main(void) { float h = 3.14159; double d, mint = 6.023E3; printf("\t\t\t\t%7.5f\n",h); printf("\t\t\t\t%7.4f\n",3.14159); printf("\t\t\t\t%7.2f\n",d); printf("\t\t\t\t%7.2f\n",mint, "\t\t\t\t%7.2f\n",6.023E3); }

36 Chapter 1 slides36 The output 3.14159 3.1416 - 9255960000000000000000000.00 6023.00

37 Chapter 1 slides37 Other output functions putchar() putchar('H'); puts() puts("This message was printed using puts");

38 Chapter 1 slides38 Input A general-purpose function is scanf Similar syntax to printf One additional feature – for technical reasons, an ampersand (&) is needed to read in the value of a variable

39 Chapter 1 slides39 #include int main(void) { int i; printf("Please enter a positive integer\n"); scanf("%d",&i); printf("%d",i*i); }

40 Chapter 1 slides40 #include int main(void) { int n,m,p,q; printf("Enter 4 integers, separated by blanks,\n"); printf("tabs, returns, or any combination of these\n"); scanf("%d %d %d %d",&n,&m,&p,&q); printf("%d %d %d %d\n",n,m,p,q); }

41 Chapter 1 slides41 Another common input function getchar() ch = getchar();

42 Chapter 1 slides42 Constants #include int main(void) { int i= 10; char c = 'Y'; float h, chicken_hawk; const float x = 3.14159; }

43 Chapter 1 slides43


Download ppt "Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost."

Similar presentations


Ads by Google