Some Computer Preliminaries

Similar documents
Use of scanf. scanf("%d", &number);

Chapter 2, Part I Introduction to C Programming

Arithmetic Expressions in C

Fundamentals of Programming Session 4

Programming for Engineers Introduction to C

Fundamentals of Programming. Lecture 3: Introduction to C Programming

AN OVERVIEW OF C. CSE 130: Introduction to Programming in C Stony Brook University

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

C: How to Program. Week /Mar/05

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

Lecture 2 Tao Wang 1

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

Chapter 2 - Introduction to C Programming

CC112 Structured Programming

First Java Program - Output to the Screen

Fundamental of Programming (C)

Intro to Computer Programming (ICP) Rab Nawaz Jadoon

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

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Preview from Notesale.co.uk Page 6 of 52

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CSc Introduction to Computing

IT 374 C# and Applications/ IT695 C# Data Structures

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

1/25/2018. ECE 220: Computer Systems & Programming. Write Output Using printf. Use Backslash to Include Special ASCII Characters

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Week 2: Data and Output

LESSON 4. The DATA TYPE char

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

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

Programming with C++ as a Second Language

Chapter 2: Basic Elements of C++

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

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

Full file at C How to Program, 6/e Multiple Choice Test Bank

4. Inputting data or messages to a function is called passing data to the function.

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Introduction to C CMSC 104 Spring 2014, Section 02, Lecture 6 Jason Tang

Chapter 2. Lexical Elements & Operators

CS102: Variables and Expressions

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Unit. Programming Fundamentals. School of Science and Technology INTRODUCTION

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

Should you know scanf and printf?

Maciej Sobieraj. Lecture 1

Chapter 1 Introduction to Computers and C++ Programming

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

Lecture 2: C Programming Basic

Table of Contents Date(s) Title/Topic Page #s. Abstraction

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

Chapter 2 Basic Elements of C++

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

Variables and Constants

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

An overview of Java, Data types and variables

Chapter 1 & 2 Introduction to C Language

Basic Programming Language Syntax

BTE2313. Chapter 2: Introduction to C++ Programming

Chapter 2 THE STRUCTURE OF C LANGUAGE

Full file at

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

Chapter 2: Programming Concepts

Lab # 02. Basic Elements of C++ _ Part1

EC312 Chapter 4: Arrays and Strings

2 nd Week Lecture Notes

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

Unit 4. Input/Output Functions

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Computer Programming : C++

CS Prof J.P.Morrison

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

Program Organization and Comments

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

A function is a named piece of code that performs a specific task. Sometimes functions are called methods, procedures, or subroutines (like in LC-3).

The Java Language Rules And Tools 3

INTRODUCTION 1 AND REVIEW

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

1/29/2018. Starting a Program Executes its main Function. ECE 220: Computer Systems & Programming. The Function main Divides into Two Parts

CSCI 1061U Programming Workshop 2. C++ Basics

Getting started with C++ (Part 2)

Language Reference Manual

AMCAT Automata Coding Sample Questions And Answers

Technical Questions. Q 1) What are the key features in C programming language?

Computers Programming Course 5. Iulian Năstac

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

A Fast Review of C Essentials Part I

Tutorial No. 2 - Solution (Overview of C)

Programming. Computer. Program. Programming Language. Execute sequence of simple (primitive) instructions What instructions should be provided?

Syntax and Variables

Basics of Java Programming

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

THE FUNDAMENTAL DATA TYPES

ME 172. Sourav Saha. Md. Mahamudul Hossain Kazi Fazle Rabbi Saddam Hossain Joy Kamruzzaman Lecturer,Dept. of ME,BUET

ET156 Introduction to C Programming

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

Transcription:

Some Computer Preliminaries Before we get started, let's look at some basic components that play a major role in a computer's ability to run programs: 1) Central Processing Unit: The "brains" of the computer that actually execute all the instructions the computer runs. 2) Main Memory: All running programs and data they use must be stored here. This is only active when the computer is physically on. 3) External Memory: This is more permanent memory, such as a hard-drive, floppy disk, CD, etc. In order for any programs stored on these medium to run, they have to be transferred into main memory. 4) Operating System: Provides an interface between the machine and the user and allocates the computer's resources. 5) Compiler: A "translator" which converts a program written in a high-level computer language into machine language, a set of 0s and 1s understood by the computer. 6) Application Programs: Programs intended to be used by end-users on a computer.

