Week 2 - Friday CS222.

Slides:



Advertisements
Similar presentations
C Programming Day 1 based upon Practical C Programming by Steve Oualline CS550 Operating Systems.
Advertisements

Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Structure of a C program
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?
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
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.
CS 161 Introduction to Programming and Problem Solving Chapter 13 C++ Preprocessor Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
Computer Science 210 Computer Organization Introduction to C.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
C Programming Lecture 4 : Variables , Data Types
Week 3 - Monday.  What did we talk about last time?  Math library  Preprocessor directives  Lab 2.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Week 2 - Friday.  What did we talk about last time?  Base systems  C literals  Representations in memory.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Compiler Directives. The C Preprocessor u The C preprocessor (cpp) changes your source code based on instructions, or preprocessor directives, embedded.
© Oxford University Press All rights reserved. CHAPTER 10 THE PREPROCESSOR DIRECTIVE.
Week 1 Lecture 2SE1SA51 Introducing C SE1SA5 Sue Walmsley.
Week 4 - Monday.  What did we talk about last time?  Precedence  Selection statements  Loops  Lab 3.
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Chapter 1.2 Introduction to C++ Programming
Arithmetic Expressions
Chapter 1.2 Introduction to C++ Programming
User-Written Functions
The Machine Model Memory
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Week 3 - Monday CS222.
Computer Science 210 Computer Organization
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Introduction to Python
Week 3 - Friday CS222.
Chapter 2 - Introduction to C Programming
Announcements Final Exam will be on August 19 at 16:00
C Programming Tutorial – Part I
Chapter 13 - The Preprocessor
Week 5 - Monday CS222.
COSC 220 Computer Science II
Functions Separate Compilation
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
C Short Overview Lembit Jürimägi.
Pointers.
Chapter 2 - Introduction to C Programming
Week 4 - Monday CS222.
Computer Science 210 Computer Organization
Preprocessor C program → Modified C program → Object Code
Announcements Midterm2 Grades to be announced NEXT Monday
Chapter 2 - Introduction to C Programming
Preprocessor.
Program Breakdown, Variables, Types, Control Flow, and Input/Output
Chapter 2 - Introduction to C Programming
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
CS150 Introduction to Computer Science 1
C Preprocessor Seema Chandak.
Chapter 2 - Introduction to C Programming
Variables Lesson Outline
Introduction to C Programming
SPL – PS1 Introduction to C++.
INTRODUCTION TO C.
Preprocessor Directives and Macros Chapter 21
Week 5 - Wednesday CS222.
Presentation transcript:

Week 2 - Friday CS222

Last time What did we talk about last time? Two's complement Floating point representation Math library

Questions?

Project 1

Quotes It ain't what you don't know that gets you into trouble. It's what you know for sure that just ain't so. Mark Twain

Preprocessor Directives

Preprocessor directives There are preprocessor directives which are technically not part of the C language These are processed before the real C compiler becomes involved The most important of these are #include #define Conditional compilation directives

#include You have already used #include before #include <stdio.h> It can be used to include any other file Use angle brackets (< >) for standard libraries Use quotes (" ") for anything else It literally pastes the file into the document where the #include directive is Never #include .c files (executable code), only .h files (definitions and prototypes) It is possible to have a circular include problem

#define The primary way to specify constants in C is with a #define When you #define something, the preprocessor does a find and replace Don't use a semicolon! #define directives are usually put close to the top of a file, for easy visibility #define SIZE 100 int main() { int array[SIZE]; int i = 0; for( i = 0; i < SIZE; i++ ) array[i] = i*i; return 0; }

#define macros You can also make macros with #define that take arguments You need to be careful with parentheses Constants and macros are usually written in ALL CAPS to avoid confusion #include <math.h> #define TO_DEGREES(x) ((x) * 57.29578) #define ADD(a,b) ((a) + (b)) int main() { double theta = TO_DEGREES(2*M_PI); int value = ADD(5 * 2, 7); return 0; }

Conditional compilation You can use directives  #if, #ifdef, #ifndef, #else, #elif and #endif These are mostly used to avoid infinite include problems Sometimes they will change what gets compiled based on compiler version, system libraries, or other stuff #ifndef SOMETHING_H #define SOMETHING_H int something(int a, int b); #endif

Other C Language Features

sizeof We said that the size of int is compiler dependent, right? How do you know what it is? sizeof is a built-in operator that will tell you the size of an data type or variable in bytes #include <stdio.h> int main() { printf("%d", sizeof(char)); int a = 10; printf("%d", sizeof(a)); double array[100]; printf("%d", sizeof(array)); return 0; }

const const double PI = 3.141592; In Java, constants are specified with the final modifier In C, you can use the keyword const Note that const is only a suggestion The compiler will give you an error if you try to assign things to const values, but there are ways you can even get around that Arrays have to have constant size in C Since you can dodge const, it isn't strong enough to be used for array size That's why #define is more prevalent const double PI = 3.141592;

Single Character I/O

getchar() int value = getchar(); if( value == -1 ) We haven't talked about any input in C yet To read the next character from input, you can use the getchar() function It will return the value of the next character (as an int) or -1 if the end of the file is reached Store the value as an int first to check to see if the end of the file has been reached If not, you can then store it as a char int value = getchar(); if( value == -1 ) printf("End of file!");

putchar() putchar() is the output equivalent of getchar() It outputs a single character at a time You could use printf() with the %c formatter instead, but putchar() can be more convenient for single characters char letter = 's'; putchar('q'); putchar(letter);

Lab 2

Upcoming

Next time… Class is canceled Monday System limits Bitwise operators As is my 2-3:20pm office hours on Monday And my 1-3pm office hours on Tuesday System limits Bitwise operators Operator precedence Selection

Reminders Class is canceled Monday Keep reading K&R chapter 2 Keep working on Project 1 Due next Friday