Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Structures Spring 2013Programming and Data Structure1.
1 C++ Syntax and Semantics The Development Process.
Dale/Weems/Headington
True or false A variable of type char can hold the value 301. ( F )
COSC 120 Computer Programming
Structure of a C program
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
Constants. 2 Objectives Describe ways to create constants –const –readonly –enum.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Data Types.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
 2007 Pearson Education, Inc. All rights reserved C Preprocessor.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Semantics CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
1 Programs Composed of Several Functions Syntax Templates Legal C++ Identifiers Assigning Values to Variables Declaring Named Constants String Concatenation.
Announcements Final NEXT WEEK (August 13 th Thursday at 16:00) Recitations will be held on August 12 th Wednesday We will solve sample final questions.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
1 C++ Syntax and Semantics, and the Program Development Process.
Structures in C++ UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) 1 ADNAN BABAR MT14028 CR.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
CS212: Object Oriented Analysis and Design Lecture 2: Introduction to C++
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.
Programming Fundamentals. Today’s Lecture The Conditional Operator Logical Operators Structures Enumerations.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 12 – C: structs, linked lists, and casts.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Introduction to Computers and Programming Class 24 Structures (structs) Professor Avi Rosenfeld.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 11: Structured Data.
CS201 – Introduction to Computing – Sabancı University 1 Announcements l Homework 5 ä Due this Wednesday (November 17), 19:00 l Common Problems and Questions.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Programming Fundamentals Enumerations and Functions.
BASIC C PROGRAMMING LANGUAGE TUTORIAL infobizzs.com.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Chapter # 2 Part 2 Programs And data
Announcements Midterm2 Grades to be announced THIS WEEK
Announcements Final Exam will be on August 19 at 16:00
Chapter 13 - The Preprocessor
14. THE PREPROCESSOR.
C Structures, Unions, Bit Manipulations and Enumerations
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Advanced Programming Basics
Announcements Midterm2 Grades to be announced NEXT Monday
C++ Compilation Model C++ is a compiled language
Chapter # 2 Part 2 Programs And data
Announcements Final will be NEXT WEEK on August 17
C Programming Language
Presentation transcript:

Weekly C-minar Week 0

Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging

What do you want to do today? #include #define typedef struct union enum

Compiler

Basic build steps Preprocess (highly program dependent) –Include all of the header files –Expand all macros –Generate code (templates) –Produce compiler input Compile (slooooooow) –Verify validity of syntax –Check static semantics (function signatures, etc.) –Produce linker input (.obj files in windows) Link (very fast) –Make machine code from the.obj files

Preprocessing Directives

#include tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive appears. #include "path-spec" #include

#include "path-spec" instructs the preprocessor to look for include files in the same directory of the file that contains the #include statement, and then in the directories of whatever files that include (#include) that file. The preprocessor then searches along the path specified by the /I compiler option, then along paths specified by the INCLUDE environment variable.

#include instructs the preprocessor to search for include files first along the path specified by the /I compiler option, then along the path specified by the INCLUDE environment variable

#include (hacks) #ifndef HELLO_WORLD_H #define HELLO_WORLD_H /* relevant headers */ #endif Why is this important?

#include Compilers don’t like multiply defined symbols or classes Encasing your headers in this fashion will prevent headers from being included multiple times /*(one.h) */ #include... /*(two.h) */ #include... /*(three.h) */ #include... /*(inf.h) */ #include /* results in multiple incls */

#define identifier … give a meaningful name to a constant in your program Replaced/removed during preprocessing #define identifier token-string opt –Replace identifier with token-string everywhere identifier is a token #define identifier –Remove identifier from the file

#define Can also be used as a functional replacement –Replaces the keywords in the macro with the values substituted at usage #define identifier[( identifier opt,..., identifier opt )] token-string opt Example #define LESSTHAN(LEFT, RIGHT) ( LEFT < RIGHT ) //usage if( LESSTHAN( j, k ) ) {... }

C Container and Type Keywords

typedef type-declaration synonym; defines a synonym for the specified type- declaration –cannot use the typedef specifier inside a function definition typedef declarations do not introduce new types — they introduce new names for existing types

