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

Size: px
Start display at page:

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

Transcription

1 1

2 2

3 CMPE 013/L Pre Processor Commands Gabriel Hugh Elkaim Spring

4 Introduction Preprocessing Affect program preprocessing and execution Capabilities Inclusion of additional C source files Definition of symbolic constants and macros Conditional preprocessing of a program Format of preprocessing directives A preprocessing directive consists of a sequence of preprocessing tokens that begins with a pound sign #, which must be the first non space character on the line. Some preprocessing directives are listed below. PreProcessor Directives Definition PreProcessor Directives arepartsofthecodethatgivespecial instructions to the compiler. They always begin with a # at the beginning of the line, and are used to direct the compiler with a number of specific commands. Conditional Compilation Symbolic Constants and Macros Source File Inclusion Macros Predefined Macros Line Control Error Directive 4

5 Direc tive #define #elif #else #endif #error #if #ifdef #ifndef #include #line #pragma # Null directive defined Desc ription Define a preprocessor macro. Alternatively include some text based on the value of another expression, if the previous #if, #ifdef, #ifndef, or #elif test failed. Alternatively include some text, if the previous #if, #ifdef, #ifndef, or #elif test failed. Terminate conditional text. Produce a compile time error with a designated message. Conditionally include text, based on the value of an expression. Conditionally include text, based on whether a macro name is defined. Conditionally include text, based on if a name is not a defined macro. Insert text from another source file. Give a line number for message. Compiler/interpreter specific features, not in C standard Preprocessing operator that yields 1 if a name is defined as a preprocessing macro and 0 otherwies; used in #if and #elif. Symbolic Constants and Macros The #define Preprocessing Directive This preprocessing directive is used to create symbolic constants and macros. Form #define identifier replacement-list defines an object like macro that causes each subsequent instance of the macro names to be replaced by the replacement list of preprocessing tokens that constitute the remainder of the directive. The new line is a character that terminates the #define preprocessing directive. 5

6 Symbolic Constants and Macros Symbolic constants The simple form of macro is particularly useful for introducing named constants into a program. It allows for easier modification of the constants later on.when programs are processed, all occurrences of symbolic constants indicated by identifier are replaced by the replacement list. Example: #define BLOCK_SIZE 0x100 we can write int size = BLOCK_SIZE; instead of int size = 0x100; in the program. Note: Cannot redefine symbolic constants with different values by multiple #define statements Symbolic Constants and Macros A preprocessing directive of the form #define identifier(identifier-list-opt) replacement-list new-line defines a function like macro with arguments, similar syntactically to a function call. The parameters are specified by the optional list of identifiers. Example: if a macro mul with two arguments is defined by #define mul(x,y) ((x)*(y)) then the source program line result = mul(5, a+b); is replaced with result = ((5)*(a+b)); 6

7 NOTE: Parentheses are important in macro definitions. Example: If macro mul is defined as #define mul(x,y) (x*y) The statement result = mul(5, a+b); in the source program becomes result = (5*a+b); The evaluation will be incorrect. #undef Undefine a symbolic constant or macro, which can later be redefined. Example: #define mul(x,y) ((x)*(y)) /* */ #undef mul int mul; /* mul can be used after it is undefined */ 7

8 The #include Preprocessing Directive Source File Inclusion Copy of a specified header file included in place of the directive. It has following two common forms. 1)#include <header.h> Searches standard library for header file and replaces the directive by the entire contents of the file. In Ch, the header is searched according to the paths specified by the the system variable _ipath. C compilers in Unix will typically search the header file in the directory /usr/include. In Visual C++, the header file is searched based on the paths in the environment variable INCLUDE. or cl I C:/home/assount/include program.c Used for standard library files 2) #include header.h" C compilers and interpreters will first search the header file in the same directory where the file is being processed, which typically is the current directory. Then search the header file in the paths as if it was included by #include <header.h>. Applications Loading header files #include <stdio.h> Programs with multiple source files to be compiled together Includes user defined header files which have common declarations and definitions (classes, structures, function prototypes, and macros) 8

9 Conditional Compilation Conditional compilation Enables the user to control the compilation of the program, screen out portions of source code that are not to be compiled. Structure The structure is similar to if and else statement in C. Conditional preprocessing directives #if, #else, #elif, and #endif Preprocessing directives of the forms #if expr1 /*... */ #elif expr2 /*... */ #else /*... */ #endif check whether the controlling expression evaluates to nonzero. Every #if ends with #endif Example: #if defined(_hpux_) printf( I am using HP-UX\n ); #elif defined(_win32_) printf( I am using Windows\n); #endif 9

