The C Programming Lecture 24.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Programming In C++ Spring Semester 2013 Lecture 2 Programming In C++, Lecture 2.
Chapter 3: Beginning Problem Solving Concepts for the Computer Programming Computer Programming Skills /1436 Department of Computer Science.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University.
1 CSE1301 Computer Programming Lecture 5 C Primitives 2.
CSE1301 Computer Programming Lecture 4: C Primitives I.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
1 CSE1301 Computer Programming Lecture 6 C Primitives 3.
Structure of a C program
B-1 Lecture 2: Problems, Algorithms, and Programs © 2000 UW CSE University of Washington Computer Programming I.
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)
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Basic Elements of C++ Chapter 2.
1 CSE1301 Computer Programming Lecture 6: Components of a C Program (Part 2)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Computer Science 101 Introduction to Programming.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Week 1 Algorithmization and Programming Languages.
Constants Numeric Constants Integer Constants Floating Point Constants Character Constants Expressions Arithmetic Operators Assignment Operators Relational.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Introduction to Programming
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
1 Topics Type Variables Keywords and Identifiers Assignments Constant Variables.
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.
CSE1301 Computer Programming Lecture 2: Introduction to C Joselito (Joey) Chua
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
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.
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Completing the Problem-Solving Process
Basic Elements of C++.
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
DCP2073 Asas Pengaturcaraan C Lecture 6: Components of a C Program (Part 3) Lecture 6: C Primitives 6.
Introduction to C Programming Language
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Introduction to CS Your First C Programs
CSI 121 Structured Programming Language Lecture 5: C Primitives 2
Introduction to C Programming
Govt. Polytechnic,Dhangar
Lecture3.
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
INTRODUCTION TO C.
Getting Started With Coding
Presentation transcript:

The C Programming Lecture 24

Summary of Previous Lecture Programming in real life. Introduction What is Problem Solving? Problem Solving process. Algorithm History Working Definition Examples Algorithms to Programs

Summary of Previous Lecture Components of Algorithms Variables and values Instructions Sequences Procedures Selections Repetitions Documentation Software Development Process Top Down Algorithm Design

Today’s Lecture Algorithms and Programs How to get started with C. A C programming language History of C C, A High level language How to get started with C. Basic Structure of a C program Data Storage and Data Types Variables, Keywords, identifiers, Assignment

Today’s Lecture printf() and scanf() functions and usage Precedence constant variable printf() and scanf() functions and usage Precedence int and float Unary operations Increment and decrement operations

Today’s Lecture Comments Error and its Types Summary

From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: talking to humans, easy to understand in plain (English) language Program: talking to computer (compiler) can be regarded as a “formal expression” of an algorithm

A C Programming Language Flexible language: Structured language Low level activities possible It can produce lean and efficient code Wide availability on a variety of computers Widely used!

History of C CPL Combined Programming Language (Barron et al., 1963) BCPL Basic CPL (Richards, 1969) B (Thompson, 1970) C K&R C (Ritchie, 1972) ANSI C American National Standards Institute C (X3J11, 1989) C99 (JTC1/SC22/WG14, ISO/IEC 9899, 1999)

A High-Level Language Source code Executable code 10100110 01110110 00100110 00000000 11111010 11111010 01001110 10100110 11100110 10010110 11001110 00101110 10100110 01001110 11111010 01100110 01001110 10000110 etc... #include <stdio.h> int main() { printf(“Hello World”); return 0; } Source code Executable code Compilers and linkers translate a high level program into executable machine code.

How to get started? Download Turbo C++ Version 3.0 a free software. http://turbo-c.soft32.com/ Install it! Follow the step by step guide for your first program!

Click New to open a program window Turbo C++ IDE Version 3 Click New to open a program window

Open a new window for writing a program Output Message window

Set the Directories in Option Menu

Set the output directory path

Write your first program here Write your program here!

Compile to check errors Compile to find errors Compile to check errors

No Errors Found

Execute the program by Run option RUN to execute

See output by pressing Ctrl+F5 Output of your first program

Basic Structure of a C Program Example: Hello World C Program: Pre Processor Directive include <stdio.h> file in this program! stdio.h contains declaration of printf used in the program #include <stdio.h> int main() { printf(“Hello World”); return 0; }

