Functions. Cedric Saule

Size: px
Start display at page:

Download "Functions. Cedric Saule"

Transcription

1 Cedric Saule

2 or procedures? In algorithmic (and some programming languages), we use two kinds of unconditional branchings : Procedures : execute computations and do not return nothing. Exemple : printing instructions, memory allocations Functions : execute computations and return a result. Exemples : log(), sin() 2

3 or procedures? In C, there is only functions. All unconditional branchings return a result. These results can be a type (classical functions). ''Procedures'' return the type ''void''. ''void'' is a real type, it cannot be omitted. 3

4 or procedures? A new datatype : ''void'' The ''void'' type is not ''nothing''. The returned value of such functions is ''void'' ''void'' can be used as an abstraction for generic pointers (Cf. next lesson) 4

5 Functions are composed of : Header : returned_type name(type1 variable_name1, type1 variable_name 2,...) Block : { Instructions. If the returned type is not ''void'', the function finishes by returning the variable compatible with the returned type as definied in the header. Examples : int computeaddition(int x, int y){return x+y; void printvalue(int value){printf(''the value is : %d\n'', value); 5

6 Function should be declared (ANSI C) Functions headers have to be found in header files. int computeaddition(int x, int y); void printvalue(int value); If no returned data type, the compiler will consider the returned type is ''int''. If no parameters, the ''void'' type should be used. void myfunction( void ); // no parameters. void myfunction(); // means the compiler will not check for parameters types. 6

7 Function call : int add(int x1, int x2) /* Declaration */ { int y = x1 + x2; return y; int myint = add(4, 5); /* Function call */ The call affects 4 to the local variable #1 and 5 to the local variable #2. 7

8 Classical mistakes : Functions Wrong number of arguments. Wrong argument type (be careful with implicit transtyping). Implicit conversions when possible. Impossible to decide the order of affectation. int i = 0; int x = myfunction(i++,i++); // We cannot predict what will happen. Is it myfunction(0,1) or myfunction(1,0)? 8

9 A surprising mistake : #include <stdio.h> int main() { double x1 = 1.2, x2 = 3.5; int i = myoddfunction(x1, x2); char myoddfunction(int a, int b) { char c; if(a < b) c = 'A'; else c = 'B'; return c; 9

10 A surprising mistake : #include <stdio.h> int main() { double x1 = 1.2, x2 = 3.5; int i = myoddfunction(x1, x2); char myoddfunction(int a, int b) { char c; if(a < b) c = 'A'; else c = 'B'; return c; main.c:12:6: error: conflicting types for 'myoddfunction' main.c:7:10: note: previous implicit declaration of 'myoddfunction' was here Explanation? 10

11 A surprising mistake : #include <stdio.h> int main() { double x1 = 1.2, x2 = 3.5; int i = myoddfunction(x1, x2); char myoddfunction(int a, int b) { char c; if(a < b) c = 'A'; else c = 'B'; return c; main.c:12:6: error: conflicting types for 'myoddfunction' Implicit declaration : int myoddfunction(double, double) New declaration : char myoddfunction(int, int) main.c:7:10: note: previous implicit declaration of 'myoddfunction' was here Explanation? 11

12 A surprising mistake : #include <stdio.h> char myoddfunction(int a, int b) { char c; if(a < b) c = 'A'; else c = 'B'; return c; int main() { double x1 = 1.2, x2 = 3.5; int i = myoddfunction(x1, x2); return 0; Lost of precision because of type conversion in the function. 12

13 A surprising mistake : #include <stdio.h> #include "main.h" int main() { double x1 = 1.2, x2 = 2.5; int i = myoddfunction(x1, x2); Main.h #ifndef MAIN_H #define MAIN_H char myoddfunction(int a, int b); #endif return 0; char myoddfunction(int a, int b) { char c; if(a < b) c = 'B'; else c = 'A'; return c; 13

14 A surprising mistake : #include <stdio.h> #include "main.h" int main() { double x1 = 1.2, x2 = 2.5; int i = myoddfunction(x1, x2); return 0; Main.h #ifndef MAIN_H #define MAIN_H char myoddfunction(int, int); #endif Type is enough char myoddfunction(int a, int b) { char c; if(a < b) c = 'B'; else c = 'A'; return c; 14

15 A surprising mistake : #include <stdio.h> #include "main.h" int main() { double x1 = 1.2, x2 = 2.5; int i = myoddfunction(x1, x2); return 0; Main.h #ifndef MAIN_H #define MAIN_H char myoddfunction(int, int); #endif Do not forget the '';'' char myoddfunction(int a, int b) { char c; if(a < b) c = 'B'; else c = 'A'; return c; 15

16 A surprising mistake : #include <stdio.h> #include "main.h" int main() { double x1 = 1.2, x2 = 2.5; int i = myoddfunction(x1, x2); Main.h #ifndef MAIN_H #define MAIN_H extern char myoddfunction(int, int); #endif return 0; char myoddfunction(int a, int b) { char c; ''extern'' functions are declared in another file. if(a < b) c = 'B'; else c = 'A'; return c; 16

17 In C language, everything is a function : main() is a function. All functions are global. No operation can be done out of a function (except global variable definition and affectation). Each type is passed by value (otherwise we have to specify it) The value of an argument is a copy of the value of the variable given as parameter. The original value is not modified. 17

18 void addone(int x) { Functions printf(''x = %d\n'', x); x++; printf(''x = %d\n'', x); Outputs: i = 0 int main { int i = 0; printf(''i = %d\n'', i); addone(i); printf(''i = %d\n'', i); return 0; x = 0 x = 1 i = 0 18

19 Unchecked number of arguments. How to use an undetermined number of variables in a clean way? ''Max'' example. #include <stdarg.h> int max(short n, int x1,...) { va_list magik; int x, i, m; m = x1; va_start(magik, x1); for (i = 2; i <= n; i++) { if ((x = va_arg(magik, int)) > m) m = x; return m; Undefined list of arguments Variable type defined in stdarg.h Functions defined in stdarg.h 19

20 Quick preview How to modify a variable inside a function? How to use a variable without knowing its type? (like arguments in ''printf'' ) How to define a generic sort function? Language C use a specific datatype : pointers. 20

Pointers. Cedric Saule

Pointers. Cedric Saule Pointers Cedric Saule cedric.saule@uni-bielefeld.de Variables and memory Each variable needs to be stored in memory. Memory is made of plenty cells of 8 bits (1 Byte) A ''char'' is stored in 1 cell, a

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 FUNCTIONS INTRODUCTION AND MAIN All the instructions of a C program are contained in functions. üc is a procedural language üeach function performs a certain task A special

More information

Unit 7. Functions. Need of User Defined Functions

Unit 7. Functions. Need of User Defined Functions Unit 7 Functions Functions are the building blocks where every program activity occurs. They are self contained program segments that carry out some specific, well defined task. Every C program must have

More information

The C standard library

The C standard library C introduction The C standard library The C standard library 1 / 12 Contents Do not reinvent the wheel Useful headers Man page The C standard library 2 / 12 The Hitchhiker s Guide to the standard library

More information

Language comparison. C has pointers. Java has references. C++ has pointers and references

Language comparison. C has pointers. Java has references. C++ has pointers and references Pointers CSE 2451 Language comparison C has pointers Java has references C++ has pointers and references Pointers Values of variables are stored in memory, at a particular location A location is identified

More information

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine

More information

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

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco CS 326 Operating Systems C Programming Greg Benson Department of Computer Science University of San Francisco Why C? Fast (good optimizing compilers) Not too high-level (Java, Python, Lisp) Not too low-level

More information

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays EE105: Software Engineering II Part 6 Pointers page 1 of 19 Part VI Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and Arrays 6) Pointers and

More information

C introduction: part 1

C introduction: part 1 What is C? C is a compiled language that gives the programmer maximum control and efficiency 1. 1 https://computer.howstuffworks.com/c1.htm 2 / 26 3 / 26 Outline Basic file structure Main function Compilation

More information

EM108 Software Development for Engineers

EM108 Software Development for Engineers EE108 Section 6 Pointers page 1 of 20 EM108 Software Development for Engineers Section 6 - Pointers 1) Introduction 2) Declaring Pointer Variables 3) Using Pointers 4) Pointer Arithmetic 5) Pointers and