10 Preprocessing directives of the forms # ifdef identifier # ifndef identifier check whether the identifier is or is not currently defined as a macro name. #ifdef identifier is the short form of #if defined(identifier) #ifndef identifier is the short form of #if!defined(identifier) Each directive s condition is checked in order. If it evaluates to false (zero), then the group that it controls is skipped: directives are processed only through the name that determines the directive in order to keep track of the level of nested conditionals. Only the first group whose control condition evaluates to true (nonzero) is processed. If none of the conditions evaluates to true, and there is a #else directive, then the group controlled by the #else is processed; if lacking a #else directive, then all the groups until the #endif are skipped. Comment out a segment of code Comment out code segment which contains /*... */ Use following format to comment out the segment of code double d = some_func(); #ifdef JUNK /* This code segment will be commented out */ printf( d = %f\n, d); #endif The code segment will be commented out when JUNK is not defined, To uncomment the code segment, define JUNK or remove #ifdef JUNK and #endif. 10

11 To include a header file in a program only once, it is typically handled using the combination of the following preprocessing directives #ifndef, #define, and #endif. For example, a header file header.h may consist of the following code fragment. #ifndef HEADER_H #define HEADER_H /* code */ #endif Example: /* File: accelhead.c */ #include <stdio.h> /* local header file */ #include "accel.h" int main() { /* declare variables */ double a, mu, m, t; /* File: accel.h */ #ifndef ACCEL_H #define ACCEL_H #define M_G 9.81 double force(double t); double accel(double t, double mu, double m); #endif } /* Initialize variables */ mu = 0.2; m = 5.0; t = 2.0; /* processing */ a = accel(t, mu, m); /* display the output and termination */ printf("acceleration a = %f (m/s^2)\n", a); return 0; double force(double t) { double p; } p = 4*(sin(t)-3)+20; return p; double accel(double t, double mu, double m) { double a, p; } p = force(t); a = (p-mu*m*m_g)/m; return a; 11

12 Macros defined in C Predefined Macros Macro Name LINE FILE DATE TIME STDC Description The line number of the current source program line which is expressed as a decimal integral constant The name of the current source file which is expressed as a string constant. The calendar date of the translation which is expressed as a string constant form Mmm dd yyy. Mmm is produced by asctime(). The current time which is expressed as a string constant of the form hh:mm:ss, as returned by asctime(). The decimal constant 1 for C comforming impelementtation. STDC_VERSION The decimal constant L for C99 comforming implementation Example: /* filename: predefined.c */ #include <stdio.h> int main() { printf(" FILE = %s\n", FILE ); printf(" LINE = %d\n", LINE ); printf(" DATE = %s\n", DATE ); printf(" TIME = %s\n", TIME ); #ifdef STDC printf(" STDC = %d\n", STDC ); #endif #ifdef STDC_VERSION printf(" STDC_VERSION = %d\n", STDC_VERSION ); #endif return 0; } 12

13 Output: FILE = predefined.c LINE = 6 DATE = Feb TIME = 00:09:42 STDC = 1 STDC_VERSION_ = /* File: printerror.c */ #include <stdio.h> #define printerror() printf("error in %s:%s():%d\n", \ FILE, func, LINE ); void funcname1() { printerror(); } void funcname2() { printerror(); } int main() { funcname1(); funcname2(); return 0; } Example Output Error in printerror.c:funcname1():7 Error in printerror.c:funcname2():10 13

14 NULL Directive A preprocessing directive of the form #new-line has no effect on the program. The line is ignored. Debugging code #define DEBUG /*... */ double x; x = some_func(); #ifdef DEBUG printf( The value of x = %f\n, x); #endif Defining DEBUG to print out the value of x. After debugging, remove #define statement. The debugging statements are ignored. 14

15 Line Control The #line directive can be used to alter the line numbers assigned to the source code. This directive gives a new line number to the following line, which is then incremented to derive the line number for subsequent lines. A preprocessing directive of the form #line digit-sequence new-line causes the implementation to behave as if the following sequence of source lines begins with a source line that has a line number as specified by the digit sequence. A preprocessing directive of the form #line digit-sequence s-char-sequence-opt new-line sets the presumed line number similarly and changes the presumed name of the source file to be the contents of the character string literal. The name of the source file is stored in the predefined macro FILE internally. Example: /* FILENAME: linefile.c */ #include <stdio.h> int main() { printf( before line directive, line number is %d \n, LINE ); printf( the FILE predefined macro = %s\n, FILE ); #line 200 newname printf( after line directive, line number is %d \n, LINE ); printf( The FILE predefined macro = %s\n, FILE ); return 0; } Output: before line directive, line number is 4 the FILE predefined macro = linefile.c after line directive, line number is 200 the FILE predefined macro = newname 15

16 Definition Macros: PreProcessor Directives Macros are text replacements created with #define that insert code into your program. Macros may take parameters like a function, but the macro code and parameters are always inserted into code by text substitution. #define text(args) Can be used instead of functions in certain instances Made to look like C functions Definition Macros Macros with #define Macros are text replacements created with #define that insert code into your program. Macros may take parameters like a function, but the macro code and parameters are always inserted into code by text substitution. Are evaluated by the preprocessor Are not executable code themselves Can control the generation of code before the compilation process Provide shortcuts 16

