ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices.

Slides:



Advertisements
Similar presentations
The Preprocessor Underlying C Language Features Copyright © 2012 by Yong-Gu Lee
Advertisements

Chapter 7 Process Environment Chien-Chung Shen CIS, UD
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 6: Dynamic Memory Allocation (DMA)
Chapter 13 & 14 C Preprocessor and Other C Topics Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education.
Kernighan/Ritchie: Kelley/Pohl:
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 5: Pointers.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 4: Arrays and Strings.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 8: Recursion.
Guide To UNIX Using Linux Third Edition
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Command line arguments. – main can take two arguments conventionally called argc and argv. – Information regarding command line arguments are passed to.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 19 - The Preprocessor Outline 19.1 Introduction 19.2 The #include Preprocessor Directive 19.3.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
C Hints and Tips The preprocessor and other fun toys.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Engineering Computing I Chapter 4 Functions and Program Structure.
Current Assignments Start Reading Chapter 6 Project 3 – Due Thursday, July 24 Contact List Program Homework 6 – Due Sunday, July 20 First part easy true/false.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
CS 261 – Data Structures Introduction to C Programming.
C Programming Day 3. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Storage Class Specifiers Every variable in a C.
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessor Midterm Review Lecture 7 Feb 17, 2004.
Function User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
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.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
The const Keyword Extreme Encapsulation. Humble Beginnings There are often cases in coding where it is helpful to use a const variable in a method or.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
Week 5 - Monday.  What did we talk about last time?  Processes  Lab 4.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
CSE 332: C++ expressions Expressions: Operators and Operands Operators obey arity, associativity, and precedence int result = 2 * 3 + 5; // assigns 11.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
C++ Functions A bit of review (things we’ve covered so far)
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Functions Students should understand the concept and basic mechanics of the function call/return pattern from CS 1114/2114, but some will not. A function.
User-Written Functions
Chapter 7: User-Defined Functions II
A bit of C programming Lecture 3 Uli Raich.
INC 161 , CPE 100 Computer Programming
Chapter 13 - The Preprocessor
Functions Separate Compilation
Programmazione I a.a. 2017/2018.
C Preprocessor(CPP).
Exam 4 review Copyright © 2008 W. W. Norton & Company.
Procedure Activations
The Function Prototype
C Preprocessor Seema Chandak.
SPL – PS1 Introduction to C++.
Presentation transcript:

ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 10: Appendices

ספטמבר 04Copyright Meir Kalech2 Arguments to main Upon program launch, main is the first function that is called. Assume we want to send parameters via the command line. For instance, a program that gets 2 strings and prints their concatenation. How to pass arguments via command line? main() can have two parameters: int argc char *argv[]

ספטמבר 04Copyright Meir Kalech3 Arguments to main The parameters have conventional names and their order is fixed: main(int argc, char *argv[ ]) argc - An integer specifying how many arguments are passed to the program via the command line. The program name is considered as an argument, hence argc is at least 1. argv - array of pointers to the arguments list. argv[0] is the program name. argv[argc-1] is the last argument.

ספטמבר 04Copyright Meir Kalech4 Arguments to main - Example main gets a string and 2 numbers and prints the string before the sum of the numbers (the execution file is called “print”). #include void main(int argc, char *argv[]) { int sum; if(argc == 4) { sum = atoi(argv[2]) + atoi(argv[3]); printf(“%d %s”, sum, argv[1]); } else puts(“required parameters missing”); } Command line> print ben argc argv /0tnirp neb 24 85

ספטמבר 04Copyright Meir Kalech5 const Defining a value as const causes its memory to be permanently allocated with this initial value: const int x = 5; Now x can’t be changed: x = 6;// compilation error The difference between const and #define: const : a location of memory is allocated. Its scope and life time follow the rules for variables. #define : no memory is allocated. Hence, no life time. The scope is determined in pre-processing.

ספטמבר 04Copyright Meir Kalech6 const &parameter Two ways to pass an object to a function: 1.By value – a copy of the object is created. 2.By address – the address of the object is passed. The second way is recommended if the function needs to change the parameter. When the function does not need to change the parameter, its better to use the first way. Problem: it’s too expensive (time and place) to create a copy. Solution: pass the argument by address anyway.

ספטמבר 04Copyright Meir Kalech7 const *parameter Problem: dangerous!!! You may change the parameter in the function! Solution: If the function needs to change the argument, pass it by address. If the function does not change the argument, pass it: By address in order to have the efficiency. With const in order to keep it unchangeable. void func(const Object *obj); Comment: when passing primitive types, if the function doesn’t change them, it is best to send them by value since they are anyway small in size.

