CSE2301. Functions. Functions and Compiler Directives

Similar documents
Review: Constants. Modules and Interfaces. Modules. Clients, Interfaces, Implementations. Client. Interface. Implementation

Introduction to Programming Systems

Programming in C. Part 1: Introduction

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

The C standard library

Chapter 8 C Characters and Strings

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Computer Programming

C Libraries. Bart Childs Complementary to the text(s)

CS3157: Advanced Programming. Outline

Scientific Programming in C V. Strings

C: How to Program. Week /May/28

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Intermediate Programming, Spring 2017*

C mini reference. 5 Binary numbers 12

High-performance computing and programming I. Introduction to C programming in *NIX

Characters and Strings

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Chapter 8 - Characters and Strings

Multiple Choice Questions ( 1 mark)

C Programs: Simple Statements and Expressions

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Course organization. Course introduction ( Week 1)

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

AIR FORCE SCHOOL,BAMRAULI COMPUTER SCIENCE (083) CLASS XI Split up Syllabus (Session ) Contents

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

,$5(0%(''(':25.%(1&+ $16,&'(9(/230(17722/6 EMBEDDED WORKBENCH ANSI C COMPILER C-SPY FOR NATIONAL SEMICONDUCTOR CORP. S &RPSDFW5,6& 70 &5

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

C: Arrays, and strings. Department of Computer Science College of Engineering Boise State University. September 11, /16

Course organization. Part I: Introduction to C programming language (Week 1-12) Chapter 1: Overall Introduction (Week 1-4)

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

CSE 333 Lecture 7 - final C details

Strings and Library Functions

COMP327 Mobile Computing Session:

independent compilation and Make

Structured programming

Outline. Computer Programming. Structure of a function. Functions. Function prototype. Structure of a function. Functions

Tutorial 5. PDS Lab Section 16 Autumn Functions The C language is termed as function-oriented programming

Contents. Preface. Introduction. Introduction to C Programming

CSE123. Program Design and Modular Programming Functions 1-1

Index. backslash character, 19 backup, off-site, 11. abs, 72 abstraction, 63, 83, 133, 141, 174, 181 acos, 72

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

SWEN-250 Personal SE. Introduction to C

Review Topics. Final Exam Review Slides

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

JTSK Programming in C II C-Lab II. Lecture 3 & 4

( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING UNIT V 2 MARKS

by Pearson Education, Inc. All Rights Reserved.

THE C STANDARD LIBRARY & MAKING YOUR OWN LIBRARY. ISA 563: Fundamentals of Systems Programming

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

OBJECTIVE QUESTIONS: Choose the correct alternative:

Introduction to string

8. Characters, Strings and Files

Language Design COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Introduction C CC. Advanced C

Programming and Data Structure

A Fast Review of C Essentials Part II

2 Compiling a C program

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13

ECET 264 C Programming Language with Applications

Chapter 9 Strings. With this array declaration: char s[10];

today cs3157-fall2002-sklar-lect05 1

CSSE 332 Standard Library, Storage classes, and Make

mith College Computer Science CSC270 Spring 2016 Circuits and Systems Lecture Notes, Week 11 Dominique Thiébaut

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Intermediate Programming, Spring 2017*

This exam is to be taken by yourself with closed books, closed notes, no calculators.

CSE2301. Arrays. Arrays. Arrays and Pointers

CSCE150A. Introduction. Basics. String Library. Substrings. Line Scanning. Sorting. Command Line Arguments. Misc CSCE150A. Introduction.

CSE2301. Arrays. Arrays. Arrays and Pointers

CSE2301. Arrays and Pointers. These slides are based on slides by Prof. Wolfgang Stuerzlinger at York University. Arrays

Using Character Arrays. What is a String? Using Character Arrays. Using Strings Life is simpler with strings. #include <stdio.

Final C Details. CSE 333 Autumn 2018

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 9. Strings. Notes. Notes. Notes. Lecture 07 - Strings

Final C Details, Build Tools

Functions. Systems Programming Concepts

Unit 4 Preprocessor Directives

C Characters and Strings

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

The C Language Reference Manual

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings

Library and function of C. Dr. Donald Davendra Ph.D. (Department of ComputingLibrary Science, andfei function VSB-TU of COstrava)

Split up Syllabus (Session )

Introduction to C Language

ch = argv[i][++j]; /* why does ++j but j++ does not? */

C introduction: part 1

C Review. SWE2004: Principles in Programming Spring 2014 Euiseong Seo

MC8051: Speichertypen und Adressräume


Personal SE. Functions, Arrays, Strings & Command Line Arguments

Transcription:

Warning: These notes are not complete, it is a Skelton that will be modified/add-to in the class. If you want to us them for studying, either attend the class or get the completed notes from someone who did CSE2301 Functions and Compiler Directives Functions Function is a small program that may receives some data, perform some computations, and may return a value. Return_type function_name(arguments declaration) { declaration statements In C, call is by value (example). void if doesn t return anything int power(int base, int exp) { int i, p = 1; for (i = 1; i <= exp; ++i) { p *= base; return p; 1

strcpy void strcpy(char *s, char *t) { int i; i=0; while( t[i]!= \0 ) { s[i]=t[i]; i++; s[i]= \0 ; strcpy void strcpy(char *s, char *t) { int i; i=0; while( (s[i] = t[i])!= \0 ) i++; strcpy void strcpy(char *s, char *t) { while( (*s = *t)!= \0 ) { s++; t++ 2

strcpy void strcpy(char *s, char *t) { while( (*s++ = *t++)!= \0 ) ; Declaration Before the use, functions must be declared. int mysum(int a, int b); Function may return a value using return Retruning a value from a function that returns void is an error Not returning a value from a function that returns a value is unpredictable. Scope Variables do exist within their block. For functions, all variables are created at the time the function is called, and vanishes after the function returns (automatic variables). If a variable is declared before main, then it is visible to all functions in the file, but could be overridden by declaring a variable by the same name in a function. 3

Scope Usually, an external variable must be declared extern in any function that uses it (extern char line[];) If the function in the same file as the variable declaration, no need for extern If a variable is declared static outside of main, it is not visible to other files (only in this file). Example extern.c #include <stdio.h> int myfun(int){ extern i; printf( %d\n,i); int j; j=myfun(i); printf( %d %d\n,i,j); extern1.c #include <stdio.h> int i=1o; int myfun(int) { printf( myfun: %d\n,j); i=17; return 7; 10 myfun: 10 17 7 Scope static could be used also with functions. If a variable in a function is declared static, the variables does not vanish after the function returns, it stays in the memory so the next call to the function will find the old value. static variables are initialized to 0. 4

Preprocessor Preprocessor processes the file before compilation. It handles #define and #include #define ON 1 #define OFF 0 #define square(x) ((x)*(x)) #define square(x) x*x??? #define square(x) x*x;? Preprocessor Conditional inclusion #if!defined(hdr) #define HDR //include the file here #ifdef HDR #define HDR #endf #undef x undefines x Preprocessor Formal parameters are not replaced within quoted strings. ## means concatenate If the parameter name is proceeded by a # in the replacement text, the combination will be expanded into a quoted string with the parameter replaced by the actual argument #define dprint(expr) printf(#expr = %g \n, expr) 5

Preprocessor #ifdef can also be used for conditional compilation. Compilation cc file.c produces a.out Compiling a C program Converting the.c file into assembly.s Compiling the assembly into a machine code (object code).o Linking the.o file to the code library and is named a.out What happens when you have several files Compilation File.c Common.h File.o a.out Myfunc.c Myfunc.o We can produce the.o file using cc c File.c 6

Compilation Usually, your program will be divided into several files. Be careful with global variables and functions name. Every file will be compiled into its.o Finally, all the.o files can be compiled to produce a.out cc File.o Myfunc.o Dependency Graph Project Comp.o Main.o io.o Comp.c Common.h Main.c io.h io.c Compilation If we changed one file, there is no need to recompile all the files. Make files are used to manage that The name of the file is makefile or Makefile 7

Project Comp.o Main.o io.o Comp.c Common.h Main.c io.h io.c Project1: Comp.o main.o io.o cc Comp.o main.o io.o o Project Comp.o : Comp.c Common.h cc c Comp.c Main.o: Main.c Common.c io.h cc c Main.c io.o: io.h io.c cc c io.c Makefile Each dependency is handled as Target: source files Command (preceded by a tab) To compile just type make Macros in Makefile OBJECTS = Comp.o Main.o io.o Project: $(OBJECTS) gcc $(OBJECTS) o Project Comp.o: Comp.c Common.h gcc c Comp.c Main.o: Main.c Common.h io.h gcc c Main.c io.o: io.c io.h gcc c io.c 8

Standard C Library assert.h ctype.h errno.h math.h limits.h signal.h stdarg.h stddef.h stdio.h stdlib.h string.h time.h assertions character mappings error numbers math functions metrics for ints signal handling variable length arg lists standard definitions standard I/O standard library functions string functions date/type functions Standard C Libraries Utility functions stdlib.h atof, atoi, rand, qsort, getenv, calloc, malloc, free, abort, exit String handling string.h strcmp, strncmp, strcpy, strncpy, strcat, strncat, strchr, strlen, memcpy, memcmp Character classifications ctype.h isdigit, isalpha, isspace, isupper, islower Mathematical functions math.h sin, cos, tan, ceil, floor, exp, log, sqrt 9