NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313.

Size: px
Start display at page:

Download "NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313."

Transcription

1 NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313

2 C Programming Functions Program Organization Separate Compilation

3 Functions vs. Methods Java classes include methods which can be called from any code with appropriate access (recall public methods) C functions are like Java methods, but they don t belong to any class. Functions are defined in a file and may be either global to your program or local to ( private in) the file in which they are defined*. Like Java methods, C functions Have a name Have a return type May have parameters Unlike Java methods, a function in C is uniquely identified by its name. Therefore, there is no concept of method overloading in C as there is in Java. There can be only one main( ) function in a C application. See the course coding standards for function naming conventions.

4 Arguments vs Parameters A parameter is defined in the function definition. It is a place holder for the argument during function execution void printint( int n ) // n is the parameter { printf( %20d\n, n); } An argument is a value passed to a function when the function is called int age = 42; printint( age ); // age is the argument These terms are often (incorrectly) used interchangeably, but the context is usually clear

5 Passing Arguments Primitive types (int, char, float, etc) and structs are passed to function by value A copy of the argument is passed to the function Changing the parameter within the function has no effect on the argument in the calling code Arrays are passed by reference A copy of the address of the array is passed to the function The parameter is an alias for the argument References to array elements in the function refer to array elements in the calling code Array parameters which are not intended to be changed by a function should be qualified as const

6 /* ages.c */ #include <stdio.h> void growolder( int a[ ], int size) { int k; for (k = 0; k < size; k++) ++a[ k ]; } Passing Arguments int avgage( const int array[ ], int size) { int k, sum = 0; for (k = 0; k < size; k++) sum += array[ k ]; return sum / size; } int main( ) { int nrstudents = 6; int ages[ 6 ] = {19, 18, 17, 22, 44, 55}; growolder( ages, nrstudents ); int avg = avgage( ages, nrstudents ); printf( The average age is %d\n, avg); return 0; }

7 /* sample.c */ #include <stdio.h> typedef double Radius; #define PI A Simple C Program /* given the radius, calculates the area of a circle */ double circlearea( Radius radius ) { return ( PI * radius * radius ); } // given the radius, calcs the circumference of a circle double calccircumference( Radius radius ) { return (2 * PI * radius ); } int main( ) { Radius radius = 4.5; double area = circlearea( radius ); double circumference = calccircumference( radius ); } printf ( Area = %10.2f, Circumference = %10.2f\n, area, circumference); return 0;

8 Reusable Functions The functions circlearea( ) and calccircumference( ) are general functions that may be used by multiple applications. To make them available to multiple applications without duplicating the code we must place them into a separate.c file. However, recall that the compiler requires that we must provide the function prototypes to the calling code. We do this by placing the prototypes and supporting declarations into a header (.h) file which is then included in.c files that wish to call the functions.

9 circleutils.h A header (.h) file is the public interface (function prototypes) for its corresponding.c file. It contains all definitions required by the prototypes so that a.c file that includes it compiles /* circleutils.h*/ // #includes required by the prototypes, if any /* supporting typedefs and #defines required by the prototypes */ typedef double Radius; /* function prototypes */ // given the radius, returns the area of a circle double circlearea( Radius radius ); // given the radius, calcs the circumference of a circle double calccircumference( Radius radius );

10 circleutils.c /* circleutils.c ** Utilites for circle calculations */ #include circleutils.h #define PI // why not in the.h file?? /* given the radius, calculates the area of a circle */ double circlearea( Radius radius ) { return ( PI * radius * radius ); } // given the radius, calcs the circumference of a circle double calccircumference( Radius radius ) { return (2 * PI * radius ); }

11 Sample Code Revisited /* sample.c */ #include <stdio.h> #include circleutils.h int main( ) { Radius radius = 4.5; double area = circlearea( radius ); double circumference = calccircumference( radius ); printf ( Area = %lf, Circumference = %lf\n, area, circumference); } return 0;

12 Header Files When a file contains functions to be reused in several programs, their prototypes and important #defines and typedefs are placed into a header (.h ) that is then included where needed. Each.h file should be stand alone. That is, it should declare any #define and typedef needed by the prototypes and #include any.h files it needs to avoid compiler errors. The.h file should contain everything needed to successfully compile any.c file that includes it. In this example, the prototypes for circlearea() and calccircumference( ) are placed into the file circleutils.h which would then be included in circleutils.c and any other.c file that uses circlearea( ) and / or calccircumference( ).

