Gator Engineering 1 Chapter 2 C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved.

Slides:



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

Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
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?
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Chapter 2: Introduction to C++.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
C programming Language and Data Structure For DIT Students.
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. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
Using C Programming Language.  The programs that run on a computer are referred to as software.  You’ll learn key programming methodology that are enhancing.
A First Book of ANSI C Fourth Edition
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Chapter 2: C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 1 Introducing C.
H.Melikian Introduction on C C is a high-level programming language that forms the basis to other programming languages such as C++, Perl and Java. It.
Programming With C.
Input, Output, and Processing
History of C 1950 – FORTRAN (Formula Translator) 1959 – COBOL (Common Business Oriented Language) 1971 – Pascal Between Ada.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
Computer Programming I Hour 2 - Writing Your First C Program.
Chapter 3: Formatted Input/Output Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Chapter 2: C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 2 C Fundamentals.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
Chapter 2 part #1 C++ Program Structure
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Input and Output.
Chapter 2: C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 2 C Fundamentals.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Run your first C program.  Windows: gcc ◦ Equation solution: ran
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Chapter 1: Introduction to Computers and Programming.
1 Chapter 2 C Fundamentals. Computer Architecture Central Processing Unit CPU Main memory Data bus Address.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 1.
CSE-102: PROGRAMMING FUNDAMENTALS LECTURE 3: BASICS OF C Course Instructor: Syed Monowar Hossain 1.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
COP 3275: Chapter 02 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA.
C Fundamentals Chapter 2 Copyright © 2008 W. W. Norton & Company.
CSCE 206 Structured Programming in C
Chapter 1: Introduction to computers and C++ Programming
CSC201: Computer Programming
CSE 220 – C Programming C Fundamentals.
Chapter 2, Part I Introduction to C Programming
Computer Science 210 Computer Organization
Programming Fundamentals Lecture #3 Overview of Computer Programming
C Fundamentals Chapter 2 Copyright © 2008 W. W. Norton & Company.
Chapter 2: Introduction to C++.
C programming Language
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

Gator Engineering 1 Chapter 2 C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved.

Gator Engineering Program: Printing HelloWorld #include int main(void) { printf(“Hello World!\n"); return 0; } The file name doesn’t matter, but the.c extension is often required. 2

Gator Engineering A few notes gcc environment To compile and run the code, you have to be in the same folder where you have put the code A few most frequently used commands: –cd [folderName] –cd.. –ls (dir in windows) –pwd –“tab” 3

Gator Engineering Compiling and Linking Before a program can be executed, three steps are usually necessary: –Preprocessing. The preprocessor obeys commands that begin with # (known as directives) –Compiling. A compiler translates then translates the program into machine instructions (object code). –Linking. A linker combines the object code produced by the compiler with any additional code needed to yield a complete executable program. The preprocessor is usually integrated with the compiler. 4

Gator Engineering The GCC Compiler GCC is one of the most popular C compilers. GCC is supplied with Linux but is available for many other platforms as well. Using this compiler is similar to using cc : % gcc -o SayHi HelloWorld.c 5

Gator Engineering Integrated Development Environments An integrated development environment (IDE) is a software package that makes it possible to edit, compile, link, execute, and debug a program without leaving the environment. –Visual C++ (Windows) –Visual Studio (Windows) –Xcode (Mac) –Eclipse (Cross-platforms) –Netbeans –CodeBlocks, etc. 6

Gator Engineering The General Form of a Simple Program Simple C programs have the form directives int main(void) { statements } 7

Gator Engineering The General Form of a Simple Program C uses { and } in much the same way that some other languages use words like begin and end. Even the simplest C programs rely on three key language features: –Directives –Functions –Statements 8

Gator Engineering Directives Before a C program is compiled, it is first edited by a preprocessor. Commands intended for the preprocessor are called directives. Example: #include is a header containing information about C’s standard I/O library. 9

Gator Engineering Directives Directives always begin with a # character. By default, directives are one line long; there’s no semicolon or other special marker at the end. 10

Gator Engineering Functions A function is a series of statements that have been grouped together and given a name. Library functions are provided as part of the C implementation. A function that computes a value uses a return statement to specify what value it “returns”: return x + 1; 11

Gator Engineering The main Function The main function is mandatory. main is special: it gets called automatically when the program is executed. main returns a status code; the value 0 indicates normal program termination. If there’s no return statement at the end of the main function, many compilers will produce a warning message. 12