How everything fits together When you boot up your computer, the first major thing that occurs, is that the operating system gets loaded into main memory. This program (the operating system), is responsible for providing the user with a nice interface, and responding to the requests of the user. For example, if the user double clicks on the Netscape icon, this invokes the computer to find the Netscape program stored on the hard drive and load it into main memory. Once the program is loaded into main memory, if no other program needs to be run, then it becomes the current program running. Any data this program needs to execute must also be loaded into main memory. All executable programs are stored in machine language, which is understood by the machine. In this class, our goal will be to learn how to write some simple programs in a high-level language, C, that can be compiled into executable programs. The vast majority of the work you all will do for this class will follow the cycle pictured on page 3 of the text. This process can be listed as follows: 1) Edit your program 2) Compile your program 3) Execute your program 4) Go to step 1, if you are not satisfied with your results. The key here is to note that changing your program doesn't automatically change how it runs. Compilation is necessary.

/* Arup Guha My First C Program 8/26/04 COP 3223 Rocks! */ #include <stdio.h> int main(void) { Our first C Program } printf("cop 3223 Rocks!\n"); return 0; Let's take this program apart line by line: /* Arup Guha My First C Program 8/26/04 COP 3223 Rocks! */ This is a comment, which is ignored by the compiler. The purpose of a comment is to help the reader identify key information about the program. The first comment in a program, the header comment, should identify the author of the program, the date the program was written, as well as a brief description of the program. We will talk about other types of comments later. The computer knows that this portion of the code is a comment because it starts with /*. Everything after these two characters until the two characters */ are encountered is ignored by the compiler and treated like a comment.

#include <stdio.h> This line is a directive to include a standard C header file. There are some pre-written functions in C that we commonly use. All the functions that control standard input and output are included in the file stdio.h. When we include this file, we are allowed to use these functions. These functions allow us to print information to the screen and read in information from the user. Nearly all of your programs will include this line. (There are many other files which can be included in for your programs as well.) int main(void) { This line signifies the beginning of the function main. All code in C resides in functions. You can think of functions as all being separate little programs. main is a special function. It is the only function that automatically EXECUTES. A program may be made up on many functions, but the computer only DIRECTLY executes main. In order for the computer to execute any other function, main must give instructions to call those functions. All functions are signified by a return type, (which is int for this function), a name (which is main for this function), and a paramter list in parentheses (which is void for this function). After this listed information, the curly brace { signifies the beginning of the function. A corresponding curly brace } ends the definition of the function.

printf("cop 3223 Rocks!\n"); This line calls a function printf which resides in stdio.h. The way this function works is that it prints out any string contained in between two sets of double quotes. However, it does not print all the characters above as seen to the screen. There are a few characters which have special codes, called escape sequences. These characters all start with a backslash: \. The code \n stands for a newline character. That simply tells the computer to advance the cursor to print information to the screen to the left hand side of the new line. This line will simply print: COP 3223 Rocks! to the screen and advance the cursor to the next line so that the next piece of information printed will start printing directly below the C instead of right after the!. return 0; Each non-void function must return a value of the proper type. For most functions except for main, this return value is meaningful. But for the function main, this value is of little significance and is only included so that the program has proper syntax. } This simply signifies the end of the function main.

Side Note about Main and Sequential Execution For your first few weeks, your programs will only comprise of one function: main. When your compiled program executes, it will simply execute each instruction in main in order, and finish when it hits the ending curly brace for main. One of the key concepts of this class is understanding sequential execution. Namely, each program specifies exact instructions for the computer to execute in a particular order without ambiguity. When writing your programs, you must take this into account. You must be precise about the statements you ask the computer to execute and exactly which order you want them to execute. Variables Computers are powerful because they can manipulate data. However, the computer must have a standardized manner of storing information. Primitive data types native to the C language aid this standardization. In particular, in order to store data in a C computer program, you must store it one of the primitive data types given by C. Of these data types, the most common ones you will use are: int, char, float, double A type specifies what will be stored. For example, an int stores an integer in between -2 31 and 2 31-1. Typically, most applications don't compute integers outside of this range, so you can mostly think of an int as storing an integer.