More information

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14 C introduction Variables Variables 1 / 14 Contents Variables Data types Variable I/O Variables 2 / 14 Usage Declaration: t y p e i d e n t i f i e r ; Assignment: i d e n t i f i e r = v a l u e ; Definition

More information

Complex data structures. Cedric Saule

Complex data structures. Cedric Saule Complex data structures Cedric Saule cedric.saule@uni-bielefeld.de Arrays in language C In language C, arrays are contiguous spaces in memory. We make the difference between two kinds of arrays : Static

More information

Everything Else C Programming and Software Tools. N.C. State Department of Computer Science

Everything Else C Programming and Software Tools. N.C. State Department of Computer Science Everything Else C Programming and Software Tools N.C. State Department of Computer Science BOOLEANS CSC230: C and Software Tools NC State University Computer Science Faculty 2 Booleans In C99, bools are

More information

Question 1. Part (a) [2 marks] error: assignment of read-only variable x ( x = 20 tries to modify a constant) Part (b) [1 mark]

Question 1. Part (a) [2 marks] error: assignment of read-only variable x ( x = 20 tries to modify a constant) Part (b) [1 mark] Note to Students: This file contains sample solutions to the term test together with the marking scheme and comments for each question. Please read the solutions and the marking schemes and comments carefully.

