1 CSE 303 Lecture 8 Intro to C programming read C Reference Manual pp. Ch. 1, 2.2 - 2.4, 2.6, 3.1, 5.1, 7.1 - 7.2, 7.5.1 - 7.5.4, 7.6 - 7.9, Ch. 8 ; Programming.

Slides:



Advertisements
Similar presentations
11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
David Notkin Autumn 2009 CSE303 Lecture 9 Dictionary.com, "c," in The American Heritage® Abbreviations Dictionary, Third Edition. Source location: Houghton.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
Introduction to C Programming CE Lecture 1 Introduction to C.
CSE1301 Computer Programming Lecture 4: C Primitives I.
Primitive Variable types Basic types –char (single character or ASCII integer value) –int (integer) –short (not longer than int) –long (longer than int)
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
B-1 Lecture 2: Problems, Algorithms, and Programs © 2000 UW CSE University of Washington Computer Programming I.
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Guide To UNIX Using Linux Third Edition
Assignment #2, 12- month Calendar CS-2301, B-Term Programming Assignment #2 12-Month Calendar CS-2301, System Programming for Non-Majors (Slides.
CSE : Programming in C Instructor: Lei Wang Office: Dreese Lab 474 Office Hour: Friday.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
Computer Science 210 Computer Organization Introduction to C.
CS 11 C track: lecture 1 Preliminaries Need a CS cluster account cgi-bin/sysadmin/account_request.cgi Need to know UNIX ITS.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Introduction to “Programming in C”
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CPCS 202 Chapter 2 – Input/Output
Chapter 11 Programming in C Compilation vs. Interpretation Different ways of translating high-level language Compilation translates code into machine.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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.
Introduction to Programming
3. FORMATTED INPUT/OUTPUT. The printf Function The first argument in a call of printf is a string, which may contain both ordinary characters and conversion.
CS 100Lecture 211 Announcements P5 due on Thursday FINAL EXAM Tuesday August 10, 8AM, Olin 155 Review sessions on Thursday and Friday Final grades posted.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
E-1 University of Washington Computer Programming I Lecture 5: Input and Output (I/O) © 2000 UW CSE.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
CMPE13Cyrus Bazeghi 1 Chapter 11 Introduction to Programming in C.
Announcements Assignment 1 due Wednesday at 11:59PM Quiz 1 on Thursday 1.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/11/2006 Lecture 7 – Introduction to C.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
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.
CSE-102: PROGRAMMING FUNDAMENTALS LECTURE 3: BASICS OF C Course Instructor: Syed Monowar Hossain 1.
C language--Introduction. History 1970: B by Ken Thompson at AT&T Bell Lab 1972: C by Dennis Ritchie and Ken Tompson at At&T Bell Lab for UNIX 1978: “The.
CSCE 206 Structured Programming in C
Formatted Input and Output
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Computer Science 210 Computer Organization
CSE 303 Lecture 9 C programming: types, functions, and arrays
Introduction to C CSE 2031 Fall /3/ :33 AM.
Getting Started with C.
C programming language
Administrative things
Programming in C Input / Output.
Computer Science 210 Computer Organization
Introduction to CS Your First C Programs
Introduction to C Topics Compilation Using the gcc Compiler
CS100A Lecture November 1998 Prelim 3 Statistics Maximum 94
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 11 Programming in C
Introduction to C EECS May 2019.
An Overview of C.
Introduction to C CSE 2031 Fall /15/2019 8:26 AM.
Administrative things
Presentation transcript:

1 CSE 303 Lecture 8 Intro to C programming read C Reference Manual pp. Ch. 1, , 2.6, 3.1, 5.1, , , , Ch. 8 ; Programming in C Ch. 1-6 slides created by Marty Stepp

2 Lecture summary History and characteristics of C major C language features  differences between C and Java basic console input and output( printf and scanf ) Our learning objectives in C:  procedural programming  deeper understanding of program compilation and execution  learn details of memory management  debugging skills  software development strategies

3 History created in 1972 by Dennis Ritchie of Bell Labs to accompany the Unix operating system  latest version standard: "C99" (1999) designed for creating system software (programs close to the OS that talk directly to hardware)  also designed to be hardware-independent (portable)  C is also used to develop high-level applications currently the 1st or 2nd most widely used language worldwide based on ALGOL; has influenced the designs of many languages  C++, Java, C#, Perl, PHP, JavaScript, Objective-C, D,...

4 Characteristics of C fairly similar basic syntax and semantics to Java  if/else, for, while, int, double, {} [] () ; +- */% ++ much smaller provided standard library / API than Java more low-level (more work for programmer, less for compiler) procedural (not object-oriented)  C (essentially) does not have objects as we know them  verb(noun); rather than noun.verb(); more unsafe (an incorrect program can cause more damage)  C programs have more direct access to the system / hardware

5 First C program #include int main(void) { printf("Hello, world!\n"); return 0; } Kernighan and Ritchie started the convention that the first program you show in a new language should be one that prints "Hello, world!"

