Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.

Slides:



Advertisements
Similar presentations
C: Advanced Topics-II Winter 2013 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
Advertisements

Introduction to C Programming
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Arithmetic in Pascal (2) Arithmetic Functions Perform arithmetic calculations Gives an argument to the function and it returns the result.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Adv. UNIX: lib/121 Advanced UNIX v Objectives of these slides: –look at some of the less familiar functions in the ANSI C Standard Library
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
CS 201 Functions Debzani Deb.
CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004.
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.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
Data Structure and C Part-6. Naming a Function Any valid variable-name can be given to the user-defined function. The main program itself is considered.
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Introduction to C programming
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
C Basic File Input/Output Manipulation C Programming – File Outline v File handling in C - opening and closing. v Reading from and writing to files.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
Functions Why we use functions C library functions Creating our own functions.
C Review. C Program Structure Compilation of a program: gcc –o hello hello.c -o specifies the name of the executable file Other flags: Wall – turn on.
CMSC 1041 Functions II Functions that return a value.
Characters and Strings File Processing Exercise C Programming:Part 3.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Formatting Output.
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
Department of Electrical and Computer Engineering Introduction to C++: Primitive Data Types, Libraries and Operations By Hector M Lugo-Cordero August 27,
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Introduction to C For the participants of Practical Course „Scientific Computing and Visualization“
Module-E- Libraries1/49 Module E- Standard Libraries Input and Validation Formatted Output Library Functions.
Copyright © 2002, Department of Systems and Computer Engineering, Carleton University 1 INPUT STREAMS ifstream xin; // declares an input stream.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
Computer Science: A Structured Programming Approach Using C1 4-5 Standard Functions C provides a rich collection of standard functions whose definitions.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Lecture 3 Expressions, Type Conversion, Math and String
Arithmetic Expressions
Mathematical Functions
TMF1414 Introduction to Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
File Access (7.5) CSE 2031 Fall July 2018.
C Short Overview Lembit Jürimägi.
C Programming:Part 3 Characters and Strings File Processing Exercise.
Chapter 5 - Functions Outline 5.1 Introduction
C Review.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Programming in C Input / Output.
Functions, Part 2 of 3 Topics Functions That Return a Value
C What you Know* Objective: To introduce some of the features of C. This assumes that you are familiar with C++ or java and concentrates on the features.
File Input and Output.
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Line at a time I/O with fgets() and fputs()
Strings #include <stdio.h>
C Characters and Strings
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions that return a value
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions for performing common tasks.

Standard C Library Many of the general purpose functions available in the the Standard C library are declared in the following: math.h strings.h stdio.h stdlib.h time.h We’ll discuss the most commonly used functions declared in these.

math.h The math library contains what you would expect: functions for performing common mathematical operations. These include Function Description abs returns the absolute value of an integer x cos returns the cosine of x, where x is in radians exp returns e x fabs returns the absolute value of a float x log returns the logarithm to base e log10 returns the logarithm to base 10 pow returns x y sin returns the sine of x, where x is in radians sqrt returns the square root of x tan returns the tangent of x, where x is in radians

math.h The math library also contains functions you may not have heard of: Function Description ceil the ceiling function rounds a number with a decimal part up to the next highest integer (written mathematically as ⌈ x ⌉ ) floor the floor function rounds a number with a decimal part down to the next lowest integer (written mathematically as ⌊ x ⌋ ) See example-library-math.c on the course webpage. Note: Most of the functions in the math library expect the function argument(s) to be of type double and return a value of type double.

strings.h There are many functions for manipulating strings. Some of the more useful are Function Description strcat concatenates (i.e., adds) two strings strcmp compares two strings strcpy copies a string strlen calculates the length of a string (not including the null) strstr finds a string within another string strtok divides a string into tokens (i.e., parts) See example-library-strings.c on the course webpage. See example-library-strings2.c on the course webpage. Note: strcpy() and strcat() do not allocate the space required for their Operations.

stdio.h We’ve already seen many of the functions in stdio.h. Function Description printf formatted printing to stdout scanf formatted input from stdin fprintf formatted printing to a file fscanf formatted input from a file getc get a character from a stream (e.g, stdin or a file) putc write a character to a stream (e.g, stdout or a file) fgets get a string from a stream fputs write a string to a stream fopen open a file fclose close a file

stdlib.h Function Description atof convert a string to a double (not a float) atoi convert a string to an int exit terminate a program, return an integer value free release allocated memory malloc allocate memory rand generate a pseudo-random number srand seed the pseudo-random number generator system execute an external command See example-library-stdlib.c on the course webpage.

time.h This library contains several functions for getting the current date and time. Function Description time get the system time ctime convert the result from time() to something meaningful See example-library-time.c on the course webpage.