Chapter 11 Introduction to Programming in C

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

1 ICS103 Programming in C Lecture 3: Introduction to C (2)
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
1 Introduction to “C” HLL’s and the Basic Syntax Patt and Patel Ch. 11 & 12.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Computer Science 210 Computer Organization Introduction to C.
Computer Science 101 Introduction to Programming.
High-Level Programming Languages: C++
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 11 Programming in C Compilation vs. Interpretation Different ways of translating high-level language Compilation translates code into machine.
Programming With C.
Chapter 11 Introduction to Programming in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Compilation.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
1 Programming a Computer Lecture Ten. 2 Outline  A quick introduction to the programming language C  Introduction to software packages: Matlab for numerical.
CMPE13Cyrus Bazeghi 1 Chapter 11 Introduction to Programming in C.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 1 Scott Marino.
C is a high level language (HLL)
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Introduction to C Topics Compilation Using the gcc Compiler
The Second C++ Program Variables, Types, I/O Animation!
Advanced Computer Systems
UMBC CMSC 104 – Section 01, Fall 2016
Basic C programming Convert C program statements to LC-3 Cache (how to bridge the speed gap between memory and CPU) Virtual memory (how to run multiple.
Computer Science 210 Computer Organization
CSC201: Computer Programming
Chapter 2, Part I Introduction to C Programming
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Data types and variables
Variables, Expressions, and IO
Introduction to C Topics Compilation Using the gcc Compiler
Getting Started with C.
Introduction to C Topics Compilation Using the gcc Compiler
Choice of Programming Language
Introduction to C Programming
Computer Science 210 Computer Organization
Chapter 2 - Introduction to C Programming
Chapter 11 Introduction to Programming in C
Introduction to CS Your First C Programs
Chapter 2 - Introduction to C Programming
Programming Funamental slides
Introduction to C Programming
Chapter 11 Introduction to Programming in C
Introduction to C Topics Compilation Using the gcc Compiler
Creating your first C program
Chapter 2 - Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Arrays.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 11 Programming in C
DATA TYPES There are four basic data types associated with variables:
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Chapter 11 Introduction to Programming in C
Presentation transcript:

Chapter 11 Introduction to Programming in C

C: A High-Level Language Gives symbolic names for containers of values don’t need to know which register or memory location Provides abstraction of underlying hardware operations do not depend on instruction set example: can write “a = b * c”, even though LC-3 doesn’t have a multiply instruction (can be implemented as an “invisible” subroutine) Provides expressiveness use meaningful symbols that convey meaning simple expressions for common control structures Enhances code readability Safeguards against bugs can enforce rules or conditions at compile-time or run-time

Compilation vs. Interpretation Different ways of translating high-level language Interpretation interpreter = program that executes program statements generally one line/command at a time less efficient than compilation easy to debug, make changes, view intermediate results languages: BASIC, LISP, Perl, Matlab, C-shell Compilation translates statements into machine language does not execute, but creates executable program more efficient than interpretation change requires recompilation can be harder to debug, since executed code may be different languages: C, C++, Fortran, Pascal

Compilation vs. Interpretation Interpretation/Compilation Hybrid Java Java language is compiled into JVM (Java Virtual Machine) byte code. The JVM byte code can be either interpreted or compiled into native machine code by a JIT (Just in Time) or AOT (Ahead of Time) compiler. Python Python program is converted into byte code like Java language – PVM (Python Virtual Machine) for CPython (the reference implementation of Python) and JVM for Jython. The byte code can be either interpreted or compiled into native machine code by a JIT (Just in Time) or AOT (Ahead of Time) compiler.

Compilation vs. Interpretation Consider the following simple program: Get W from the keyboard. X = W + W Y = X + X Z = Y + Y Print Z to screen. If interpreting, how many arithmetic operations occur? If compiling, we can analyze the entire program and possibly reduce the number of operations. Can we simplify the above algorithm to use a single arithmetic operation?

Compiling a C Program Entire mechanism is usually called the “compiler” Preprocessor macro substitution conditional compilation “source-level” transformations output is still C Compiler generates object file machine instructions Linker combine object files (including libraries) into executable image

Compiler Source Code Analysis Code Generation Symbol Table “front end” parses programs to identify its pieces variables, expressions, statements, functions, etc. depends on language (not on target machine) Code Generation “back end” generates machine code from analyzed source may optimize machine code to make it run more efficiently very dependent on target machine Symbol Table map between symbolic names and items like assembler, but more kinds of information

A Simple C Program #include <stdio.h> #define STOP 0 /* Function: main */ /* Description: counts down from user input to STOP */ main() { /* variable declarations */ int counter; /* an integer to hold count values */ int startPoint; /* starting point for countdown */ /* prompt user for input */ printf("Enter a positive number: "); scanf("%d", &startPoint); /* read into startPoint */ /* count down and print count */ for (counter=startPoint; counter >= STOP; counter--) printf("%d\n", counter); }

Preprocessor Directives #include <stdio.h> Before compiling, copy contents of header file (stdio.h) into source code. Header files typically contain constants, descriptions of functions, and variables needed by the program. no restrictions -- could be any C source code #define STOP 0 Before compiling, replace all instances of the string "STOP" with the string "0" Called a macro Used for values that won't change during execution, but might change if the program is reused. (Must recompile.)

Comments Begins with /* and ends with */ Can span multiple lines Cannot have a comment within a comment Comments are not recognized within a string example: "my/*don't print this*/string" would be printed as: my/*don't print this*/string As before, use comments to help reader, not to confuse or to restate the obvious

main Function Every C program must have a function called main(). This is the code that is executed when the program is run. The code for the function lives within brackets: main() { /* code goes here */ } (): 소괄호, round bracket, parenthesis {}: 중괄호, curly bracket, brace []: 대괄호, square bracket

Variable Declarations Variables are used as names for data items. Each variable has a type, which tells the compiler how the data is to be interpreted (and how much space it needs, etc.). int counter; int startPoint; int is a predefined integer type in C.

Input and Output Variety of I/O functions in C Standard Library. Must include <stdio.h> to use them. printf("%d\n", counter); String contains characters to print and formatting directions for variables. This call says to print the variable counter as a decimal integer, followed by a linefeed (\n). scanf("%d", &startPoint); String contains formatting directions for looking at input. This call says to read a decimal integer and assign it to the variable startPoint. (Don't worry about the & yet.) ‘ ‘: 작은 따옴표, single quotation mark “ “: 큰 따옴표, double quotation mark

More About Output Can print arbitrary expressions, not just variables printf("%d\n", startPoint - counter); Print multiple expressions with a single statement printf("%d %d\n", counter, startPoint - counter); Different formatting options: %d decimal integer %x hexadecimal integer %c ASCII character %f floating-point number

Examples This code: produces this output: 43 is a prime number. printf("%d is a prime number.\n", 43); printf("43 plus 59 in decimal is %d.\n", 43+59); printf("43 plus 59 in hex is %x.\n", 43+59); printf("43 plus 59 as a character is %c.\n", 43+59); produces this output: 43 is a prime number. 43 + 59 in decimal is 102. 43 + 59 in hex is 66. 43 + 59 as a character is f.

Examples of Input Many of the same formatting characters are available for user input. scanf("%c", &nextChar); reads a single character and stores it in nextChar scanf("%f", &radius); reads a floating point number and stores it in radius scanf("%d %d", &length, &width); reads two decimal integers (separated by whitespace), stores the first one in length and the second in width Must use ampersand (&) for variables being modified. (Explained in Chapter 16.)

Compiling and Linking Various compilers available cc, gcc includes preprocessor, compiler, and linker Lots and lots of options! level of optimization, debugging preprocessor, linker options intermediate files -- object (.o), assembler (.s), preprocessor (.i), etc.

Remaining Chapters A more detailed look at many C features. Variables and declarations Operators Control Structures Functions Data Structures I/O Emphasis on how C is converted to LC-3 assembly language. Also see C Reference in Appendix D.

꼭 기억해야 할 것 Interpretation vs. Compilation and its hybrids High-Level Programming Language provides a high-level abstraction of machine language variables (registers and memory locations) operations (arithmetic / logic instructions) control structures (branch / jump instructions) - sequence conditional iterative