C Hints and Tips The preprocessor and other fun toys.

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

ARDUINO CLUB Session 1: C & An Introduction to Linux.
Pemrograman C Risanuri Hidayat. Why C  Compact, fast, and powerful  “Mid-level” Language  Standard for program development (wide acceptance)  It is.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Introduction: C Pointers Day 2 These Slides NOT From Text.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 17 - The Preprocessor Outline 17.1Introduction 17.2The #include Preprocessor Directive 17.3The.
CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
© 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.
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
 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.
CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Windows Programming Lecture 05. Preprocessor Preprocessor Directives Preprocessor directives are instructions for compiler.
1 Further C  Multiple source code file projects  Structs  The preprocessor  Pointers.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
The Structure of a C++ Program. Outline 1. Separate Compilation 2. The # Preprocessor 3. Declarations and Definitions 4. Organizing Decls & Defs into.
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.
Input, Output, and Processing
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Chapter 13 C Preprocessor C How to Program, 8/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved.
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.
CSCI 171 Presentation 8 Built-in Functions, Preprocessor Directives, and Macros.
1 Preprocessor –How it works File Inclusion: #include Macro Definition: #define Predefined macros –__LINE__, __FILE__, __DATE__, __TIME__ Conditional Compilation.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Preprocessing Lecture 12 April 7, 2005.
 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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
THE PREPROCESSOR
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
C OMPUTER P ROGRAMMING 1 Input and Variables. I/O S TATEMENTS : I NPUT & V ARIABLES Input is the term used to describe the transfer of information from.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 13 - The Preprocessor Outline 13.1Introduction 13.2The #include Preprocessor Directive 13.3The.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
1 Object-Oriented Programming -- Using C++ Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
C PREPROCESSOR. Introduction  It is a program that processes our source program before it is passed to the compiler.  Preprocessor commands (often known.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
C language + The Preprocessor. + Introduction The preprocessor is a program that processes that source code before it passes through the compiler. It.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
13 C Preprocessor.
Chapter 2 Introduction to C++ Programming
CSC201: Computer Programming
Chapter 2, Part I Introduction to C Programming
Chapter 13 - The Preprocessor
14. THE PREPROCESSOR.
Pre-processor Directives
C Arrays.
Introduction to C++ Programming
Register Variables Declaring a variable as a "register" variable is an advisory to the compiler to keep the normal location of the variable in a register,
C Preprocessor(CPP).
Introduction to Programming - 1
Conditional Compilation
Presentation transcript:

C Hints and Tips The preprocessor and other fun toys

Operators and Types Most C operators can work with many different types of numbers +, -, *, /, %, >>, ++, etc.. int, double, float, char, etc.. Understanding how the compiler interprets your instructions is crucial! it will perform some conversions, but they might not always make sense (to you!) Like the rest of programming, experience is essential

Type Conversions Types in C can be thought of as having a heirarchy char -> int -> float -> double ie, double is the ‘broadest’ type, char is the ‘narrowest’ C will convert variables to broader types in certain conditions when the other operand of the operator is of a broader type when passing to a function which takes a larger type It will NOT convert in other situations! some compilers will shrink values to fit into narrower types, but data loss may occur It will ONLY consider the current operator, NOT any future operators!! this means that unexpected results can be obtained in some very common circumstances..

Common Errors Consider the following code fragment: int a = 16 ; double d ; d = a / 3 ; What will the final value of d be?!? ? 5? something else? Keep in mind that ‘/’ and ‘=‘ are totally separate operators when determining what, if any, type conversions to perform on the operands of ‘a/3’, the ‘=‘ operator which will follow is ignored!

More Examples.. What will be the result of these statements? char c = ‘a’ ; int i = a << 8 ; double d = 15 / 3 ; d = 15 / 3.0 ;// is this different?! i = * 4 ;// will this work? You can write a simple program with printf statements after every line to display these values..

The C Pre-Processor Directives The C pre-processor is a program which is run on a C source file before compiling Its main function is to modify the program in certain ways removes comments substitutes certain identifiers includes other files It is a very powerful tool, and knowing how to use it is an essential C programming skill!

Using Pre-Processor Directives Unlike normal C statements, spacing is important for pre- processor directives each directive should be on a single line, with the first character always ‘#’ directives are terminated by a newline character, and should NOT have a ‘;’ at the end! The pre-processor is automatically run when you compile the program the output is sent straight to the ‘real’ compiler You can also run the pre-processor separately with the command ‘cpp’ test this on one of your C source files, and have a look at the result, it might explain a few things!

#include Directives The most commonly used pre-processor directive is #include inserts the named file at the current position <> brackets mean the file is in the system directories “” quotes mean that the filename is relative to the current directory Examples: #include includes the system header /usr/include/stdio.h #include “blah.h”includes the file blah.h in the current directory #include “headers/blah.c”includes the file blah.h in the headers subdirectory Almost all #include statements you will use will be system header files, so <> are used

#define Directive #define is used to declare constants and macros Syntax (constants): #define NAME replacement text for the remainder of the file, NAME is replaced with whatever follows on the #define line #define statements should be used for all constants for which a name is more meaningful than a value The macro form of #define is beyond the scope of this course..

#define Example #include #define ARRAY_SIZE 50 int main ( void ){ int ar[ARRAY_SIZE], i ; for ( i = 0 ; i < ARRAY_SIZE ; i++ ){ … } The value 50 is substituted for ARRAY_SIZE wherever it appears…

Why Use #define? Using constants is considered good programming practice, for a number of reasons: it makes your code much more readable changing the value requires on ONE change, instead of possibly hundreds throughout your program you do not have to remember the actual values when programming consider colour tables, where each colour has a number – it is MUCH easier to remember BLUE than a random pallete number.. A good rule of thumb: if you use a value more than once (with the same meaning), #define it! Many libraries (that you #include) will have many constants defined already: has M_PI defined!

#undef Directive The opposite of #define Clears a previously #defined constant or macro

#if Directive Simple control statement, allowing very basic comparisons to be made Three basic forms: #ifdef perform action if constant/macro exists #ifndef perform action if constant/macro does NOT exist #if CONST == VALUE perform action if constant has the given value The actions are all the lines after the #if directive, until the #endif line is encountered these can be C statements, other pre-processor directives, or anything else.. Basically, the lines between #if and #endif are only included if the condition of the #if directive are met

#if Example #include #define DEBUG 0 int main ( void ){ #if DEBUG == 1 printf ( “The program has started\n” ) ; #endif } The printf statement is ONLY run if DEBUG is equal to 1. In this example it is not, so nothing is printed! This is a very useful way to develop programs, as it allows for messages to be displayed when writing the code, then easily turned off for the ‘real’ program.

Warnings and Compiler Messages The C compiler is very forgiving many ‘errors’ are not treated as such, and compilation is allowed to continue In some cases, ‘warnings’ are generated indicate that your code has done something a little naughty, but not serious enough to warrant an error Warning messages should not be ignored they often mean you have made a mistake which will cause the program to crash when run, or do the wrong thing! the only warnings you can safely ignore are the ones you expect, when you know your code is correct