More information

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

#include <stdio.h> int main() { printf (hello class\n); return 0; } C #include int main() printf ("hello class\n"); return 0; Working environment Linux, gcc We ll work with c9.io website, which works with ubuntu I recommend to install ubuntu too Also in tirgul

More information

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments

Annotation Annotation or block comments Provide high-level description and documentation of section of code More detail than simple comments Variables, Data Types, and More Introduction In this lesson will introduce and study C annotation and comments C variables Identifiers C data types First thoughts on good coding style Declarations vs.

More information

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265 Parameter passing Programming in C UVic SEng 265 Daniel M. German Department of Computer Science University of Victoria 1 SEng 265 dmgerman@uvic.ca C implements call-by-value parameter passing int a =

More information

The Three Attributes of an Identifier

The Three Attributes of an Identifier The Three Attributes of an Identifier Identifiers have three essential attributes: storage duration scope linkage Identifier Attributes 1 Storage Duration storage duration Identifier Attributes 2 determines

More information

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above P.G.TRB - COMPUTER SCIENCE Total Marks : 50 Time : 30 Minutes 1. C was primarily developed as a a)systems programming language b) general purpose language c) data processing language d) none of the above

More information

UNIT III (PART-II) & UNIT IV(PART-I)

UNIT III (PART-II) & UNIT IV(PART-I) UNIT III (PART-II) & UNIT IV(PART-I) Function: it is defined as self contained block of code to perform a task. Functions can be categorized to system-defined functions and user-defined functions. System

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information

15 FUNCTIONS IN C 15.1 INTRODUCTION

15 FUNCTIONS IN C 15.1 INTRODUCTION 15 FUNCTIONS IN C 15.1 INTRODUCTION In the earlier lessons we have already seen that C supports the use of library functions, which are used to carry out a number of commonly used operations or calculations.

More information

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Science & Engineering 150A Problem Solving Using Computers Computer Science & Engineering 150A Problem Solving Using Computers Lecture 06 - Stephen Scott Adapted from Christopher M. Bourke 1 / 30 Fall 2009 Chapter 8 8.1 Declaring and 8.2 Array Subscripts 8.3 Using

More information

TDDE18 & 726G77. Functions

