CSCI 171 Presentation 2. Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

11-2 Identify the parts of the “main” function, which include Preprocessor Directives main function header main function body which includes Declaration.
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Javascript Essentials How do I write it??  Start Homesite  Between the start and end BODY tags type: 
CMT Programming Software Applications
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Data types and variables
Chapter 2: Introduction to C++.
Introduction to C Programming
Basic Elements of C++ Chapter 2.
CSCI 130 Chapter 2. Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition Comments.
CSCI 130 Chapter 4 Statements, Expressions, and Operators.
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
CPS120: Introduction to Computer Science Lecture 8.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
2440: 211 Interactive Web Programming Expressions & Operators.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
UniMAP Sem1-07/08EKT120: Computer Programming1 Week2.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
UNIMAP Sem2-07/08EKT120: Computer Programming1 Week 2 – Introduction to Programming.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Chapter 2 - Introduction to C Programming
Basic Elements of C++.
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Basic Elements of C++ Chapter 2.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to CS Your First C Programs
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2: Introduction to C++.
Chapter 2 - Introduction to C Programming
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Presentation transcript:

CSCI 171 Presentation 2

Program Components main() #include Variable Definition Function Prototype Program Statements Function Call Function Definition Comments

main() Required in every program –Under normal circumstances: program starts at first statement in main program ends at last statement in main –Ex: void main() { printf(“Hello World”); }

#include Instructs C compiler to add contents of another file during linkage #include statements at top of file Usually called header files (.h) –Some supplied with compiler Never modified –Some user defined Can be modified

Example of #include Library function –#include –greater than, less than indicate library function User defined function –#include “myfile.h” –quotation marks indicate user-defined function –if not in same directory as main file, need to specify the path

Program Statements These statements do the ‘real’ work One per line, end with semi-colon Statements perform various functions: –input/output write to screen/file/printer accept from screen/file –call functions –perform calculations –other

Functions Components of a function –Prototype (signature) –Definition (header and body) –Call Functions will be discussed later –Chapter 4

Comments No effect on program execution Any text can be stored in a comment May take up part of a line, a whole line, more than one line

Examples of Comments Comments may use one line, part of a line, or more than one line: /*A single line*/ for (int x; x<10; x++) /*start of a loop*/ /*Comments can span multiple lines as well*/

Philosophy You never have too many comments Too few comments may: –cost you points in this class –cost you friends on the job –cause gray hairs Use comments when the logic is fresh in your mind Crucial to program maintenance

Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type of the variable Variables MUST be declared before they are used

Sample Program 2.1 //Example 2.1 #include int main( void ) { int a = 1, b = 2, c = 0; c = a + b; printf("%d", c); }

Naming rules for variables Only certain characters are legal –letters, digits, underscore ( _ ) First character must be a letter –underscore allowed but not advised Case sensitive –count, Count are two different variables Cannot use keywords

Variable Types Nonnumeric –character Numeric –signed, unsigned (signed is the default) –char, integer, floating point

Ranges / Memory Allocation Each type has a range of numbers How you use the variables in the program will determine what type you use –memory usage (efficiency) –magnitude of range

Variable Types (p. 47) Var TypeKeywordBytesRange Characterchar1-128 to 127 Short Integershort to Long Integer Or Integer long int 4-2,147,483,648 to 2,147,483,647 Singe precision floating point float415 digit prec. Max exp: 1308 Max value: e1308 Double precision floating point or long double double long double 819 digit prec. Max exp: Max value: e14392

Numeric Variables Variables need to be initialized before being used Ex 1: –int x;Sets aside storage space for x –x = 3;Stores the value 3 in x Ex 2: –int x = 3; –int x = 3, y, z = 4;

Sample Program 2.2 //Sample program 2.2 #include int main( void ) { char num1 = 0; int num2 = 1; double num3 = 7.2; printf("The variable num1 is in memory location: %d", &num1); printf("\nThe variable num1 utilizes %d byte(s) of memory", sizeof(num1)); printf("\nThe numeric value of num1 is: %d", num1); printf("\n\nThe variable num2 is in memory location: %d", &num2); printf("\nThe variable num2 utilizes %d byte(s) of memory", sizeof(num2)); printf("\nThe numeric value of num2 is: %d", num2); printf("\n\nThe variable num3 is in memory location: %d", &num3); printf("\nThe variable num3 utilizes %d byte(s) of memory", sizeof(num3)); printf("\nThe numeric value of num3 is: %lf", num3); }

Symbolic Constants Created using the #define preprocessor directive: –#define PI no equals sign no semi-colon no variable type PI is a symbolic constant –area = PI * radius * radius; Cannot change value –compiler error

Sample Program 2.3 //Example 2.3 #include #define PI int main( void ) { double radius = 0.0, area = 0.0; printf("Enter radius: "); scanf("%lf", &radius); area = PI * radius * radius; printf("\nThe area of your circle is: %.2lf", area); }

