BLM2031 Structured Programming. Zeyneb KURT

Similar documents
A Fast Review of C Essentials Part I

C: How to Program. Week /Mar/05

INTRODUCTION 1 AND REVIEW

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Chapter 2 - Introduction to C Programming

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Data Types and Variables in C language

Fundamental of Programming (C)

Presented By : Gaurav Juneja

Basic Elements of C. Staff Incharge: S.Sasirekha

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Variables in C. Variables in C. What Are Variables in C? CMSC 104, Fall 2012 John Y. Park

ET156 Introduction to C Programming

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

The C++ Language. Arizona State University 1

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

Fundamentals of Programming

Tokens, Expressions and Control Structures

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

CMSC 104 -Lecture 5 John Y. Park, adapted by C Grasso

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Data types, variables, constants

DETAILED SYLLABUS INTRODUCTION TO C LANGUAGE

Chapter 1 & 2 Introduction to C Language

COMP322 - Introduction to C++


Introduction to C Final Review Chapters 1-6 & 13

Review of the C Programming Language

Review of the C Programming Language for Principles of Operating Systems

Work relative to other classes

printf( Please enter another number: ); scanf( %d, &num2);

ANSI C Programming Simple Programs

VARIABLES AND CONSTANTS

Introduction to Computing Lecture 01: Introduction to C

Objectives. In this chapter, you will:

Procedures, Parameters, Values and Variables. Steven R. Bagley

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

6.096 Introduction to C++ January (IAP) 2009

>B<82. 2Soft ware. C Language manual. Copyright COSMIC Software 1999, 2001 All rights reserved.

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

ET156 Introduction to C Programming

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

EL2310 Scientific Programming

Programming and Data Structures

C Language, Token, Keywords, Constant, variable

Lexical Considerations

Variables in C. CMSC 104, Spring 2014 Christopher S. Marron. (thanks to John Park for slides) Tuesday, February 18, 14

Lexical Considerations

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

CS Programming In C

from Appendix B: Some C Essentials

Programming in C. What is C?... What is C?

Chapter 2: Overview of C. Problem Solving & Program Design in C

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Number Systems, Scalar Types, and Input and Output

Fundamentals of Programming. Lecture 3: Introduction to C Programming

CMPE-013/L. Introduction to C Programming

Syntax and Variables

Chapter-8 DATA TYPES. Introduction. Variable:

Creating a C++ Program

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

Fundamentals of Programming Session 4

Ch. 3: The C in C++ - Continued -

C - Basic Introduction

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Computers Programming Course 5. Iulian Năstac

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

Programming in C++ 4. The lexical basis of C++

Preview from Notesale.co.uk Page 6 of 52

EL2310 Scientific Programming

Basic Types, Variables, Literals, Constants

Computer Programming & Problem Solving ( CPPS ) Turbo C Programming For The PC (Revised Edition ) By Robert Lafore

CSCI 171 Chapter Outlines

Programming in C and Data Structures [15PCD13/23] 1. PROGRAMMING IN C AND DATA STRUCTURES [As per Choice Based Credit System (CBCS) scheme]

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

DECLARATIONS. Character Set, Keywords, Identifiers, Constants, Variables. Designed by Parul Khurana, LIECA.

UNIT- 3 Introduction to C++

COP 3275: Chapter 02. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Fundamental of C programming. - Ompal Singh

M1-R4: Programing and Problem Solving using C (JAN 2019)

DEPARTMENT OF MATHS, MJ COLLEGE

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Introduction to C programming. By Avani M. Sakhapara Asst Professor, IT Dept, KJSCE

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Chapter 21: Introduction to C Programming Language

Transcription:

BLM2031 Structured Programming Zeyneb KURT 1

Contact Contact info office : D-219 e-mail zeynebkurt@gmail.com, zeyneb@ce.yildiz.edu.tr When to contact e-mail first, take an appointment What to expect help on your problems about the course and your projects you should have spent some time on it!!! 2

General Information Lecture Hours: Tuesday between 09:00-12:00 at D111 Lab Hours: The schedule will be announced. Please check webpage at https://www.ce.yildiz.edu.tr/personal/zeyneb 3