TDDE18 & 726G77. Functions TDDE18 & 726G77 Functions Labs update No more one time password. We will note who have demonstrated during the lab and register this in webreg. Use the terminal to send in your lab! Dont use Visual studio

More information

2 Compiling a C program

2 Compiling a C program 2 Compiling a C program This chapter describes how to compile C programs using gcc. Programs can be compiled from a single source file or from multiple source files, and may use system libraries and header

More information

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes Distributed Real-Time Control Systems Lecture 17 C++ Programming Intro to C++ Objects and Classes 1 Bibliography Classical References Covers C++ 11 2 What is C++? A computer language with object oriented

More information

Week 5 Lecture 3. Compiler Errors

Week 5 Lecture 3. Compiler Errors Lecture 3 Compiler Errors Agile Development Backlog of stories defines projects Backlog contains all of the requirements currently known Stories define features of the project Three elements: feature user,

More information

In addition to name. Each variable in C program is characterized by its Type Scope Storage Class. Type Have already discussed

In addition to name. Each variable in C program is characterized by its Type Scope Storage Class. Type Have already discussed Variables, Storage Class, Scope, Multiple File Multiple Module Projects Overview Will examine how variables characterized Will introduce and study variable s storage class Will examine a variable s visibility

More information

Conditional Compilation

Conditional Compilation Conditional Compilation printf() statements cab be inserted code for the purpose of displaying debug information during program testing. Once the program is debugged and accepted as "working'', it is desirable

More information

// file2.c. // file1.c #include <stdio.h> int A1 = 42; // 1.1 static int B1; // 1.2. int A2 = 12; // 2.1 int B2; // 2.2. extern int A2; // 1.

// file2.c. // file1.c #include <stdio.h> int A1 = 42; // 1.1 static int B1; // 1.2. int A2 = 12; // 2.1 int B2; // 2.2. extern int A2; // 1. Instructions: This homework assignment focuses primarily on issues arising when compiling and linking C programs that consist of multiple source files. The answers to the following questions can be determined

More information

Fundamentals of Programming & Procedural Programming

Fundamentals of Programming & Procedural Programming Universität Duisburg-Essen PRACTICAL TRAINING TO THE LECTURE Fundamentals of Programming & Procedural Programming Session Four: Functions: Built-in, Parameters and Arguments, Fruitful and Void Functions

More information

DECLARAING AND INITIALIZING POINTERS

