0% found this document useful (0 votes)
23 views16 pages

1

Uploaded by

Nurul Adnin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
23 views16 pages

1

Uploaded by

Nurul Adnin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 16
INTRODUCTION CHAPTER 1 Objective: Having completed this chapter the student should be able: = to understand the general overview of the C programming language ~ to-use flowchart symbols - to write pseudocode to represent simple programming logic = to be able to compile, link and run sample C programs 11 THE COMPUTER Before considering the programming language, we give a brief overview of the basic structure of a computer. The architecture of a typical computer consists of input devices, a central processing unit (CPU), main memory, output devices and auxiliary storage devices as illustrated in Figure 1. These are known as the hardware component of a computer systems Keyboard, mouse ae Input Devices Auxiliary Storage Devices Output Devices Figure 1: Basic hardware components of a computer i, input devices: usually @ keyboard where programs and data are entered into the computer. Examples of other input devices include mouse, touch sereen, 12 13 joystick, microphone; others are sensors or transducers, which convert external signals into data, e.g. a scanner. i. CPU: executing instructions such as arithmetic calculations and data comparisons, The CPU consists of two main components: an. arithmetic logic unit (ALU) and a control unit (CU). +The Control Unit (CU) — controls the fetching of instructions from the main memory and the subsequent execution of these instructions. Among other tasks carried out are the control of input and output devices and the passing of data to the Arithmetic/Logical unit for computation, +The Arithmetic/Logical Unit (ALU) — carries out arithmetic operations on integer (whole number) and real (with a decimal point) operands. It can also perform simple logical tests for equality and greater than and less than between operands. ii, main memory: where the programs and data are stored temporarily during processing, v. output devices: usually 2 monitor or a printer to show output. The ouput display on the computer is referred as soft copy and the output is printed on the printer then we say we have a hard copy. v. auxiliary storage devices: a place where the programs and data are stored petmanently even when we turn off the computer. Examples of storage devices include tapes, hard disks, floppy disks and CDs. What is a programming language? A programming language is a language with grammar rules, symbols and special characters used to construct a computer program. Programming is a problem solving activity. Algorithm is alist of steps for solving a problem. Algorithm for a programming problem: 1. get the data 2. perform the computations 3. display the results Implementing the algorithm involves writing it as a program. Each step in the algorithm is converted into statements in a programming language, A Brief History of C Ken Thompson created the B language in 1969 from Martin Richard's BCPL (Basic Combined Programming Language). He used assembly language and B to produce the initial versions of the UNIX operating system, BCPI. and B were typeless languages in which variables were simply words in memory. Many of the important ideas of C stem from the language BCPL, developed by Martin Richards. The influence of BCPL on C proceeded indirectly through the language B, which was written by Ken ‘Thompson in 1970 at Bell Labs, for the first UNIX system on a DECPDP-7. BCPL and B are “type less” languages whereas C provides a variety of data types. C was invented specifically to implement UNIX. 14 15 In 1972, Dennis Ritchie of Bell Laboratories later converted B into C by retaining most of B’s syntax and in 1978, Kernighan and Ritchie wrote a book entitled “The C Programming Language” that became the language definition for almost a decade. In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern, comprehensive definition of C. The resulting definition, the ANSI standard, or “ANSI C” was completed late 1988 ‘You can learn more about the history and development of C by visiting the following Web page: hitp://cm,bell-labs.com/cm/cs/who/dme/chist html Why use C? C is 2 popular structured programming language, which allows you to develop programs using well-defined control structures and provides modularity (breaking the task into multiple sub tasks that are simple enough to understand and to reuse). Powerful features, simple syntax, and portability make C a preferred language among programmers for business and industrial applications. In recent years, it has replaced Pascal and Fortran as the language of choice among students, hobbyists, scientists, and professional software developers. C itself is being replaced with C++, an object oriented language, for developing desktop computer programs. ‘The largest measure of C’s success seems to be based on practical considerations: the portability of the compiler the standard library concept a powerful and varied repertoire of operations an elegant syntax ready access to the hardware when needed and the ease with which applications ean be optimized by hand coding isolated procedures In fact, popular operating systems such as Unix and Linux are written entirely in C. Additionally, even compilers and interpreters for other languages such as FORTRAN, Pascal, and BASIC are written in C. A partial list of areas where C language is used: Systems Programming, Artificial Intelligence Computer Graphics Image Processing EXECUTING A ‘C’ PROGRAM The process of exceuting a C program requires at least four ste 1. editing (or writing) the program 2. compiling the progtam. 3. linking the program with functions that are needed from the C library 4 executing the program 16 Editing ‘You write a computer program with words and symbols that are understandable to human beings. This is the editing part of the development cycle. You type the program directly into a window on the screen and save the resulting text as a separate file, The text of a C program is stored in a file with the extension .c. filenames ‘This file will be input to compiler which is known as a source file. Compiling You cannot directly execute the source file. To run on any computer system, the source file must be translated into binary numbers understandable to the computer's Central Processing Unit, This process produces an intermediate object file — with the extension .obj, the .obj stands for object. Linking Linking is necessary since many compiled languages come with library routines which can be added to your program. These routines are written by the manufacturer of the compiler to perform a variety of tasks, ftom input/output to complicated mathematical functions. In the case of C the standard input and output functions ate contained in a library (stdio.h) so even the most basic program will require a library function. After linking the file extension is .exe which are executable files. Executable files ‘The text editor will produces .c source files, which go to the compiler, which produces .obj object files, which go to the linker, which produces .exe executable file. In a typical program execution, the program reads data either from the user or from a file for processing. After the program processes the data, the output is prepared to the user’s monitor o to a file. FLOWCHARTS Flowchart is a graphie representation of an algorithm, often used in the design phase of programming to work out the logical flow of a program. Standard graphical symbols are used to represent the algorithm, Flowchart Symbols Cc) Start/End ‘The terminator symbol marks the starting or ending point of the programs, It usually contains the word "Start" or "End." dgGhog 05 Some guidelines in flowcharti Action or Process Computational steps or processing function of a program. Document A printed document or report. Decision A decision or branching point. Lines representing different decisions emerge ftom different points of the diamond. Input/Output Input of output operation Connector Connector or joining of two parts of program Flow Line Lines indicate the sequence of steps and the direction of flow. Delay Indicates a delay in the process. Merge Indicates a tep where two or mote sub-lists or sub-processes become one, Subroutine Indicates a sequence of actions that perform a specific task embedded within a larger process. This sequence of actions could be deseribed in more detail on a separate flowchart. 1g a. In drawing a proper flowchatt, all necessary requirements should be listed out in logical order. b. The flowchart should be clear, neat and easy to follow. There should not be any room for ambiguity in understanding the flowchart. c._The usual direction of the flow of a procedure or system is from left to right or top to bottom. Only one flow line should come out from a process symbol, J o Basic Flowchart (start x AN m] Action eno Figure 2: Basic flowchart Example 1: Draw a flowchart to find the sum of first 50 natural numbers. Answer: [ starr suM=0 Wel + SUM=SUM+¥ Fig. 3: Flowchart for the sum of first 50 natural numbers Example 2: Draw a flowchart to find the largest of three numbers A, B, and C. Answer: Fig 4: Flowchart for finding out the largest of thtee numbers 1.7 PSEUDOCODE Pseudocode consists of short, English phrases used to explain spe program's algorithm, fc tasks within a It describes the entire logic of the algorithm so that implementation becomes a task of translating line by line into source code. Most of the English-like statements in the pseudocode are easy to understand, Example 3: Draw a flowchart and write a pseudocode for a program that adds a series of user-entered numbers. ‘The program should display the sum and terminate when the user enters 0. Answer: Pseudocode Flowchart: Begin procedure sum =0 Do Input number sum = sum + number Loop until number = 0 output sum End procedure cum = sum + aumber Yee Example 4: Vrite a pseudocode for the average of three integers and print the average of those numbers, Answer: Pseudocode Flowchart Begin input x,y,z sum=x+y+z avg=sum/3.0 End sum = xt+ytz avg = sum/3.0 print avg, Comments in Programs Programmers can make a program casier to understand by using comments to describe the purpose of the program and the purpose of each program step. A comment is a note to youtself (or others) that you put into your source code. All comments ate ignored by the compiler. ‘A comment starts with a /* and ends with */ Example 5: OM Hearne /* Purpose: This is @ comment ¥ /*Filename: testl.c */ /*Programming Language: C [REE EA EEA T EAE ‘The second format, the line comment, uses two slashes // to identify a comment isa line comment 18 YOUR FIRST C PROGRAM ‘The following program is written in the C programming language. Hinclude main() { } printf("Programming in C is easy.\n")s ‘Though the program is simple, let us analyze the program. Hinclude /*standard input output header file*/ main() /*every C program requires a function called main*/ { /*start of program*/ printf("Programming in C is easy.\n"); /*prints the words on the screen*/ } /*end of program*/ ‘This program when executed, will produce the following output: Programming in C is easy. Figure below shows the structure of a simple C program, Preprocessor Directives (Global Declarations maid) { Local Declarations Statements 3 ‘Other functions All preprocessor commands start with a pound sign (#) and come at the beginning of the program. There can be no space between the pound sign and the keyword ‘The syntax for the preprocessor command which tells the compiler to include the standard input/output library file in the program is shown below. Hinclude “The name of the header file is stdio.h which is the abbreviation for the standard input/output library file Simple steps to create and run a C Program in Visual C++ Step 1: Start Visual C+ using Start | Programs | Microsoft Visual C++ or by double clicking the VC++ icon on the desktop. 10 Step 2: The main menu of the developer studio with multiple windows should be displayed when C1+ finishes initializing, see Fig 5 (below). opsres ts secten is Mia asof Wis! Cr Step 3: Click File, then Click New, see Fig § (above). Step 4: Select the Files tab in the dialog window, and then select C++ Source File, see Fig 6 (below). |. select this tab 2. Choose C++ Source File Sa AERTS] il Step 5: Type in your program using standard keys. C+ will color code keywords to help you in your entry. See Fig 7 acug “(ome a_i Smet a Type your program here s Sw ea TART OA jal Pa Fig7 Step 6: When you have finished typing in, click on Compile in the menu bar and, select Compile, see Fig 8 (below), C++ will ask you if you want to create default workspace, click Yes. 12 eee ce) me o--- ORY sai eS —E 2. Click YES he erm ! Fig 8 Step 7: Click on Build icon in the menu bar. See Fig 9. (ee er rt Be Bl ea @ 260) seo. BREA [a a j Fig 9 13 Step 8: If your program is clean when compiles, then clicking on the ! icon, see Fig 10 (below), will execute your program in a separate DOS window, see Fig 11. Dc GR so Bort Bee BA Dok Hr BA ee (me o-- BAYH [ea “aga — I earrus [Bieta | eit ("Promeawog a8 C20 aun) 4, Click to execute 5. Ready to execute since no TD CL RTT RTE RT —T. Fig 10 14 eee ch GS Dent Bt BA Wid x 2 eee jae = nine Sin ea Slide — = HRs oer tae = Blea a Sona — |), s! TE aa ETT ig 11 Step 9: If your program have errors during compiling, the errors are listed in the bottom window. Double click on a line with an error number highlights the source line which has the error, Fix all the errors, then do the build again until you get a clean compile and then follow Step 8. Step 10: When you execute your program, if there are logic errors, go back to step 6 and make the changes and repeat the other steps. EXERCISE, 1, TRUE or FALSE AC program must starts with a function MAIN(). b. The statement printf(\a\n\n\n); will create 4 blank lines. c. The statement printf(“\n\n\n\n"); will create 4 blank lines. 4. Comments are ignored by the compiler and are used to improve program readability €. Callows us to write a nested comments. £ main() { } is a complete and correct C program. g AC statement must be terminated with a period 2. ‘The C program main { printf(“hello” “world” “\n"); t will print out “hello world” will print out “hello” on one line followed by “world” on the next line ‘will not compile correctly . will ust print out “hello” coeP 3. What is the only fanetion all C programs must contain? A. start() B. system() C. main() D. program() 4, Which of the following is a correct comment? A. */ Comments */ B.** Comment * C. /* Comment */ D. { Comment } 5 C source files have names ending with Ave B. cpp C. bas D. him B. doc 16

You might also like