17 Macros with #define Simple Macros Text substitution as seen earlier Syntax #define label text Every instance of label in the current file will be replaced by text text can be anything you can type into your editor Arithmetic expressions evaluated at compile time Example #define Fosc #define Tcy (0.25 * (1/Fosc)) #define Setup InitSystem(Fosc, 250, 0x5A) Macros with #define Argument Macros Create a function like macro Syntax #define label(arg 1,,arg n ) code The code must fit on a single line or use '\' to split lines Text substitution used to insert arguments into code Each instance of label() will be expanded into code This is not the same as a C function! Example #define min(x, y) ((x)<(y)?(x):(y)) #define square(x) ((x)*(x)) #define swap(x, y) { x ^= y; y ^= x; x ^= y; } 17

18 Example Macros with #define Argument Macros Side Effects #define square(a) ((a)*(a)) Extreme care must be exercised when using macros. Consider the following use of the above macro: i = 5; x = square(i++); Results: Wrong Answers! x = 30 x = square(i++); i= 7 expands to: x = ((i++)*(i++)); So igets incremented twice, not once at the end as expected. The # token appearing within a macro definition is recognized as a unary stringization operator. In a replacement list, if a parameter is immediately preceded by a # preprocessing token, both are replaced by a single character string literal preprocessing token that contains the spelling of the preprocessing token sequence for the corresponding argument. Example: Converting Token to Strings > #define TEST(a) #a > printf( %s, TEST(abcd)) abcd The macro parameter abcd has been converted to the string constant abcd. It is equivalent to > printf( %s, abcd ) 18

19 Each occurrence of white space between the argument s preprocessing tokens becomes a single space character in the character string literal. White space before the first preprocessing token and after the last preprocessing token composing the argument is deleted. Example: > #define TEST(a) #a > printf( 1%s2, TEST( a b )) 1a b2 The argument is turned into the string constant a b. Token Merging in Macro Expansions The merging of tokens to form new tokens in C is controlled by the presence of the merging operator ## in macro definitions. The common use of concatenation is concatenating two names into a longer name. It is possible to concatenate two numbers, or a number and a name, such as 1.5 and e3, into a number. Example: > #define CONC2(a, b) a ## b > #define CONC3(a, b, c) a ## b ## c > CONC2(1, 2) 12 // numbers 1 and 2 concatenated to 12 > CONC3(3, +, 4) 7 // 3, +, and 4 becomes 3+4, which equals 7 > printf( CONC2(1,2) = %d, CONC2(1,2)) CONC2(1,2) = 12 19

20 A preprocessing directive of the form #error pp-tokens-opt new-line causes the implementation to produce a diagnostic message that includes the specified sequence of preprocessing tokens and the interpretation to cease. Example: Error Directive /* FILENAME: error.c */ #include <stdio.h> #define MACRO int main() { #ifdef MACRO #error This is an error, if your code reach here /* the code here will not be processed */ #else printf("ok \n"); #endif return 0; } Output: ERROR: #error: This is an error, if your code reach here ERROR: syntax error before or at line 6 in file error.c' ==>: #error This is an error, if your code reach here BUG: #error This is an error, if your code reach here<==??? WARNING: cannot execute command error.c 20

21 Lab Exercise 19 Macros with #define Exercise 19 #define Macros Open the project s workspace: On the class website /Examples/Lab19.zip > Load Lab19.mcw 1 Open MPLAB and select Open Workspace from the File menu. Open the file listed above. If you already have a project open in MPLAB, close it by selecting Close Workspace from the File menu before opening a new one. 21

22 Exercise 19 #define Macros Compile and run the code: 2 Compile (Build All) 3 Run 4 Halt 2 Click on the Build All button. 3 If no errors are reported, click on the Run button. 4 Click on the Halt button. Exercise 19 #define Macros #define Macro Definition and Use /* MACROS */ #define square(m) ((m) * (m)) #define BaudRate(DesiredBR, FoscMHz) ((((FoscMHz * )/DesiredBR)/64)-1) /*============================================================================ FUNCTION: main() ============================================================================*/ int main(void) { x = square(3); printf("x = %d\n", x); } SPBRG = BaudRate(9600, 16); printf("spbrg = %d\n", SPBRG); 22

23 Exercise 19 Conclusions #define macros can dramatically simplify your code and make it easier to maintain Extreme care must be taking when crafting a macro due to the way they are substituted within the text of your code Questions? 23

Chapter 7: Preprocessing Directives

Chapter 7: Preprocessing Directives Chapter 7: Preprocessing Directives Outline We will only cover these topics in Chapter 7, the remain ones are optional. Introduction Symbolic Constants and Macros Source File Inclusion Conditional Compilation

More information

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

C for Engineers and Scientists: An Interpretive Approach. Chapter 7: Preprocessing Directives Chapter 7: Preprocessing Directives Introduction Preprocessing Affect program preprocessing and execution Capabilities Inclusion of additional C source files Definition of symbolic constants and macros

More information

OBJECT ORIENTED PROGRAMMING USING C++

OBJECT ORIENTED PROGRAMMING USING C++ OBJECT ORIENTED PROGRAMMING USING C++ Chapter 17 - The Preprocessor Outline 17.1 Introduction 17.2 The #include Preprocessor Directive 17.3 The #define Preprocessor Directive: Symbolic Constants 17.4 The