DECLARAING AND INITIALIZING POINTERS DECLARAING AND INITIALIZING POINTERS Passing arguments Call by Address Introduction to Pointers Within the computer s memory, every stored data item occupies one or more contiguous memory cells (i.e.,

More information

Exercise Session 2 Simon Gerber

Exercise Session 2 Simon Gerber Exercise Session 2 Simon Gerber CASP 2014 Exercise 2: Binary search tree Implement and test a binary search tree in C: Implement key insert() and lookup() functions Implement as C module: bst.c, bst.h

More information

LESSON 6 FLOW OF CONTROL

LESSON 6 FLOW OF CONTROL LESSON 6 FLOW OF CONTROL This lesson discusses a variety of flow of control constructs. The break and continue statements are used to interrupt ordinary iterative flow of control in loops. In addition,

More information

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/ CS61C Machine Structures Lecture 5 C Structs & Memory Mangement 1/27/2006 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L05 C Structs (1) C String Standard Functions

More information

Basic Pointers. CSCI 112: Programming in C

Basic Pointers. CSCI 112: Programming in C Basic Pointers CSCI 112: Programming in C What the #$@# is a pointer? Pointers are variables Instead of holding values, they hold the address of another variable. They point to where that other variable

More information

the gamedesigninitiative at cornell university Lecture 6 C++: Basics

the gamedesigninitiative at cornell university Lecture 6 C++: Basics Lecture 6 C++: Basics So You Think You Know C++ Most of you are experienced Java programmers Both in 2110 and several upper-level courses If you saw C++, was likely in a systems course Java was based on

More information

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011 CS 61C: Great Ideas in Computer Architecture (Machine Structures) Introduction to C (Part II) Instructors: Randy H. Katz David A. Patterson http://inst.eecs.berkeley.edu/~cs61c/sp11 Spring 2011 -- Lecture

More information

CIS 190: C/C++ Programming. Classes in C++

CIS 190: C/C++ Programming. Classes in C++ CIS 190: C/C++ Programming Classes in C++ Outline Header Protection Functions in C++ Procedural Programming vs OOP Classes Access Constructors Headers in C++ done same way as in C including user.h files:

More information

Chapter 14 - Advanced C Topics

Chapter 14 - Advanced C Topics Chapter 14 - Advanced C Topics Outline 14.1 Introduction 14.2 Redirecting Input/Output on UNIX and DOS Systems 14.3 Variable-Length Argument Lists 14.4 Using Command-Line Arguments 14.5 Notes on Compiling

More information

PESIT-BSC Department of Science & Humanities

PESIT-BSC Department of Science & Humanities LESSON PLAN 15PCD13/23 PROGRAMMING IN C AND DATA Course objectives: STRUCTURES The objective of this course is to make students to learn basic principles of Problem solving, implementing through C programming

More information

Chapter 7 Functions. Now consider a more advanced example:

Chapter 7 Functions. Now consider a more advanced example: Chapter 7 Functions 7.1 Chapter Overview Functions are logical groupings of code, a series of steps, that are given a name. Functions are especially useful when these series of steps will need to be done

More information

Chapter 10. Programming in C

Chapter 10. Programming in C Chapter 10 Programming in C Lesson 05 Functions in C C program Consists of three parts preprocessor directives macros main function functions 3 Function Each has a name (for identity ID) May have the arguments

More information

Functions. Systems Programming Concepts

Functions. Systems Programming Concepts Functions Systems Programming Concepts Functions Simple Function Example Function Prototype and Declaration Math Library Functions Function Definition Header Files Random Number Generator Call by Value

More information

CMPE-013/L. Expressions and Control

CMPE-013/L. Expressions and Control CMPE-013/L Expressions and Control Gabriel Hugh Elkaim Winter 2015 #include Directive Three ways to use the #include directive: Syntax #include Look for file in the compiler search path The compiler

More information

More on C programming

More on C programming Applied mechatronics More on C programming Sven Gestegård Robertz sven.robertz@cs.lth.se Department of Computer Science, Lund University 2017 Outline 1 Pointers and structs 2 On number representation Hexadecimal

More information

Lecture 8 Tao Wang 1

Lecture 8 Tao Wang 1 Lecture 8 Tao Wang 1 Objectives In this chapter, you will learn about: Sorting 3 numbers review Function overview Function and parameter declarations Function with empty parameter lists Default arguments

More information

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

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University Unit 1 Programming Language and Overview of C 1. State whether the following statements are true or false. a. Every line in a C program should end with a semicolon. b. In C language lowercase letters are

More information

CGS 3460 Summer 07 Midterm Exam

CGS 3460 Summer 07 Midterm Exam Short Answer 3 Points Each 1. What would the unix command gcc somefile.c -o someotherfile.exe do? 2. Name two basic data types in C. 3. A pointer data type holds what piece of information? 4. This key

More information

1 You seek performance 3

1 You seek performance 3 Why? 2 1 You seek performance 3 1 You seek performance zero-overhead principle 4 2 You seek to interface directly with hardware 5 3 That s kinda it 6 C a nice way to avoid writing assembly language directly

More information

Advanced Pointer & Data Storage

Advanced Pointer & Data Storage 18, 19: storage classes 14: Preprocessor & Polymorphism in C) 15 : command line building 26 : stdarg Advanced Pointer & Data Storage (for ch. 14, 15 18, 19, 26) Contents Preprocessor & Polymorphism in

More information

Introduction to C An overview of the programming language C, syntax, data types and input/output

