ET156 Introduction to C Programming Unit 1 INTRODUCTION TO C PROGRAMMING: THE C COMPILER, VARIABLES, MEMORY, INPUT, AND OUTPUT Instructor : Stan Kong Email : skong@itt tech.edutech.edu
Figure 1.3 Components of a Computer
Key Components of a Central Processing Unit (Processor) Processor Clock Instruction control unit Arithmetic and logic unit Registers
Main Memory Holds active programs and data. Contents: Bit: One binary digit 0 it or 1 Byte: Eight bits 1 character Word: A group of bytes Write is a destructive operation. Read is not a destructive operation. ROM Read only Memory. (non volatile). RAM Random Access Memory (volatile)
Input Input and Output The act of transferring data into memory from a peripheral p device. (for example : keyboard) Output The act of transferring data from memory to a peripheral device. (for example : Computer monitor) Input and Output Support the interaction between the user and Support the interaction between the user and the computer.
Secondary Storage Extension of main memory Fast, accurate, and inexpensive High capacity and nonvolatile Long term storage Contents must be transferred into main memory. Machine readable only People cannot read contents directly. Mdi Media Hard disk and floppy disk Magnetic tape CD ROM and DVD
Figure 1.5 Relationship between a Byte and a Bit
Figure 1.11 Entering, Translating, and Running a High Level Language Program
Figure 1.12 Flow of Information during Program Execution
Problem Solving
The Software Development Method 1. Specify the problem requirements. 2. Analyze the problem. 3. Design the algorithm to solve the problem. 4. Implement the algorithm. 5. Test and verify the completed program. 6. Maintain and updatetheprogram the program.
Variable Declarations Thememorycells used for storing a program s input data and its computational results are called variables. The variable declarations tell the C compiler the names of all variables and what kind of information will be stored in each variable. The general format for variable declaration is: Data type variable name or variable list; Example : int count; where int is data type of integer and count is the variable name.
C Reserved Words auto double include static break elif int struct case else long switch char enum main typedef const extern register union continue float return unsigned default for short void define goto signed volatile do if sizeof while
Hello World Example #include <stdio.h> /*Hello World Example */ int main(void) { printf( Hello World"); /* Comments can go here */ } return (0);
C Program Example #include <stdio.h> #define PI 3.14159 /*Your name*/ /*This programacceptsaccepts the radius ofa circle, calculates the area, andoutputs the radius and the area.*/ int main(void) ){ double radius; /*Stores the radius entered by the user*/ double cir; /* circumferre */ double area; /*Stores the area after the calculation*/ printf("enter the radius of the circle>"); scanf("%lf", &radius); area = PI * radius * radius; /*Calculates the area as PI X radius X radius and stores the results in area*/ printf("the area of a circle with a radius of %f is %f\n", radius, area); return (0); }
The Software Development Method 1. Specify the problem requirements. 2. Analyze the problem. 3. Design the algorithm to solve the problem. 4. Implement the algorithm. 5. Test and verify the completed program. 6. Maintain and updatetheprogram the program.
Algorithm to Program Include required libraries Define constants Declare variables Get the input Perform process to transform input tinto the output Display output t Refine the program
Preprocessor Directives Provide instructions to the preprocessor. Begin with # #include Used to load a library, such as a standard library #include <stdio.h> #define Used to define a constant macro #define PI 3.14159
Function main The point at which program execution begins Contains declarations and executable statements Standard format is: int main(void) { /*declarations*/ */ } /*executable statements*/ return (0);
scanf Function scanf("%lf", &variable name); Read the floating point number into the address of the memorycell referenced by the variable miles.
printf Function function arguments printf("that equals %f kilometers.\n", kms); function name format string print list That equals 16.090000 kilometers
Start Pelles C IDE/Create Project
Add win32 console project Enter project name for example : lab1 Select win32 console and check create new workspace.
Add source File to Project
Key in source code
Build /Execute Project
Output
Homework, Labs Homework Assignment due Week 2 Lab Assignment due to day. Email your homework assignment, lb labs to skong@itt tech.edu