lecuter1.pdf
Document Details

Uploaded by IllustriousSuprematism4194
Zagazig University
Full Transcript
CSE121: Computer Programming (2) )2( برمجة الحاسب:121 هحس 1st Year Computer and Systems Engineering & Power and Electrical Machines Engineering...
CSE121: Computer Programming (2) )2( برمجة الحاسب:121 هحس 1st Year Computer and Systems Engineering & Power and Electrical Machines Engineering Zagazig University Fall 2022 Lecture #1 Dr. Ahmed Amer Shahin Dept. of Computer & Systems Engineering These slides are adapted from the slides accompanying the text “C How to Program, 8/e” slides, https://deitel.com/c-how-to-program-8-e/ Copyright Deitel 2016 Teaching Staff Instructors: – Dr. Ahmed Amer Shahin – Email: [email protected] – Dr. Sanaa Fikry – Email: [email protected] – Lectures: Thursday 08:30am – 10:00am (1)ج Teaching Assistant: – Eng. Aya, Eng. Hisham, & Eng. Mahmoud Course Info منصة الكلية – Materials – Quizzes Microsoft Teams (as a backup) –Online Classes –Assignments Course Info Textbook: – “C How to Program,” Paul Deitel, 8th Edition, 2016, – “C Programming - A Modern Approach,” K. N. King, 2nd Edition, 2008 – “The C Programming Language,” B. W. Kernighan and D. M. Ritchie, 2nd Edition, 1988 Course Info (Cont.) Grading: Distribution Distribution Course work حاسبات قوى Semester Work 25pt 20pt Practical/Oral Exam 20pt 20pt Final Exam 80pt 60pt Total Points 125pt 100pt *Grade distribution is subject to change Course Overview Introduction to C Input/ output Arithmetic Operations Decision Making Loops Functions Arrays Pointers Structures … OVERVIEW OF COMPUTERS AND PROGRAMMING Electronic Computers Then and Now computer (processor) chip – a silicon chip containing the circuitry for a computer processor hardware – the actual computer equipment software – the set of programs associated with a computer © 2016 Pearson Education, Ltd. All rights reserved. 8 Electronic Computers Then and Now program – a list of instructions that enables a computer to perform a specific task binary number – a number whose digits are 0 and 1 © 2016 Pearson Education, Ltd. All rights reserved. 9 Computer Hardware main memory secondary memory central processing unit input devices output devices © 2016 Pearson Education, Ltd. All rights reserved. 10 Memory memory cell – an individual storage location in memory address of a memory cell – the relative position of a memory cell in the computer’s main memory contents of a memory cell – the information stored in a memory cell, either a program instruction or data © 2016 Pearson Education, Ltd. All rights reserved. 11 Memory byte – the amount of storage required to store a single character bit – a binary digit, a 0 or a 1 Bit 00101100 Byte © 2016 Pearson Education, Ltd. All rights reserved. 12 Computer Languages machine language – binary number codes understood by a specific CPU assembly language – mnemonic codes that correspond to machine language instructions high-level language – machine-independent programming language that combines algebraic expressions and English symbols © 2016 Pearson Education, Ltd. All rights reserved. 13 Computer Languages compiler – software that translates a high-level language program into machine language source file – file containing a program written in a high-level language; the input for a compiler syntax – grammar rules of a programming language object file – file of machine language instructions that is the output of a compiler © 2016 Pearson Education, Ltd. All rights reserved. 14 Computer Languages linker – software that combines object files and resolves cross-references to create an executable machine language program integrated development environment (IDE) – software package combining a word processor, compiler, linker, loader, and tools for finding errors © 2016 Pearson Education, Ltd. All rights reserved. 15 Entering, Translating, and Running a High-Level Language Program © 2016 Pearson Education, Ltd. All rights reserved. 16 Executing a Program input data – the data values that are scanned by a program program output – the lines displayed by a program © 2016 Pearson Education, Ltd. All rights reserved. 17 The Software Development Method Specify the problem Analyze the problem. requirements. Design the algorithm Implement the to solve the problem. algorithm Test and verify the Maintain and update completed program. the program. © 2016 Pearson Education, Ltd. All rights reserved. 18 INTRODUCTION TO C PROGRAMMING © 2016 Pearson Education, Ltd. All rights reserved. 19 Why C Currently, the most commonly-used language for embedded systems Very portable: compilers exist for virtually every processor Easy-to-understand compilation Produces efficient code © 2016 Pearson Education, Ltd. All rights reserved. 20 History of C Refer to – https://en.wikipedia.org/wiki/C_(programming_l anguage)#History © 2016 Pearson Education, Ltd. All rights reserved. 21 Where to Write and Run C Programs PC – CodeBlocks IDE – VSCode – Visual Studio Online – https://www.onlinegdb.com/online_c_compiler – https://www.programiz.com/c- programming/online-compiler/ © 2016 Pearson Education, Ltd. All rights reserved. 22 A Simple C Program: Printing a Line of Text We begin by considering a simple C program. Our first example prints a line of text © 2016 Pearson Education, Ltd. All rights reserved. 23 A Simple C Program: Comments // Fig. 2.1: fig02_01.c // A first program in C – begin with //, indicating that these two lines are comments. – Comments document programs and improve program readability. – Comments do not cause the computer to perform any action when the program is run. © 2016 Pearson Education, Ltd. All rights reserved. 24 A Simple C Program: Comments (Cont.) Comments are ignored by the C compiler and do not cause any machine-language object code to be generated. Comments also help other people read and understand your program. © 2016 Pearson Education, Ltd. All rights reserved. 25 A Simple C Program: Comments (Cont.) You can also use multi-line comments in which everything from at the end of the line is a comment. We prefer // comments because they’re shorter and they eliminate the common programming errors that occur with comments, especially when the closing */ is omitted. © 2016 Pearson Education, Ltd. All rights reserved. 26 A Simple C Program: Preprocessor Directive #include Preprocessor Directive #include – is a directive to the C preprocessor. Lines beginning with # are processed by the preprocessor before compilation. Line 3 tells the preprocessor to include the contents of the standard input/output header () in the program. This header contains information used by the compiler when compiling calls to standard input/output library functions such as printf. © 2016 Pearson Education, Ltd. All rights reserved. 27 A Simple C Program: White Spaces You use blank lines, space characters and tab characters (i.e., “tabs”) to make programs easier to read. Together, these characters are known as white space. White-space characters are normally ignored by the compiler. © 2016 Pearson Education, Ltd. All rights reserved. 28 A Simple C Program: The main Function int main( void ) – is a part of every C program. – The parentheses after main indicate that main is a program building block called a function. C programs contain one or more functions, one of which must be main. Every program in C begins executing at the function main. © 2016 Pearson Education, Ltd. All rights reserved. 29 A Simple C Program: The main Function (Cont.) The keyword int to the left of main indicates that main “returns” an integer (whole number) value. We’ll explain what it means for a function to “return a value” when we demonstrate how to create your own functions. © 2016 Pearson Education, Ltd. All rights reserved. 30 A Simple C Program: The main Function (Cont.) For now, simply include the keyword int to the left of main in each of your programs. Functions also can receive information when they’re called upon to execute. The void in parentheses here means that main does not receive any information. © 2016 Pearson Education, Ltd. All rights reserved. 31 A Simple C Program: Blocks of Code A left brace, {, begins the body of every function A corresponding right brace , }, ends each function This pair of braces and the portion of the program between the braces is called a block. © 2016 Pearson Education, Ltd. All rights reserved. 32 A Simple C Program: Output Statement printf( "Welcome to C!\n" ); – instructs the computer to perform an action, namely to print on the screen the string of characters marked by the quotation marks. – A string is sometimes called a character string, a message or a literal. The entire line, including the printf function (the “f” stands for “formatted”), its argument within the parentheses and the semicolon (;), is called a statement. Every statement must end with a semicolon (also known as the statement terminator). © 2016 Pearson Education, Ltd. All rights reserved. 33 A Simple C Program: Output Statement (Cont.) When the preceding printf statement is executed, it prints the message Welcome to C! on the screen. The characters normally print exactly as they appear between the double quotes in the printf statement. © 2016 Pearson Education, Ltd. All rights reserved. 34 A Simple C Program: Escape Sequences Notice that the characters \n were not printed on the screen. The backslash (\) is called an escape character. It indicates that printf is supposed to do something out of the ordinary. When encountering a backslash in a string, the compiler looks ahead at the next character and combines it with the backslash to form an escape sequence. © 2016 Pearson Education, Ltd. All rights reserved. 35 A Simple C Program: Escape Sequences (Cont.) The escape sequence \n means newline. When a newline appears in the string output by a printf, the newline causes the cursor to position to the beginning of the next line on the screen. © 2016 Pearson Education, Ltd. All rights reserved. 36 A Simple C Program: Escape Sequences (Cont.) Some common sequences are listed Below © 2016 Pearson Education, Ltd. All rights reserved. 37 A Simple C Program: Escape Sequences (Cont.) Because the backslash has special meaning in a string, i.e., the compiler recognizes it as an escape character, we use a double backslash (\\) to place a single backslash in a string. Printing a double quote also presents a problem because double quotes mark the boundaries of a string—such quotes are not printed. By using the escape sequence \" in a string to be output by printf, we indicate that printf should display a double quote. © 2016 Pearson Education, Ltd. All rights reserved. 38 A Simple C Program: Using Multiple printfs The printf function can print Welcome to C! several different ways. For example, the following program produces the same output as before. © 2016 Pearson Education, Ltd. All rights reserved. 39 A Simple C Program: Using Multiple printf This works because each printf resumes printing where the previous printf stopped printing. The first printf prints Welcome followed by a space and the second printf begins printing on the same line immediately following the space. © 2016 Pearson Education, Ltd. All rights reserved. 40 A Simple C Program: printf (Multiple Lines) One printf can print several lines by using additional newline characters as shown below. Each time the \n (newline) escape sequence is encountered; output continues at the beginning of the next line. © 2016 Pearson Education, Ltd. All rights reserved. 41