CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Computer Programming w/ Eng. Applications
Introduction to C Programming
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Basic Elements of C++ Chapter 2.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Computer Science 210 Computer Organization Introduction to C.
Expressions, Data Conversion, and Input
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
Operator Overloading and Type Conversions
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
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.
Introduction to Programming
Chapter 21: The Standard Library Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 21 The Standard Library.
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
CS115 FALL Senem KUMOVA-METİN1 The Fundamental Data Types CHAPTER 3.
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
Recap……Last Time [Variables, Data Types and Constants]
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Week 1 Lecture 2SE1SA51 Introducing C SE1SA5 Sue Walmsley.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
BASIC C PROGRAMMING LANGUAGE TUTORIAL infobizzs.com.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Variables, Operators, and Expressions
Numbers in ‘C’ Two general categories: Integers Floats
Arithmetic Expressions
Chapter Topics The Basics of a C++ Program Data Types
Computer Science 210 Computer Organization
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
Functions, Part 2 of 2 Topics Functions That Return a Value
Basic Elements of C++.
Variables, Expressions, and IO
C Short Overview Lembit Jürimägi.
Programming Fundamentals Lecture #7 Functions
User-Defined Functions
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Data Type.
Basic Elements of C++ Chapter 2.
Computer Science 210 Computer Organization
Functions Declarations CSCI 230
Introduction to the C Language
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Data Type.
Functions Chapter 3 of the text Motivation:
A First Book of ANSI C Fourth Edition
Functions, Part 2 of 3 Topics Functions That Return a Value
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
WEEK-2.
Data Type.
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions that return a value
Presentation transcript:

CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004

C Pre-processor C has a pre-processor which runs prior to compilation. Directives to the pre- processor are prefixed with the character # which must appear in the first column of text. The directive #include is used for including text that is contained in another file. Two notations are available, one for including system files and one for including user files. Other uses of the pre-processor are for declaring constants and for macros.

include directories Examples of each notation are given in turn below: #include #include "MyHeaderFile.h“ The form ( include<> ) causes a search for files in certain implementation- specific (include) directories, for example /usr/local/lib The second form ( include””) searches in local directories including the current directory. You can give absolute or relative paths.

Macros The pre-processor also supplies a mechanism for macro substitution with the #define directive. The general form is: #define [ ( ) ] Simple examples are given below: #define GOLDEN_RATIO #define ADD(x, y) (x)+(y) #define SWAP(type, num1, num2){ \ type temp; \ temp = num1; \ num1 = num2; \ num2 = temp; \ }

Macro Example Notes: It is conventional to use uppercase letters for macro names. The \ character is needed at the end of every line to extend the macro over more than one line. Here we can see the use of a macro: int a=5,b=7; SWAP(int,a,b) The pre-processor will replace the source code line SWAP(int,a,b) with the block of code above substituting int for type, a for num1, and b for num2.

Macros v. Functions Macros replace the short hand text with the longer definition. A function definition occurs only once whereas if a macro is used twenty times in your code, in all twenty instances the longer definition will be substituted in, and thus your code will grow. A small macro is convenient for getting a basic piece of functionality implemented. A rule of thumb is to employ macros for small tasks (like SQUARE, MIN, and MAX), but use functions for anything complex. If a macro is used frequently in your code, it would also result in slower compile times, because the pre-processor has to perform all those text replacements.

The typedef facility C has a facility for naming types with user-defined identifiers. For example we could declare two long int variables l1 and l2 as follows: typedef long int lint; lint l1, l2; Thus the typedef mechanism allows a programmer to associate a known type with a more meaningful identifier, e.g. typedef int speed; speed max_speed_kph= 205;

Explicit type conversions - casts If you want to convert a value from a variable of one type to a variable of another type you can use a cast. For example, to convert an int into a float, you can use the following code: int anInteger = 6; float aFloat; aFloat = (float)anInteger; The general form is to give the target type in parentheses before the source variable.

Implicit type conversions An implicit type conversion is a type conversion that is performed by the compiler. C had an extensive set of rules for implicit type conversion. Here is a simple example: int i = 2; float d = 1.32; printf(“Result is %f”, d + i); In the expression above, as in all arithmetical expressions, the target type becomes the largest type in the expression, thus the target type is float and the 2 is converted to 2.0. The result of the expression is 3.32.

Intro. to Standard Library We’ve made use of the standard C library already by including the appropriate header files (names end in.h). Here are listed some of the header files. stdio.h - standard input and output functions (like printf, scanf, etc.) stdlib.h - memory management (see later) and communication with environment (abort, exit) limits.h - compiler imposed limits on various data types math.h - all sorts of maths functions, sin, cos, tan, logarithms, powers, square- roots, etc. Employs type double throughout. string.h - functions for manipulating strings, searching for things in strings etc. (see later) ctype.h - useful functions for testing characters, e.g. “is it alphabetic?” etc. (see later)

Integer Limits The header file limits.h contains constants defined to show what limits a particular compiler has on the size of integer types, e.g. it contains: INT_MAX, INT_MIN, ULONG_MAX, etc. Consider this situation: int a = 800, b = 700, c; c = a * b; Clearly the arithmetic result is 560,000 but an int may not be big enough to hold this resulting in an overflow. C has no clear rules for this. It is necessary to raise the size of the receiving location (‘c’) to, say, type long int and one of the operands to ensure the calculation is done with enough space. long int c; int a=800, b = 700; c = (long int)a * b;

Divide By Zero Consider division by zero: int a = 1, b = 0, c; c = a/b; Such a computation will result in a run time error: It is therefore sensible to look out for attempts to divide by zero and block them: int a = 0, b = 800, c; if (a == 0){ printf("divide by zero error!"); exit(1); } c = b/a; note: exit() is from