1 / 18

CS156: Introduction to C

CS156: Introduction to C Lecture 1 What is a Program? A program is set of instructions for a computer Examples: Unix, Shell, Find, cd A compiled program is translated into a format that the CPU can read directly.

andrew
Download Presentation

CS156: Introduction to C

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. CS156: Introduction to C Lecture 1

  2. What is a Program? • A program is set of instructions for a computer • Examples: Unix, Shell, Find, cd • A compiled program is translated into a format that the CPU can read directly. • An interpreted program is not translated into machine code but executed by an interpreter. CS156: Introduction to C

  3. What is a Programming Language? • A programming language is a language for describing programs. • Examples: C, Java, Perl, VAX Assembly • Programming languages are defined by a set of rules defining valid programs, syntax, and a set of meaning for valid programs, semantics. CS156: Introduction to C

  4. What is C? • C is an imperative programming language. • C was originally written for use in UNIX, but is now used on many operating systems. • It was designed for writing system software and much of UNIX is written in C. • C is a relatively low-level language that allows the programmer great control over how a program executes. CS156: Introduction to C

  5. What is Compilation? • Compilation is a process of translating a program into machine readable code. CS156: Introduction to C

  6. An Example C Program • All C programs must have a main function. • The program execution begins with the first statement in this function. #include <stdio.h> int main(int argc, char *argv[]) { // Print Hello World printf("Hello, world!\n"); return 0; } Preprocessor Directive Function Comment Function Call Function return value CS156: Introduction to C

  7. An Example C Program • #include <stdio.h> • stdio.h • Standard input/output • .h = header file • Header files include: • Declaration of functions, macros, templates and other prgrmg elements • #include • Add a copy of the header file to this file during compilation #include <stdio.h> int main(int argc, char *argv[]) { // Print Hello World printf("Hello, world!\n"); return 0; } Preprocessor Directive Function Comment Function Call Function return value CS156: Introduction to C

  8. An Example C Program • int main ( int argc, char *argv[ ] ) • Function header • int = return type (int = integer) • main = name of the function • In ( ) are parameters • { } designate beginning and end of function #include <stdio.h> int main(int argc, char *argv[]) { // Print Hello World printf("Hello, world!\n"); return 0; } Preprocessor Directive Function Comment Function Call Function return value CS156: Introduction to C

  9. The type of main • main() returns an int • main() does not return void I don’t care if it void works for your compiler. ANSI says that main() returns int, and ANSI wins all arguments. We teach ANSI C in this class, not Microsoft C or GNU C or any particular compiler. CS156: Introduction to C

  10. An Example C Program • // Print Hello World • Comment • Begins with // • The rest of the line is ignored by the compiler #include <stdio.h> int main(int argc, char *argv[]) { // Print Hello World printf("Hello, world!\n"); return 0; } Preprocessor Directive Function Comment Function Call Function return value CS156: Introduction to C

  11. An Example C Program • printf ( “Hello, world!\n” ); • printf = name of method • In ( ) are parameters • printf prints the parameter values to the screen • \n = new line character #include <stdio.h> int main(int argc, char *argv[]) { // Print Hello World printf("Hello, world!\n"); return 0; } Preprocessor Directive Function Comment Function Call Function return value CS156: Introduction to C

  12. An Example C Program • return 0; • The method main has a return type of int,so we must return an integer value #include <stdio.h> int main(int argc, char *argv[]) { // Print Hello World printf("Hello, world!\n"); return 0; } Preprocessor Directive Function Comment Function Call Function return value CS156: Introduction to C

  13. Creating a C Program in Unix • For this class you will be doing all programming on the Linux operating system. • You will create your programs using an editor: pico, vim or emacs. • Advanced editors such as vim and emacs provide tools that help with program writing. • Syntax highlighting is the most common tool and is very helpful. CS156: Introduction to C

  14. gcc: The GNU C Compiler • The gcc is a front end for a number of compilation tools. • Preprocessor – handle # directives • Compiler – Convert code to assembly • Assembler – translate assembly to machine code • Linker – combine machine code files into executable file • The gcc is equipped with many options that change its behavior. The options affect such things as optimization, output format, etc. CS156: Introduction to C

  15. Common gcc Options • Many gcc options you won’t need to remember, but some are used often. • -o file – place output in a file named fileinstead of the default file a.out • -v – give verbose output, show the commands being executed • --help – give some basic usage information about the compiler preisner> gcc –o hello hello_world.c CS156: Introduction to C

  16. Example Continued preisner> ls hello_world.c preisner> cat hello_world.c #include <stdio.h> int main(int argc, char *argv[]) { // Print Hello World printf("Hello, world!\n"); return 0; } preisner> gcc hello_world.c preisner> ls -l total 16 -rwx------ 1 cs156 class 4705 Feb 20 12:15 a.out -rw------- 1 cs156 class 95 Feb 20 12:10 hello_world.c preisner> ./a.out Hello, world! preisner> CS156: Introduction to C

  17. Example Continued, using -o preisner> ls hello_world.c preisner> cat hello_world.c #include <stdio.h> int main(int argc, char *argv[]) { // Print Hello World printf("Hello, world!\n"); return 0; } preisner> gcc –o hello hello_world.c preisner> ls -l total 16 -rwx------ 1 cs156 class 4705 Feb 20 12:15 hello -rw------- 1 cs156 class 95 Feb 20 12:10 hello_world.c preisner> ./hello Hello, world! preisner> CS156: Introduction to C

  18. Example Continued, using -o preisner> ls hello.c preisner> cat hello.c #include <stdio.h> int main() { // Print Hello World printf("Hello, world!\n"); return 0; } preisner> gcc hello.c preisner>./a.out Hello, world! preisner> CS156: Introduction to C

More Related