C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.
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
41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Chapter 2 Data Types, Declarations, and Displays
Introduction to C Programming
Basic Input/Output and Variables Ethan Cerami New York
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
C Programming Lecture 4. Tokens & Syntax b The compiler collects the characters of a program into tokens. Tokens make up the basic vocabulary of a computer.
Chapter 18 I/O in C. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Standard C Library I/O commands.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
A First Book of ANSI C Fourth Edition
1 IPC144 Session 11 The C Programming Language. 2 Objectives To format a #define statement correctly To use a #define statement in a C program To construct.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Input & Output: Console
Number Systems Spring Semester 2013Programming and Data Structure1.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Input, Output, and Processing
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 3: Introduction to C: Input & Output, Assignments, Math functions.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Program Development Cycle 1.Edit program 2.Compile program - translates it from C to machine language 3. Run/execute your program. 4. If not satisfied,
Characters and Strings
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
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.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 3: Formatted Input/Output 1 Chapter 3 Formatted Input/Output.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 2.
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.
INTRODUCTION TO PROGRAMING System Development Mansoura October 2015.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Formatted I/O ä ä Standard Output ä ä printf() family of functions ä ä Standard Input ä ä scanf() family of functions.
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.
CSCE 206 Structured Programming in C
Chapter 2 - Introduction to C Programming
Introduction to C CSE 2031 Fall /3/ :33 AM.
Chapter 2 - Introduction to C Programming
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to CS Your First C Programs
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lecture3.
Chapter 2 - Introduction to C Programming
Introduction to C EECS May 2019.
Introduction to C Programming
Presentation transcript:

C Programming Lecture 3

The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked The modified source code is translated into object code (an object file) b The loader is invoked The loader uses the object code to produce an executable file

Preprocessing Directives b Source code lines that begin with a # are called preprocessing directives. Examples #include #define PI 3.14 b Preprocessing directives indicate to the preprocessor that certain header files should be included with the source code and certain substitutions should be made.

The Standard Library b Some functions have been written for us as part of the C system. These functions are compiled into object code and stored in libraries. The linker will link object code from the libraries with object code from the functions we write to produce the executable code. b The header files provide needed information about the library functions.

Example of a Library Function printf(“Hello, world!\n”); printf() is a library function. When we provide printf() with an argument such as the string “Hello, World!” the printf() function causes the argument to be displayed on the screen. The header file stdio.h provides information needed by the printf() library function.

Variables b While a program is executing, it is usually necessary to store values that the program is using in RAM memory. Values such as numbers or strings of characters that are being manipulated by our program. b In our program, we obtain the needed memory locations by declaring names for the locations. We refer to these locations and their names as variables.

Why “Variables”? b We call these memory locations and the names we give them “variables” because we can (in our programs) cause the values there to be changed as the program executes. b We are now going to look at how values (numbers, characters, strings of characters, etc.) are stored in memory.

Number Systems b We are accustomed to using the decimal number system. Computers use the binary number system. b Each digit in a decimal number has a value that equals the value of that digit times some power of ten.

Example of a Decimal Number b The digits in the decimal number 234 actually have values (starting from the right or low-order position): 4 x 10 0 = 4 x 1 = 4 3 x 10 1 = 3 x 10 = 30 2 x 10 2 = 2 x 100 = 200 Total = 234

The Binary Number System b The only digits used in binary numbers are zero and one. Each digit in a binary number has a value that equals the value of that digit times some power of two.

Example of a Binary Number b The digits in the binary number 1011 actually have values (starting from the right or low-order position): 1 x 2 0 = 1 x 1 = 1 (numbers 1 x 2 1 = 1 x 2 = 2 on the 0 x 2 2 = 0 x 4 = 0 right are 1 x 2 3 = 1 x 8 = 8 decimal Total = 11 numbers)

Binary Numbers in Computers b You can think of each location in RAM as having eight tiny switches that can be on (a one) or off (a zero). b We say that each of the “switches” represents a bit (binary digit). And all eight bits make up a byte.

Storing a Character in a Byte b To store a character such as ‘A’ or ‘B’, etc. in a byte of memory, we store a number that represents the character. b We encode characters using an established code, the American Standard Code for Information Interchange (ASCII)

The ASCII Code b Look on pages of your text. You will find the ASCII codes for characters on your keyboard and for what we call control characters. Note that the codes are given as decimal numbers, as hexadecimal numbers and as octal numbers in this chart.

The ASCII Codes for ‘A’ and ‘a’ b On the chart we find that the ASCII code for ‘A’ is 65 and the code for ‘a’ is 97. b Expressed as binary numbers, 65 is and 97 is Note that the ASCII code is a 7- bit code, but each byte is 8- bits, thus I have included the leading 0.

