1 Building a program in C: Preprocessor, Compilation and Linkage.

Slides:



Advertisements
Similar presentations
Compilation and Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
Advertisements

Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
. Compilation / Pointers Debugging 101. Compilation in C/C++ hello.c Preprocessor Compiler stdio.h tmpXQ.i (C code) hello.o (object file)
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
© 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.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
 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.
Chapter 2 Software Tools and Assembly Language Syntax.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
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.
Computer Programming I Hour 2 - Writing Your First C Program.
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 The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
CS 261 – Data Structures Introduction to C Programming.
Algorithms and Programming Functions Lecture 28. Summary of Previous Lecture while statement for statement break statement Nested loops.
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.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
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.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
C is a high level language (HLL)
Programming Fundamentals Enumerations and Functions.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Basic Data Types & Memory & Representation. Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA,
Revisiting building. Preprocessing + Compiling 2 Creates an object file for each code file (.c ->.o) Each.o file contains code of the functions and structs.
C++ Functions A bit of review (things we’ve covered so far)
Windows Programming Lecture 06. Data Types Classification Data types are classified in two categories that is, – those data types which stores decimal.
Welcome to C and Computer Structure #include int main() { printf ("hello class\n"); return 0; }
C #include int main() { printf ("hello class\n"); return 0; }
Lecture 3 Translation.
13 C Preprocessor.
Basic Data Types & Memory & Representation
ECE 382 Lesson 20 Lesson Outline Structs Functions Headers Example
The Machine Model Memory
C #include <stdio.h> int main() { printf ("hello class\n");
Chapter 13 - The Preprocessor
Additional Assembly Programming Concepts
Functions Separate Compilation
C #include <stdio.h> int main() { printf ("hello class\n");
C Basics.
Choice of Programming Language
Pre-processor Directives
Introduction to C Programming
פרטים נוספים בסילבוס של הקורס
פרטים נוספים בסילבוס של הקורס
C Preprocessor(CPP).
Govt. Polytechnic,Dhangar
Programming in C Miscellaneous Topics.
Programming in C Miscellaneous Topics.
2. Second Step for Learning C++ Programming • Data Type • Char • Float
C Preprocessor Seema Chandak.
C Language B. DHIVYA 17PCA140 II MCA.
Executable program (p1)
SPL – PS1 Introduction to C++.
Presentation transcript:

1 Building a program in C: Preprocessor, Compilation and Linkage

Building a program in C – Reminder 2 Main Preprocessor, Compiler Main.c Main.o Linker libc.a

3 The Preprocessor

Preprocessor 4 A single-pass program that: 1. Include header files 2. Expands macros 3. Control conditional compilation 4. Remove comments Outputs – a code ready for the compiler to work on.

#include directive 5 #include "foo.h" Include the file “foo.h”, from current directory #include Include the file “stdio.h” from the standard library directory (part of compiler installation)

Modules & Header files 6 Complex.c int area (int x1,int y1, int x2, int y2);... square.h #include "square.h" #include // implementation int area (int x1,int y1,int x2, int y2) {... } square.c #include "square.h" int main() { area (2,3, 5,6); } MyProg.c

Header files 7 Header file contain 1. Definition of data types 2. Declarations of functions & constants 3. That are shared by multiple modules. #include directive allows several modules to share the same set of definitions/declarations

#define directive 8 #define FOO 1 int x = FOO; is equivalent to int x = 1;

#define with arguments 9 #define SQUARE(x) x*x b = SQUARE (a); is the same as b = a*a;

10 #define SQUARE (x) x*x b = SQUARE (a+1); c = SQUARE (a++); Is it what we intended? b = a+1*a+1; //Actually: b = 2*a+1; c = a++*a++; //Actually: c = a*a; a+=2; #define – cautions

11 #define SQUARE (x) ((x)*(x)) b = SQUARE (a+1); c = SQUARE (a++); Is it what we intended? b = ((a+1)*((a+1)); //Now ok c = ((a++)*(a++)); //Did not help: c = a*a; a+=2;