More information

Conditional Compilation

Conditional Compilation Conditional Compilation printf() statements cab be inserted code for the purpose of displaying debug information during program testing. Once the program is debugged and accepted as "working'', it is desirable

More information

Programming for Engineers C Preprocessor

Programming for Engineers C Preprocessor Programming for Engineers C Preprocessor ICEN 200 Spring 2018 Prof. Dola Saha 1 C Preprocessor The C preprocessor executes before a program is compiled. Some actions it performs are the inclusion of other

More information

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

Have examined process Creating program Have developed program Written in C Source code Preprocessing, Compiling, Assembling, and Linking Introduction In this lesson will examine Architecture of C program Introduce C preprocessor and preprocessor directives How to use preprocessor s directives

More information

Unit 4 Preprocessor Directives

Unit 4 Preprocessor Directives 1 What is pre-processor? The job of C preprocessor is to process the source code before it is passed to the compiler. Source Code (test.c) Pre-Processor Intermediate Code (test.i) Compiler The pre-processor

More information

fpp: Fortran preprocessor March 9, 2009

fpp: Fortran preprocessor March 9, 2009 fpp: Fortran preprocessor March 9, 2009 1 Name fpp the Fortran language preprocessor for the NAG Fortran compiler. 2 Usage fpp [option]... [input-file [output-file]] 3 Description fpp is the preprocessor

More information

A Fast Review of C Essentials Part II

A Fast Review of C Essentials Part II A Fast Review of C Essentials Part II Structural Programming by Z. Cihan TAYSI Outline Macro processing Macro substitution Removing a macro definition Macros vs. functions Built-in macros Conditional compilation

More information

Appendix A. The Preprocessor

Appendix A. The Preprocessor Appendix A The Preprocessor The preprocessor is that part of the compiler that performs various text manipulations on your program prior to the actual translation of your source code into object code.

More information

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

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Data Types Basic Types Enumerated types The type void Derived types

More information

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

C and C++ 2. Functions Preprocessor. Alan Mycroft C and C++ 2. Functions Preprocessor Alan Mycroft University of Cambridge (heavily based on previous years notes thanks to Alastair Beresford and Andrew Moore) Michaelmas Term 2013 2014 1 / 1 Functions

More information

Problem Solving and 'C' Programming

Problem Solving and 'C' Programming Problem Solving and 'C' Programming Targeted at: Entry Level Trainees Session 15: Files and Preprocessor Directives/Pointers 2007, Cognizant Technology Solutions. All Rights Reserved. The information contained

More information

3 PREPROCESSOR. Overview. Listing 3-0. Table 3-0.

3 PREPROCESSOR. Overview. Listing 3-0. Table 3-0. 3 PREPROCESSOR Listing 3-0. Table 3-0. Overview The preprocessor program (pp.exe) evaluates and processes preprocessor commands in your source files. With these commands, you direct the preprocessor to

More information

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

Macros and Preprocessor. CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang Macros and Preprocessor CGS 3460, Lecture 39 Apr 17, 2006 Hen-I Yang Previously Operations on Linked list (Create and Insert) Agenda Linked List (More insert, lookup and delete) Preprocessor Linked List

More information

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

Control flow and string example. C and C++ Functions. Function type-system nasties. 2. Functions Preprocessor. Alastair R. Beresford. Control flow and string example C and C++ 2. Functions Preprocessor Alastair R. Beresford University of Cambridge Lent Term 2007 #include char s[]="university of Cambridge Computer Laboratory";

More information

C Preprocessor. Prabhat Kumar Padhy

C Preprocessor. Prabhat Kumar Padhy C Preprocessor Prabhat Kumar Padhy 1 C Preprocessor? Creating C program, Compiling and Runnings. Create using some editor Compilation gcc test.c (or) gcc o test test.c Running./test C Preprocessor The

More information

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

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead. Chapter 9: Rules Chapter 1:Style and Program Organization Rule 1-1: Organize programs for readability, just as you would expect an author to organize a book. Rule 1-2: Divide each module up into a public

More information

Lecture 2: C Programming Basic

Lecture 2: C Programming Basic ECE342 Introduction to Embedded Systems Lecture 2: C Programming Basic Ying Tang Electrical and Computer Engineering Rowan University 1 Facts about C C was developed in 1972 in order to write the UNIX

More information

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

Macros in C/C++ Computer Science and Engineering College of Engineering The Ohio State University. Lecture 33 Macros in C/C++ Computer Science and Engineering College of Engineering The Ohio State University Lecture 33 Macro Definition Directive: #define #define Example: #define BUFF_SIZE 1000 A

More information

Errors During Compilation and Execution Background Information

Errors During Compilation and Execution Background Information Errors During Compilation and Execution Background Information Preprocessor Directives and Compilation #define - defines a macro, identified by . During compilation, all instances of

More information

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

