COMsW Introduction to Computer Programming in C

Similar documents
Programming for Engineers C Preprocessor

COMsW Introduction to Computer Programming in C

Conditional Compilation

OBJECT ORIENTED PROGRAMMING USING C++

Chapter 7: Preprocessing Directives

Unit 4 Preprocessor Directives

make and makefile CS 211

Intermediate Programming, Spring 2017*

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

Appendix A. The Preprocessor

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

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

Lecture 2: C Programming Basic

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

CS3157: Advanced Programming. Outline

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

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

PROGRAMMAZIONE I A.A. 2017/2018

C introduction: part 1

A Fast Review of C Essentials Part II

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

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

CS240: Programming in C

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

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

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang

Errors During Compilation and Execution Background Information

Friday, September 16, Lab Notes. Command line arguments More pre-processor options Programs: Finish Program 1, begin Program 2 due next week

More on C programming

Topic 6: A Quick Intro To C

C Preprocessor. Prabhat Kumar Padhy

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

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

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

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

Lectures 5-6: Introduction to C

Friday, February 10, Lab Notes

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

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

COMP322 - Introduction to C++

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 C: Linked list, casts, the rest of the preprocessor (Thanks to Hal Perkins)

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

CSCI 171 Chapter Outlines

G52CPP C++ Programming Lecture 6. Dr Jason Atkin

Symbols, Compilation Units, and Pre-Processing

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

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

Lectures 5-6: Introduction to C

CS240: Programming in C

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

INTRODUCTION 1 AND REVIEW

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

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

CSE 374 Programming Concepts & Tools

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

Symbols, Compilation Units, and Pre-Processing

Come and join us at WebLyceum

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

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Decision Making -Branching. Class Incharge: S. Sasirekha

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

The Preprocessor. CS 2022: Introduction to C. Instructor: Hussam Abu-Libdeh. Cornell University (based on slides by Saikat Guha) Fall 2011, Lecture 9

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Course organization. Course introduction ( Week 1)

C Formatting Guidelines

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

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

EL6483: Brief Overview of C Programming Language

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Computational Methods of Scientific Programming Fall 2007

G52CPP C++ Programming Lecture 8. Dr Jason Atkin

Final C Details. CSE 333 Autumn 2018

JTSK Programming in C II C-Lab II. Lecture 1 & 2


CS 107 Lecture 18: GCC and Make

Fundamentals of Programming

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Lecture 5: C programming

Chapter 11 Introduction to Programming in C

Page 1. Agenda. Programming Languages. C Compilation Process

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

CS113: Lecture 7. Topics: The C Preprocessor. I/O, Streams, Files

BLM2031 Structured Programming. Zeyneb KURT

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

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

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

CMPE-013/L. Introduction to C Programming

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros.

A Crash Course in C. Steven Reeves

C Language, Token, Keywords, Constant, variable

EL2310 Scientific Programming

Chapter 11 Introduction to Programming in C

Materials covered in this lecture are: A. Completing Ch. 2 Objectives: Example of 6 steps (RCMACT) for solving a problem.

Physics 2660: Fundamentals of Scientific Computing. Lecture 3 Instructor: Prof. Chris Neu

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

CSCI 2132 Software Development. Lecture 8: Introduction to C

Transcription:

OMsW 1003-1 Introduction to omputer Programming in Lecture 9 Spring 2011 Instructor: Michele Merler http://www1.cs.columbia.edu/~mmerler/comsw1003-1.html 1

Are omputers Smarter than Humans? Link http://latimesblogs.latimes.com/technology/2011/02/ibms-watson-on-jeopardy-computer-takes-big-leadover-humans-in-round-2.html 2

Today Homework 1 orrection Debugging (from Lecture 8) Preprocessor 3

onditional Assignment Another way of embedding if - else in a single statement Uses the? : operators variable = ( condition )? val1 : val2 ; If condition is true, we assign val1 to variable If condition is false, we assign val2 to variable int x = 7, y; int x = 7, y; y = ( x > 5 )? x : 5; y= 7 if( x > 5 ) { y = x; } else{ y = 5; } 4