Introduction to C An overview of the programming language C, syntax, data types and input/output Introduction to C An overview of the programming language C, syntax, data types and input/output Teil I. a first C program TU Bergakademie Freiberg INMO M. Brändel 2018-10-23 1 PROGRAMMING LANGUAGE C is

More information

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/ CS61C Machine Structures Lecture 4 C Structs & Memory Management 9/5/2007 John Wawrzynek (www.cs.berkeley.edu/~johnw) www-inst.eecs.berkeley.edu/~cs61c/ CS 61C L04 C Structs (1) C String Standard Functions

More information

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0; 1. Short answer questions: (a) Compare the typical contents of a module s header file to the contents of a module s implementation file. Which of these files defines the interface between a module and

More information

Decision Making -Branching. Class Incharge: S. Sasirekha

Decision Making -Branching. Class Incharge: S. Sasirekha Decision Making -Branching Class Incharge: S. Sasirekha Branching The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the

More information

CSE 333 Midterm Exam 5/9/14 Sample Solution

CSE 333 Midterm Exam 5/9/14 Sample Solution Question 1. (20 points) C programming. Implement the C library function strncpy. The specification of srncpy is as follows: Copy characters (bytes) from src to dst until either a '\0' character is found

More information

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor. 3.Constructors and Destructors Develop cpp program to implement constructor and destructor. Constructors A constructor is a special member function whose task is to initialize the objects of its class.

More information

Lesson 5: Functions and Libraries. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Lesson 5: Functions and Libraries. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology Lesson 5: Functions and Libraries 1 Functions 2 Overview Function is a block of statements which performs a specific task, and can be called by others Each function has a name (not identical to any other),

More information

Stream Model of I/O. Basic I/O in C

Stream Model of I/O. Basic I/O in C Stream Model of I/O 1 A stream provides a connection between the process that initializes it and an object, such as a file, which may be viewed as a sequence of data. In the simplest view, a stream object

More information

A Crash Course in C. Steven Reeves

A Crash Course in C. Steven Reeves A Crash Course in C Steven Reeves This class will rely heavily on C and C++. As a result this section will help students who are not familiar with C or who need a refresher. By the end of this section

More information

CS61C : Machine Structures

CS61C : Machine Structures inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #3 C Strings, Arrays, & Malloc 2007-06-27 Scott Beamer, Instructor Sun announces new supercomputer: Sun Constellation CS61C L3 C Pointers

More information

CSE 333 Midterm Exam Sample Solution 7/28/14

CSE 333 Midterm Exam Sample Solution 7/28/14 Question 1. (20 points) C programming. For this question implement a C function contains that returns 1 (true) if a given C string appears as a substring of another C string starting at a given position.

More information

Sums. Here's a (poor) program to add three numbers

Sums. Here's a (poor) program to add three numbers Arrays 1 2 Sums 3 Here's a (poor) program to add three numbers /* Add three numbers */ #include add.c int main() { int numbers[3]; numbers[0] = 16; numbers[1] = 12; numbers[2] = 14; int sum =

More information

Memory Allocation in C