Course Outline Review of Introduction to Computer Engineering II course (Introduction to Procedural Programming new name) Data types, control flow, and so on... C preprocessor Storage classes Scope & duration Arrays Strings, multi-dimensional arrays, pointers Dynamic memory allocation Multi-dimensional 4

Course Outline Cont d Functions parameter passing, return values recursion function pointers Structures structures, unions linked lists FILE I/O high level operations file descriptors, low level functions 5

Course Metarial Lecture notes Slides will be available at www.ce.yildiz.edu.tr Web resources Always cite your resources! Book : Peter A. Darnell and Philip E. Margolis, C: A Software Engineering Approach, Springer-Verlag, 1996 (3 rd ) edition. 6

Grading Policy Two midterms 8 th week 12 th or 13 th week Homeworks a few One term-project Labs (4+1) will be announced One final exam You have to attend 70% of the theoretical lectures and 80% of the labs Final 40% Evaluation HWs/Projects/ Labs 30% Midterms 30% 7

Please Do NOT!!! try to take credit for work done by someone else. reuse the same work for more than one class. use any resource not permitted by the instructor during an exam. communicate with an another person during an exam. 8

A fast review of C 9

Outline Program development C Essentials Functions Variables & constants Names Formatting Comments Preprocessor Data types Mixing types 10

program development The task of compiler is to translate source code into machine code source file source file source file The compiler s input is source code and output is object code. The linker combines separate object files into a single file and forms an exe file compile object file compile object file compile object file The linker also links in the functions from the runtime library, if necessary. Linking usually handled automatically. link Executable Code Runtime Library 11

program development CONT D One of the reasons C is such a small language is that it defers many operations to a large runtime library. The runtime library is a collection of object files each file contains the machine instructions for a function that performs one of a wide variety of services The functions are divided into groups, such as I/O, memory management, mathematical operations, and string manipulation. for each group there is a source file, called a header file, that contains information you need to use these functions by convention, the names for header files end with.h extention For example, one of the I/O runtime routines, called printf(), enables you to display data on your terminal. To use this function you must enter the following line in your source file #include <stdio.h> 12

C essentıals function type function name ( arguments ) { declaration s C statements A C function is a collection of C language operations. performs an operation that is more complex than any of the operations built into C language at the same time, a function should not be so complex that it is difficult to understand Arguments represent data that are passed from calling function to function being called. } 13

varıables & constants The statement j = 5 + 10; Variable Address Contents A constant is a value that never changes A variable achieves its variableness by representing a location, or address, in computer memory. j 2482 2486 2490 4 bytes 15 14

names In the C language, you can name just about anything: variables, constants, functions, and even location in a program. Names may contain letters, numbers, and the underscore character ( _ ) but must start with a letter or underscore... The C language is case sensitive which means that it differentiates between lowercase and uppercase letters VaR, var, VAR A name can NOT be the same as one of the reserved keywords. 15

names cont D LEGAL NAMES j j5 system_name sesquipedalial_name UpPeR_aNd_LoWeR_cAsE_nA me ILLEGAL NAMES 5j $name int bad%#*@name 16

names cont d - reserved keywords auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while 17

EXPRESSIONS An expression is any combination of operators, numbers, and names that donates the computation of a value. Examples 5 j 5 + j f() f()/4 A constant A variable A constant plus a variable A function call A function call, whose result is divided by a constant 18

ASSIGNMENT STATEMENTS lvalue = rvalue ; The left hand side of an assignment statement, called an lvalue, must evaluate to a memory address that can hold a value. The expression on the right-hand side of the assignment operator is sometimes called an rvalue. answer = num * num; num * num = answer; 19

formatting source code int square (num) { int answer; answer = num * num; return answer; } int square (num) { int answer; answer = num * num; return answer; } int square (num) { int answer; answer = num * num; return answer; } 20

comments A comment is text that you include in a source file to explain what the code is doing! Comments are for human readers compiler ignores them! The C language allows you to enter comments between the symbols /* and */ Nested comments are NOT supported What to comment? Function header changes in the code /* square() * Aurhor : P. Margolis * Initial coding : 3/87 * Params : an integer * Returns : square of its parameter */ 21

THE main() FUNCTION int main ( ) { extern int square(); int solution; solution = square(5); exit(0); } The exit() function is a runtime library routine that causes a program to end, returning control to operating system. If the argument to exit() is zero, it means that the program is ending normally without errors. Non-zero arguments indicate abnormal termination of the program. Calling exit() from a main() function is exactly the same as executing return statement. 22