ספטמבר 04Copyright Meir Kalech8 const *parameter Const before the pointer type refers to the pointed value: void strcpy(char *destination, const char *source); The destination pointed value can be changed, but the source pointed value can’t. Const after the pointer type refers to the pointer value itself: void func(char const *source); source = (char *) malloc(10);//error

ספטמבר 04Copyright Meir Kalech9 Preprocessor The C preprocessor is the first to process the C source file. When we compile our code, the preprocessor processes it before the compiler. The preprocessor is no more than a simple text editor that substitutes text in our code with other requested text. It scans the source file, searching for preprocessor directives (lines that start with # ). Once a preprocessor directive is found, the preprocessor makes the relevant text substitutions and then continues to scan. Once it finishes, it passes the output to the C language compiler. In other words, the preprocessor ’ s output is the compiler ’ s input.

ספטמבר 04Copyright Meir Kalech10 Preprocessor - #define #define 'creates' a new token (symbolic constant) that can be used later in the source code. Instead of writing: const int num = 3; write: #define NUM 3 Wherever the token is found in the source code (excluding in strings and comments), it is replaced by the replacing string. The scope of a #define token is from the point it is declared until the end of the source file. Convention: use all capital letters for a token.

ספטמבר 04Copyright Meir Kalech11 Preprocessor - Macros #define with parameters is called a macro. Each occurrence of the parameter name in the macro body is replaced by the actual parameter that was ‘ sent ’ to the macro. A macro has a value, like a function, but unlike a function, a macro is not sent to the stack (we are in preprocessing stage). #define PROD(a,b) ((a) * (b)) int k=3, v; v = PROD(12, k); The preprocessor translates it to: v = ((12) * (k)); A macro can be written over a few lines, using a backslash. #define Max3(a,b,c) ((a)>(b) ? (a)>(c) ? (a) : (c) \ : (b) > (c) ? (b) : (c))

ספטמבר 04Copyright Meir Kalech12 Conditional Compilation Conditional compilation means that under certain conditions, the preprocessor can make some code lines ‘ disappear ’ before the compiler receives them, so that they will not be a part of the program. Motivation: there are a lot of outputs and log files that are necessary only during tests but not in the final version. Or code that differs for different platforms. We wish to ignore these in the formal version.

ספטמבר 04Copyright Meir Kalech13 #ifdef, #ifndef, #endif If constant_name was previously #define, then everything between #ifdef and #endif will be passed to the compiler. Otherwise, it will be not passed to the compiler. If constant_name was NOT previously #define, then everything between #ifndef and #endif will be passed to the compiler. Otherwise, it will not be passed to the compiler. #ifndef constant_name C code #endif #ifdef constant_name C code #endif

ספטמבר 04Copyright Meir Kalech14 Pointer to Function Assume the following problem: An array of students’ grades is defined. We want to provide the following operations: Add 5 to each one of the grades. OR: Multiply each one of the grades by 1.1. OR: Divide each one of the grades by 0.8. What is the preferred way? Define 3 functions where each one of them contains a loop. But this is in contrast to the idea of function reuse.

ספטמבר 04Copyright Meir Kalech15 Pointer to Function Solution: use pointer to function. We can build just one function which goes over the array elements (run_loop). Besides this function we need to build 3 more functions that get a single element and factor as parameters, and: 1.Add the factor to the element (add). 2.Multiply the element by the factor (mul). 3.Divide the element by the factor (div). The function run_loop gets a pointer to function suitable to the prototype of the above functions, and operates it via the pointer, with the element and the factor as arguments.

ספטמבר 04Copyright Meir Kalech16 Pointer to Function - Example void add(int *x, int factor) { *x += factor; } void mul(int *x, int factor) { *x *= factor; } void div(int *x, int factor) { *x /= factor; } void run_loop(int arr[], int factor, void (* pFunc)(int *x, int factor) ) { for(int i=0; i<3; i++) pFunc(&arr[i], factor); } Pointer to function with the prototype: Return: void Parameters: (int &, int) The same prototype

ספטמבר 04Copyright Meir Kalech17 Pointer to Function - Example void main() { int grades[3]={88,99,87}; run_loop(grades, 5, add); run_loop(grades, 1.1, mul); run_loop(grades, 0.8, div); } void add(int *x, int factor) {…} Address: 100 Address: 230 void mul(int *x, int factor) {…} Address: 320 void div(int *x, int factor) {…} Address: grades: 200 main run_loop arr factor pFunc void run_loop(int arr[], int factor, void (* pFunc)(int *x, int factor) ) { for(int i=0;i<3;i++) pFunc(&arr[i],factor); } add 5 x factor 200