Memory Allocation in C Memory Allocation in C When a C program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment and heap segment. The text segment (also called

More information

Tutorial 10 Pointers in C. Shuyue Hu

Tutorial 10 Pointers in C. Shuyue Hu Tutorial 10 Pointers in C Shuyue Hu Content Basic concept of pointers Pointer arithmetic Array of pointers Pointer to pointer Passing pointers to functions in C Return pointer from functions in C 2 Content

More information

Function a block of statements grouped together, to perform some specific task

Function a block of statements grouped together, to perform some specific task Function a block of statements grouped together, to perform some specific task Each program written in C / C++ includes at least one function with pre-defined name: main( ). In our previous programs, we

More information

Computer Programming. The greatest gift you can give another is the purity of your attention. Richard Moss

Computer Programming. The greatest gift you can give another is the purity of your attention. Richard Moss Computer Programming The greatest gift you can give another is the purity of your attention. Richard Moss Outline Modular programming Modularity Header file Code file Debugging Hints Examples T.U. Cluj-Napoca

More information

Array Initialization

Array Initialization Array Initialization Array declarations can specify initializations for the elements of the array: int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ; initializes primes[0] to 2, primes[1] to 3, primes[2]

More information

Lecture 3. Variables. Variables

Lecture 3. Variables. Variables Lecture 3 Variables Variables Data processed by programs are input from keyboard by user, are read from the storage medium or are obtained by evaluating expressions. For this purpose it is necessary to

More information

CS113: Lecture 4. Topics: Functions. Function Activation Records

CS113: Lecture 4. Topics: Functions. Function Activation Records CS113: Lecture 4 Topics: Functions Function Activation Records 1 Why functions? Functions add no expressive power to the C language in a formal sense. Why have them? Breaking tasks into smaller ones make

More information

G52CPP C++ Programming Lecture 9

G52CPP C++ Programming Lecture 9 G52CPP C++ Programming Lecture 9 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last lecture const Constants, including pointers The C pre-processor And macros Compiling and linking And

More information

Course organization. Course introduction ( Week 1)

Course organization. Course introduction ( Week 1) Course organization Course introduction ( Week 1) Code editor: Emacs Part I: Introduction to C programming language (Week 2-9) Chapter 1: Overall Introduction (Week 1-3) Chapter 2: Types, operators and

More information

OBJECTIVE QUESTIONS: Choose the correct alternative:

OBJECTIVE QUESTIONS: Choose the correct alternative: OBJECTIVE QUESTIONS: Choose the correct alternative: 1. Function is data type a) Primary b) user defined c) derived d) none 2. The declaration of function is called a) function prototype b) function call

More information

A Fast Review of C Essentials Part I

A Fast Review of C Essentials Part I A Fast Review of C Essentials Part I Structural Programming by Z. Cihan TAYSI Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types

More information

Chapter-8 DATA TYPES. Introduction. Variable:

Chapter-8 DATA TYPES. Introduction. Variable: Chapter-8 DATA TYPES Introduction To understand any programming languages we need to first understand the elementary concepts which form the building block of that program. The basic building blocks include

More information

Flow Chart. The diagrammatic representation shows a solution to a given problem.

Flow Chart. The diagrammatic representation shows a solution to a given problem. low Charts low Chart A flowchart is a type of diagram that represents an algorithm or process, showing the steps as various symbols, and their order by connecting them with arrows. he diagrammatic representation

More information

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016 Oregon State University School of Electrical Engineering and Computer Science CS 261 Recitation 2 Spring 2016 Outline Programming in C o Headers o Structures o Preprocessor o Pointers Programming Assignment

More information

ARRAYS(II Unit Part II)

ARRAYS(II Unit Part II) ARRAYS(II Unit Part II) Array: An array is a collection of two or more adjacent cells of similar type. Each cell in an array is called as array element. Each array should be identified with a meaningful

More information

Topic 6: A Quick Intro To C

Topic 6: A Quick Intro To C Topic 6: A Quick Intro To C Assumption: All of you know Java. Much of C syntax is the same. Also: Many of you have used C or C++. Goal for this topic: you can write & run a simple C program basic functions

More information

Principles of C and Memory Management

Principles of C and Memory Management COMP281 Lecture 8 Principles of C and Memory Management Dr Lei Shi Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic, Functions Last Lecture Pointer Basics Previous Lectures Arrays, Arithmetic,

More information

A brief introduction to C programming for Java programmers

A brief introduction to C programming for Java programmers A brief introduction to C programming for Java programmers Sven Gestegård Robertz September 2017 There are many similarities between Java and C. The syntax in Java is basically

More information

Chapter 7: Preprocessing Directives

Chapter 7: Preprocessing Directives Chapter 7: Preprocessing Directives Outline We will only cover these topics in Chapter 7, the remain ones are optional. Introduction Symbolic Constants and Macros Source File Inclusion Conditional Compilation

More information

Midterm Exam 2 Solutions, C programming

