1 / 32

Chapter 1 Introduction to C Language

Chapter 1 Introduction to C Language. By C. Shing ITEC Dept Radford University. Objectives. Understand brief history of C Describe C components Understand C features Understand Program Structure in C Understand how to run a C program using GNU C compiler and in .NET environment

tibor
Download Presentation

Chapter 1 Introduction to 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. Chapter 1 Introduction to C Language By C. Shing ITEC Dept Radford University

  2. Objectives • Understand brief history of C • Describe C components • Understand C features • Understand Program Structure in C • Understand how to run a C program using GNU C compiler and in .NET environment • Understand how to use a vi editor

  3. C Brief History • 1967 Martin Richards • Develop BCPL language for writing OS and compilers • 1969 Ken Thompson, Bell Lab • Develop B language for Unix (on DEC PDP-7) • machine dependent • 1970 Dennis Ritchie, Bell Lab • developed C language on DEC PDP-11 (traditional C: cc) • machine independent • 1978 C++: Bjarne Stroustrup • Object-oriented • Super-set of C • 1989 ANSI C (e.g. GNU C: gcc) • 1999 updated ANSI C • Variable-length array • Restrict pointer modifier • 2002 Microsoft C# for .NET framework

  4. C Components • Compiler • traditional • ANSI • Preprocessor: code begins with # at column 1 • Process code before compiler starts translating • Library • Standard Library (/usr/lib/libc.a) • Examine table of contents: ar t /usr/lib/libc.a • Math Library (/usr/lib/libm.a) • Utility Library

  5. C Features • Middle-level language • Can access bit level • Machine independent • Case sensitive • Type language (weak type) • Variable must be declared a type • Allow automatic type conversion • No boolean data type (0: false, non-zero: true) • No run-time error checking • Programmer’s responsibility: array boundary

  6. C Features • Small language, very efficient • 32 keywords in ANSI C compared to BASIC (> 100 reserved words) • All subprograms are functions • Function uses pass-by-value • Function cannot be nested within another function • Can manipulate memory • System programming tool

  7. Running C Program • C source program -> C Compiler -> Object program -> Link (to get executable program) ->(feed data) Execution

  8. Program Structures • File Extension • .c (or .cpp for C++) • Compile & link (Solaris) , then execute • gcc mainfile.c [sub1filename.c sub2filename.c] or in traditional C: (if math library is called in the program) • cc mainfile.c [sub1filename.c sub2filename.c] –lm then • a.out or • gcc -o executable mainfile.c [sub1filename.c sub2filename.c] then • executable

  9. Program Structures (Cont.) • Compile & link (Solaris) to your library, then execute • gcc mainfile.c [sub1filename.c sub2filename.c] -lyourlibraryname or in traditional C: (if yourlibrary is called in the program) • cc mainfile.c [sub1filename.c sub2filename.c] –llyourlibraryname then • a.out or • gcc -o executable mainfile.c [sub1filename.c sub2filename.c] –llyourlibraryname then • executable

  10. Program Structures (Cont.) • Compile only (Solaris) • gcc –c filename.c or • cc -c filename.c (traditional C)

  11. Program Structures (Cont.) • mainfile.c /* ********************************** * This is a comment * * Block * ********************************** */ // Comment line // Declaration for standard library: in /usr/include/stdio.h // The following line is required #include <stdio.h>

  12. Program Structures (Cont.) #include <math.h> //if math library is used // The following file yourdeclaration.h // is in your current directory #include “yourdeclaration.h” // define macro constants #define PI 3.14159 #define MY_MESSAGE “Good Morning!”

  13. Program Structures (Cont.) // declare named global constants const int number = 4; // declare types typedef char myCharacter; // declare global variables int a; char b=‘a’, c=‘\n’; myCharacter e; long d = 14000L; // this means long int (default int) float x=-2.5F; double y=2.5;

  14. Program Structures (Cont.) // declare function prototype int sub1(int); // define main function int main (void) { // declare local variables int lb, lc; lc = 2; // call function lb = sub1(lc); printf(“%d\n”,lb); return 0; // no error exit }

  15. Program Structures (Cont.) // define function int sub1 (int l) { // declare local variables int la; // call function la = 5*l; return la; }

  16. Appendix: • Traditional C (not recommended to write) • No const data type (use #define instead) • No void data type • Main function does not return anything • No function prototype • Return type for function default is int if not specified • Function formal parameters are defined after ()

  17. Program Structures (Traditional C) • mainfile.c /* ********************************** * This is a comment * * Block * ********************************** */ // Comment line // Declaration for standard library: in /usr/include/stdio.h // The following line is required #include <stdio.h>

  18. Program Structures (Cont.) #include <math.h> //if math library is used // The following file yourdeclaration.h // is in your current directory #include “yourdeclaration.h” // define macro constants #define PI 3.14159 #define MY_MESSAGE “Good Morning!” #define NUMBER 4

  19. Program Structures (Cont.) // declare types typedef char myCharacter; // declare global variables int a; char b=‘a’, c=‘\n’; myCharacter e; long d = 14000L; float x=-2.5F; double y=2.5;

  20. Program Structures (Cont.) // define function sub1 must appear before main function sub1 () int l; { // declare local variables int la; // call function la = 5*l; return la; }

  21. Program Structures (Cont.) // define main function main () { // declare local variables int lb, lc; lc = 2; // call function lb = sub1(lc); printf(“%d\n”,lb); }

  22. vi/vim editor • vi filename.c • Please refer to http://www.chem.brown.edu/instructions/vi.html

  23. Use vi Editor • 2 modes: • Editor mode (press <esc>): make correction • Insert/open/append mode (press<i>/<o>/<a>): add text

  24. Use vi Editor (Cont.) • Editor mode: (Common keys) • <h>:move left, <l>: move right, <j>: move down, <k>: move up • <0>: beginning of line, <$>: end of line • <w>: next word, <b>: previous word • <:><n>: go to line n

  25. Use vi Editor (Cont.) • Editor mode: (Cont.) • <x>: delete character, <d><w>: delete word • <d><d>: delete line (to clip board), <Y>: copy line • <p>: paste from clip board after/below cursor • <P>: paste from clip board before/above cursor • Move cursor to line m,<d><d>, then move cursor to line n, <p>: move line m to below line n • Move cursor to line m,<Y>, then move cursor to line n, <p>: copy line m to below line n

  26. Use vi Editor (Cont.) • Editor mode: (Cont.) • Note: put a number n before an action will repeat the action n times. For example: 10<j>: move cursor down 10 lines 10<x> delete 10 characters 10<d><d>: delete 10 lines 10<Y>: copy 10 lines

  27. Use vi Editor (Cont.) • Editor mode: (Cont.) • <r>: replace a character • <c><w> type in word<esc>: replace a word • <R> type in words<esc>: replace/type over words • </>type in word<enter>: search the 1st occurrence of the word • <:><w>: save, <:><w><q>: save and quit, • <:><w><q>type in filename<enter>: save to filename and quit • <:><s></><word1></><word2></><enter>: substitute the 1st occurrence of word1 for word2 • <:><1><,><$><s></><word1></><word2></><enter>: substitute word1 for word2 from line 1 to lat line

  28. Use vi Editor (Cont.) • Editor mode: (Cont.) • <J>: join the cursor line and the line below into one line • Move cursor to position <i><esc>: split at the cursor position into 2 lines • <.>: repeat the previous command • <n>: next searched word • <G>: go to last line • <ctrl><g>: file status

  29. Use vi Editor (Cont.) • Insert/open/append mode: (Common keys) • <i>type in words<esc>:insert words before the cursor • <a>type in words<esc>:append words after the cursor • <o>type in words<esc>:open lines after the cursor and append words • <O>type in words<esc>:open lines before the cursor and append words

  30. Class Example • Hands On Example0: edit the following 2 lines in file .exrc (vi ~/.exrc) :abbr #b /************************ :abbr #e ************************/ Then when you use vi template.c And after press<i>/<a>/<o> to insert text, you type #b followed by <Enter>, you add a line /******** in. Similarly, when you type #e followed by <Enter>, you add a line ********/ in • Example 1

  31. Programming Environment in .NET • Running C/C++ Instruction (Reference) • Not used in this course

  32. Reference: • Brian W. Kernighan & Dennis M. Ritchie: C Programming Language, ANSI Edition Prentice Hall • Al Kelley & Ira Pohl: A Book on C, 4th ed. Addison Wesley • Deitel & Deitel: C How to Program, 4th ed., Chapter 1, Prentice Hall

More Related