Basic Structure of a C Program Example: Hello World C Program: #include <stdio.h> int main() { printf(“Hello World”); return 0; } Program control is started from the main function.

Main Function = Main Gate You can enter the premises of the building through main gate! Similarly program control is entered through main function..!

Basic Structure of a C Program Example: Hello World C Program: #include <stdio.h> int main() { printf(“Hello World”); return 0; } int indicates that only an integer value can come out of this function

Basic Structure of a C Program Example: Hello World C Program: #include <stdio.h> int main() { printf(“Hello World”); return 0; } Curly braces mark the beginning and end of a block of instructions.

Basic Structure of a C Program Example: Hello World C Program: Instruction (function call) to output “Hello World”. This will print Hello World on the output screen #include <stdio.h> int main() { printf(“Hello World”); return 0; }

Basic Structure of a C Program “Statements” (lines of instructions) always end with a semi-colon (;) Example: Hello World C Program: #include <stdio.h> int main() { printf(“Hello World”); return 0; }

Where to store data? A data type is a representation of data that defines a size and valid range for data. Built-in types: char, int, float Type modifiers: long, short, const User-defined types (arrays and records) What about “strings”? Strings are arrays of char (discussed later)

Character Representation Characters are stored as a small integer Each character has a unique integer equivalent specified by its position in the ASCII table (pronounced “as-key”) American Standard Code for Information Interchange

Character Representation The ASCII values range from 0 to 127 value 0: special character ’\0’ (a.k.a. NUL character) value 127: special character <DEL> other special characters: ’\n’ ’\t’ ’\’’ ’\\’ etc. various “extended” sets from 128 to 255

Remember: Variables Are containers for values – places to store values Example: This jar can contain 10 cookies 50 grams of sugar 3 slices of cake etc. Values Variable One form: strain the juices, put aside and let cool …(further instruction)… now add the reserved mix In mathematics much more precisely: Call the sum of all items X. Call the number of items N… divide X by N 32

Variable Is a logical name for a container (an actual piece of computer memory for values) Has a type associated with it tells the computer how to interpret the bits Must be declared before use: int i; float result; int i=0; char initial=’K’;

Variable Declaration: Examples int myID; myID

Variable Declaration: Examples int myID; char myInitial = ’J’; Single “forward quotes” or apostrophe (’) rather than “back quotes” (‘)

Variable Declaration: Examples int myID; char myInitial = ’J’; 01001010 myInitial

Variable Declaration: Examples int myID; char myInitial = ’J’; char myInitial = 74 ; 01001010 myInitial

Variable Declaration: Examples float commission = 0.05; short int myHeight = 183; /* cm */ long int mySalary = 100000000000000000000; long float chanceOfADate = 3e-500; double chanceOfA2ndDate = 1.5e-500;

Variable Declaration: Examples float commission = 0.05; short int myHeight = 183; /* cm */ long int mySalary = 100000000000000000000; long float chanceOfADate = 3e-500; double chance_of_a_2nd_date = 1.5e-500; “Keywords”

Keyword ...has a special meaning in C ...is “case-sensitive” ...cannot be used as variable names Examples: int, char, long, main, float, double, const, while, for, if, else, return, break, case, switch, default, typedef, struct, etc.

Variable Declaration: Examples float commission = 0.05; short int myHeight = 183; /* cm */ long int mySalary = 100000000000000000000; long float chanceOfADate = 3e-500; double chanceOfA2ndDate = 1.5e-500; “Identifiers”

Identifier ...is a series of characters consisting of letters, digits and underscores ( _) ...cannot begin with a digit ...must not be a keyword ...is “case-sensitive” Examples: sUmoFA, x1, y2, _my_ID_, Main (careful!)

not to be confused with == Assignment Puts a specified value into a specified variable Assignment operator: = <variable name> = <expression> ; not to be confused with ==

Assignment: Examples float x = 2.5 ; char ch ; int number ; ch = ’\n’ ; number = 4 + 5 ; /* current value of number is 9. */ number = number * 2; /* current value of number is now 18. */

Assignment Value must have a type assignable to the variable Value may be automatically converted to fit the new container Example: various.c

various.c #include <stdio.h> /* Do various assignment statements */ int main() { int integer; char character; float floatingPoint; integer = 33; character = 33; floatingPoint = 33; integer = 'A'; character = 'A'; floatingPoint = 'A'; integer = 33.33; character = 33.33; floatingPoint = 33.33; integer = floatingPoint; floatingPoint = integer; return 0; } various.c