Topic 6: A Quick Intro To C. Reading. goto Considered Harmful History Topic 6: A Quick Intro To C Reading Assumption: All of you know basic Java. Much of C syntax is the same. Also: Some of you have used C or C++. Goal for this topic: you can write & run a simple C program

More information

Contents Lecture 3. C Preprocessor, Chapter 11 Declarations, Chapter 8. Jonas Skeppstedt Lecture / 44

Contents Lecture 3. C Preprocessor, Chapter 11 Declarations, Chapter 8. Jonas Skeppstedt Lecture / 44 Contents Lecture 3 C Preprocessor, Chapter 11 Declarations, Chapter 8 Jonas Skeppstedt (js@cs.lth.se) Lecture 3 2014 1 / 44 C Preprocessor Predefined macros Macro replacement Conditional inclusion Source

More information

COMsW Introduction to Computer Programming in C

COMsW Introduction to Computer Programming in C 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

More information

Topic 6: A Quick Intro To C

Topic 6: A Quick Intro To C Topic 6: A Quick Intro To C Assumption: All of you know Java. Much of C syntax is the same. Also: Many of you have used C or C++. Goal for this topic: you can write & run a simple C program basic functions

More information

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

Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles. CS449 Fall 2017 Practical C Issues:! Preprocessor Directives, Multi-file Development, Makefiles CS449 Fall 2017 Multi-file Development Multi-file Development Why break code into multiple source files? Parallel development

More information

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22 COSC 2P91 Bringing it all together... Week 4b Brock University Brock University (Week 4b) Bringing it all together... 1 / 22 A note on practicality and program design... Writing a single, monolithic source