A char stores a single character. A character includes any key on the keyboard, along with a few "special" characters that are not necessarily printable. Incidentally, all characters are stored inside the computer as integers in between 0 and 255. The numeric value of a character is known as its ascii value. We'll talk more about these later. Strings, which we will often use, technically are not primitive data types, but are rather several char variables strung together. float and double store decimal numbers to varying precision. floats store decimal numbers to about 6 or 7 digits of precision, while doubles store decimal numbers to about 13 digits of precision. How to use a variable in a program In order to use a variable in a program, you must first declare the variable. Once you declare it, typically, you should initialize the value of the variable. These two things can be done in one step. Here is an example of a variable declaration: int feet_in_mile; After this line is executed, the computer allocates space for an integer variable. The name given to that space is feet_in_mile. Pictorially, we have something as follows: feet_in_mile Based on this line of code, the variable (the box) may store any value. We can initialize the value as follows: feet_in_mile = 0;

This tells the computer to take the value on the right of the =, and store it in the variable to the left of the =. Note that all statements in C end in a semicolon. (Occasionally, you will see situations that seem to contradict this rule, but in fact, in those situations, the structure in question turns out not to be a single statement.) The picture after this statement is as follows: feet_in_mile 0 Once a variable has a value, it can be used in statements for computational purposes. First we will show a sample program that uses variables, then we will discuss common practices with variables, assignment statements and arithmetic expressions. (These are the separate components of the following program.) /* Arup Guha My Second C Program 8/26/04 Computes the number of feet in a mile */ #include <stdio.h> int main(void) { int feet_in_mile, yards_in_mile; int feet_in_yard; } yards_in_mile = 1760; feet_in_yard = 3; feet_in_mile = yards_in_mile*feet_in_yard; printf("mile = %d Feet.\n", feet_in_mile); return 0;

The first two lines of the program declare all the variables for the function main. All variables inside of a function must be declared at the beginning of the function. Thus, after these two lines, our pictures looks like: feet_in_mile yards_in_mile feet_in_yard Note, these boxes may NOT be contiguous in the computer's memory. I have only drawn them this way because it's easier for me to do so. The next two lines initialize the variables yards_in_mile and feet_in_yard. Here is the picture after these two lines are executed: feet_in_mile yards_in_mile feet_in_yard 1760 3 The next line is an assignment statement. (Technically speaking, so are the two previous lines we just went over.) The syntax of an assignment statement is that a variable is always on the left hand side of an equal sign, and an arithmetic expression is always on the right hand side, followed by a semicolon. The order of execution is as follows: 1) The value of the arithmetic expression on the right is determined using the current values of the variables in this expression. Note: There may also be constants in this expression.

2) This value is then stored in the variable on the left hand side. Using this procedure, we evaluate the expression: yards_in_mile*feet_in_yard This evaluates to 1760*3, using the current values of each of the respective variables. Simplifying this numerical expression we get 5280. Finally, this value is stored in feet_in_mile. The picture after this step is as follows: feet_in_mile yards_in_mile feet_in_yard 5280 1760 3 Finally, the last line prints out some information to the screen. Up until now, we only talked about how to print characters to the screen. But now, we would like to print the value of a variable to the screen. In order to do this, we must specify the format of the variable. %d is the format for an integer variables. A list of format codes is given in the text on page 16. (Note: The code for a float is %f, and a double is %lf.) This code tells the computer what type of variable will be printed. After the ending double quote, the actual variable itself must be specified. This is done by first typing a comma, followed by the name of the variable. The printf function call must then be closed with a close parethesis. The end result of running this line the statement: Mile = 5280 Feet. will print to the screen.