typedef // Example of the typedef keyword typedef unsigned long ulong; ulong ul; // Equivalent to "unsigned long ul;" typedef struct mystructtag { int i; float f; char c; } mystruct; mystruct ms; // Equivalent to "struct mystructtag ms;" typedef int (*funcptr)(); // funcptr is synonym for "pointer // to function returning int" funcptr table[10]; // Equivalent to "int (*table[10])();"

typedef More examples typedef unsigned char BYTE; // 8-bit unsigned entity. typedef BYTE * PBYTE; // Pointer to BYTE. BYTE Ch; // Declare a variable of type BYTE. PBYTE pbCh; // Declare a pointer to a BYTE // variable.

Containers Why have ‘em? –Makes for easier definition of “objects” –Define return types for functions with multiple values What needs are there? –one type, lots of objects contained (struct) –one type, lots of types of values it can have (union) –one type, useful for logical definition (enum) –others, of course...

struct [tag] { member-list } [declarators]; The struct keyword defines a structure type and/or a variable of a structure type A structure type is a user-defined composite type. –It is composed of "fields" or "members" that can have different types.

struct In C –you must explicitly use the struct keyword to declare a structure. In C++ –a structure is the same as a class except that its members are public by default –struct keyword unnecessary once the type has been defined.

struct You have the option of declaring variables when the structure type is defined by placing one or more comma-separated variable names between the closing brace and the semicolon.

struct Example 1 struct PERSON // Declare PERSON struct type { int age; // Declare member types long ss; float weight; char name[25]; } family_member; // Define object of type PERSON struct PERSON sister; // C style structure declaration PERSON brother; // C++ style structure declaration sister.age = 13; // assign values to members brother.age = 7;

struct Example 2 (initialization) struct POINT // Declare POINT structure { int x; // Define members x and y int y; } spot = { 20, 40 }; // Variable spot has // values x = 20, y = 40 struct POINT there; // Variable there has POINT type struct CELL // Declare CELL bit field { unsigned character : 8; // ???????? unsigned foreground : 3; // 00000??? unsigned intensity : 1; // 0000? unsigned background : 3; // 0??? unsigned blink : 1; // ? } screen[25][80]; // Array of bit fields

union [tag] { member-list } [declarators]; declares a union type and/or a variable of a union type user-defined data type that can hold values of different types at different times –all of its members start at the same location in memory –can contain only one of its members at a time –is (basically) the size of the largest member

union union UNKNOWN // Declare union type { char ch; int i; long l; float f; double d; } var1; // Optional declaration of union variable

union In C –you must explicitly use the union keyword to declare a union. In C++ –union keyword unnecessary once the type has been defined. Example 1 union UNKNOWN var2; // C declaration of a union variable UNKNOWN var3; // C++ declaration of a union variable Example 2 A variable of a union type can hold one value of any type declared in the union. Use the member-selection operator (.) to access a member of a union: var1.i = 6; // Use variable as integer var2.d = 5.327; // Use variable as double

enum [tag] {enum-list} [declarator]; a user-defined type consisting of a set of named constants called enumerators –the first enumerator has a value of 0, –each successive enumerator is one larger than the value of the previous one, unless you explicitly specify a value for a particular enumerator

enum Enumerators needn’t have unique values name of each enumerator must be unique within the scope where the enum is defined. An enumerator can be promoted to an integer value. However, converting an integer to an enumerator requires an explicit cast, and the results are not defined

enum In C –you must explicitly use the enum keyword to declare an enum. In C++ –you can use just the tag. // Example of the enum keyword enum Days // Declare enum type Days { saturday, // saturday = 0 by default sunday = 0, // sunday = 0 as well monday, // monday = 1 tuesday, // tuesday = 2 wednesday, // etc. thursday, friday } today; // Variable today has type Days int tuesday; // Error, redefinition of tuesday enum Days yesterday; // Legal in C and C++ Days tomorrow; // Legal in C++ only yesterday = monday; int i = tuesday; // Legal; i = 2 yesterday = 0; // Error; no conversion yesterday = (Days)0; // Legal, but results undefined

Christopher, take over

Homework: See the webpage