1 / 30

C Language

C Language. Brief history In 1972, Dennis Ritchie designed C and it was used on PDP-11 mini-computers In 1974, Unix was rewritten in C C++ and C Advantages and disadvantages. A short example. #include &lt;stdio.h&gt; main ( ) { printf (“Is anybody out there?<br>”);

alder
Download Presentation

C 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 Language • Brief history • In 1972, Dennis Ritchie designed C and it was used on PDP-11 mini-computers • In 1974, Unix was rewritten in C • C++ and C • Advantages and disadvantages

  2. A short example • #include <stdio.h> • main ( ) • { • printf (“Is anybody out there?\n”); • printf (“Hello world!\n”); • }

  3. /* Another example-read items, compute their average */ • #include <stdio.h> • main ( ) • { • int ItemsRead = 0; • double ThisItem, Sum = 0.0; • printf (“Enter as many items as you want\n”); • printf (“Terminate with non-double or EOF marker\n”); • while (scanf (“%lf”, &ThisItem ) ==1 ) • { • ItemsRead ++; • Sum += ThisItem; • } • printf( “The average of %d items was %f\n”, ItemsRead, Sum / ItemsRead ); • return 0; • }

  4. Simple C • 1. Create a c program • Any line or screen editor will do. Vi editor is also a good choice. For simple ones, one can use cat > fileName as well. • 2. Compile the program • cc fileName.c will create a executable file called a.out regardless of the fileName.

  5. Compile with a style • To create a complied file with the same name: • Use ccfileName.c –o fileName • Will create a file with the fileName instead of a.out

  6. Compiling Problems • It is not as straightforward as the Code Warrior you did in C++ course. • However, you will get used to it once you do more. • Unix debugger dbx is available for debugging.

  7. Memory leak in C • Memory leaks (in which malloc() memory is never released with corresponding free() calls) and buffer overruns (writing past memory that has been allocated for an array, for example) are some of the common problems and can be difficult to detect.

  8. Example • #include <stdlib.h> • #include <stdio.h> • #include "memwatch.h" • int main(void) • { • char *ptr1; • char *ptr2; • ptr1 = malloc(512); • ptr2 = malloc(512); • ptr2 = ptr1; • free(ptr2); free(ptr1); • }

  9. What is the problem with that program? • The code in Listing 1 allocates two 512-byte blocks of memory, and then the pointer to the first block is set to the second block. As a result, the address of the second block is lost, and there is a memory leak.

  10. Debug Tools • There are various ways to track down user-space and kernel problems using debug tools on Linux. Build and debug your source code with these tools and techniques: • Memory tools: MEMWATCH and YAMD • strace • GNU debugger (gdb) • Magic key sequence

  11. Compiling Process • 1. cc command translate the source file to an equivalent assembly program. • 2. Once an equivalent assembly language is generated, another program called assembler is called to convert the program into binary file or machine code file. • 3. Finally, linker is called to combine the binary file with the library to create a executable file.

  12. cc versus gcc • gcc – the GNU C Compiler, it was written by Richard Stallman and it is now maintained by Cyguns solutions. gcc exists for practically all major operating systems and processor architectures and is widely used in embedded systems. • 1. It is the complier we are using in Linux. • 2. When you invoke cc, you are invoking gcc

  13. gcc interpretation of filename Suffixes • .c --- C source code which must be pre- • processed before compilation. • .i --- C source code has been pre-processed. • .cc, .cxx, .cpp, .C --- C++ source file as in .c • .ii --- C++ source that has been pre-proce… • .h --- C header file • .s --- Assembly code • .S --- Assembly code which must be pre-processed

  14. Compiling C++ Programs • To compile and link C++ programs, you should invoke the complier as g++ rather than gcc. This is especially important at link time because it force the linker to link in certain libraries required by C++ programs.

  15. Basic Rules in C • The basic unit of a C program is a character. • A-Z, a-z, 0-9, white space: blanks, newlines, tabs, … and the following 29 characters: • ! # % ^ & * ( ) _ - + = [ ] { } | \ ; : “ ‘ < > • , . / ? ~ • White space is important for readability.

  16. Comments in C • 1. C comments begins with a /* and end with */ and there should be no space between the asterisk and the slash. • 2. No nest in comment in C. • Common error: • /* 1 */ /* This comment ends early on the next line. • /* 2 */ * This is not in the comment ! • /* 3 */ * Nether is this;

  17. Identifiers and Keywords in C • 1. A name may use A-Z, a-z, 0-9, _ • Begin with a digit is prohibited. • 2. The length cannot exceed 31 chars and in • some situations 6 is the limit. • 3. Similar to the UNIX, c is case sensitive. • 4. Avoid using the 32 keywords. • They are:

  18. Keywords in C • auto, break, case, char, const, continue, • default, do, double, else, enum, extern • float, for, goto, if, int, long, register, return • short, signed, sizeof, static, struct, switch • typedef, union, unsigned, void, volatile, • while

  19. Pre-Processor in C • #include <stdio.h> • #define Minimum Wage 5.75 • #---the first non-white-space character line begins with # will be treated as pre-processor • < ***> inside indicates the *** file doe not reside in the current directory, (it resides in the system directory) • .h --- h refer to the header file similar to C++

  20. Pointers in C • Pointer is a variable to store an address of another object. • That object is accessed by dereferencing the • Pointer. • Pointer is one of the most important concept in C language

  21. How to define a pointer? • int *p; • int x = 8; • int *p = &x; /* p points at x */ • ++*p will change x to 9 while • *P++ will make the pointer points to a different number in memory.

  22. Pointer in C • You can give a pointer any name you like, such as: • int *p, int *ptr, int *Ptr, int *xPointer • However, we recommend use *xptr or xPtr to point x and *yPtr to point y. • * --- is called indirection operator or dereferencing operator.

  23. Initializing Pointer • Pointer should be initialized either when they are declared or in an assignment statement. • int *xPtr, *yPtr, *zeroPtr; • int x = 8, y = 9, zero = 0; • xPtr = &x; • yPtr = & y; • zeroPtr = NULL or zeroPtr = 0; • NULL is a symbolic constant defined in the <stdio.h> header file (and in several other header files)

  24. What we care is p points to the variable by store the address in it. The computer will reserve a address for p. However, we don’t care where it will store it. address p 8 p ++*p will make 8 becomes 9 and *p++ will make p points to another address

  25. Program Example • #include <stdio.h> • Void • Swap (int * const X, int * const Y) • { • int Temp; • Temp = *X; • *X = *Y; • *Y = Temp; • } • main (void) • { • int A = 5, B = 7; • Swap (&A, &B); • printf (“%/d %/d\n” , A, B); /* Must pass the address */ • return 0; • }

  26. Input from c Programs scanf • #include <stdio> • main (void) • { • int x; • double y; • printf (“Enter an integer and a real: “); • scanf ( “%d %lf” , &x , &y); • Print (“You entered %d and %f\n” , x, y); • }

  27. Example • #include <stdio.h> • int main ( ) { printf(“\”So what? \“ said she.\n”); return 0; }

  28. Input in c • #include <stdio.h> • int main( void ) • { • int intvar; • printf("The address of intvar is %p.\n", &intvar); printf("\nEnter an integer value: "); • scanf("%d", &intvar); printf("The value you entered was %d.\n", intvar); • return 0; • }

  29. Notice the problem when you run the program • 1. When you input a legal integer. • 2. When you input a serious of characters. • 3. When you input a number that is too big to the computer.

More Related