Constant Variables ...are variables that don’t vary ...may not be assigned to. ...must be initialized const float Pi = 3.14159; const int classSize = 100;

Constant Variables: Examples const int myID = 192; myID = 666; /* Error! */ const int passMark = 80; short char pAsSgRaDe = ’P’; const float pi = 3.1415926; /* oops */ const double golden_ratio = 1.61803398874989;

Example: Constants #include <stdio.h> /* Converts an angle in degrees to radians. */ const float PI = 3.1415926; int main() { float angleInDegs; float angleInRads; printf("Enter angle in degrees:"); scanf("%f", &angleInDegs); angleInRads = PI/180*angleInDegs; printf("%f\n", angleInRads); return 0; } Converts an angle from degrees to radians output “Enter angle in degrees” input angleInDegrees angleInRadians =  / 180 * angleInDegrees output angleInRadians

“Global” constant variable #include <stdio.h> /* Converts an angle in degrees to radians. */ const float PI = 3.1415926; int main() { float angleInDegs; float angleInRads; printf("Enter angle in degrees: "); scanf("%f", &angleInDegs); angleInRads = PI/180*angleInDegs; printf("%f\n", angleInRads); return 0; } Example: Constants “Global” constant variable “Local” variables more on this later...

Print text on the screen #include <stdio.h> /* Converts an angle in degrees to radians. */ const float PI = 3.1415926; int main() { float angleInDegs; float angleInRads; printf("Enter angle in degrees: "); scanf("%f", &angleInDegs); angleInRads = PI/180*angleInDegs; printf("%f\n", angleInRads); return 0; } Example: Constants Print text on the screen scanf function gets values from user

Result will be stored in angleInRads #include <stdio.h> /* Converts an angle in degrees to radians. */ const float PI = 3.1415926; int main() { float angleInDegs; float angleInRads; printf("Enter angle in degrees: "); scanf("%f", &angleInDegs); angleInRads = PI/180*angleInDegs; printf("%f\n", angleInRads); return 0; } Example: Constants Result will be stored in angleInRads

Will be printed on the screen #include <stdio.h> /* Converts an angle in degrees to radians. */ const float PI = 3.1415926; int main() { float angleInDegs; float angleInRads; printf("Enter angle in degrees: "); scanf("%f", &angleInDegs); angleInRads = PI/180*angleInDegs; printf("%f\n", angleInRads); return 0; } Example: Constants Will be printed on the screen

This will print 9 on the output screen printf() function In order to use printf(), function you need to include <stdio.h> as #include<stdio.h> To print an integer %d is used in printf For example This will print 9 on the output screen

This will print Hello world! This is 9 printf() function To print a string on the screen %s is used in printf or simply type words with printf. For example This will print Hello world! This is 9

This will print 9.200000 on the screen printf() function To print a floating point number %f is used For example This will print 9.200000 on the screen

This will print 9.20 on the screen printf() function Floating point with 2 digit precision, use %0.2f For example This will print 9.20 on the screen

scanf function Same include directive is used, #include<stdio.h> Used to get a value from user at run time. For example scanf(“%d”, &i); Will get an integer value in the variable i. Can get character, floating or integer value from the user.

scanf example Ask user to enter a value scans the value Prints the user input value on the screen

Output of previous program

We have covered Type Variables Keyword and Identifiers Assignments Constant Variables

Precedence in Expressions Defines the order in which an expression is evaluated

Precedence in Expressions -- Example 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5) B.O.D.M.A.S. B stands for brackets, O for Order (exponents), D for division, M for multiplication, A for addition, and S for subtraction.

More on precedence +, - are at the same level of precedence For operators at the same “level”, left-to-right ordering is applied. 2 + 3 – 1 = (2 + 3) – 1 = 4 2 – 3 + 1 = (2 – 3) + 1 = 0 2 * 3 / 4 = (2 * 3) / 4 = 6 / 4 2 / 3 * 4 = (2 / 3) * 4 = 0 * 4

Precedence in Expressions – Example 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5) 6.2

Precedence in Expressions –Example.. 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5) 6.2

Precedence in Expressions – Example.. 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5) Integer division results in integer quotient