const const double PI = ; –data type, equals sign, and semi-colon PI is a constant Can be used in formulas –area = PI * radius * radius;

Sample Program 2.4 //Example 2.4 #include int main( void ) { const double PI = ; double radius = 0.0, area = 0.0; printf("Enter radius: "); scanf("%lf", &radius); area = PI * radius * radius; printf("\nThe area of your circle is: %.2lf", area); }

printf Will print information out to the screen –printf(“Hello world”); –printf(“Hello world\n”); prints Hello World and a new line –printf(“The area is %lf”, area); area must be a double prints: The area is x –x is the value held in the variable area

scanf Will accept information from the screen –scanf(“%d”, &area) –scanf(“%lf%lf”, &area, &diameter) Allows the user to enter a value from the keyboard –knows to wait for the enter key Stores value in corresponding variable

Conversion Specifiers Conversion char:Displays type: %ccharacter %dsigned integer %ffloat %lfdouble %sstring of text

Escape sequences Sequence:Represents: \abeep \nnew line \ttab \\backslash \”double quotes

Sample Program 2.5 //Example 2.5 #include int main( void ) { char a = 0, b = 0, name[50]; printf("Enter 2 integers separated by a comma: "); scanf("%d, %d", &a, &b); printf("Enter your name first: "); scanf(“ %s", name); printf("\nThe sum of the two numbers you entered is: %d", a + b); printf("\nThe character corresponding to ascii %d is: %c", a, a); printf("\nThe character corresponding to ascii %d is: %c", b, b); printf("\nYour name is: \"%s\"", name); printf("\n\n\aSuccessful Completion"); }

Sample Program 2.6 //Example 2.6 #include int main( void ) { char a = 0, b = 0; printf("Enter a letter: "); scanf(“ %c", &a); printf("Enter another letter: "); scanf(“ %c", &b); printf("The two characters you entered are: %c and %c", a, b); }

Statements Complete direction to carry out single task Usually one per line Whitespace ignored except in strings Ex: –x = a + b; –printf(“Hello World”);

Code Block 2 or more C statements enclosed in braces Allowed anywhere a single statement is allowed Usually only used where necessary Use indentation for readablility Ex: for (int x = 0; x < 5; x++) { printf(“The value of x is “); printf(“%d”, x); }

Expressions Anything whose evaluation yields a numeric value –PI(symbolic expression defined in program) –20literal constant –ratea variable –700 / –x = a + 10 –x = 6 + (y = 4 + 5)

Operators Instructs C to perform some operation Assignment = Mathematical

Assignment Operator Evaluates the left hand side and assigns that to the variable on the right hand side Variables should be initialized at declaration time Examples of assignment operator: int x = 3; (Declaration and initialization) x = 7; y = x + 17;

Unary Mathematical Operators Increment++increases value by 1 Decrement--decreases value by 1 Ex: x = 10; y = x++;(post increment) Ex 2: x = 10; y = ++x;(pre-increment)

Sample Program 2.7 //Example 2.7 #include int main( void ) { int x = 0, y = 0; printf("Enter a number: "); scanf("%d", &x); y = x++; y = ++x; printf("\nThe value of x is: %d", x); printf("\nThe value of y is: %d", y); }

Binary Mathematical Operators Addition+ Subtraction- Multiplication* Division/ Modulus% Ex: –int x = 100; –int y = 9; –z = x % y;(z holds the value 1)

Precedence 1.Parenthesis 2.Multiplication, division, modulus 3.Addition and subtraction Parenthesis can be used to give priority Ex: 3 * (17 +1) *

Compound Assignment Operators Shorthand method to combine assignment and binary math operations General form: –exp1 op= exp2 Examples: x *= yis equivalent tox = x * (y) x – = 6 + zis equivalent to x = x – (6 + z) y % =3is equivalent to y = y % (3)

Sample Program 2.8 //Example 2.8 #include int main( void ) { int x = 2, y = 5, z = 13, d = 0; d = x + y * x; printf("\n%d", d); d = z % y; printf("\n%d", d); d += z; printf("\n%d", d); d *= y++; printf("\n%d", d); printf("\n%d", y); }

Conditional Operator The only C ternary operator (3 operands) General form: exp1 ? exp2 : exp3; exp1 is true - entire expression evaluates as exp2 exp1 is false - entire expression evaluates as exp3

Conditional Example x = (z < 36) ? 0: 1; –If z < 36, x is set to 0, otherwise 1 z = (x < y) ? x : y; –sets z equal to the smaller of x and y

Sample Program 2.9 //Example 2.9 #include int main( void ) { int x = 0; printf("Enter an integer: "); scanf("%d", &x); (x%2 == 0) ? printf("Even") : printf("Odd"); }