6 Dissecting Hello World #include int main(void) { printf("Hello, world!\n"); return 0; } like import in Java; links the program to the standard I/O library (includes printf function) the main function header; you don't need to say public static because these are the default in C main returns an int error code to the OS (0 on success, > 0 on failure) like println in Java (actually more like System.out.printf ); prints output to console

7 Second C program /* Computes greatest common divisor (GCD) with Euclid's algorithm. */ #include int main(int argc, char** argv) { int a, b, temp, r; printf("Please enter two positive integers: "); scanf("%d %d", &a, &b); if (b > a) { temp = a; a = b; b = temp; } while ((r = a % b) != 0) { a = b; b = r; } printf("The GCD is %d.\n", b); return 0; }

8 Compiling/running to compile a program, type: gcc -o target source.c (where target is the name of the executable program to build)  the compiler builds an actual executable file, not a.class like Java  example: gcc -o hi hello.c to run your program, just execute that file  example:./hi commanddescription gcc GNU C compiler

9 gcc options (partial) most common usage for this course: gcc -g -Wall -o target source.c  the warnings from -Wall will protect us from unwise idioms optiondescription -W level of warnings to display (common usage: -Wall for all warnings) -o output executable file name (if omitted, compiles to file a.out ) -g generates information for debugger tools

10 printf printf(" format string ", parameters ); A format string contains placeholders to insert parameters into it:  %d or %i an integer  %lf a double ('long floating-point')  %s a string  %p a pointer (seen later) int x = 3; int y = 2; printf("(%d, %d)\n", x, y); // (3, 2) functiondescription printf prints formatted output to console

11 printf continued A placeholder can specify the parameter's width or precision:  %8d an integer, 8 characters wide, right-aligned  %-8d an integer, 8 characters wide, left-aligned  %.4f a real number, 4 digits after decimal  %6.2f a real number, 6 total characters wide, 2 after decimal  Examples: int age = 45; double gpa = ; printf("%8d %7.3f\n", age, gpa); printf("%8.2f %.1f %10.5f", gpa, gpa, gpa);  Output:

12 Same as Java general syntax for statements, control structures, function calls types int, double, char, long  type-casting syntax expressions, operators, precedence + - * / % = += -= *= /= %= >= && || ! scope (within set of { } braces) comments: /*... */, // ( // not officially legal until C99)

13 Mostly the same as Java variables  can be used without being initialized (!)  must be declared at the start of a function or block (changed in C99) for loops  variable cannot be declared in the loop header if/else statements, while and do/while loops  there is no boolean type(changed in C99)  any type of value can be used as a test  0 means false, every other number means true parameters / returns  C has certain features for values vs. references ("pointers")

14 Very different from Java Strings  very clunky to use in C; essentially just arrays of characters  are not objects; do not contain methods (external string functions) I/O to console and files  no Scanner ; must use input functions such as scanf  console I/O different than file I/O errors and exceptions  C has no try/catch and does not represent errors as objects  errors are usually returned as integer error codes from functions  crashes are mostly called "segmentation faults" and are evil

15 Also very different arrays  are just bare contiguous blocks of memory  have no methods and do not know their own length (!) objects  C doesn't have them  closest similar feature: struct (a set of fields; no methods) memory management  most memory that you consume, you must explicitly free afterward API and provided libraries  C doesn't have very many, compared to Java  you must write many things yourself (even data structures)

16 scanf scanf(" format string ", variables );  uses same syntax for formatted strings, placeholders as printf double s use %lf ('long float')  must precede each variable with an & (address-of operator) int x; int y; printf("Type your x and y values: "); scanf("%d %d", &x, &y); functiondescription scanf reads formatted input from console

17 scanf continued scanf returns the number of values successfully read  can be examined to see whether the reading was successful if # of variables listed doesn't match # of format placeholders:  too many variables:later ones ignored  too few variables:program crashes! string can be complex to match a specific input pattern int x; int y; printf("What is your (x, y) point?\n"); scanf("My point is (%d, %d)", &x, &y);

18 Exercise Write a C program that makes change:  prompts the user for an amount of money  reports the number of pennies, nickels, dimes, quarters, and dollars Example: Amount of money? Pennies : 2 Nickels : 1 Dimes : 1 Quarters: 3 Dollars : 17

19 Exercise solution #include int main(void) { int pennies = 0, nickels = 0, dimes = 0, quarters = 0, dollars; double money; printf("Amount of money? "); scanf("%lf", &money); dollars = (int) money; pennies = (int) (money * 100) % 100; while (pennies >= 25) { pennies -= 25; quarters++; } while (pennies >= 10) { pennies -= 10; dimes++; } while (pennies >= 5) { pennies -= 5; nickels++; } printf("Pennies : %3d\n", pennies); printf("Nickels : %3d\n", nickels); printf("Dimes : %3d\n", dimes); printf("Quarters: %3d\n", quarters); printf("Dollars : %3d\n", dollars); return 0; }