CSE-102: PROGRAMMING FUNDAMENTALS LECTURE 3: BASICS OF C Course Instructor: Syed Monowar Hossain 1.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 1 Engineering Problem Solving.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
COSC 120 Computer Programming
Introduction to C Programming CE Lecture 1 Introduction to C.
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
Three types of computer languages
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?
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
C programming Language and Data Structure For DIT Students.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
CHAPTER 1: INTORDUCTION TO C LANGUAGE
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Computer Science 210 Computer Organization Introduction to C.
Programming Design Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Chapter 1 Engineering Problem Solving 1. Hardware and Software 2 A computer is a machine designed to perform operations specified with a set of instructions.
Chapter 2: C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 1 Introducing C.
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.
History of C and C++ C++ evolved from C ANSI C C++ “spruces up” 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 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.
1 Programming in C Hello World! Soon I will control the world! Soon I will control the world!
EG280 Computer Science for Engineers Fundamental Concepts Chapter 1.
Introduction to Programming
Computing Systems & Programming ECE Fundamental Concepts Chapter 1 Engineering Problem Solving.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Chapter 2: C Fundamentals Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 2 C Fundamentals.
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.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 1 Scott Marino.
An overview of C Language. Overview of C C language is a general purpose and structured programming language developed by 'Dennis Ritchie' at AT &T's.
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.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Introduction to C Programming
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Chapter 1: Introduction to Computers and Programming.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 1.
COP 3275: Chapter 02 Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA.
CSCE 206 Structured Programming in C
Computer Science 210 Computer Organization
Engineering Problem Solving With C An Object Based Approach
Chapter 1: Introduction to computers and C++ Programming
CSC201: Computer Programming
Introduction to C Language
C Language VIVA Questions with Answers
INTRODUCTION c is a general purpose language which is very closely associated with UNIX for which it was developed in Bell Laboratories. Most of the programs.
Computer Science 210 Computer Organization
Programming Fundamentals Lecture #3 Overview of Computer Programming
C programming Language
The C Language: Intro.
An Overview of C.
Introduction to C Programming
Computer Science Department
Presentation transcript:

CSE-102: PROGRAMMING FUNDAMENTALS LECTURE 3: BASICS OF C Course Instructor: Syed Monowar Hossain 1

2 Introduction to C  General purpose programming language.  Closely related to UNIX  Often referred as a system programming language as useful for writing compilers and operating systems.

C History 3  Developed by Dennis Ritchie at AT&T, early 70s, for DEC PDP-11  Unix written in, closely associated with, C  Family of languages:  BCPL, Martin Richards  B (typeless), Ken Thompson, 1970  C, Dennis Ritchie, Bell Labs, early 70s  C++, Bjarne Stroustrup, Bell Labs, 80s  Java, James Gosling Sun, 1995  C#, Microsoft, recently  C++, Java, C# conserve (much) C syntax

4 Features of C  Portability  Implemented on many diverse systems  Implemented on wide variety of hardware  Defined libraries are themselves portable  Applications are portable  Programs are portable  Powerful  Completeness – business to scientific applications  Flexibility – preprocessor, conditional compilation, header files

5 Features of C  Structured Programming – readability and maintainability  Both a high level and low level language  High level – symbolic references, user written functions, powerful operator set, derived data types  Low level – allow reference to storage directly, bits and bit patterns manipulation

First C Program 6 /* My first simple C program */ void main () { printf (“Welcome to C!\n”); }

Basics of a Typical C Program Development Environment Phases of C Programs: 1. Edit 2. Preprocess 3. Compile 4. Link 5. Load 6. Execute 7

C Compilers, Linkers, Loaders 8

9 Some Key Terms  Source Program  printable/Readable Program file  Object Program  nonprintable machine readable file  Executable Program  nonprintable executable code  Syntax errors  reported by the compiler  Linker errors  reported by the linker  Execution/Run-time errors  reported by the operating system

Compile a program  In Windows Environment (using Visual C++ 6):  Compile a program cl is used to compile and link a program cl first.c  Run a program first.exe  In Linux Environment:  Compile a program gcc is used to compile and link a program gcc first.c –o first.o  Run a program./first.out 10

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.  Example: Visual C++ 6, Dev CPP, Eclipse  You can find a document file in course website (named “Visual C++ 6.ppt” under resources folder) where it is described how to create a simple program, how to compile, build and run it. 11

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

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

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.

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.

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;

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.

Statements  A statement is a command to be executed when the program runs.  first.c uses only two kinds of statements. One is the return statement; the other is the print statement.  Asking a function to perform its assigned task is known as calling the function.  first.c calls printf to display a string: printf(“Welcome to CSE102.\n");

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.

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

Printing Strings  The statement printf(“Hello World\n"); could be replaced by two calls of printf : printf(“Hello "); printf(“World\n");  The new-line character can appear more than once in a string literal: printf(“Hello\nWOrld\n");

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

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,767.

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

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;

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

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.

Assignment  A constant assigned to a float variable usually contains a decimal point: profit = ;  It’s best to append the letter f to a floating-point constant if it is assigned to a float variable: profit = f; Failing to include the f may cause a warning from the compiler.

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.

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.

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.

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);

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);

PROBLEM SOLVING  Very Important 34

35 Problem Solving Methodology (must read) 1. State the problem clearly 2. Describe the input/output information 3. Work the problem by hand, give example 4. Develop a solution (Algorithm Development) and Convert it to a program (C program) 5. Test the solution with a variety of data

36 Example 1 1. Problem statement Compute the straight line distance between two points in a plane 2. Input/output description Point 1 (x1, y1) Point 2 (x2, y2) Distance between two points (distance) (x1,y1) (x2, y2)

37 3. Hand example Example 1(cont’d) side1 = = 3 side2 = = 2

38 Example 1(cont’d) 4. Algorithm development and coding a. Generalize the hand solution and list/outline the necessary operations step-by-step 1) Give specific values for point1 (x1, y1) and point2 (x2, y2) 2) Compute side1=x2-x1 and side2=y2-y1 3) Compute 4) Print distance b. Convert the above outlined solution to a program using any language you want (see next slide for C imp.)

39 Example 1(cont’d)

40 Example 1(cont’d) 5. Testing  After compiling your program, run it and see if it gives the correct result.  Your program should print out The distance between two points is 3.61  If not, what will you do?

41 Modification to Example 1 x1=2, y1=5, x2=10, y2=8, How will you find the distance between two other points (2,5) and (10,8)?