Midterm Exam 2 Solutions, C programming Midterm Exam 2 Solutions, C programming April 26, 2010 Rules: Open book, open notes, open any printed or handwritten material. No electronic devices (except a music player). If you use a music player nobody

More information

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides From Java to C Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides 1 Outline Overview comparison of C and Java Good evening Preprocessor

More information

C - Func(on, Pointer, Array. Zhaoguo Wang

C - Func(on, Pointer, Array. Zhaoguo Wang C - Func(on, Pointer, Array Zhaoguo Wang A gigantic main loop https://github.com/qemu/qemu/blob/master/vl.c Function Readability Reusability Function int add(int a, int b) { int r = a + b; return r; name

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

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

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3). cs3157: another C lecture (mon-21-feb-2005) C pre-processor (1). today: C pre-processor command-line arguments more on data types and operators: booleans in C logical and bitwise operators type conversion

More information

Dynamic memory allocation

Dynamic memory allocation Dynamic memory allocation outline Memory allocation functions Array allocation Matrix allocation Examples Memory allocation functions (#include ) malloc() Allocates a specified number of bytes

More information

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

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso Why C? Test on 21 Android Devices with 32-bits and 64-bits processors and different versions

More information

C Functions. 5.2 Program Modules in C

C Functions. 5.2 Program Modules in C 1 5 C Functions 5.2 Program Modules in C 2 Functions Modules in C Programs combine user-defined functions with library functions - C standard library has a wide variety of functions Function calls Invoking

More information

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019 Hacking in C Pointers Radboud University, Nijmegen, The Netherlands Spring 2019 Allocation of multiple variables Consider the program main(){ char x; int i; short s; char y;... } What will the layout of

More information

Programing in C. Pierre-Alain FOUQUE

Programing in C. Pierre-Alain FOUQUE Programing in C Pierre-Alain FOUQUE Summary 1 - The C Language 2 - Layout of a program 3 - Data Typing 4 - Conditions 5 - Conditional loops while 2 Programming Language The processor controls everything,

More information

Functions in C C Programming and Software Tools

Functions in C C Programming and Software Tools Functions in C C Programming and Software Tools N.C. State Department of Computer Science Functions in C Functions are also called subroutines or procedures One part of a program calls (or invokes the

More information

CS 580 FINAL EXAM. Fall April 29, 2014

CS 580 FINAL EXAM. Fall April 29, 2014 CS 580 FINAL EXAM Fall 201 April 29, 2014 You are to build a range tree for your final exam. A range tree is a tree where each node contains a minimum and a maximum value as well as a linked list to store

More information

CS240: Programming in C

CS240: Programming in C CS240: Programming in C Lecture 6: Recursive Functions. C Pre-processor. Cristina Nita-Rotaru Lecture 6/ Fall 2013 1 Functions: extern and static Functions can be used before they are declared static for

More information

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5 C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5 1. Pointers As Kernighan and Ritchie state, a pointer is a variable that contains the address of a variable. They have been

More information

MODULE 5: Pointers, Preprocessor Directives and Data Structures

MODULE 5: Pointers, Preprocessor Directives and Data Structures MODULE 5: Pointers, Preprocessor Directives and Data Structures 1. What is pointer? Explain with an example program. Solution: Pointer is a variable which contains the address of another variable. Two

More information

Kurt Schmidt. October 30, 2018

Kurt Schmidt. October 30, 2018 to Structs Dept. of Computer Science, Drexel University October 30, 2018 Array Objectives to Structs Intended audience: Student who has working knowledge of Python To gain some experience with a statically-typed

More information

C programming basics T3-1 -

C programming basics T3-1 - C programming basics T3-1 - Outline 1. Introduction 2. Basic concepts 3. Functions 4. Data types 5. Control structures 6. Arrays and pointers 7. File management T3-2 - 3.1: Introduction T3-3 - Review of

More information

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down CS61C L3 C Pointers (1) inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #3 More C intro, C Strings, Arrays, & Malloc Albert Chae Instructor 2008-06-25 Number Review Sign and magnitude

More information