Precedence in Expressions – Example 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5) D’oh = 0

Precedence in Expressions .. 1 + 2 * 3 - 4 / 5 = 1 + (2 * 3) - (4 / 5) 7

int and float float is a “communicable” type Example: 1 + 2 * 3 - 4.0 / 5 = 1 + (2 * 3) - (4.0 / 5) = 1 + 6 - 0.8 = 6.2 All integers are in Black

int and float – Example 2 (1 + 2) * (3 - 4) / 5 = ((1 + 2) * (3 - 4)) / 5 = (3 * -1) / 5 = -3 / 5 = 0

int and float – Example 2 (1 + 2.0) * (3 - 4) / 5 = ((1 + 2.0) * (3 - 4)) / 5 = (3.0 * -1) / 5 = -3.0 / 5 = -0.6

int and float – Example 3 (1 + 2.0) * ((3 - 4) / 5) = (1 + 2.0) * (-1 / 5) = 3.0 * 0 = 0.0

Unary operators Called unary because they require one operand. Example i = +1; /* + used as a unary operator */ j = -i; /* - used as a unary operator */ The unary + operator does nothing, just emphasis that a numeric constant is positive. The unary – operator produces the negative of its operand. i=+1 is equal to i=i+1

Increment and decrement operators ++ is the increment operator i++; is equivalent to i = i + 1; -- is the decrement operator j--; j = j - 1;

Example -- Simple Expressions Evaluate an expression set result to 1 + 2 * 3 - 4 / 5 output result #include <stdio.h>

Example -- Simple Expressions Evaluate an expression set result to 1 + 2 * 3 - 4 / 5 output result #include <stdio.h> /* Evaluate an expression */

Example -- Simple Expressions Evaluate an expression set result to 1 + 2 * 3 - 4 / 5 output result #include <stdio.h> /* Evaluate an expression */ int main() { return 0; }

Example -- Simple Expressions Evaluate an expression set result to 1 + 2 * 3 - 4 / 5 output result #include <stdio.h> /* Evaluate an expression */ int main() { float result; return 0; }

Example -- Simple Expressions Evaluate an expression set result to 1 + 2 * 3 - 4 / 5 output result #include <stdio.h> /* Evaluate an expression */ int main() { float result; result = 1 + 2 * 3 - 4 / 5; return 0; }

Example -- Simple Expressions Evaluate an expression set result to 1 + 2 * 3 - 4 / 5 output result #include <stdio.h> /* Evaluate an expression */ int main() { float result; result = 1 + 2 * 3 - 4 / 5; printf(“%f\n”, result); return 0; }

Example -- Simple Expressions Evaluate an expression set result to 1 + 2 * 3 - 4 / 5 output result Output: 7.000000 #include <stdio.h> /* Evaluate an expression */ int main() { float result; result = 1 + 2 * 3 - 4 / 5; printf(“%f\n”, result); return 0; }

Comments Essential for documenting programs Run from a /* to the next */ Examples: /* THIS IS A COMMENT */ /* So is this */ /* ** ...and this. ** */

Comments .. Comments do not “nest” /* Comments start with a “/*” and end with a “*/” but they don’t nest! */

Errors in C Program Do not get Fear of an Error! All good programmers started with lot of Errors Understand them and remove them!

Types of Errors In c program you may get Syntax error Logical error Run time errors

Syntax Error These errors occur because of wrongly typed statements, which are not according to the syntax or grammatical rules of the language. For example, in C, if you don’t place a semi-colon after the statement (as shown below), it results in a syntax error. printf(“Hello,world”)

Logical Error These errors occur because of logically incorrect instructions in the program. Let us assume that in a 1000 line program, if there should be an instruction, which multiplies two numbers and is wrongly written to perform addition. This logically incorrect instruction may produce wrong results. Detecting such errors are difficult!

Run Time Errors These errors occur during the execution of the programs though the program is free from syntax and logical errors. Some of the most common reasons for these errors are When you instruct your computer to divide a number by zero. When you instruct your computer to find logarithm of a negative number. When you instruct your computer to find the square root of a negative integer.

Summary High level language is converted into computer understandable by a compiler. Compiler converts source code into computer understandable code C Programming is a way to program computers It has a syntax It has commands and structure C can not be learnt! It can be understood by implementing it!