#define 12 #define directive should be used with caution! Alternative to macros: Constants enum { FOO = 1 }; or const int FOO = 1; Functions – inline functions (C99,C++)

#define 13 Multi-line: All preprocessor directive effect one line (not c statement). To insert a line-break, use “\”: BAD: #define x (5 + 5) // x == 10 ! GOOD : #define x (5 + \ 5) // x == 10 !

#if directive 14 Allows to have conditional compilation #if defined(DEBUG) // compiled only when DEBUG exists printf("X = %d\n", X); #endif

Preprocessor – summary 15 Text processing program. Does not know c rules Operates before compilation, output passed to compiler. Can do copy and paste / cut #include paste the included file here Commonly included file (.h by convention) contains forward declarations #define copy the macro body, paste it where macro name appears - constants, simple "functions" #if condition not fulfilled, cut the code - Conditional compilation – e.g. debugging code

16 vs Debug/Test mode vs Release mode

17 vs Debug/Test mode vs Release mode #include #define MAX_INTS 100 int main() { int ints[MAX_INTS]; // i should be in bounds, but is it really? i = foo( ); // safety assertions assert(i>=0); assert(i<MAX_INTS); ints[i] = 0;...

18 #include #define MAX_INTS 100 int main() { int ints[MAX_INTS]; // i should be in bounds, but is it really? i = foo( ); // safety assertions assert(i>=0); assert(i<MAX_INTS); ints[i] = 0;... vs Debug/Test mode vs Release mode

19 #include #define MAX_INTS 100 int main() { int ints[MAX_INTS]; // i should be in bounds, but is it really? i = foo( ); // safety assertions assert(i>=0); assert(i<MAX_INTS); ints[i] = 0;... #define NDEBUG

20 assert bad // bad, foo() will not be called // if the compiler removes the assert()  // if NDEBUG is defined assert(foo() == 0); good // good int errCode = foo(); assert(errCode == 0);

21 Use for: Catching bugs Don't use for: checking malloc, user input,... assert

assert.h 22 #include // Sqrt(x) - compute square root of x // Assumption: x non-negative double sqrt(double x ) { assert( x >= 0 ); // aborts if x < 0 …

assert.h 23 // procedure that actually prints error message void __assert(char* file,int line,char* test); #ifdef NDEBUG #define assert(e) ((void)0) #else #define assert(e) \ ((e) ? (void)0 : \ __assert(__FILE__, __LINE__, #e)) #endif

assert.h 24 Important coding practice Declare implicit assumptions Sanity checks in code Check for violations during debugging/testing

25 Compilation

26 Translate the c code to machine code. Every machine architecture has a different code. Need separate compilation. x86 : older but still common PCs “32-bit” (Intel/AMD) x64 : newer PCs “64-bit” (Intel/AMD) ARM : (many versions) Phones, tablets (ARM) Translated code is an “executable” – the OS can load it to the machine memory, and the let the CPU do whatever you wrote in the code.

“Assembly” 27 int y=2; int mult(int x) { return x*y; mov eax,dword ptr [x] imul eax,dword ptr [ h] } int main() { return mult(5); push 5 call 11711A4h } Only part of the assembly code is shown here. Actually each command has binary code, and the sequence of these code is the object file.

Compiling warnings

29 Add “-Wall” flag to catch things like this: if (i=3) { //bug, we meant i==3 … }

Basic Data Types & Memory & Representation

Basic data types Primitive data types are similar to JAVA: char int short long float double Unlike in JAVA, All can be signed/unsigned Default: signed Unlike in JAVA, the types sizes are machine dependant! 31

Memory – few definitions Bit – a binary digit - zero or one Byte – 8 bits Not C specific 0/1 32

Basic data types The types sizes must obey the following rules. 1. Size of char = 1 byte 2. Size of short ≤ size of int ≤ size of long Undetermined type sizes Advantage: hardware support for arithmetic operations Disadvantage: problems with porting code from one machine to another 33

signed VS. unsigned Each type could be signed or unsigned int negNum = -3; int posNum = 3; unsinged int posNum = 3; unsinged int posNum = -3; 34

Integers binary representation 35