1 / 29

Introduction to C Programming

Introduction to C Programming. Overview of C Hello World program Unix environment C programming basics. Overview of C. Preceded by BCPL and B Developed by Dennis Ritchie Bell Labs in 1972 Portable (machine-independent) Used in UNIX, networks. Hello World Program.

lapis
Download Presentation

Introduction to C Programming

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. Introduction to C Programming • Overview of C • Hello World program • Unix environment • C programming basics

  2. Overview of C • Preceded by BCPL and B • Developed by Dennis Ritchie • Bell Labs in 1972 • Portable (machine-independent) • Used in UNIX, networks

  3. Hello World Program /*Will ouput "Aloha!"*/ #include <stdio.h> int main(void) { printf("Aloha!\n"); return 0; }

  4. Hello World Program /*comments*/ /*Two lines of comments*/ • Ignored by the C compiler

  5. /*no comment*/

  6. Hello World Program #include <stdio.h> • Directive to C preprocessor • # (pound sign) – line processed by preprocessor before the program is compiled • Tells preprocessor to include contents of stdio.h file in the program • stdio.h - standard input/output header file • Contains information & declarations about functions • In this case, the printf library function

  7. Hello World Program int main(void){return 0;} • int – return type of function • return 0; • Returns 0 to operating system • Indicates that program executed successfully • main • Every program consists of 1 or more functions • Must have a main function • (void) – function arguments • {} – body of function = left & right brackets (block)

  8. 6 Phases to Execute C Program • Edit – write program & store on disk • Creates file: program.c • Preprocess – add other files & text replacement • Compile – creates object (machine) code • Creates file: program.o • Link – links code & libraries to make executable • Creates file: a.out • Load – takes from disk & put in memory • Execute – CPU executes each instruction

  9. UNIX Environment • Edit program with Emacs (Vi or Pico) • emacs program.c • Preprocess, Compile & Link (using the GNU C compiler) • gcc program.c (will make “a.out”) • gcc program.c -o program (“program” will be the executable) • Load & Execute • ./a.out (./program)

  10. Makefile • You can compile & link separately • gcc –c program.c (creates “program.o”) • gcc program.o (creates “a.out”) • Or gcc program.o –o program (creates “program”) • Easier to do with makefile • A file that tells how to build executables from multiple files • Name of the file is “makefile” • To run this file, type “make” • The command “make” finds the “makefile” and executes the commands listed in it

  11. Creating a Makefile • Makefiles consist of sets of three lines • Target: list of dependencies • <tab>Commands needed to build target • Blank line (newline character) • Example makefile: • program: program.o • gcc program.o -o program • program.o: program.c • gcc -c program.c • Note: the last line in this file is a newline!

  12. Running a Makefile • Type “make” • Should get the following output • gcc -c program.c • gcc program.o -o program • Type “./program” to run executable • "." is the current directory • ./program is the complete path to program

  13. Problems with Makefile • UNIX make program is unreasonably picky about the format of the make file • Must use a tab at the beginning of each command line • Must put a blank line (newline character - \n) after each group of statements, including at the end of the file • If you create it on your PC & ftp it to UNIX, the newline characters will cause problems in UNIX, so need to create with a UNIX editor

  14. File Transfer • If you are creating files on your PC & ftping to UNIX, then might get error message • warning: invalid white space character in directive • Why? • UNIX newline is one character (0x0A) • PC newline is two characters • 0x0Dh = carriage return • 0x0Ah = line feed • Solution: make sure you ftp in ASCII mode, not in binary mode

  15. Variables int x=1, y=2, z=3; • Variables declared at beginning of a function • Before any executable statements • Should initialize variables when declare them • Uninitialized variables have unpredictable values • Number of bytes (= 8 bits) for each data type (Unix) • char = 1 byte (-128 to 127) • short = 2 bytes (-32768 to 32767) • int & long = 4 bytes (-2,147,483,648 to +2,147,483,647) • float = 4 bytes (± ~10-44 to ~1038 ) • double = 8 bytes (± ~10-323 to ~10308 )

  16. Data Types • Unsigned integers • Represents positive integers only • Example: ASCII character codes • Not necessary to indicate a sign, so all 8 or 16 bits can be used for the magnitude: • unsigned char = 1 byte = 8 bits = 28 = 256 (0 to 255) • unsigned short = 2 bytes = 16 bits = 216 = 65,536 (0 to 65,535) • unsigned int & unsigned long = 4 bytes = 32 bits = 232 = 4,294,967,296 (0 to 4,294,967,295)

  17. Data Types • Signed integers • Default is signed (int = signed int) • Represents positive and negative integers • MSB (Most Significant Bit – leftmost bit) used to indicate sign • 0 = positive, 1 = negative • (LSB = Least Significant Bit – rightmost bit)

  18. Variable Names • Restrictions • Made up of letters & digits • 1st character must be a letter • Underscore (“_”) counts as a letter • Often used in library routines • Case sensitive • APPLE & apple are different variables • Less than 31 characters • Cannot use reserved keywords • auto, break, case, …, void, volatile, while

  19. Formatted Output - Printf • int printf(char *format, arg1, arg2, …) • Translates internal values to character output • Returns the number of characters printed

  20. Conversion Specifiers • u • Unsigned decimal • X • Unsigned hexadecimal • UPPERCASE output for hex letters • % • Print a “%” • d • Signed decimal integer • x • Unsigned hexadecimal • lowercase output for hex letters • c • Single character • f • Floating points • m.dddddd

  21. Variable Output • See output.c as an example • The output for the same variable will differ based on what kind of format is specified

  22. Arithmetic Operators • ( ) • Parenthesis (highest precedence) • *, /, % • Multiplication, division, modulus • Evaluated left to right • +, - • Addition and subtraction • Evaluated left to right

  23. Arithmetic Operators • Integer division produces an integer result • 1 / 2 evaluates to 0 (0.5, but no rounding up!) • 19 / 5 evaluates to 3 (3.8, but no rounding up!) • Modulus (%) = remainder after division • 1 % 2 evaluates to 1 (0 remainder 1) • 17 % 5 evaluates to 2 (3 remainder 2) • Implicit conversion – if an operation has operands of different types, the “narrower” one will be converted to the “wider” one • 2.0 / 5 evaluates to 0.4 (a float)

  24. Equality, Relational Operators • Equality operators • == (equal) – common error: = instead of == • != (not equal) • Relational operators • > (greater than) • < (less than) • >= (greater than or equal) • <= (less than or equal) • Will return a 0 (false) or 1(true)

  25. if Control Structure • A program can make a decision using the if control structure and the equality or relational operators /*See if.c*/ #include <stdio.h> int main(void) { if(1 > 2) {printf("%d > %d\n",1,2);} if(1 <= 2) { printf("%d <= %d\n",1,2);} return 0; }

  26. Class Exercise 1 • Trace the following code, then run it to see if you get your expected output • exercise1.c

  27. Input in C /*See input.c*/ #include <stdio.h> int main(void){ int x = 0; printf("Enter an integer: "); scanf("%d", &x); printf("The integer is %d\n", x); return 0; }

  28. scanf Function • scanf("%d", &x); • First argument: format control string • Indicates type of data to be entered • Second argument: location in memory • Use an ampersand (&) to give the address where the variable is stored • The computer will wait for the user to enter a value and press the “Enter” key

  29. Class Exercise 2 • Write a program that does the following: • inputs two integers • adds them together • outputs the sum • determines if the sum is a multiple of 3 (evenly divisible by 3)

More Related