The comma operator In statements can also be separated by, not only ; int x = 2; int y; x++, y = x/3, y += 2; int x = 2; int y; x++; y = x/3; y = y+2; Be careful with declarations! int x = 2, char c = m ; Different types, NO int x = 2, y; V Same type, OK 5

The comma operator Special case, the for loop statement Example: the palindrome word checking. heck if a word is the same when read right to left int i, flag = 1; char word[100] = radar ; for( i=0, j=strlen(word)-1 ; i < strlen(word)/2 ; i++, j-- ) { } if( word[i]!= word[j] ) { flag = 0; break; } 6

The comma operator Special case, the for loop statement Example: the palindrome word checking Initial conditions change conditions for( i=0, j=strlen(word)-1 ; i < strlen(word)/2 ; i++, j-- ) { } if( word[i]!= word[j] ) { flag = 0; break; } 7

Advanced Types - onst const defines a variable whose value cannot be changed const double PI = 3.14; double r = 5, circ; circ = 2 * PI * r; PI = 7; 8

Advanced Types - onst const defines a variable whose value cannot be changed const double PI = 3.14; double r = 5, circ; circ = 2 * PI * r; PI = 7; Once it s initialized, a const variable cannot change value 9

Preprocessor 10

Preprocessor Preprocessor is a facility to handle Header files Macros Independent from itself, it s basically a text editor that modifies your code before compiling Preprocessor statements begin with # and do not end with ; 11

Preprocessor myfile.c (program)...... ompiler myfile (executable) 0100101010021 0101001010000 11110011... 010010100001 1110001110101 12

Preprocessor myfile.c (program) myfile.c (preprocessor code) myfile (executable)...... Preprocessor...... ompiler 0100101010021 0101001010000 11110011... 010010100001 1110001110101 13

View Preprocessor ode gcc has a special option that allows to run only the preprocessor gcc -E myfile.c We can send output to a file using the UNIX > operator gcc -E myfile.c > outfile.txt Saves gcc s output to outfile.txt 14

Header files Header files are fundamentally libraries Their extension is.h They contain function definitions, variables declarations, macros In order to use them, the preprocessor uses the following code #include <nameofheader.h> #include nameofheader.h For standard libraries For user defined headers So far, we have used predefined header files, but we can create our own! (more on this in upcoming Lectures) 15

Header files stdio.h myfile.c myfile.c #include <stdio.h>...... Preprocessor... 16

Macros A macro is a piece of code c which has been given a name n Every time we use that n in our program, it gets replaced with c The preprocessor allows you to declare them with #define Two types: Object-like macros Function-like macros 17

Object like macros macros.c onstants, usually defined on top of programs #define name text_to_substitute #define SIZE 10 #define FOR_ALL for( i=0; i< SIZE; i++ ) 18

Object like macros macros.c #define SIZE 10 /* main function */ int main(){ From now on, every time we write SIZE inside our program it is going to be replaced by 10 int arr[size]; } return(0); 19

Object like macros macros.c Some compilers do not allow you to declare arrays with a variable as size int size1 = 10; int arr1[size1]; /* should always cause error */ const int size2=10; int arr2[size2]; /* causes errors in many compilers */ #define SIZE 10 int arr3[size]; /* OK in any compiler */ 20

macros.c Function-like macros Macros that can take parameters like functions #define SQR(x) ((x) * (x)) #define MAX(x,y) ((x) > (y)? (x) : (y)) Parameters MUST be included in parentheses in the macro name, without spaces It is a good habit to include parameters in parentheses also in the text to be substituted 21

condomp.c onditional ompilation Allows to use or not certain parts of a program based on definitions of macros #ifdef var #ifndef var #else #endif #undef var if var is defined, consider the following code if var is not defined, consider the following code close if(n)def undefine var (opposite of #define) 22

condomp.c onditional ompilation #define DEBUG #ifdef DEBUG printf( The value of x is %d\n, x); #endif... If DEBUG was defined earlier in the program, then the statement printf( ); is considered, otherwise the preprocessor does not copy it to the file to be compiled 23