13 Guarding Header Files Because a.h file may include other.h files, there is the possibility that one or more.h files may unintentionally be included in a single.c file more than once, leading to compiler errors (multiple name definitions). To avoid these errors,.h files should be guarded using the compiler directives #ifndef (read as if not defined ) and #endif

14 #ifndef CIRCLEUTIL_H #define CIRCLEUTIL_H /* circleutils.h */ Guarded circleutils.h /* #include.h files as necessary */ /* supporting typedefs and #defines */ typedef double Radius; /* function prototypes */ // given the radius, returns the area of a circle double circlearea( Radius radius ); // given the radius, calcs the circumference of a circle double calccircumference( Radius radius ); #endif

15 1/20/10 Compiling and linking When a program s code is separated into multiple.c files, we must compile each.c file and then link the resulting.o files to create an executable program. The files may be compiled separately and then linked together. The -c flag in the first two command tells gcc to compile only which results in the creation of.o (object) files. In the 3 rd command, the presence of the.o extension tells gcc to link the files into an executable gcc -c -Wall circleutils.c gcc -c -Wall sample.c gcc -Wall -o sample sample.o circleutils.o Or if there only a few files, compiling and linking can be done all in one step gcc -Wall -o sample sample.c circleutils.c

16 Compiler vs linker The compiler translates one.c file into a.o file Verifies that all functions are being called correctly Verifies that all variables exist Verifies language syntax The linker combines multiple.o files (and C libraries) to create an executable Finds functions called by one.c/.o file, but defined in another E.g. printf( ), scanf( ) Finds global variables used by one.c/.o file, but defined in another (more on this soon) Both can be invoked with gcc (see previous lecture)

17 Linking with C libraries By default, the standard C library which includes printf, scanf and char and string functions is always linked with your program. Other libraries such as the math library must be explicitly linked with your code. The names of the C libraries alll have the form libxxx.a. The standard C library is libc.a. The C math library is libm.a. Specify libraries to be linked using the -l flag and the xxx part of the library name. E.g. to link the math library with your program, use gcc -Wall -o sample sample.c circleutils.c -lm

18 Project Organization main( ) is generally defined in its own.c file and generally just calls helper functions E.g. project1.c Project-specific helper functions may be in the same.c file as main( ) main( ) comes first Helper function order that makes sense to you Reusable functions in their own.c file Group related functions in the same file E.g. circleutils.c Prototypes, typedefs, #defines, etc. for reusable function in a.h file Same file root name as the.c file. E.g. circleutils.h

19 Variable Scope and Lifetime The scope of a variable refers to that part of a program that may refer to the variable. The lifetime of a variable refers to the time in which a variable occupies a place in memory The scope and lifetime of a variable are determined by how and where the variable is defined

20 static and extern The keyword static is used to Limit the scope of a function or global variable Extend the lifetime of a variable The keyword extern is used to Inform the compiler that a (global) variable is defined in a different.c file

21 Function Scope All functions not found in the.c file that calls them are considered external because standard C does not allow nesting of function definitions. So no extern declaration is needed All functions may be called from any.c file in your project unless they are also declared as static. The linker will find external functions called from one.c file but defined in a different.c file static functions may only be called from within the.c file in which they are defined Their scope is limited

22 Local variables Local variables are defined within the opening and closing braces of a function, loop, if-statement, etc. (a code block ). Function parameters are considered local variables in their function. Are usable only within the block in which they are defined Exist only during the execution of the block unless also defined as static Initialized variables are reinitialized each time the block is executed (if not defined as static) static local variables retain their values for the duration of your program. When used in functions, they retain their values between calls to the function.

23 Global Variables Global (external) variables are defined outside of any function, typically near the top of a.c file. May be used anywhere in the.c file in which they are defined. Exist for the duration of your program May be used by any other.c file in your application that declares them as extern unless also defined as static (see below) Static global variables may only be used in the.c file that declares them extern declarations for global variables should be placed into a header file

