Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 2: Getting Started.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
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.
 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. A Simple Program: Print a Line of Text // My First C++ Program #include int main( ) { cout
Introduction to C Programming
Linux+ Guide to Linux Certification, Second Edition
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Guide To UNIX Using Linux Third Edition
Introduction to C Programming
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.
Introduction to C Topics Compilation Using the gcc Compiler
Using Macs and Unix Nancy Griffeth January 6, 2014 Funding for this workshop was provided by the program “Computational Modeling and Analysis of Complex.
Introduction to Shell Script Programming
Spring 2005, Gülcihan Özdemir Dağ BIL104E: Introduction to Scientific and Engineering Computing, Spring Outline 1.1Introduction 1.2What Is a Computer?
Gator Engineering 1 Chapter 2 C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved.
LINUX Tuesday, 5 July :00 pm. Remote Login l Use Secure Shell (ssh) l Machine name/IP address E.g. ssh hydra.sma.nus.edu.sg Or ssh
Programming With C.
Linux Operations and Administration
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.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
CGS 3460 Why we choose UNIX n Powerful lMulti-user operating system lGood programming tools Most heavy-duty database management systems started out on.
8-2 What is a program? What is a “Window Manager” ? What is a “GUI” ? How do you navigate the Unix directory tree? What is a wildcard? Readings: See CCSO’s.
Chapter 2 part #1 C++ Program Structure
 2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Discussion 1 Commonly used commands Executing program.
Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 7: Preprocessing.
CMSC 104, Lecture 111 Introduction to C Topics l Compilation l Using the gcc Compiler l The Anatomy of a C Program l 104 C Programming Standards and Indentation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Learning Unix/Linux Based on slides from: Eric Bishop.
 CSC 215 : Procedural Programming with C C Compilers.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 1.
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.
Introduction to C Topics Compilation Using the gcc Compiler
UMBC CMSC 104 – Section 01, Fall 2016
CSCE 206 Structured Programming in C
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE
CSC201: Computer Programming
Introduction to C Language
Chapter 2 - Introduction to C Programming
Computer Programming Chapter 1: Introduction
Chapter 2, Part I Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C Topics Compilation Using the gcc Compiler
Getting Started with C.
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Creating your first C program
Chapter 2 - Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Programs written in C and C++ can run on many different computers
Chapter 2 - Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Presentation transcript:

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 2: Getting Started Outline A Simple C Program Run Program hello.c in ChIDE Startup in Ch Compile and Link Program hello.c in Windows and Unix in a Command shell Compile and Link Program hello.c Using ChIDE Command chmod in Unix Commonly Used Commands A Practical Engineering Problem of Solving Acceleration Setup Command Search Paths

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach A Simple C Program /* File: hello.c Print ‘Hello, world’ on the screen */ #include int main() { printf(“Hello, world\n"); return 0; }

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Comments –Text surrounded by /* and */ is ignored by computer –Used to describe program #include –Preprocession directive - tells computer to load contents of a header file – allows standard input/output operations

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach int main() –C programs contain one or more functions, exactly one of which must be main –Parenthesis used to indicate a function –int means that main "returns" an integer value –Braces indicate a block The bodies of all functions must be contained in braces printf( “hello, world\n" ); –Instructs computer to perform an action Specifically, prints string of characters within quotes –Entire line called a statement All statements must end with a semicolon –\ - escape character Indicates that printf should do something out of the ordinary \n is the newline character \a is the character for a sound.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach return 0; –A way to exit a function –return 0, in this case, means that the program terminated normally –The returned value of 0 is displayed in the exit status in ChIDE Right brace } –Indicates end of main has been reached

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Executing Program hello.c Using Ch in ChIDE Click “Run” or “Start” to execute the program

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Startup a Ch Shell Startup in Unix If Ch is the login shell, you can readily use the Ch language environment. If not, you can type command ch at a terminal prompt to launch the Ch language environment. Startup in Windows There are five ways to get into the Ch language environment. For example, to start Ch Standard Edition 6.1 – Click the icon Ch Standard on the Desktop screen – Click Start->Programs->SoftIntegration Ch 6.1 Standard->Ch 6.1. – Click Start, followed by Run, then type ch.exe. – Go to the MS-DOS prompt, and type ch. – In ChIDE, click Ch

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Run program hello.c using Visual C++ in Windows > cl hello.c /*... create hello.exe */ > hello.exe Hello, world Run program hello.c using gcc in Unix/Linux/Mac OS X > cc hello.c Or > gcc hell.c /*... create a.out */ > a.out Hello, world Run program hello.c in Ch. > hello.c Hello, world Run Program hello.c

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Compile hello.c Using Visual C++ in Windows and gcc in Linux from ChIDE

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach A C program can be executed without compilation in a Ch language environment. C programs are called command files or simply commands in Ch. A command file shall have both read and execute permissions. Command chmod in Unix/Linux/Mac OS X can be used to change the permission mode of a file. Make a file, say hello.c, executable > chmod +x hello.c Make a file readable > chmod +r hello.c Make a file both executable and readable > chmod +xr hello.c Command chmod in Unix

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Accept the input values and print out the results directly or use the function printf() in C. C:/Ch> 1+2*3 7 C:/Ch> sin(0.5) C:/Ch> printf("hello, world") hello, world C:/Ch> Interactive Command Mode

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Commonly Used Commands Ch supports most Unix and Windows commands CommandUsageDescription cd Change to the home directory cd dirChange to the directory dir cpcp file1 file2Copy file1 to file2 ls List contents in the working directory mkdirmkdir dirCreate a new directory dir pwd Print (display) the name of the working directory rmrm fileremove file chmodchmod +x fileChange the mode of file to make it executable chidechide file.cEdit and execute program file.c vivi fileEdit file

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Examples of Commands C:/Documents and Settings/Administrator> mkdir eme5 C:/Documents and Settings/Administrator> cd eme5 C:/Documents and Settings/Administrator/eme5> pwd C:/Documents and Settings/Administrator/eme5 C:/Documents and Settings/Administrator/eme5> cp C:/Ch/demos/bin/hello.c hello.c C:/Documents and Settings/Administrator/eme5> ls hello.c C:/Documents and Settings/Administrator/eme5> chide hello.c

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Files in Ch C:/Ch> pwd C:/Ch C:/Ch> ls bin/ demos/ docs/ include/ license/ README.TXT sbin/ config/ dl/ extern/ lib/ package/ release/ toolkit/ C:/Ch> cd docs C:/Ch/docs> C:/Ch/docs> ls README.TXT chguide.pdf chinstall.pdf chref.pdf man/

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach A Sample Problem: The system in Figure1 (a) consists of a single body with mass m moving on a horizontal surface. An external force p acts on the body. The coefficient of kinetic friction between body and horizontal surface is . The freebody diagram for the system is shown in Figure1 (b). Figure1: The system diagram and FBD of a sample problem

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The nomenclature related to the modeling of the system is listed below. m -- mass of the body x -- position of the body v -- velocity of the body a -- acceleration of the body g -- gravitational acceleration  -- friction coefficient f -- friction force N -- normal force p -- applied external force Equation of motion: The equation of the motion of the system can be derived based on the Newton's second law. N = mg (1) f =  N (2) p-f = ma (3) From equation (1), (2) and (3), the formula for calculating the acceleration of the rigid body can be derived as follows. a = (p-  mg)/m (4)

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Problem Statement: For the system shown in Figure1(a), given m = 5 kg, g = 9.81 m/s 2,  = 0.2. The external force p is expressed as a function of time t, p(t) = 20 when t >= 0 calculate the acceleration a when t = 2 seconds. Solutions. 1.Interactive solution. 2.Write a simple C program to print out the value of acceleration directly.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach > (20-0.2*5*9.81)/ > Interactive Solution

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach /* File: accel.c */ #include int main() { printf("Acceleration a = %f (m/s^2)\n", (20-0.2*5*9.81)/5); return 0; } Program 1: A simple C program. Acceleration a = (m/s^2) Output:

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Finding Commands in Ch Shell – The system variable _path of string type contains the directories to be searched for the command. – When a command shell is launched, the system variable _path contains some default search paths. – The user can add new directories to the search paths for the command shell by the following statements. Create an individual startup file.chrc in Unix and _chrc in Windows in your home directory by typing the command > ch –d in a Ch command shell.

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Windows: adding the following statement in the startup file _chrc in the user’s home directory can add C:/Documents and Settings/Administrator/eme5 directory to the command search paths. _path = stradd(_path, “C:/Documents and Settings/Administrator/eme5;”); Unix, Linux, and Mac machines: adding following two statements in the startup file.chrc in the user’s home directory can add /home/harry/eme5 directory and the current working directory to the command search paths. _ path = stradd(_path, “/home/harry/eme5;”); Add an alias alias(“go”, “cd C:/Documents and Settings/Administrator/eme5”); // for Windows alias(“go”, “cd /home/harry/eme5”); // for Unix Run programs without typing the complete directory

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Open startup file _chrc in Windows or.chrc for Unix for editing through ChTE

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach ‡ Slides for optional topics in Ch

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach The function main() is optional for a Ch script program. Like a command file, a script file shall have both read and execute permissions. /* File: welcome.ch */ printf(“Welcome to Ch\n”); Ch Scripts

Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach A program with #!/bin/ch can be invoked by other programs such as Bash, C shell. Statements, functions, and commands can be grouped as a script file or script in Ch #!/bin/ch /* File: hello.ch */ #include int main() { printf(“Hello, World\n”); return 0; } Ch Scripts (Cont.)