The C Language - public.asu.eduatrow/ser456/notes/clanguage.pdf · The C Language
C Language
description
Transcript of C Language
![Page 1: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/1.jpg)
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
![Page 2: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/2.jpg)
A short example
• #include <stdio.h>
• main ( )
• {
• printf (“Is anybody out there?\n”);
• printf (“Hello world!\n”);
• }
![Page 3: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/3.jpg)
• /* 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;• }
![Page 4: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/4.jpg)
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.
![Page 5: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/5.jpg)
Compile with a style
• To create a complied file with the same name:
• Use cc fileName.c –o fileName
• Will create a file with the fileName instead of a.out
![Page 6: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/6.jpg)
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.
![Page 7: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/7.jpg)
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.
![Page 8: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/8.jpg)
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); • }
![Page 9: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/9.jpg)
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.
![Page 10: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/10.jpg)
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
![Page 11: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/11.jpg)
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.
![Page 12: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/12.jpg)
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
![Page 13: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/13.jpg)
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
![Page 14: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/14.jpg)
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.
![Page 15: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/15.jpg)
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.
![Page 16: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/16.jpg)
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;
![Page 17: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/17.jpg)
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:
![Page 18: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/18.jpg)
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
![Page 19: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/19.jpg)
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++
![Page 20: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/20.jpg)
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
![Page 21: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/21.jpg)
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.
![Page 22: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/22.jpg)
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.
•
![Page 23: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/23.jpg)
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)
![Page 24: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/24.jpg)
paddress
p 8
The computer will reserve a addressfor p. However, we don’t care whereit will store it.
What we care is p points to the variable by store the address in it.
++*p will make 8 becomes 9 and*p++ will make p points to anotheraddress
![Page 25: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/25.jpg)
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;
• }
![Page 26: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/26.jpg)
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);• }
![Page 27: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/27.jpg)
Example
• #include <stdio.h>
• int main ( )
{
printf(“\”So what? \“ said she.\n”);
return 0;
}
![Page 28: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/28.jpg)
Input in c• #include <stdio.h>
• int main( void )• {• int intvar;• printf("The address of intvar is %p.\n", &intvar); /*
Notes 1 and 2 */
• printf("\nEnter an integer value: ");• scanf("%d", &intvar); /* Note
3 */• printf("The value you entered was %d.\n", intvar);• return 0;• }
![Page 29: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/29.jpg)
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.
![Page 30: C Language](https://reader035.fdocuments.us/reader035/viewer/2022062222/568159fb550346895dc7478b/html5/thumbnails/30.jpg)