C Programming Tips. Andrew Cortese

Similar documents
Illustration 1: Diagram of program function and data flow

INTRODUCTION TO OBJECTIVE-C CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 12 09/29/2011

Hypercosm. Studio.

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Stacks. Linear data structures

Getting Started with the Internet Communications Engine

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

How to Write a Simple Makefile

Module 816. File Management in C. M. Campbell 1993 Deakin University

Using Karel with Eclipse

iphone Objective-C Exercises

Installing C++ compiler for CSc212 Data Structures

Object Oriented Software Design II

Building Qualtrics Surveys for EFS & ALC Course Evaluations: Step by Step Instructions

Serialization in Java (Binary and XML)

Version Control with Subversion and Xcode

Introduction to Eclipse

Basics of I/O Streams and File I/O

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Brent A. Perdue. July 15, 2009

MA-WA1920: Enterprise iphone and ipad Programming

How to create PDF maps, pdf layer maps and pdf maps with attributes using ArcGIS. Lynne W Fielding, GISP Town of Westwood

Building Software Systems. Multi-module C Programs. Example Multi-module C Program. Example Multi-module C Program

Session 7 Fractions and Decimals

How to test and debug an ASP.NET application

The Little Go Book is licensed under the Attribution-NonCommercial-ShareAlike 4.0 International license. You should not have paid for this book.

Chapter 7: Additional Topics

Facebook Twitter YouTube Google Plus Website

Lecture 5: Java Fundamentals III

C++ INTERVIEW QUESTIONS

The C Programming Language course syllabus associate level

1 Abstract Data Types Information Hiding

Moving from CS 61A Scheme to CS 61B Java

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Programming Languages CIS 443

Creating tables of contents and figures in Word 2013

Java from a C perspective. Plan

1 Description of The Simpletron

Introduction to Data Structures

3 - Lift with Monitors

csce4313 Programming Languages Scanner (pass/fail)

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Introduction to Objective-C. Kevin Cathey

So you want to create an a Friend action

Microsoft Migrating to PowerPoint 2010 from PowerPoint 2003

Java Interview Questions and Answers

Running your first Linux Program

Section 4.1 Rules of Exponents

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

CPSC 226 Lab Nine Fall 2015

Tutorial on C Language Programming

Avatar: Appearance Changing Your Shape Introduction Instruction Practice LEVEL: 1 MODULE: AVATAR: APPEARANCE MISSION 2

Sources: On the Web: Slides will be available on:

Jonathan Worthington Scarborough Linux User Group

Lab 4: Socket Programming: netcat part

Microsoft Migrating to Word 2010 from Word 2003

Using Form Scripts in WEBPLUS

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Section 1.5 Exponents, Square Roots, and the Order of Operations

Glossary of Object Oriented Terms

MAIL MERGE MADE EASY A STEP-BY-STEP GUIDE FOR LABELS OR MERGES

ACCESS Importing and Exporting Data Files. Information Technology. MS Access 2007 Users Guide. IT Training & Development (818)

Podcasting with Audacity

PLEASE NOTE: The client data used in these manuals is purely fictional.

Waspmote IDE. User Guide

Migrating to Excel 2010 from Excel Excel - Microsoft Office 1 of 1

Beginner s Guide to MailChimp

Improve C++ Code Quality with Static Security Analysis (SSA)

13 Classes & Objects with Constructors/Destructors

CISC 181 Project 3 Designing Classes for Bank Accounts

A quick giude to... Affiliate program

Java Application Developer Certificate Program Competencies

[PRAKTISCHE ASPEKTE DER INFORMATIK WS 13/14]

Excel & Visual Basic for Applications (VBA)

DSD For Windows Basic Setup Instructions

As previously noted, a byte can contain a numeric value in the range Computers don't understand Latin, Cyrillic, Hindi, Arabic character sets!

The Windows Shortcut File Format as reverse-engineered by Jesse Hager Document Version 1.0

Member Functions of the istream Class

CAs and Turing Machines. The Basis for Universal Computation

