Introduction: C Pointers Day 2 These Slides NOT From Text.

Slides:



Advertisements
Similar presentations
Separate compilation Large programs are generally separated into multiple files, e.g. tuples.h, ray.h, ray.c, tuples.c main.c With several files, we can.
Advertisements

Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Programming in Visual Basic
JavaScript 101 Lesson 01: Writing Your First JavaScript.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.1 Introduction to Java.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
1 CSC103: Introduction to Computer and Programming Lecture No 26.
A First Program Using C#
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Rossella Lau Lecture 1, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 1: Introduction What this course is about:
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
C Hints and Tips The preprocessor and other fun toys.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 2 part #1 C++ Program Structure
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
C++ / G4MICE Course Session 1 - Introduction Edit text files in a UNIX environment. Use the g++ compiler to compile a single C++ file. Understand the C++
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Pointers: Basics. 2 Address vs. Value Each memory cell has an address associated with it
Chapter Topics The Basics of a C++ Program Data Types
User-Written Functions
Chapter 6 CS 3370 – C++ Functions.
CSC201: Computer Programming
Introduction to Computer Science / Procedural – 67130
Chapter 2 part #1 C++ Program Structure
Basic Elements of C++.
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Loop Structures.
Introduction to Programming
14. THE PREPROCESSOR.
Pre-processor Directives
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
C Preprocessor(CPP).
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Introduction to C++ Programming
Chapter 2: Introduction to C++.
Developing a Program.
Chapter 1 c++ structure C++ Input / Output
Conditional Compilation
SPL – PS1 Introduction to C++.
Presentation transcript:

Introduction: C Pointers Day 2 These Slides NOT From Text.

Constant Declarations Up to now we have used #define to define a named constant. This depends on the pre-processor. Another way is to define a constant inside the regular code. e.g. const float PI = ; PI = 3.0; is now illegal!

Constant Declaration (cont.) The keyword const indicates a variable that doesn’t change.

const Restrictions Constants must be initialized when they are defined. Constant values can never be changed. I.E. They can never appear on the left of an assignment operator, =

const vs. #define C checks the syntax of const statements immediately. The #define directive is not checked until the macro is used. const uses C syntax, while #define has a syntax all its own. const follows normal C scope rules, while constants defined by a #define directive continue on forever.

const vs. #define (cont.) The #define directive can only define simple constants. The const statement can define almost any type of C constant, including things like structures. The #define directive is essential for things like conditional compilation and other specialized uses.

Conditional Compilation One use is to “comment out” a bunch of code. Suppose you had: … i=12; /* This is a one line comment */ J = 15; /* This is a comment at the end of a line */ K = -10; X = sqrt (102.34); /* Another one line comment */ …

Conditional Compilation You would like to eliminate all this code temporarily. Comment before and after? No, that doesn’t work. /* i=12; /* This is a one line comment */ J = 15; /* This is a comment at the end of a line */ K = -10; X = sqrt (102.34); /* Another one line comment */ */ Terminates the comment here.

Conditional Compilation You can use a pre-processor statement to do this easily. #ifdef _RAY_01 i=12; /* This is a one line comment */ J = 15; /* This is a comment at the end of a line */ K = -10; X = sqrt (102.34); /* Another one line comment */ #endif None of this will be compiled into your program because you haven’t ever defined a shell variable called _RAY_01

Conditional Compilation Want to quickly turn on all the segments so marked? Simply define _RAY_O1. #ifdef _RAY_01 i=12; /* This is a one line comment */ J = 15; /* This is a comment at the end of a line */ K = -10; X = sqrt (102.34); /* Another one line comment */ #endif #define _RAY_01 All this will be part of your program. And, any other section marked with _RAY_01.

Other Base Constants Base 10Base 8Base x x xF

const Pointers const char * answer_ptr = “Forty_Two”; Does NOT tell C that the variable answer_ptr is a constant! Instead, it tells C that the data pointed to by answer_ptr is a constant. The data cannot be changed, but the pointer can.

Pointer Is Constant If we put the const after the *, we tell C that the pointer is constant. char * const name_ptr = “Test”; The data can be changed, but the pointer cannot.

Or Both Unchangeable To make them both constants, put two const in. const char * const title_ptr = “Title”; The data cannot be changed, and the pointer cannot be changed.

But WHY? I said newer programs like Ada and Java provide automatic protection for the programmer. Similar to guards and safety switches on a table saw. I also said C was like a spinning blade in space. Using const carefully can protect your code from inadvertent side effects.

Side Effects? One of the worst habits of people my age is being proud of their mastery of C’s side effects! Look how clever I am! However, no one can read the code!!! Read the enrichment puzzles for examples of these. But, we have looked at some, the increment and decrement operator.

variable = other_variable ++; Main effect, assign the value of other_variable to variable. Side effect, increment other_variable.

if(variable = expression) x=0; Main effect: assign 0 to x if the value of the assignment is true (!= 0). Side effect: assign the value of expression to variable. Side effects are sort-of hidden actions taken by the language. Try to avoid them. If you use them, add comments!

Compiler Options Depend on the compiler, but many similar ones. Added on the command line, after the gcc command, before the file names. gcc {… options … } file_names; You’ve seen a couple: -c compile only. -o define output run image file name.

Other Useful Compiler Options -g : insert debugging statements. -Wall : Print all warnings. Provides cleaner executing code. -E : preprocess only and produce the results of all preprocessor directives.

Lab #5 Hints Use a separate file for each function. lab5.c get_problem.c get_rate_drop_factor.c get_kg_rate_conc.c get_units_conc.c fig_drops_min.c fig_ml_hr.c by_weight.c by_units.c proto.h These are small functions. Some have pass by reference (pointer) parameters. (book calls them output)

Lab5: Intermediate Makefiles Use Makefile variables: OBJS=lab5.o get_problem.o get_rate_drop_factor.o \ get_kg_rate_conc.o get_units_conc.o fig_drops_min.o \ fig_ml_hr.o by_weight.o by_units.o lab5: $(OBJS) gcc –o lab5 $(OBJS) Lab5.o: lab5.c proto.h gcc –c lab5.c get_problem.o: get_problem.c proto.h gcc –c get_problem.c …

Lab5: Sample Input File

Lab5: Sample Input File Problem 1 Problem 2 Problem 3 Problem 4 Sentinel -> quit!

Lab5: Sample output You test your lab by running it from the screen and keyboard. Then, when it’s working, redirect BOTH input and output. $ lab5 MyOutput MyOutput will NOT have the responses to the input prompts, just the prompts all run together.

Lab5: Matching Exactly! A little pain for you. Saves a big pain for us reading 65 submissions! See web page write-up, I added a sample output which you can easily count blanks.