More information

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

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming 1 2 3 4 CMPE 013/L Pointers and Functions Gabriel Hugh Elkaim Spring 2013 Pointers and Functions Passing Pointers to Functions Normally, functions operate on copies of the data passed to them (pass by

More information

The C Preprocessor (and more)!

The C Preprocessor (and more)! The C Preprocessor (and more)! Peter Kristensen 2012-11-19 Peter Kristensen The C Preprocessor (and more)! Outline 1 C Pre Processor Compiler Assembler Linker Frontend 2 Simple directives Headers Macros

More information

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

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Basic C Programming Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Announcements Exam 1 (20%): Feb. 27 (Tuesday) Tentative Proposal Deadline:

More information

Intermediate Programming, Spring 2017*

Intermediate Programming, Spring 2017* 600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general

More information

CS6202 - PROGRAMMING & DATA STRUCTURES UNIT I Part - A 1. W hat are Keywords? Keywords are certain reserved words that have standard and pre-defined meaning in C. These keywords can be used only for their

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 STEPS OF GCC STEPS file.c Preprocessor Compiler file1.o file2.o Assembler Linker executable file PREPROCESSOR PREPROCESSOR The C preprocessor is a macro processor that is

More information

CS240: Programming in C

CS240: Programming in C CS240: Programming in C Lecture 6: Recursive Functions. C Pre-processor. Cristina Nita-Rotaru Lecture 6/ Fall 2013 1 Functions: extern and static Functions can be used before they are declared static for

More information

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance. 2.1 Introduction (No questions.) 2.2 A Simple Program: Printing a Line of Text 2.1 Which of the following must every C program have? (a) main (b) #include (c) /* (d) 2.2 Every statement in C

More information

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

Slide Set 5. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary Slide Set 5 for ENCM 339 Fall 2016 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary October 2016 ENCM 339 Fall 2016 Slide Set 5 slide 2/32

More information

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things. A Appendix Grammar There is no worse danger for a teacher than to teach words instead of things. Marc Block Introduction keywords lexical conventions programs expressions statements declarations declarators

More information

Full file at C How to Program, 6/e Multiple Choice Test Bank

Full file at   C How to Program, 6/e Multiple Choice Test Bank 2.1 Introduction 2.2 A Simple Program: Printing a Line of Text 2.1 Lines beginning with let the computer know that the rest of the line is a comment. (a) /* (b) ** (c) REM (d)

More information

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

C Programming. The C Preprocessor and Some Advanced Topics. Learn More about #define. Define a macro name Create function-like macros. C Programming The C Preprocessor and Some Advanced Topics June 03, 2005 Learn More about #define Define a macro name Create function-like macros to avoid the time might be longer #define SUM(i, j) i+j

More information

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune

Unit 1: Introduction to C Language. Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune Unit 1: Introduction to C Language Saurabh Khatri Lecturer Department of Computer Technology VIT, Pune Introduction to C Language The C programming language was designed by Dennis Ritchie at Bell Laboratories

More information

The C Pre Processor ECE2893. Lecture 18. ECE2893 The C Pre Processor Spring / 10

The C Pre Processor ECE2893. Lecture 18. ECE2893 The C Pre Processor Spring / 10 The C Pre Processor ECE2893 Lecture 18 ECE2893 The C Pre Processor Spring 2011 1 / 10 The C Pre Processor 1 The C pre processor is the very first step in any C or C++ program compilation. 2 It is a very

More information

Decision Making -Branching. Class Incharge: S. Sasirekha

Decision Making -Branching. Class Incharge: S. Sasirekha Decision Making -Branching Class Incharge: S. Sasirekha Branching The C language programs presented until now follows a sequential form of execution of statements. Many times it is required to alter the

More information

Modules:Context-Sensitive Keyword

Modules:Context-Sensitive Keyword Document Number: P0924r1 Date: 2018-11-21 To: SC22/WG21 EWG Reply to: Nathan Sidwell nathan@acm.org / nathans@fb.com Re: Merging Modules, p1103r2 Modules:Context-Sensitive Keyword Nathan Sidwell The new

More information

Language Reference Manual simplicity

Language Reference Manual simplicity Language Reference Manual simplicity Course: COMS S4115 Professor: Dr. Stephen Edwards TA: Graham Gobieski Date: July 20, 2016 Group members Rui Gu rg2970 Adam Hadar anh2130 Zachary Moffitt znm2104 Suzanna

More information

MODULE 10 PREPROCESSOR DIRECTIVES

MODULE 10 PREPROCESSOR DIRECTIVES MODULE 10 PREPROCESSOR DIRECTIVES My Training Period: hours Abilities Able to understand and use #include. Able to understand and use #define. Able to understand and use macros and inline functions. Able

More information

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

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above P.G.TRB - COMPUTER SCIENCE Total Marks : 50 Time : 30 Minutes 1. C was primarily developed as a a)systems programming language b) general purpose language c) data processing language d) none of the above

More information

The C Preprocessor. Richard M. Stallman, Zachary Weinberg. For gcc version (GCC)

The C Preprocessor. Richard M. Stallman, Zachary Weinberg. For gcc version (GCC) The C Preprocessor For gcc version 5.4.0 (GCC) Richard M. Stallman, Zachary Weinberg Copyright c 1987-2015 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or modify this document

More information

The New C Standard (Excerpted material)

The New C Standard (Excerpted material) The New C Standard (Excerpted material) An Economic and Cultural Derek M. Jones derek@knosof.co.uk Copyright 2002-2008 Derek M. Jones. All rights reserved. 1854 preprocessor directives syntax preprocessing-file:

More information

ISO/IEC JTC1/SC22/WG5 N1247

ISO/IEC JTC1/SC22/WG5 N1247 To: WG5 and X3J3 From: Larry Rolison Date: 24 January 1997 Subject: Proposed alternative draft CD to N 1243 ISO/IEC JTC1/SC22/WG5 N1247 The model of conditional compilation presented in N1243 suffers from

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #8 Feb 27 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline More c Preprocessor Bitwise operations Character handling Math/random Review for midterm Reading: k&r ch

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

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

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #54. Organizing Code in multiple files Introduction to Programming in C Department of Computer Science and Engineering Lecture No. #54 Organizing Code in multiple files (Refer Slide Time: 00:09) In this lecture, let us look at one particular

More information

CMPE-013/L. Expressions and Control

CMPE-013/L. Expressions and Control CMPE-013/L Expressions and Control Gabriel Hugh Elkaim Winter 2015 #include Directive Three ways to use the #include directive: Syntax #include Look for file in the compiler search path The compiler

More information

Tutorial No. 2 - Solution (Overview of C)

Tutorial No. 2 - Solution (Overview of C) Tutorial No. 2 - Solution (Overview of C) Computer Programming and Utilization (2110003) 1. Explain the C program development life cycle using flowchart in detail. OR Explain the process of compiling and

More information

The C Preprocessor. Richard M. Stallman, Zachary Weinberg. For gcc version (pre-release) (GCC)

The C Preprocessor. Richard M. Stallman, Zachary Weinberg. For gcc version (pre-release) (GCC) The C Preprocessor For gcc version 8.0.0 (pre-release) (GCC) Richard M. Stallman, Zachary Weinberg Copyright c 1987-2017 Free Software Foundation, Inc. Permission is granted to copy, distribute and/or

More information

CSCI 171 Chapter Outlines

CSCI 171 Chapter Outlines Contents CSCI 171 Chapter 1 Overview... 2 CSCI 171 Chapter 2 Programming Components... 3 CSCI 171 Chapter 3 (Sections 1 4) Selection Structures... 5 CSCI 171 Chapter 3 (Sections 5 & 6) Iteration Structures

More information

Computer Programming

Computer Programming Computer Programming Introduction Marius Minea marius@cs.upt.ro http://cs.upt.ro/ marius/curs/cp/ 26 September 2017 Course goals Learn programming fundamentals no prior knowledge needed for those who know,

More information

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

C: Program Structure. Department of Computer Science College of Engineering Boise State University. September 11, /13 Department of Computer Science College of Engineering Boise State University September 11, 2017 1/13 Scope Variables and functions are visible from the point they are defined until the end of the source

More information

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

COMP322 - Introduction to C++ Lecture 02 - Basics of C++ COMP322 - Introduction to C++ Lecture 02 - Basics of C++ School of Computer Science 16 January 2012 C++ basics - Arithmetic operators Where possible, C++ will automatically convert among the basic types.

More information

CSCI 2132 Software Development. Lecture 8: Introduction to C

CSCI 2132 Software Development. Lecture 8: Introduction to C CSCI 2132 Software Development Lecture 8: Introduction to C Instructor: Vlado Keselj Faculty of Computer Science Dalhousie University 21-Sep-2018 (8) CSCI 2132 1 Previous Lecture Filename substitution

More information

Unit 7. Functions. Need of User Defined Functions

Unit 7. Functions. Need of User Defined Functions Unit 7 Functions Functions are the building blocks where every program activity occurs. They are self contained program segments that carry out some specific, well defined task. Every C program must have

More information

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

JTSK Programming in C II C-Lab II. Lecture 1 & 2 JTSK-320112 Programming in C II C-Lab II Lecture 1 & 2 Xu (Owen) He Spring 2018 Slides modified from Dr. Kinga Lipskoch Who am I? Owen - PhD Candidate in Computer Science Contact details: Office: Research

More information

Pointers and Functions Passing Pointers to Functions CMPE-013/L. Pointers and Functions. Pointers and Functions Passing Pointers to Functions

Pointers and Functions Passing Pointers to Functions CMPE-013/L. Pointers and Functions. Pointers and Functions Passing Pointers to Functions CMPE-013/L Gabriel Hugh Elkaim Winter 2014 Passing Pointers to Functions Normally, functions operate on copies of the data passed to them (pass by value) int x = 2, y = 0; Value of variable passed to function

More information

ME 461 C review Session Fall 2009 S. Keres

ME 461 C review Session Fall 2009 S. Keres ME 461 C review Session Fall 2009 S. Keres DISCLAIMER: These notes are in no way intended to be a complete reference for the C programming material you will need for the class. They are intended to help

More information

CODE TIME TECHNOLOGIES. Abassi RTOS MISRA-C:2004. Compliance Report

CODE TIME TECHNOLOGIES. Abassi RTOS MISRA-C:2004. Compliance Report CODE TIME TECHNOLOGIES Abassi RTOS MISRA-C:2004 Compliance Report Copyright Information This document is copyright Code Time Technologies Inc. 2012. All rights reserved. No part of this document may be

More information

Lesson 5: Functions and Libraries. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Lesson 5: Functions and Libraries. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology Lesson 5: Functions and Libraries 1 Functions 2 Overview Function is a block of statements which performs a specific task, and can be called by others Each function has a name (not identical to any other),

More information

Model Viva Questions for Programming in C lab

Model Viva Questions for Programming in C lab Model Viva Questions for Programming in C lab Title of the Practical: Assignment to prepare general algorithms and flow chart. Q1: What is a flowchart? A1: A flowchart is a diagram that shows a continuous

More information

Lecture 3. More About C

Lecture 3. More About C Copyright 1996 David R. Hanson Computer Science 126, Fall 1996 3-1 Lecture 3. More About C Programming languages have their lingo Programming language Types are categories of values int, float, char Constants

More information

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

INDEX. Figure I-0. Listing I-0. Table I-0. Symbols.DIRECTIVE (see Assembler directives)? preprocessor operator 3-34 I INDEX Figure I-0. Listing I-0. Table I-0. Symbols.DIRECTIVE (see Assembler directives)? preprocessor operator 3-34 Numerics Assembler command-line switch -21 2-21 A Address alignment 2-39 Address of

More information

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath UNIT - I Introduction to C Programming Introduction to C C was originally developed in the year 1970s by Dennis Ritchie at Bell Laboratories, Inc. C is a general-purpose programming language. It has been

More information

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

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco CS 326 Operating Systems C Programming Greg Benson Department of Computer Science University of San Francisco Why C? Fast (good optimizing compilers) Not too high-level (Java, Python, Lisp) Not too low-level

More information

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

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3). cs3157: another C lecture (mon-21-feb-2005) C pre-processor (1). today: C pre-processor command-line arguments more on data types and operators: booleans in C logical and bitwise operators type conversion

More information

More on C programming

More on C programming Applied mechatronics More on C programming Sven Gestegård Robertz sven.robertz@cs.lth.se Department of Computer Science, Lund University 2017 Outline 1 Pointers and structs 2 On number representation Hexadecimal

More information

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

#include. Practical C Issues: #define. #define Macros. Example. #if #include Practical C Issues: Preprocessor Directives, Typedefs, Multi-file Development, and Makefiles Jonathan Misurda jmisurda@cs.pitt.edu Copies the contents of the specified file into the current file

More information

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

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS Syllabus: Pointers and Preprocessors: Pointers and address, pointers and functions (call by reference) arguments, pointers and arrays, address arithmetic, character pointer and functions, pointers to pointer,initialization

More information

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

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009 Compiler Theory (GCC the GNU Compiler Collection) Sandro Spina 2009 GCC Probably the most used compiler. Not only a native compiler but it can also cross-compile any program, producing executables for

More information

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5 C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5 1. Pointers As Kernighan and Ritchie state, a pointer is a variable that contains the address of a variable. They have been

More information

The C Preprocessor. for GCC version Richard M. Stallman Zachary Weinberg

The C Preprocessor. for GCC version Richard M. Stallman Zachary Weinberg The C Preprocessor for GCC version 4.0.2 Richard M. Stallman Zachary Weinberg Copyright c 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software

More information

Compiler, Assembler, and Linker

Compiler, Assembler, and Linker Compiler, Assembler, and Linker Minsoo Ryu Department of Computer Science and Engineering Hanyang University msryu@hanyang.ac.kr What is a Compilation? Preprocessor Compiler Assembler Linker Loader Contents

More information

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW IMPORTANT QUESTIONS IN C FOR THE INTERVIEW 1. What is a header file? Header file is a simple text file which contains prototypes of all in-built functions, predefined variables and symbolic constants.

More information

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p. Lecture 10 p.1 Faculty of Computer Science, Dalhousie University CSCI 2132 Software Development Lecture 10: Formatted Input and Output 27-Sep-2017 Location: Goldberg CS 127 Time: 14:35 15:25 Instructor:

More information

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

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah Lecturer Department of Computer Science & IT University of Balochistan 1 Outline p Introduction p Program development p C language and beginning with

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler

More information

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4 Jim Lambers ENERGY 211 / CME 211 Autumn Quarter 2008-09 Programming Project 4 This project is due at 11:59pm on Friday, October 31. 1 Introduction In this project, you will do the following: 1. Implement

More information

Course Outline Introduction to C-Programming

Course Outline Introduction to C-Programming ECE3411 Fall 2015 Lecture 1a. Course Outline Introduction to C-Programming Marten van Dijk, Syed Kamran Haider Department of Electrical & Computer Engineering University of Connecticut Email: {vandijk,

More information

Preprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name.

Preprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name. Unit-III Preprocessor: The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it

More information

A Crash Course in C. Steven Reeves

A Crash Course in C. Steven Reeves A Crash Course in C Steven Reeves This class will rely heavily on C and C++. As a result this section will help students who are not familiar with C or who need a refresher. By the end of this section

More information

Columbus Schema for C/C++ Preprocessing

Columbus Schema for C/C++ Preprocessing Columbus Schema for C/C++ Preprocessing László Vidács, Rudolf Ferenc and Árpád Beszédes Department of Software Engineering University of Szeged, Hungary Introduction C/C++ Preprocessor useful and widely

More information

Programming for Engineers Introduction to C

Programming for Engineers Introduction to C Programming for Engineers Introduction to C ICEN 200 Spring 2018 Prof. Dola Saha 1 Simple Program 2 Comments // Fig. 2.1: fig02_01.c // A first program in C begin with //, indicating that these two lines

More information

emacs customisation ($HOME/.emacs)

emacs customisation ($HOME/.emacs) emacs customisation ($HOME/.emacs) slide 1 ;; F5 loads in the.gdbinit file ready for debugging ioquake (defun my-find-file-debug () "load a file" (interactive) (find-file (concat (getenv "HOME") "/Sandpit/ioquake-latest/ioquake3/.gdbinit")))

More information

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

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 2. Spring 2016 Oregon State University School of Electrical Engineering and Computer Science CS 261 Recitation 2 Spring 2016 Outline Programming in C o Headers o Structures o Preprocessor o Pointers Programming Assignment

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

SOFTWARE ARCHITECTURE 5. COMPILER

SOFTWARE ARCHITECTURE 5. COMPILER 1 SOFTWARE ARCHITECTURE 5. COMPILER Tatsuya Hagino hagino@sfc.keio.ac.jp slides URL https://vu5.sfc.keio.ac.jp/sa/ 2 Programming Language Programming Language Artificial language to express instructions

More information

CSE 374 Programming Concepts & Tools

CSE 374 Programming Concepts & Tools CSE 374 Programming Concepts & Tools Hal Perkins Fall 2017 Lecture 8 C: Miscellanea Control, Declarations, Preprocessor, printf/scanf 1 The story so far The low-level execution model of a process (one

More information

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

More information

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS Contents Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS 1.1. INTRODUCTION TO COMPUTERS... 1 1.2. HISTORY OF C & C++... 3 1.3. DESIGN, DEVELOPMENT AND EXECUTION OF A PROGRAM... 3 1.4 TESTING OF PROGRAMS...

More information

Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

More information

This exam is to be taken by yourself with closed books, closed notes, no calculators.

This exam is to be taken by yourself with closed books, closed notes, no calculators. Student ID CSE 5A Name Final Signature Fall 2004 Page 1 (12) cs5a This exam is to be taken by yourself with closed books, closed notes, no calculators. Page 2 (33) Page 3 (32) Page 4 (27) Page 5 (40) Page

More information

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University Fundamental Data Types CSE 130: Introduction to Programming in C Stony Brook University Program Organization in C The C System C consists of several parts: The C language The preprocessor The compiler

More information

Unit 3. Operators. School of Science and Technology INTRODUCTION

Unit 3. Operators. School of Science and Technology INTRODUCTION INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

More information