Data Migration from Magento 1 to Magento 2 Including ParadoxLabs Authorize.Net CIM Plugin Last Updated Jan 4, 2016

An Introduction to Modern Software Development Tools Creating A Simple GUI-Based Tool Appleʼs XCode Version 3.2.6

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Motion Control Products Application note Exporting AC500 tags for use with Panel Builder

SAML single sign-on configuration overview

CSE 308. Coding Conventions. Reference

A Sample OFBiz application implementing remote access via RMI and SOAP Table of contents

Alteryx Predictive Analytics for Oracle R

An Overview of Java. overview-1

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Chapter 1 Fundamentals of Java Programming

CS193D Handout 06 Winter 2004 January 26, 2004 Copy Constructor and operator=

Figure 1: Graphical example of a mergesort 1.

MEAP Edition Manning Early Access Program Hello! ios Development version 14

A Step-by-Step Patient Guide to Upload Medical Images to the Cleveland Clinic Neurological Institute

Specifications of Paradox for Windows

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Client-server Sockets

CRM CUSTOMER RELATIONSHIP MANAGEMENT

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Transcription:

C Programming Tips Andrew Cortese

Overview: 1. Error Handling 2. Wrapper/Helper Functions 3. Return Values and Conditionals 4. Structures as classes 5. Makefiles (With step-by-step example)

Tip #1 - Error Checking Many programmers neglect error checking during I/O and memory function calls. This is just asking for segmentation faults and other strange program behavior. You should perform checks of return values, pointers, lengths, etc whenever possible.

Example: Malloc Code without error checking:

Example: Malloc Improved code:

An Even Better Solution: Wrapper Create a wrapper function that calls malloc for you, so you can you can call that function rather than repeating yourself with the extra code. The wrapper includes the error check and the exit code so you don t have to type it each time.

Malloc Wrapper You can now call this in place of malloc and won t need to do the error check each time.

Other Error Checks: Input When you call fprintf, fread, gets, and other data input functions, make sure you check the return value. A return value of 0 means no bytes were read, and a return value less than 0 means there was an error.

Other Error Checks: Null Check Before you dereference (use) a pointer, make sure it isn t null. Example: This will prevent a segfault and instead stop the program and print a useful message.

Other Error Checks: File Access When you call fopen or fclose, check the return value to be sure it was successful:

Tip #2: Wrappers / Helper Functions We already looked at one wrapper function in action with malloc. Try to apply that concept as often as possible. Any time you find yourself repeating code or doing lots of copy/pasting, ask yourself if you could encapsulate the behavior in a function.

Helper Example: Expand An Array Say you were using an array of integers and found yourself adding values beyond the original length. Each time you added another, you d have to create a new array with length 1+oldLength, copy everything over, and then add the new value in the new position.

Helper Example: Expand An Array Instead of copy/pasting this code a dozen times, writing a helper function could help. By encapsulating the code in a function, we don t have to repeat ourselves. Debugging also becomes easier, because we only need to look for errors in one spot.

Expanding An Array:

Tip #3: Beware of Return Values Any time you re writing an if statement, a while statement, or other conditional check, I recommend checking for explicit values. This includes comparisons, equality checks, and return-value investigations. This also means being aware of expected return values.

Example: String Compare Which of these will execute somefunction when str1 and str2 are the same?

Example: String Compare Which of these will execute somefunction when str1 and str2 are the same? Answer: Example B

Example: Were Any Bytes Read? Which of these will execute somefunction() when bytes were successfully read?

Example: Were Any Bytes Read? Which of these will execute somefunction() when bytes were successfully read? Answer: Example A

Explanation The reason this is important is that sometimes zero indicates a positive statement, as in the strings are the same. And other times, it indicates a negative statement, like no bytes were read. However, conditional statements (if, while) always evaluate true if they see a non-zero value.

Tip #4: Treat Structures Like Classes Structures in C can be treated very similarly to the way you deal with classes in Java. It takes some extra work, and lacks certain features like inheritance, polymorphism and built-in constructors, but object-oriented programming can be somewhat achieved.