preprocessor The preprocessor executes automatically, when you compile your program All preprocessor directives begin with pound sign (#), which must be the first non-space character on the line. unlike C statements a preprocessor directive ends with a newline, NOT with a semicolon It is also capable of macro processing conditional compilation debugging with built-in macros 23

PRINTF() and SCANF() functıons The printf() function can take any number of arguments. The first argument called the format string. It is enclosed in double quotes and may contain text and format specifiers The scanf() function is the mirror image of printf(). Instead of printing data on the terminal, it reads data entered from keyboard. The first argument is a format string. The major difference between scanf() and printf() is that the data item arguments must be lvalues int num; scanf( %d, &num); printf( num : %d\n, num); 24

preprocessor cont d The define facility it is possible to associate a name with a constant #define NOTHING 0 It is a common practice to all uppercase letters for constants naming constants has two important benefits it enables you to give a descriptive name to a nondescript number it makes a program easier to change be careful NOT to use them as variables NOTHING = j + 5 25

preprocessor cont d The include facility #include directive causes the compiler to read source text from another file as well as the file it is currently compiling the #include command has two forms #include <filename> the preprocessor looks in a special place designated by the operating system. This is where all system include files are kept. #include filename the preprocessor looks in the directory containing the source file. If it can not find the file, it searches for the file as if it had been enclosed in angle brackets!!! 26

hello world!!! #include <stdio.h> include standard input output library int main ( void ) { starting point of your program } printf( Hello World...\n ); return 0; return a value to the calling program in this case 0 to show success? 27

data types Data types void Scalar types Aggregate types Arithmetic types Pointers enum Integral types Floating types 28

data types There are 9 reserved words for scalar data types Basic types char, int, float, double, enum Qualifiers long, short, signed, unsigned To declare j as an integer int j; char double short signed int enum long unsigned float You can declare variablers that have the same type in a single declaration int j,k; All declaretions in a block must appear before any executable statements 29

different types of ıntegers The only requirement that the ANSI Standard makes is that a byte must be at least 8 bits long, and that ints must be at least 16 bits long and must represent the natural size for computer. natural means the number of bits that the CPU usually handles in a single instruction 30

different types of ıntegers Integer constants Decimal Octal Hexadecimal In general, an integer constant has type int, if its value can fit in an int. Otherwise it has type long int. Suffixes u or U l or L Decimal Octal Hexadecimal 3 003 0x3 8 010 0x8 15 017 0xF 16 020 0x10 21 025 0x15-87 -0127-0x57 255 0377 0xFF 31

floating poınt types to declare a variable capable of holding floating-point values float double The word double stands for double-precision it is capable of representing about twice as much precision as a float A float generally requires 4 bytes, and a double generally requires 8 bytes read more about limits in <limits.h> Decimal point 0.356 5.0 0.000001.7 7. Scientific notation 3e2 5E-5 32

initialization A declaration allocates memory for a variable, but it does not necessarily store an initial value at the location If you read the value of such a variable before making an explicit assignment, the results are unpredictable To initialize a variable, just include an assignment expression after the variable name char ch = A ; It is same as char ch; ch = A ; 33

ENUMERATION DATA TYPE enum { red, blue, green, yellow } color; enum { bright, medium, dark } intensity; color = yellow; color = bright; conflict intensity = bright; intensity = blue; color = 1; // Type conflict color = green + blue; usage // OK // Type // OK // Type conflict // Misleading Enumeration types enable you to declare variables and the set of named constants that can be legally stored in the variable. The default values start at zero and go up by one with each new name. You can override default values by specifying other values 34

#include<stdio.h> enum week{mon, Tue, Wed, Thur, Fri, Sat, Sun}; int main() { enum week day; day = Wed; printf("%d",day); return 0; } 35

VOID DATATYPE The void data type has two important purposes. The first is to indicate that a function does not return a value void func (int a, int b); The second is to declare a generic pointer We will discuss it later! 36

TYPEDEFS typedef keyword lets you create your own names for data types. Semantically, the variable name becomes a synonym for the data type. By convention, typedef names are capitalized. typedef long int int_32; long int j; int_32 j; 37