Gator Engineering Statements A statement is a command to be executed when the program runs. HelloWorld.c uses only two kinds of statements. One is the return statement; the other is the function call. Asking a function to perform its assigned task is known as calling the function. HelloWorld.c calls printf to display a string: printf(“Hello World!\n"); 13

Gator Engineering Statements C requires that each statement end with a semicolon. –There’s one exception: the compound statement. Directives are normally one line long, and they don’t end with a semicolon. 14

Gator Engineering Printing Strings When the printf function displays a string literal—characters enclosed in double quotation marks—it doesn’t show the quotation marks. printf doesn’t automatically advance to the next output line when it finishes printing. To make printf advance one line, include \n (the new-line character) in the string to be printed. 15

Gator Engineering Printing Strings The statement printf("To C, or not to C: that is the question.\n"); could be replaced by two calls of printf : printf("To C, or not to C: "); printf("that is the question.\n"); The new-line character can appear more than once in a string literal: printf("Brevity is the soul of wit.\n --Shakespeare\n"); 16

Gator Engineering Comments A comment begins with /* and end with */. /* This is a comment */ Comments may appear almost anywhere in a program, either on separate lines or on the same lines as other program text. Comments may extend over more than one line. /* Name: HelloWorld.c Purpose: Prints a greeting message. Author: XXX */ 17

Gator Engineering Comments Warning: Forgetting to terminate a comment may cause the compiler to ignore part of your program: printf("My "); /* forgot to close this comment... printf("cat "); printf("has "); /* so it ends here */ printf("fleas"); 18

Gator Engineering Comments in C99 In C99, comments can also be written in the following way: // This is a comment This style of comment ends automatically at the end of a line. Advantages of // comments: –Safer: there’s no chance that an unterminated comment will accidentally consume part of a program. –Multiline comments stand out better. 19

Gator Engineering Variables and Assignment Most programs need to a way to store data temporarily during program execution. These storage locations are called variables. 20

Gator Engineering Types Every variable must have a type. C has a wide variety of types, including int and float. A variable of type int (short for integer) can store a whole number such as 0, 1, 392, or –2553. –The largest int value is typically 2,147,483,647 but can be as small as 32,

Gator Engineering Types A variable of type float (short for floating- point) can store much larger numbers than an int variable. Also, a float variable can store numbers with digits after the decimal point, like Drawbacks of float variables: –Slower arithmetic –Approximate nature of float values 22

Gator Engineering Declarations Variables must be declared before they are used. Variables can be declared one at a time: int height; float profit; Alternatively, several can be declared at the same time: int height, length, width, volume; float profit, loss; 23

Gator Engineering Declarations When main contains declarations, these must precede statements: int main(void) { declarations statements } 24

Gator Engineering Assignment A variable can be given a value by means of assignment: height = 8; The number 8 is said to be a constant. Before a variable can be assigned a value—or used in any other way—it must first be declared. 25

Gator Engineering Assignment A constant assigned to a float variable usually contains a decimal point: profit = ; 26

Gator Engineering Assignment An int variable is normally assigned a value of type int, and a float variable is normally assigned a value of type float. Mixing types (such as assigning an int value to a float variable or assigning a float value to an int variable) is possible but not always safe. 27

Gator Engineering Assignment Once a variable has been assigned a value, it can be used to help compute the value of another variable: height = 8; length = 12; width = 10; volume = height * length * width; /* volume is now 960 */ The right side of an assignment can be a formula (or expression, in C terminology) involving constants, variables, and operators. 28

Gator Engineering Printing the Value of a Variable printf can be used to display the current value of a variable. To write the message Height: h where h is the current value of the height variable, we’d use the following call of printf : printf("Height: %d\n", height); %d is a placeholder indicating where the value of height is to be filled in. 29

Gator Engineering Printing the Value of a Variable %d works only for int variables; to print a float variable, use %f instead. By default, %f displays a number with six digits after the decimal point. To force %f to display p digits after the decimal point, put. p between % and f. To print the line Profit: $ use the following call of printf : printf("Profit: $%.2f\n", profit); 30

Gator Engineering Printing the Value of a Variable There’s no limit to the number of variables that can be printed by a single call of printf : printf("Height: %d Length: %d\n", height, length); 31