Source Files For Structures I usually create two files per structure when doing structure-oriented programming. 1. A header file with the structure definition, the typedef, and method prototypes. 2. A C file for the method (function) definitions

Example: Employee Let s sketch a simple Employee class using structs. An employee has: 1. A name (string) 2. An id (int) 3. A constructor function for allocation 4. Getter/setter functions for 1 and 2 5. Utility functions (isequal(), print(), etc)

Employee Structure: Pretty straightforward so far. Notice the two typedefs: one for a value and one for a pointer. This would all go in employee.h

Prototypes Next we add function prototypes for our methods. These also go in employee.h Based on the names, you should be able to tell what each one does.

Methods (helper functions) The actual definitions for the functions will go in their own file, called employeefunctions.c We ll want to #include employee.h in employeefunctions.c so we have access to the structure definition.

employeefunctions.c Here is the first part of employeefunctions.c

employeefunctions.c The getters and setters would access the fields (they should encapsulate any nullchecks or mallocs needed). The isequal function would compare two employees by comparing each field. The print function would print info about an employee

Using the Employee class To use this class in a program, we need to: 1. #include employee.h 2. Make sure employee.h and employeefunctions.c are in the directory and listed in the makefile 3. Do NOT #include employeefunctions.c (this could cause compilation errors)

Tip #5: Makefiles Makefiles, as you know, are essentially just scripts you can use to compile your programs. They are useful because you can issue one command, make, to build your whole project with all the necessary files and dependencies.

Simple Makefile Example: The labels on the left are called targets. To the right of the target are its dependencies. On the next line is a tab and a build command.

Makefile Tips Make sure your default target (the first target) is the desired name of the executable Don t add extra spaces or newline characters. Beware of indentation: build commands need to be indented with 1 tab (no spaces) The end of the makefile should have an extra newline

Makefile Step-By-Step Example Say we have a program with the following files: main.c employeefunctions.c employee.h

Makefile example continued Assume: 1. We need to access the structure and functions for the employee class in main.c 2. employee.h contains the structure definition for the structure, and the function prototypes 3. employeefunctions.c contains the function definitions.

Makefile Example - Dependencies The default target will be myprog, and its dependencies will be: 1. main.o (the object file for main.c) 2. employeefunctions.o (object file for employeefunctions.c)

Makefile: The makefile so far: We have our dependencies for the default target. Now we need its build command.

Makefile: With the build command added: Notes The gcc line is indented with one tab. The -o indicates we re generating an executable from object files

Makefile: Our first entry tells make how to create our default target (myprog) from its dependencies (main.o and employeefunctions.o) by using the specified build command. Now we need to tell it how to create those individual dependencies.

Makefile: main.o dependency main.o depends on its source file (main.c) and any files whose code it needs access to (employee.h). Remember that the function prototypes are in employee.h, so we will #include that in main.c but we won t #include employeefunctions.c

Main.o dependencies Here is the target and dependencies for main.o We ve listed the files that main.o depends on, now let s add the build command.

Main.o with build command: Notes: The gcc line is indented with 1 tab. The -c indicates that we re generating an object file from a c source file.

Makefile So Far Note the use of -o for building the default target (since it s an executable), and the use of -c for building main.o (since it s an object file).

Next Target We still need to tell make how to create employeefunctions.o This file depends on its source file (employeefunctions.c) and also depends on the employee.h file, since it needs access to the structure definition.

employeefunctions.o target We have the target and dependencies. And just like before, we need a build command.

With the build command: Again, note that the gcc line is indented with one tab character (no spaces). Also note that we use -c since we re generating an object file from a source file.

The makefile so far We now have rules to build each target from its corresponding dependencies using the specified build command.

If you re ever having strange compile/linking errors, it can be useful to try a make clean command and then re-issue the make to rebuild. Clean target Last (and yes, least), is the clean target. This gives an easy way to clean the project so you can do a fresh build.

The makefile with clean The rm line is also indented with a tab. It gives the command to force-delete all object files and core files.

The Employee Program I will post the entire Employee program, including makefile, on Blackboard. On ITSUnix, you can download it to your folder with the following command: And unzip with: