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

Similar documents
Modifiers. int foo(int x) { static int y=0; /* value of y is saved */ y = x + y + 7; /* across invocations of foo */ return y; }

Conditional Compilation

A Fast Review of C Essentials Part II

Errors During Compilation and Execution Background Information

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

PROGRAMMAZIONE I A.A. 2017/2018

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

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

Unit 4 Preprocessor Directives

The C Pre Processor ECE2893. Lecture 18. ECE2893 The C Pre Processor Spring / 10

CS2141 Software Development using C/C++ Compiling a C++ Program

Generic Programming in C

Appendix A. The Preprocessor

Topic 6: A Quick Intro To C

KYC - Know your compiler. Introduction to GCC

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

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

Macros in C/C++ Computer Science and Engineering College of Engineering The Ohio State University. Lecture 33

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017

Page 1. Agenda. Programming Languages. C Compilation Process

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

Chapter 7: Preprocessing Directives

C and C++ 2. Functions Preprocessor. Alan Mycroft

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

Programming for Engineers C Preprocessor

Compiling and Linking

The C Preprocessor (and more)!

1 You seek performance 3

OBJECT ORIENTED PROGRAMMING USING C++

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford.

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files

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

CS240: Programming in C

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

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

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Programs. Function main. C Refresher. CSCI 4061 Introduction to Operating Systems

fpp: Fortran preprocessor March 9, 2009

Summer May 11, 2010

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

Your first C and C++ programs

Programming in C S c o t t S c h r e m m e r

make and makefile CS 211

INDEX. Figure I-0. Listing I-0. Table I-0. Symbols.DIRECTIVE (see Assembler directives)? preprocessor operator 3-34


2 Compiling a C program

CMPSC 311- Introduction to Systems Programming Module: Build Processing

COSC350 System Software

Conduite de Projet Cours 4 The C build process

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

Separate Compilation of Multi-File Programs

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

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

Compiler, Assembler, and Linker

BİL200 TUTORIAL-EXERCISES Objective:

Programming Tools. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

CMPSC 311- Introduction to Systems Programming Module: Build Processing

EL6483: Brief Overview of C Programming Language

6.s096. Introduction to C and C++

SISTEMI EMBEDDED. The C Pre-processor Fixed-size integer types Bit Manipulation. Federico Baronti Last version:

EL2310 Scientific Programming

Problem Solving and 'C' Programming

The C Preprocessor Compiling and Linking Using MAKE

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.


Chapter 1 INTRODUCTION

CS201 - Introduction to Programming Glossary By

The Compilation Process

Continue: How do I learn C? C Primer Continued (Makefiles, debugging, and more ) Last Time: A Simple(st) C Program 1-hello-world.c!

EL2310 Scientific Programming

GCC: the GNU Compiler Collection

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

OBJECT ORIENTED PROGRAMMING

PRINCIPLES OF OPERATING SYSTEMS

G52CPP C++ Programming Lecture 9

COMP322 - Introduction to C++

C for Engineers and Scientists: An Interpretive Approach. Chapter 7: Preprocessing Directives

#include. Practical C Issues: #define. #define Macros. Example. #if

Software Engineering /48

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

CS3157: Advanced Programming. Outline

Chapter 11 Introduction to Programming in C

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

CSE 374 Programming Concepts & Tools

independent compilation and Make

Short Notes of CS201

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

Lectures 5-6: Introduction to C

More on C programming

Chapter 11 Introduction to Programming in C

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

COMP26912: Algorithms and Imperative Programming

Introduction: The Unix shell and C programming

Lecture 2. Xiaoguang Wang. January 16th, 2014 STAT 598W. (STAT 598W) Lecture 2 1 / 41

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Chapter 11 Introduction to Programming in C

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

Computational Methods of Scientific Programming Fall 2007

Compiler Drivers = GCC

Transcription:

Department of Computer Science College of Engineering Boise State University September 11, 2017 1/13

Scope Variables and functions are visible from the point they are defined until the end of the source file. Creating function prototypes at the start of the source file (or in an included header file) makes them visible throughout the source file. Add modifier static in front of a function declaration makes the scope of the function to be limited to the containing source file (similar to private modifier in Java). 2/13

Scope (2) Global variables are not visible to code in another source file. To make a global variable be visible to multiple source files, define it normally in only one source file and then declare it as extern in all the other source files. The declaration of an external variable announces the properties of the variable (e.g. type, name). The definition of an external variable will actually create and set aside storage for the variable. There must be only one definition of an external variable among all files that make up the source program. /* file1.c */ int moomoo = 10; /* file2.c */ extern int moomoo; /* file3.c */ extern int moomoo; 3/13

Example Examples: ourhdr.h, scope1.c, scope2.c. 4/13

Convention for writing header files Note that by using the ifdef s to wrap the header file is a standard way of declaring header files. This prevents the contents of the header file from being included multiple times (which would cause compilation errors) The convention is to use two leading underscores, convert the letters in the name of the header file to all uppercase and convert periods to underscores. You should always follow this convention. /* C- examples / scope / ourhdr.h */ # ifndef OURHDR_H # define OURHDR_H /* declarations */ # endif /* OURHDR_H */ 5/13

Compiling, Linking and Running Programs Source code file1.c file2.c... preprocessor cpp Source code compiler gcc Object code file1.o file2.o... Shared Libraries (aka Dynamically Loaded Libraries) Static Libraries linker ld/ld.so Running Program (process) loader ld/ld.so Executable Image (default name: a.out) Note: The above illustrates the GCC compiler tool chain but the concepts are the same for any C/C++ compiler. 6/13

GCC: The GNU C/C++ Compiler options An integrated C/C++ open-source compiler, invoked as gcc. Available on a wide variety of platforms. The following shows some commonly used command line. See the man page for more options. -c Compile but do not link. -Wall Print all warnings. Always use this option...some of the warnings can save you hours of debugging! -O Optimize option. Usually speeds up the execution time significantly. -g Enable debugging. Need this option if you want to use a debugger. Also gcc allows you to use the optimize option (-O) at the same time as the debug option. -o <filename> Name the output executable file to the given filename. Otherwise the default file name is a.out. -I dir Add the directory dir to the list of directories to be searched for header files. Directories named by -I are searched before the standard system include directories. 7/13

The GNU C/C++ compiler options -D<macro>=[<value>] Define a macro. The C file is processed with the macro defined. For example gcc -Wall -DDEBUG ex1.c would define the DEBUG macro to be true for the file being compiled. -E Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output. -S Stop after the stage of compilation proper; do not assemble. The output is in the form of an assembler code file for each non-assembler input file specified. version Display the version number and copyrights of the invoked GCC 8/13

Example: Design Using Functions and Files We will study a postfix calculator example from C-examples/scope/calculator Uses a stack data structure. Divides the program into logical files (similar to Java classes) 9/13

In-class Exercise Revisit the remove blanks example here: https://github.com/boisestate/cs253- resources/tree/master/c-examples/intro/remove-blanks Refactor the program to use one function per state transition. Replace the code in the main to use calls to the functions you have created. Recompile and retest. 10/13

C Preprocessor File inclusion. The command is #include. Files in the standard library can be specified using angle brackets. Files specified in double quotes are relative to the current directory. For example: # include <stdio.h> # include " myheader.h" Defining constants # define MAX 323239283 Macro substitution. The command is #define. Macros expand into in-line code. There is no compile-time check for type correctness. For example: # define max (a,b) ((a) >(b)? (a) : (b)) Example: C-examples/c-preprocessor/macro.c. 11/13

Conditional Inclusion (1) # ifdef linux printf ("I am the Happy Penguin!\n"); # elif _WIN32 printf (" Welcome to MS Windows ( I rule!).\n"); # elif APPLE && MACH printf (" Welcome to I am cool!\n"); # else printf ("Uh! who am i?\n"); # endif The above ifdef strings are standard ways of detecting the Operating System the code is being compiled on. For full list of identifying strings for various systems, see http: //sourceforge.net/p/predef/wiki/operatingsystems Example: C-examples/c-preprocessor/ex2.c. 12/13

Conditional Inclusion (2) ifdef statements are also used for toggling debug statements on and off. # ifdef DEBUG printf ("nc =%d\n",nc); # endif Three ways of triggering the DEBUG flag The DEBUG preprocessor flag can be defined at the top of the source file (or in a common header file) but then we have to change the code if we want to turn the debug output on, which isn t desirable. We can define the DEBUG flag in the command line arguments to the C compiler as shown below: gcc - Wall - DDEBUG =1 ex1.c We can define the DEBUG flag in the command line arguments to the make command and have it pass that on to the C compiler. For example: make " DEBUG =- DDEBUG =1" See example below for more details on this option. Example: C-examples/c-preprocessor/ex1.c. 13/13