Week 2 - Friday.  What did we talk about last time?  Base systems  C literals  Representations in memory.

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Fundamentals of Computer and programming in C Programming Languages, Programs and Programming Rohit Khokher.
Computer Programming w/ Eng. Applications
Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function.
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?
Functions. COMP104 Lecture 13 / Slide 2 Review of Array: Bubble Sort for (j=0; j List[j+1]) swap(List[j], List[j+1]); }
Introduction to Java Programming, 4E Y. Daniel Liang.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
1 Chapter 5 Methods. 2 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Topic 2A – Library Functions and Casting. CISC 105 – Topic 2A Functions A function is a piece of code which performs a specific task. When a function.
1 Topic 04 Methods Programming II/A CMC2522 / CIM2561 Bavy Li.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Python Libraries Importing and Calling functions.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
1 Last of the basics Controlling output Overflow and underflow Standard function libraries Potential pitfalls of getting information with cin Type casting.
1 Functions. 2 Function A program segment that carries out some specific, well-defined task Example  A function to add two numbers  A function to find.
Lecture 5 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Chapter 5: Methods 1. Objectives To declare methods, invoke methods, and pass arguments to a method (§ ). To use method overloading and know ambiguous.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Method Abstraction You can.
Chapter 4 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Week 2 - Friday.  What did we talk about last time?  Using Scanner to get input  Basic math operations.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
Week 2 - Wednesday.  What did we talk about last time?  C compilation model  Lab 1.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Week 15 - Monday.  What did we talk about last time?  STL  Lab 14.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 4 Methods Chapter.
Exam / Homework Exam 1 Starting K&R chapter 4 tonight
1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Chapter 4: Methods Method Structure Method Structure Declaring Methods Declaring Methods Calling Methods Calling Methods Passing Parameters Passing Parameters.
1 CS 1430: Programming in C++. 2 C++ Loops Sentinel Controlled Count Controlled EOF Controlled.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
Adv. UNIX:pre/111 Advanced UNIX v Objectives of these slides: –look at the features of the C preprocessor Special Topics in Comp. Eng.
Operators and Expressions. Introduction C operators can be classified into a number of categories 1.Arithmetic(+, -, *, /, %) 2.Relational (, >=, ==,
Lecture 5: java.lang.Math, java.lang.String and Characters Michael Hsu CSULA.
Chapter 5 Methods F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters F Pass by Value F Overloading Methods F Method Abstraction.
Chapter 4 Mathematical Functions, Characters, and Strings 1.
Chapter 4 Mathematical Functions, Characters, and Strings
Week 3 - Monday CS222.
Week 5 - Friday CS222.
Week 3 - Friday CS222.
Type casting Algorithm & flowchart
Chapter 4 Mathematical Functions, Characters, and Strings
Functions, Part 2 of 2 Topics Functions That Return a Value
Functions Separate Compilation
Functions.
Week 4 - Monday CS222.
Chapter 3 Methods.
Chapter 5 – Part 2 Methods Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4: Mathematical Functions, Characters, and Strings
Using Free Functions Chapter 3 Computing Fundamentals with C++
C Preprocessor(CPP).
Chapter 5 Methods.
Chapter 4 Methods Introducing Methods Declaring Methods
Week 2 - Friday CS222.
Functions in C Math Library Functions Functions Function Definitions
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

Week 2 - Friday

 What did we talk about last time?  Base systems  C literals  Representations in memory

It ain't what you don't know that gets you into trouble. It's what you know for sure that just ain't so. Mark Twain It ain't what you don't know that gets you into trouble. It's what you know for sure that just ain't so. Mark Twain

FunctionResultFunctionResult cos(double theta) Cosine of theta exp(double x) ex ex sin(double theta) Sine of theta log(double x) Natural logarithm of x tan(double theta) Tangent of theta log10(double x) Common logarithm of x acos(double x) Arc cosine of x pow(double base, double exponent) Raise base to power exponent asin(double x) Arc sine of x sqrt(double x) Square root of x atan(double x) Arc tangent of x ceil(double x) Round up value of x atan2(double y, double x) Arc tangent of y/x floor(double x) Round down value of x fabs(double x) Absolute value of x fmod(double value, double divisor) Remainder of dividing value by divisor

 You must #include to use math functions #include int main() { double a = 3.0; double b = 4.0; double c = sqrt(a*a + b*b); printf("Hypotenuse: %f\n", c); return 0; } #include int main() { double a = 3.0; double b = 4.0; double c = sqrt(a*a + b*b); printf("Hypotenuse: %f\n", c); return 0; }

 Just using #include gives the headers for math functions, not the actual code  You must link the math library with flag –lm  Now, how are you supposed to know that? > gcc -lm hypotenuse.c -o hypotenuse > man 3 sqrt

 Man (manual) pages give you more information about commands and functions, in 8 areas: 1. General commands 2. System calls 3. Library functions (C library, especially) 4. Special files and devices 5. File formats 6. Miscellaneous stuff 7. System administration  Try by typing man topic for something you're interested in  If it lists topics in different sections, specify the section  For more information: > man 3 sqrt > man man

 We haven't talked about any input in C yet  To read the next character from input, you can use the getchar() function  It will return the value of the next character (as an int ) or -1 if the end of the file is reached  Store the value as an int first to check to see if the end of the file has been reached  If not, you can then store it as a char int value = getchar(); if( value == -1 ) printf("End of file!"); int value = getchar(); if( value == -1 ) printf("End of file!");

 putchar() is the output equivalent of getchar()  It outputs a single character at a time  You could use printf() with the %c formatter instead, but putchar() can be more convenient for single characters char letter = 's'; putchar('q'); putchar(letter); char letter = 's'; putchar('q'); putchar(letter);

 There are preprocessor directives which are technically not part of the C language  These are processed before the real C compiler becomes involved  The most important of these are  #include  #define  Conditional compilation directives

 You have already used #include before  #include  It can be used to include any other file  Use angle brackets ( ) for standard libraries  Use quotes ( " " ) for anything else  It literally pastes the file into the document where the #include directive is  Never #include.c files (executable code), only.h files (definitions and prototypes)  It is possible to have a circular include problem

 The primary way to specify constants in C is with a #define  When you #define something, the preprocessor does a find and replace  Don't use a semicolon!  #define directives are usually put close to the top of a file, for easy visibility #define SIZE 100 int main() { int array[SIZE]; int i = 0; for( i = 0; i < SIZE; i++ ) array[i] = i*i; return 0; } #define SIZE 100 int main() { int array[SIZE]; int i = 0; for( i = 0; i < SIZE; i++ ) array[i] = i*i; return 0; }

 You can also make macros with #define that take arguments  You need to be careful with parentheses  Constants and macros are usually written in ALL CAPS to avoid confusion #include #define TO_DEGREES(x) ((x) * ) #define ADD(a,b) ((a) + (b)) int main() { double theta = TO_DEGREES(2*M_PI); int value = ADD(5 * 2, 7); return 0; } #include #define TO_DEGREES(x) ((x) * ) #define ADD(a,b) ((a) + (b)) int main() { double theta = TO_DEGREES(2*M_PI); int value = ADD(5 * 2, 7); return 0; }

 You can use directives #if, #ifdef, #ifndef, #else, #elif and #endif  These are mostly used to avoid infinite include problems  Sometimes they will change what gets compiled based on compiler version, system libraries, or other stuff #ifndef SOMETHING_H #define SOMETHING_H int something(int a, int b); #endif #ifndef SOMETHING_H #define SOMETHING_H int something(int a, int b); #endif

 System limits  const  Bitwise operators  Operator precedence

 Keep reading K&R chapter 2