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

Similar documents
Programming for Engineers C Preprocessor

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

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

The C language. Introductory course #1

Appendix A. The Preprocessor

Chapter 7: Preprocessing Directives

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

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Unit 4 Preprocessor Directives

OBJECT ORIENTED PROGRAMMING USING C++

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

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

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

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Problem Solving and 'C' Programming

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

BSM540 Basics of C Language

Topic 6: A Quick Intro To C

Introduction to C. Systems Programming Concepts

A Fast Review of C Essentials Part II

Fundamentals of Programming Session 20

Introduction: The Unix shell and C programming

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

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

BİL200 TUTORIAL-EXERCISES Objective:

Principles of C and Memory Management

Lecture 3. More About C

Goals of this Lecture

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Fundamental of Programming (C)


Computational Methods of Scientific Programming Fall 2007

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

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

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Conditional Compilation

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

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

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Programming and Data Structures

CSI33 Data Structures

Chapter 11 Introduction to Programming in C

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

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

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

A Fast Review of C Essentials Part I

Chapter 11 Introduction to Programming in C

Solutions to Assessment

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

Chapter 4 Homework Individual/Team (1-2 Persons) Assignment 15 Points

C: How to Program. Week /Mar/05

Fundamentals of Programming Session 19

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

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

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

Learning C Language. For BEGINNERS. Remember! Practice will make you perfect!!! :D. The 6 th week / May 24 th, Su-Jin Oh

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

Technical Questions. Q 1) What are the key features in C programming language?

EL6483: Brief Overview of C Programming Language

Subject: Fundamental of Computer Programming 2068

CSCI 171 Chapter Outlines

Essar Placement Paper

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

Introduction to the C Programming Language

Arrays. Example: Run the below program, it will crash in Windows (TurboC Compiler)

Name Roll No. Section

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

MODULE 10 PREPROCESSOR DIRECTIVES

EECE.2160: ECE Application Programming Fall 2017

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

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

Programming and Data Structure

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Fundamentals of Programming

More examples for Control statements

Princeton University COS 333: Advanced Programming Techniques A Subset of C90

JTSK Programming in C II C-Lab II. Lecture 3 & 4

High Performance Programming Programming in C part 1

Fundamentals of Programming

(2½ Hours) [Total Marks: 75

CGS 3460 Summer 07 Midterm Exam

Stream Model of I/O. Basic I/O in C

211: Computer Architecture Summer 2016

ME 461 C review Session Fall 2009 S. Keres

Question Bank (SPA SEM II)

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

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

Errors During Compilation and Execution Background Information

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

EL2310 Scientific Programming

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

Transcription:

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 int sum; sum = SUM(10, 20); printf("%d\n", sum);

Precedence of All the C Operators ( ) [ ] -> >.! ~ + - ++ -- (type cast) * & sizeof * / % + - << >> < <= > >= ==!= & ^ &&? : = += -= = *= /= etc. Highest Lowest Sample : #define #define MAX(i, j) ((i)>(j))? (i) : (j) printf("max(1, 2) = %d\n", MAX(1, 2)); printf("max(1, -1) = %d\n", MAX(1, -1)); printf("max(100 && -1, 0) = %d\n", MAX(100 && -1, 0));

Result : #define Learn More about #include #include <filename> search a special directory devoted to the standard header files #include filename searching a current working directory header files that you create #include "stdlib.h" printf("this is a random number: %d\n", rand());

Understand Conditional Compilation #if #if #else #elif #ifdef #ifndef parts of the source code of a program to be selectively compiled constant-expression: no variables may be used!! #if constant-expression #if constant-expression #else #if constant-expression-1 #elif constant-expression-2 #elif constant-expression-3.. #ifdef macro-name #ifndef macro-name Sample : #ifdef, #include <string.h> #define DEBUG int i, j; char buf[256], buf2[256]; printf("enter a string: "); scanf("%s", buf); #ifdef DEBUG printf("buf: %s\n", buf); i = strlen(buf); for(j=0; j<i; j++) buf2[j]=buf[i-1-j]; buf2[j]=0; #ifdef DEBUG printf("buf2: %s\n", buf2);

Result : #ifdef, Learn about #error #error error-message Stop compilation and issue the error-message number of line, name of file, not enclosed between quotes principal use debugging int i; i = 10; #error This is an error message. printf("%d\n", i); This line will not be compiled

Learn about #undef undefine a macro name #undef macro-name #define DEBUG #ifdef DEBUG printf("debug is defined.\n"); #undef DEBUG #ifdef DEBUG printf("this line is not compiled.\n"); Learn about #line #line line-num filename change the number of line and the name of the source file currently being compiled principal use debugging and managing large projects #line 1000 "myporg.c" #error Check the line number and file name.

Examine C s Built-In Macros Five predefined macro names at least LINE : (integer value) line number of the source line currently being complied FILE : (string) name of file currently being complied DATE : (string) current system date month/day/year TIME : (string) time the compilation of a program began hours:minutes:seconds STDC : (0 or 1) value 1 if the compiler conforms to the ANSI standard Sample : C s Built-In Macros printf("compiling %s, line %d, on %s, at %s\n", FILE, LINE, DATE, TIME );

Sample : #line #line 1000 "myporg.c" printf("compiling %s, line %d, on %s, at %s\n", FILE, LINE, DATE, TIME ); Use the # Operator Turn the argument of a function-like macro into a quoted string #define MKSTRING(str) # str int value; value = 10; printf("%s is %d\n", MKSTRING(value), value);

Use the ## Operator Concatenate two identifiers #define OUTPUT(i) printf("%d %d\n", i ## 1, i ## 2) int count1, count2; int i1, i2; count1 = 10; count2 = 20; i1 = 99; i2 = -10; OUTPUT(count); OUTPUT(i); Sample: ## Operator #define JOIN(a, b) a ## b printf("%s\n", JOIN("one ", "two"));

Understand Function Pointers Variable that contains the address of the entry point to a function int sum(int a, int b); int (*p)(int x, int y); int result; p = sum; result = (*p)(10, 20); // result = p(10, 20); printf("result = %d\n", result); int sum(int a, int b) return a+b; Sample: Function Pointers (1) float add(float a, float b); float subtract(float a, float b); float multiply(float a, float b); float divide(float a, float b); float (*op[4])(float x, float y) = add, subtract, multiply, divide ; float i, j, result; int k; printf("enter two numbers: "); scanf("%f%f", &i, &j); printf("[0] Add, [1] Subtract, [2] Multiply, [3] Divide\n"); do printf("enter number of operation: "); scanf("%d", &k); while( k<0 k>3 );

Sample: Function Pointers (2) result = (*op[k])(i, j); printf("result = %f\n", result); float add(float a, float b) return a+b; float subtract(float a, float b) return a-b; float multiply(float a, float b) return a*b; float divide(float a, float b) if(b) return a/b; Result: Function Pointers