Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Doing C with Style.

Slides:



Advertisements
Similar presentations
Lecture 3 Some commonly used C programming tricks. The system command Project No. 1: A warm-up project.
Advertisements

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Structure of a C program
The C Preprocessor Yongjoon Lee April 22, What is it? Things with # –#include Processes the C source files BEFORE handing it to compiler. –`Pre`-process.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
Chapter 2: Introduction to C++.
Programming Introduction to C++.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Introduction to C++ Programming Introduction to C++ l C is a programming language developed in the 1970's alongside the UNIX operating system. l C provides.
Programming Style and Documentation Objective(s) F To become familiar with Java Style and Documentation Guidelines.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
The Java Programming Language
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
C Hints and Tips The preprocessor and other fun toys.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
Engineering Computing I Chapter 4 Functions and Program Structure.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Lecture 3: Getting Started & Input / Output (I/O) “TRON” Copyright 1982 (Walt Disney Productions)
Introduction to Programming
C++ Basics L KEDIGH. Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List.
Code Conventions Tonga Institute of Higher Education.
C++ Basics L KEDIGH. Objectives 1. basic components of a c++ program using proper terminology and programming conventions adopted for this class. 2. List.
Flex Fast LEX analyzer CMPS 450. Lexical analysis terms + A token is a group of characters having collective meaning. + A lexeme is an actual character.
DATA TYPE AND DISPLAY Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Documentation and Style. Documentation and Comments  Programs should be self-documenting.  Use meaningful variable names.  Use indentation and white.
Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.
CSC Programming for Science Lecture 4: Beginning Programming.
Minimal standard C program int main(void) { return 0 ; }
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Multi-dimensional Arrays and other Array Oddities Rudra Dutta CSC Spring 2007, Section 001.
1 2. Program Construction in Java. 01 Java basics.
Unit 10 Code Reuse. Key Concepts Abstraction Header files Implementation files Storage classes Exit function Conditional compilation Command-line arguments.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
1 CS 192 Lecture 4 Winter 2003 December 8-9, 2003 Dr. Shafay Shamail.
Lecture 3: Getting Started & Input / Output (I/O)
Bill Tucker Austin Community College COSC 1315
C Primer.
CISC105 – General Computer Science
Command Line Arguments
Command Line Arguments
Programmazione I a.a. 2017/2018.
Instructor: Ioannis A. Vetsikas
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CMSC 104, Section 4 Richard Chang
C Stuff CS 2308.
Variables in C Topics Naming Variables Declaring Variables
Chapter 1: Computer Systems
C Preprocessor(CPP).
Variables in C Topics Naming Variables Declaring Variables
Merging CI and AI it can be done
Documentation and Style
Programming Introduction to C++.
Functions Reasons Concepts Passing arguments to a function
2008/10/01: Lecture 8 CMSC 104, Section 0101 John Y. Park
CSCE-221 C++ Coding Standard/Guidelines
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
15213 C Primer 17 September 2002.
Variables and Constants
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Doing C with Style

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Style vs. Standards “Standards are important! Everyone should have one!” ”A foolish consistency is the hobgoblin of little minds, adored by little statesmen and philosophers and divines.” Ralph Waldo Emerson “Consistency is the last refuge of the unimaginative.” Oscar Wilde

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Disclaimer The rest of this lecture describes the style I prefer. You may prefer other style conventions. There is no “right” or “wrong” style! Your style should reflect your view of the world… not mine! No points will ever be deducted for using a different style Points may be deducted for obfuscation e.g. don’t choose a style that uses variable names like: AaTtrrXxFfVvvG

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Capatilization #defines are all caps: #define ARRAY_SIZE 256 Variable/function names are lower case: int area; Unless two word names Either separate words with underscores: int box_width=6; Or capitalize first letter of second word: int boxWidth=6; Constants - First letter capitalized: const double Avogadro=6.23e26;

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Indentation Use small tab settings – 2 or 3 spaces or 0.3” Indent lines between “{“ and “}” Indent continued lines int main(int argc, char **argv) { if (argc==0) printf(“No arguments\n); else { printf(“Arguments…\n”); for(i=0; i<argc; i++) { printf(“ %d: %s\n”, i,argv[i]); }

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Alternate Indentation Put “{“ and “}” on their own lines Allows “{“ to line up vertically with “}” Takes more vertical space int main(int argc, char **argv) { if (argc==0) printf(“No arguments\n); else { printf(“Arguments…\n”); for(i=0; i<argc; i++) { printf(“ %d: %s\n”, i,argv[i]); }

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Comments Only when code is not obvious Line comments using // Block comments using /* */ Normal comments use indentation Debug comments out-dented … x = i>>3; // i/8 round <- … while(x>0) { /* Process this stuff as … … */ x=myfunc(z) … /*DBG*/ printf(“x is now %d\n”,x); }

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Commenting Out Code Best: #if 0 / #endif Single Lines… // Debug /* DBG */ …. or /* DBG … */

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 C File Order #defines #includes (order?.. I put mine first) Global Variable Definitions Function Declaraions (except main, in call order?) Function Definitions (in call order?)

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Multi-File Organization Limit number of functions in one file Use files to group similar functions One header file for each auxiliary C file Header file : function declarations (defined in C file) Header file contains #includes required for function declarations Use #ifdef XYZ_H #define XYZ_H … #endif around xyz.h contents