The Non-Printable or Control Codes b The first 32 ASCII codes represent non-printable characters known as control characters. “Control Characters” because they can be used to do things like make the bell (beeper) on your computer sound (bel, 7 decimal or binary) or make printed output go to the next line (nl, 10 decimal or binary)

Storing Numbers in Memory b The ASCII code is used for storing characters (including the digits when they are stored as digits 0,1,2,3,4,5,6,7,8 9). b A number is stored as its binary equivalent in one or more bytes.

Representing Numbers in Memory Gets a Bit Complicated b Exactly how we store the binary number in memory depends upon: How big we allow the number to be. –Maximum number of digits allowed (both before and after a decimal point). What kind of number. –Integer versus floating point. –Positive versus negative. Choice of representation –One’s complement, two’s complement, etc. b We will wait until later in the course to discuss specifics.

Wreck of the Hesperus b How deep in the ocean is the wreck of the Hesperus? We are told that it is 7 fathoms deep, but we want to know how deep that would be in feet or how deep in inches.

An Algorithm for Converting Fathoms to Feet and to Inches  Assign the number of fathoms to a variable.  Convert fathoms to feet and store in a variable.  Convert feet to inches and store in a variable.  Print the different units of measure neatly on the screen.

Convert the Algorithm to C Code #include int main(void) { /* declaration of integer variables */ int inches, feet, fathoms; /* initialization using the ‘=‘ assignment operator*/ fathoms = 7; /* we were given this */ /* calculations using expressions and assignments */ feet = 6 * fathoms; inches = 12 * feet; /* Display the output on the computer’s screen. */ printf(“Wreck of the Hesperus:\n”); printf(“Its depth at sea in different units:\n”); printf(“ %d fathoms\n”, fathoms); printf(“ %d feet\n”, feet); printf(“ %d inches\n”, inches); return 0; }

Some Things to Note about the Hesperus Program b When variables are declared you must indicate the variable type (the kind of values that will be stored there -- integers in this case). You can declare several variables of the same type by separating the variable names by commas.

Hesperus Program Notes (2) b The equals sign ‘=‘ is used as the assignment operator in C. The value calculated in the expression on the right of ‘=‘ is placed in the memory location named on the left side of ‘=‘ Example: feet = 6 * fathoms;

Hesperus Program Notes (3) b In the statement printf(“ %d feet\n”, feet); the spaces before %d are printed on output because they are within the quotes. %d tells the printf() function to display the value stored in the variable feet as an integer.

Hesperus Program Notes (5) b In the statement printf(“ %d feet\n”, feet); the word feet inside quotes is simply printed, it is not the variable feet. b %d is a conversion specification that says display feet as an integer (no decimal point). b \n, the linefeed character, says to start the next output on the next line.

Math Operators Used in C b You have seen the multiplication operator *. b Other mathematical operators are: + addition - subtraction / division % modulus

What is the Modulus Operator b a % b yields the remainder after a is divided by b. b Example 13 % 5 yields 3 b Note that ‘%’ is also used in a printf() statement to start a conversion specification.

Use of printf ( ) b printf() is used for printing output. When printf() is called it is passed a list of arguments of the form: control string & other arguments b The arguments to printf() are separated by commas.

printf ( ) Example printf(“Get set: %d %s %f %c%c\n”, 1, “two”, 3.33, ‘G’, ‘O’); The first argument is the control string “Get set: %d %s %f %c%c\n” The formats in the control string are matched (in order of occurrence) with the other arguments.

The Formats in the Control String printf(“Get set: %d %s %f %c%c\n”, 1, “two”, 3.33, ‘G’, ‘O’); b %d Print 1 as a decimal number b %s Print “two” as a string –“string” means a sequence of characters. b %f Print 3.33 as a float –decimal or floating-point number b %c Print ‘G’ & ‘0’ as characters.

Use of scanf() b scanf() is analogous to printf(), but is used for input rather than output. scanf()in a program stops the execution of the program while you type something in from the keyboard.

scanf ( ) Arguments b The first argument is a control string with formats similar to those used with printf(). The formats determine how characters in the input stream (what you are typing) will be interpreted so they can be properly stored in memory.

Scanf ( )’s Other Arguments b After the control string, the other arguments are addresses. b Example: assume x is declared as an integer variable. scanf(“%d”, &x); The & is the address operator. It says “store the value entered at the address of the memory location named x”.

scanf ( ) Conversion Conversion How characters in the Character input stream are converted. c d f lf Lf s Character decimal integer floating-pint number (float) floating-point number (double) floating-point number (long double) string

A Peculiarity of scanf ( ) b With printf() the %f format is used to print either a float or a double. b With scanf() the format %f is used to read in a float, and %lf is used to read in a double.

Another scanf() Peculiarity b When reading in numbers, scanf() will skip white space characters (blanks, newlines, and tabs). b When reading characters, white space is not skipped.