24 randomint.c /* a global variable to be used by code in other.c files. ** This variable exists until the program ends ** Other.c files must declare this variable as "extern */ long randomint; /* a function that can be called from any other function ** sets randomint to a value from 1 to max, inclusive */ void getrandomint( int max ) { randomint = getnext( ); } /* a function that can only be called from functions within this file */ static long getnext( ) { /* lastrandom is a local variable that can only be used inside this function, but persists between calls to this function */ static long lastrandom = ; } lastrandom = (lastrandom * 125) % ; return (lastrandom % max) + 1;

25 randomint.h #ifndef RANDOMINT_H #define RANDOMINT_H // global variable in randomint.c extern int randomint; // prototypes for non-static ( public ) // functions in randomint.c void getrandomint( int max ); #endif

26 variablescope.c - part 1 #include <stdio.h> // extern definition of randomint and prototype for getrandomint #include randomint.h /* a global variable that can only be used by functions in this.c file */ static int inputvalue; /* a function that can only be called by other functions in this.c file */ static void inputpositiveint( char prompt[ ] ) { /* init to invalid value to enter while loop */ inputvalue = -1; while (inputvalue <= 0) { printf( "%s", prompt); scanf( "%d", &inputvalue); } }

27 variablescope.c - part 2 /* main is the entry point for all programs */ int main( ) { /* local/automatic variables that can only be used in this function and that are destroyed when the function ends */ int i, maxvalue, nrvalues; inputpositiveint("input max random value to generate: "); maxvalue = inputvalue; inputpositiveint("input number of random ints to generate: "); nrvalues = inputvalue; } for (i = 0; i < nrvalues; i++) { getrandomint( maxvalue ); printf( %d: %d\n", i + 1, randomint ); } return 0;

28 The make Utility Typing out the gcc commands for a project gets less appealing as the project gets bigger. The "make" utility automates the process of compiling and linking. With make, the programmer specifies what the files are in the project and how they fit together. make takes care of the appropriate compile and link steps. make can speed up your compiles since it is smart enough to know that if you have 10.c files but you have only changed one, then only that one file needs to be compiled before the link step. make has some complex features, but using it for simple things is pretty easy. This slide and those that follow are adapted from Section 2 of UnixProgrammingTools.pdf by Nick Parlante, et al at Stanford. Used with permission.

29 How make works You tell make what files are in your project, how they fit together, how to compile, and how to link them by creating a file that make reads and interprets By default, make looks for a file named either makefile or Makefile At the Unix console, simply type the command make to compile all necessary files and create a new executable unix> make make can perform other tasks defined in the makefile as well (more on this later)

30 The makefile A makefile consists of variable definitions, dependency/build rules and comments Comments begin with the # character and extend to the end of the line # makefile for project 1 # name, date, etc

31 The makefile (2) Variable definitions A variable name is defined to represent a string of text, similar to #define in C. They are most often used to represent names of files, directories, and the compiler. Variable are defined simply by setting them to some value (string) The most common variables are typically CC -- the name of your C compiler CFLAGS -- the set of flags that you wish to pass to the compiler LDFLAGS -- the set of flags that you wish to pass to the linker OBJS -- the set of object (.o) files that are linked together to create your executable To use a variable, use the dollar sign ($) followed by the name of the variable in parenthesis or curly braces CC = /usr/local/bin/gcc CFLAGS = -g -Wall $(CC) $(CFLAGS) -c main.c Variables that are not initialized are set to the empty string

32 The make file (3) A dependency/build rule defines how to make a target based on changes to the files on which the target depends. The order of the rules is irrelevant, except that the first rule is the default rule -- the rule that will be used when make is executed with no arguments A dependency/build rule is made up of two parts A dependency line followed by one or more command lines

33 The make file (4) Example dependency/build rule sample.o : sample.c circleutils.h <TAB> $(CC) $(CFLAGS) -c sample.c The first (dependency) line of this rule says that sample.o must be rebuilt whenever sample.c or circleutils.h changes. Generally a.o file depends on its own.c file and any non-library.h files it #includes. In this example, we would expect that sample.c #includes circleutils.h The second (command) line tells make how to rebuild sample.o Gotcha -- the command line MUST be indented with a TAB character. Spaces won t do. This can be a problem when cutting/pasting the contents of a makefile from a terminal screen. Some editors will automatically change a TAB into spaces.

34 Sample makefile See /afs/umbc.edu/users/c/m/cmsc313/pub/code/ makefile

35 make features The make utility has some built-in default rules. In particular, the default rule for C files is $(CC) $(CFLAGS) -c source-file.c Special syntax can be used to create the list of the.o files OBJS = $(SRCS:.c=.o)

36 Other Function Topics Automatic type promotion Passing 2-d Arrays Recursion In-line functions

37 Auto Type Conversion As we ve seen, the compiler can and will automatically convert a small data type to a larger data type without problems. char initial = B ; short age = 42; int intage = 42, intinitial = initial; long longage = age; The same type conversion occurs for function arguments. short myage = 42, yourage = 33; long max( long a, long b); long older = max ( myage, yourage);

38 Passing 2-D Arrays Passing a 2-d array to a function is similar to passing a 1-d array Basic function prototype void printchessboard( char [ 8 ][ 8 ] theboard); Calling the function char chessboard[ 8 ] [ 8 ]; printchessboard( chessboard ); As we will see, the compiler needs to know the size of each row, but not the number of rows. This allows an alternative prototype void printchessboard( char[ ] [ 8 ] theboard );

39 Recursion C functions may be called recursively. Typically a function calls itself A properly written recursive function has the following properties A base case - a condition which does NOT make a recursive call because a simple solution exists A recursive call with a condition (usually a parameter value) that is closer to the base case than the condition (parameter value) of the current function call Each invocation of the function gets its own set of arguments and local variables We ll see how recursion is implemented later

40 Recursion Example /* print an integer in decimal ** K & R page 87 (may fail on largest negative int) */ #include <stdio.h> void printd( int n ) { if ( n < 0 ) { printf( - ); n = -n; } if ( n / 10 ) /* (n / 10!= 0) -- more than 1 digit */ printd( n / 10 ); /* recursive call: n has 1 less digit */ } printf( %c, n % ); /* base case digit */

41 Recursive Exercise Complete the following recursive function that sums all of the integers from 1 to N int sumton( int N ) { if ( ) return N; // base case } else // recursive call return ;

42 Inline Functions C99 only Short functions may be defined as inline. This is a suggestion to the compiler that calls to the function should be replaced by the body of the function. inline functions provide code structure and readability and (may) increase performance inline bool iseven( int n ) { return n % 2 == 0; } inline max( int a, int b ) { return a > b? a : b; }

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012 CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 12, FALL 2012 TOPICS TODAY Assembling & Linking Assembly Language Separate Compilation in C Scope and Lifetime LINKING IN ASSEMBLY

More information

Introduction to the C Programming Language

Introduction to the C Programming Language Introduction to the C Programming Language Alexander Nelson August 24, 2018 University of Arkansas - Department of Computer Science and Computer Engineering History of C Began at Bell labs between 1969

More information

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University AN OVERVIEW OF C, PART 3 CSE 130: Introduction to Programming in C Stony Brook University FANCIER OUTPUT FORMATTING Recall that you can insert a text field width value into a printf() format specifier:

More information

Workspace for '3-ctips' Page 1 (row 1, column 1)

Workspace for '3-ctips' Page 1 (row 1, column 1) Workspace for '3-ctips' Page 1 (row 1, column 1) ECEN 449 Microprocessor System Design Some tips to good programming and Makefiles 1 Objectives of this Lecture Unit Some tips to good programming 2 Variable

More information

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program Syntax What the Compiler needs to understand your program 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line Possibly replacing it with other

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 03 - Stephen Scott (Adapted from Christopher M. Bourke) 1 / 41 Fall 2009 Chapter 3 3.1 Building Programs from Existing Information

More information

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions Computer Science & Engineering 150A Problem Solving Using Computers Lecture 03 - Functions Stephen Scott (Adapted from Christopher M. Bourke) Fall 2009 1 / 1 cbourke@cse.unl.edu Chapter 3 3.1 Building

More information

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

JTSK Programming in C II C-Lab II. Lecture 3 & 4 JTSK-320112 Programming in C II C-Lab II Lecture 3 & 4 Xu (Owen) He Spring 2018 Slides modified from Dr. Kinga Lipskoch Planned Syllabus The C Preprocessor Bit Operations Pointers and Arrays (Dynamically

More information

Programming in C. Pointers and Arrays

Programming in C. Pointers and Arrays Programming in C Pointers and Arrays NEXT SET OF SLIDES FROM DENNIS FREY S FALL 2011 CMSC313 http://www.csee.umbc.edu/courses/undergraduate/313/fall11/" Pointers and Arrays In C, there is a strong relationship

More information

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information. Chapter 3 Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus Lecture 03 - Introduction To Functions Christopher M. Bourke cbourke@cse.unl.edu 3.1 Building Programs from Existing

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3 Programming in C main Level 2 Level 2 Level 2 Level 3 Level 3 1 Programmer-Defined Functions Modularize with building blocks of programs Divide and Conquer Construct a program from smaller pieces or components

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

A Fast Review of C Essentials Part II

A Fast Review of C Essentials Part II A Fast Review of C Essentials Part II Structural Programming by Z. Cihan TAYSI Outline Macro processing Macro substitution Removing a macro definition Macros vs. functions Built-in macros Conditional compilation

More information

CS Programming In C

CS Programming In C CS 24000 - Programming In C Week Two: Basic C Program Organization and Data Types Zhiyuan Li Department of Computer Science Purdue University, USA 2 int main() { } return 0; The Simplest C Program C programs

More information

make and makefile CS 211

make and makefile CS 211 make and makefile CS 211 Steps of a C Program Compilation Step 1 Run the Preprocessor Deals with #include, #define, #ifndef, Step 2 Compile C code into Machine Code Step 2a Lexical Analysis Step 2b Parsing

More information

independent compilation and Make

independent compilation and Make independent compilation and Make Geoffrey Brown David S. Wise Chris Haynes Bryce Himebaugh Computer Structures Fall 2013 Independent Compilation As a matter of style, source code files should rarely be

More information

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in: CS 215 Fundamentals of Programming II C++ Programming Style Guideline Most of a programmer's efforts are aimed at the development of correct and efficient programs. But the readability of programs is also

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

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

CMPSC 311- Introduction to Systems Programming Module: Build Processing

CMPSC 311- Introduction to Systems Programming Module: Build Processing CMPSC 311- Introduction to Systems Programming Module: Build Processing Professor Patrick McDaniel Fall 2014 UNIX Pipes Pipes are ways of redirecting the output of one command to the input of another Make

More information

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CHAPTER 4 FUNCTIONS. 4.1 Introduction CHAPTER 4 FUNCTIONS 4.1 Introduction Functions are the building blocks of C++ programs. Functions are also the executable segments in a program. The starting point for the execution of a program is main

More information

Introduction: The Unix shell and C programming

Introduction: The Unix shell and C programming Introduction: The Unix shell and C programming 1DT048: Programming for Beginners Uppsala University June 11, 2014 You ll be working with the assignments in the Unix labs. If you are new to Unix or working

More information

Outline. COMP 2718: Software Development Tools: gcc and make. C and Java 3/28/16

Outline. COMP 2718: Software Development Tools: gcc and make. C and Java 3/28/16 Outline COMP 2718: Software Development Tools: gcc and make Slides adapted by Andrew Vardy ( Memorial University) Originally created by Marty Stepp, Jessica Miller, and Ruth Anderson (University of Washington)

More information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

More information

Lecture 3: C Programm

Lecture 3: C Programm 0 3 E CS 1 Lecture 3: C Programm ing Reading Quiz Note the intimidating red border! 2 A variable is: A. an area in memory that is reserved at run time to hold a value of particular type B. an area in memory

More information

Programming and Data Structure

Programming and Data Structure Programming and Data Structure Sujoy Ghose Sudeshna Sarkar Jayanta Mukhopadhyay Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur Spring Semester 2012 Programming and Data

More information

C: How to Program. Week /Mar/05

C: How to Program. Week /Mar/05 1 C: How to Program Week 2 2007/Mar/05 Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers

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

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

CMPSC 311- Introduction to Systems Programming Module: Build Processing

CMPSC 311- Introduction to Systems Programming Module: Build Processing CMPSC 311- Introduction to Systems Programming Module: Build Processing Professor Patrick McDaniel Fall 2016 UNIX Pipes Pipes are ways of redirecting the output of one command to the input of another Make

More information

This course has three parts. Elements of C programming. Arithmatic and Error analysis. Some algorithms and their implementation

This course has three parts. Elements of C programming. Arithmatic and Error analysis. Some algorithms and their implementation This course has three parts Elements of C programming. Arithmatic and Error analysis. Some algorithms and their implementation 2 There are three steps involved in converting your idea of what is to be

More information

CC112 Structured Programming

CC112 Structured Programming Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer Engineering Department CC112 Structured Programming Lecture 3 1 LECTURE 3 Input / output operations

More information

CIS 190: C/C++ Programming. Lecture 2 Pointers and More

CIS 190: C/C++ Programming. Lecture 2 Pointers and More CIS 190: C/C++ Programming Lecture 2 Pointers and More Separate Compilation to prevent the file containing main() from getting too crowded and long function prototypes in their own file (functions.h) function

More information

Motivation was to facilitate development of systems software, especially OS development.

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

Chapter 2 - Introduction to C Programming

Chapter 2 - Introduction to C Programming Chapter 2 - Introduction to C Programming 2 Outline 2.1 Introduction 2.2 A Simple C Program: Printing a Line of Text 2.3 Another Simple C Program: Adding Two Integers 2.4 Memory Concepts 2.5 Arithmetic

More information

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

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 5 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2016 ENCM 339 Fall 2016 Slide Set 5 slide 2/32

More information

CMSC 313 Fall2009 Midterm Exam 1 Section 01 October 12, 2009

CMSC 313 Fall2009 Midterm Exam 1 Section 01 October 12, 2009 CMSC 313 Fall2009 Midterm Exam 1 Section 01 October 12, 2009 Name Score UMBC Username Notes: a. Please write clearly. Unreadable answers receive no credit. b. For short answer questions your answer should

More information

Lecture 7. Log into Linux New documents posted to course webpage

Lecture 7. Log into Linux New documents posted to course webpage Lecture 7 Log into Linux New documents posted to course webpage Coding style guideline; part of project grade is following this Homework 4, due on Monday; this is a written assignment Project 1, due next

More information

Software Development With Emacs: The Edit-Compile-Debug Cycle

Software Development With Emacs: The Edit-Compile-Debug Cycle Software Development With Emacs: The Edit-Compile-Debug Cycle Luis Fernandes Department of Electrical and Computer Engineering Ryerson Polytechnic University August 8, 2017 The Emacs editor permits the

More information

Lecture 10: building large projects, beginning C++, C++ and structs

Lecture 10: building large projects, beginning C++, C++ and structs CIS 330: / / / / (_) / / / / _/_/ / / / / / \/ / /_/ / `/ \/ / / / _/_// / / / / /_ / /_/ / / / / /> < / /_/ / / / / /_/ / / / /_/ / / / / / \ /_/ /_/_/_/ _ \,_/_/ /_/\,_/ \ /_/ \ //_/ /_/ Lecture 10:

More information

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park Variables in C CMSC 104, Fall 2012 John Y. Park 1 Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement 2 What Are Variables in C? Variables in C have the

More information

Lecture 04 FUNCTIONS AND ARRAYS

Lecture 04 FUNCTIONS AND ARRAYS Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions

More information

Have examined process Creating program Have developed program Written in C Source code

Have examined process Creating program Have developed program Written in C Source code Preprocessing, Compiling, Assembling, and Linking Introduction In this lesson will examine Architecture of C program Introduce C preprocessor and preprocessor directives How to use preprocessor s directives

More information

Fundamentals of Programming Session 12

Fundamentals of Programming Session 12 Fundamentals of Programming Session 12 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel s slides Sharif University of Technology Outlines

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

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved. Functions In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive. You can either use the built-in library functions or you can create

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

Introduction to Programming (Java) 4/12

Introduction to Programming (Java) 4/12 Introduction to Programming (Java) 4/12 Michal Krátký Department of Computer Science Technical University of Ostrava Introduction to Programming (Java) 2008/2009 c 2006 2008 Michal Krátký Introduction

More information

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING 1. Object Oriented Programming Paradigms 2. Comparison of Programming Paradigms 3. Basic Object Oriented Programming

More information

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012 CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, FALL 2012 TOPICS TODAY Project 5 Pointer Basics Pointers and Arrays Pointers and Strings POINTER BASICS Java Reference In Java,

More information

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

More information

CSCI 2132 Final Exam Solutions

CSCI 2132 Final Exam Solutions Faculty of Computer Science 1 CSCI 2132 Final Exam Solutions Term: Fall 2018 (Sep4-Dec4) 1. (12 points) True-false questions. 2 points each. No justification necessary, but it may be helpful if the question

More information

CSE 333 Lecture 7 - final C details

CSE 333 Lecture 7 - final C details CSE 333 Lecture 7 - final C details Steve Gribble Department of Computer Science & Engineering University of Washington Today s topics: - a few final C details header guards and other preprocessor tricks

More information

CSE 230 Intermediate Programming in C and C++ Functions

CSE 230 Intermediate Programming in C and C++ Functions CSE 230 Intermediate Programming in C and C++ Functions Fall 2017 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse230/ Concept of Functions

More information

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor CS 261 Fall 2017 Mike Lam, Professor C Introduction Variables, Memory Model, Pointers, and Debugging The C Language Systems language originally developed for Unix Imperative, compiled language with static

More information

Motivation was to facilitate development of systems software, especially OS development.

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

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

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language 1 History C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC

More information

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers Functions 1 Function n A program segment that carries out some specific, well-defined task n Example A function to add two numbers A function to find the largest of n numbers n A function will carry out

More information

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions CSE 2421: Systems I Low-Level Programming and Computer Organization Functions Read/Study: Reek Chapters 7 Gojko Babić 01-22-2018 Predefined Functions C comes with libraries of predefined functions E.g.:

More information

Syntax and Variables

Syntax and Variables Syntax and Variables What the Compiler needs to understand your program, and managing data 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace

C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace C++ Style Guide 1.0 General The purpose of the style guide is not to restrict your programming, but rather to establish a consistent format for your programs. This will help you debug and maintain your

More information

COMP322 - Introduction to C++

COMP322 - Introduction to C++ COMP322 - Introduction to C++ Winter 2011 Lecture 2 - Language Basics Milena Scaccia School of Computer Science McGill University January 11, 2011 Course Web Tools Announcements, Lecture Notes, Assignments

More information

IT 374 C# and Applications/ IT695 C# Data Structures

IT 374 C# and Applications/ IT695 C# Data Structures IT 374 C# and Applications/ IT695 C# Data Structures Module 2.1: Introduction to C# App Programming Xianrong (Shawn) Zheng Spring 2017 1 Outline Introduction Creating a Simple App String Interpolation

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

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

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

More information

Lecture 2: C Programming Basic

Lecture 2: C Programming Basic ECE342 Introduction to Embedded Systems Lecture 2: C Programming Basic Ying Tang Electrical and Computer Engineering Rowan University 1 Facts about C C was developed in 1972 in order to write the UNIX

More information

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14

COSC 2P91. Introduction Part Deux. Week 1b. Brock University. Brock University (Week 1b) Introduction Part Deux 1 / 14 COSC 2P91 Introduction Part Deux Week 1b Brock University Brock University (Week 1b) Introduction Part Deux 1 / 14 Source Files Like most other compiled languages, we ll be dealing with a few different

More information

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes

COMP26120: Algorithms and Imperative Programming. Lecture 5: Program structuring, Java vs. C, and common mistakes COMP26120: Algorithms and Imperative Programming Lecture 5: Program structuring, Java vs. C, and common mistakes Lecture outline Program structuring Functions (defining a functions, passing arguments and

More information

Lecture 04 FUNCTIONS AND ARRAYS

Lecture 04 FUNCTIONS AND ARRAYS Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Introduction to C Programming Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan Outline Printing texts Adding 2 integers Comparing 2 integers C.E.,

More information

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26 COSC 2P95 Procedural Abstraction Week 3 Brock University Brock University (Week 3) Procedural Abstraction 1 / 26 Procedural Abstraction We ve already discussed how to arrange complex sets of actions (e.g.

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

Function Call Stack and Activation Records

Function Call Stack and Activation Records 71 Function Call Stack and Activation Records To understand how C performs function calls, we first need to consider a data structure (i.e., collection of related data items) known as a stack. Students

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

EL6483: Brief Overview of C Programming Language

EL6483: Brief Overview of C Programming Language EL6483: Brief Overview of C Programming Language EL6483 Spring 2016 EL6483 EL6483: Brief Overview of C Programming Language Spring 2016 1 / 30 Preprocessor macros, Syntax for comments Macro definitions

More information

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces Basic memory model Using functions Writing functions Basics Prototypes Parameters Return types Functions and memory Names and namespaces When a program runs it requires main memory (RAM) space for Program

More information

2. Numbers In, Numbers Out

2. Numbers In, Numbers Out COMP1917: Computing 1 2. Numbers In, Numbers Out Reading: Moffat, Chapter 2. COMP1917 15s2 2. Numbers In, Numbers Out 1 The Art of Programming Think about the problem Write down a proposed solution Break

More information

CS11 Advanced C++ Fall Lecture 4

CS11 Advanced C++ Fall Lecture 4 CS11 Advanced C++ Fall 2006-2007 Lecture 4 Today s Topics Using make to automate build tasks Using doxygen to generate API docs Build-Automation Standard development cycle: Write more code Compile Test

More information

CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools CSE 374 Programming Concepts & Tools Hal Perkins Fall 2017 Lecture 14 Makefiles and Compilation Management 1 Where we are Onto tools... Basics of make, particular the concepts Some fancier make features

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

Functions and Recursion

Functions and Recursion Functions and Recursion CSE 130: Introduction to Programming in C Stony Brook University Software Reuse Laziness is a virtue among programmers Often, a given task must be performed multiple times Instead

More information

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files The Make Utility Independent compilation Large programs are difficult to maintain Problem solved by breaking the program into separate files Different functions placed in different files The main function

More information

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files

The Make Utility. Independent compilation. Large programs are difficult to maintain. Problem solved by breaking the program into separate files The Make Utility Independent compilation Large programs are difficult to maintain Problem solved by breaking the program into separate files Different functions placed in different files The main function

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

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah Lecturer Department of Computer Science & IT University of Balochistan 1 Outline p Introduction p Program development p C language and beginning with

More information

UEE1302 (1102) F10: Introduction to Computers and Programming

UEE1302 (1102) F10: Introduction to Computers and Programming Computational Intelligence on Automation Lab @ NCTU Learning Objectives UEE1302 (1102) F10: Introduction to Computers and Programming Programming Lecture 00 Programming by Example Introduction to C++ Origins,

More information

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

COMP322 - Introduction to C++ Lecture 02 - Basics of C++ COMP322 - Introduction to C++ Lecture 02 - Basics of C++ School of Computer Science 16 January 2012 C++ basics - Arithmetic operators Where possible, C++ will automatically convert among the basic types.

More information

Exercise Session 2 Systems Programming and Computer Architecture

Exercise Session 2 Systems Programming and Computer Architecture Systems Group Department of Computer Science ETH Zürich Exercise Session 2 Systems Programming and Computer Architecture Herbstsemester 216 Agenda Linux vs. Windows Working with SVN Exercise 1: bitcount()

More information

Programming and Data Structures

Programming and Data Structures Programming and Data Structures Teacher: Sudeshna Sarkar sudeshna@cse.iitkgp.ernet.in Department of Computer Science and Engineering Indian Institute of Technology Kharagpur #include int main()

More information

C Introduction. Comparison w/ Java, Memory Model, and Pointers

C Introduction. Comparison w/ Java, Memory Model, and Pointers CS 261 Fall 2018 Mike Lam, Professor C Introduction Comparison w/ Java, Memory Model, and Pointers Please go to socrative.com on your phone or laptop, choose student login and join room LAMJMU The C Language

More information

FUNCTIONS POINTERS. Pointers. Functions

FUNCTIONS POINTERS. Pointers. Functions Functions Pointers FUNCTIONS C allows a block of code to be separated from the rest of the program and named. These blocks of code or modules are called functions. Functions can be passed information thru

More information

APCS Semester #1 Final Exam Practice Problems

APCS Semester #1 Final Exam Practice Problems Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester

More information

CS201 Latest Solved MCQs

CS201 Latest Solved MCQs Quiz Start Time: 09:34 PM Time Left 82 sec(s) Question # 1 of 10 ( Start time: 09:34:54 PM ) Total Marks: 1 While developing a program; should we think about the user interface? //handouts main reusability

More information

C / C++ Coding Rules

C / C++ Coding Rules C / C++ Coding Rules Luca Abeni luca.abeni@unitn.it March 3, 2008 Abstract This short document collects some simple and stupid coding rules for writing understandable C or C++ code, and has been written

More information

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009 Compiler Theory (GCC the GNU Compiler Collection) Sandro Spina 2009 GCC Probably the most used compiler. Not only a native compiler but it can also cross-compile any program, producing executables for

More information