CSCE 206 Structured Programming in C

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 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
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
41 A Depth Program #include int main(void) { int inches, feet, fathoms; //declarations fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“Wreck.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Introduction to C Programming
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
A First Book of ANSI C Fourth Edition
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
C Programming Lecture 4 : Variables , Data Types
Programming With C.
© 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.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
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.
Lecture2.
Bill Tucker Austin Community College COSC 1315
Chapter 1: Introduction to computers and C++ Programming
Chapter 2 Introduction to C++ Programming
CSC201: Computer Programming
CSE 220 – C Programming C Fundamentals.
Chapter 2 - Introduction to C Programming
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Getting Started with C.
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to CS Your First C Programs
INPUT & OUTPUT scanf & printf.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
CSCE 206 Lab Structured Programming in C
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Lecture3.
Introduction to C Topics Compilation Using the gcc Compiler
Programs written in C and C++ can run on many different computers
Chapter 2 - Introduction to C Programming
An Overview of C.
CSCE 206 Lab Structured Programming in C
Introduction to C Programming
Chapter 2 part #1 C++ Program Structure
Getting Started With Coding
Presentation transcript:

CSCE 206 Structured Programming in C Ki Hwan Yum

Chapter 1 Writing an ANSI C Program

CSCE 206 Struectured Programming in C Basic Terms Programs: instruct machines to carry out specific tasks or to solve specific problems. Algorithm: a step-by-step procedure that accomplishes a desired task. Computer: a digital electronic machine composed of 3 main components: processor, memory, input/output. The processor carries out instructions stored in the memory. CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C Basic Terms Operating System: a collection of special programs OS oversees and coordinates the resources of the machine. OS provides tools to users. (e.g. text editor, C compiler, etc.) C code is called source code. A file containing source code is called a source file. CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C First Program – hello.c /* First program */ #include <stdio.h> int main(void) { printf(“Hello, world!\n”); return 0; } CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C Dissection of hello.c /* ……… */: comments. Ignored by the compiler. Lines that begin with #: preprocessing directives. #include causes the preprocessor to include a copy of the standard header file stdio.h at this point in the code. The angle brackets(< >) indicate that this file is to be found in the “usual place”. This stdio.h file is included because it contains information about the printf() function. CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C Dissection of hello.c {: A left brace begins the body of each function. A corresponding right brace must end the function. Braces are also used to group statements together. printf(): a standard library function that prints on the screen. “Hello, world\n”: string constant surrounded by double quotes. \n represent a single character called newline. CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C Dissection of hello.c All declarations and statements in C end with a semicolon. return 0: zero is returned by main() to the OS. Zero means the program completed successfully. Nonzero values are used to tell the OS that main() has been unsuccessful. }: This right brace matches the left brace and ends main(). CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C Another hello.c #include <stdio.h> int main(void) { printf(“Hello, “); printf(“world!\n”); return 0; } Looks different, but the output is the same. CSCE 206 Struectured Programming in C

Adding “Hello, universe!” #include <stdio.h> int main(void) { printf(“Hello, world!\n“); printf(“Hello, universe!\n”); return 0; } Two printf() statements can be replace with one printf() statement. printf(“Hello, world!\nHello, universe!\n); CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C Variables Variables are used to store values. Different kinds of variables are used to store different kinds of data. => The type of each variable must be specified. (integer, float, etc.) CSCE 206 Struectured Programming in C

Example Program - Hesperus Algorithm for the Hesperus Assign the number of fathoms to a variable. Convert fathoms to feet and store in a variable. Convert feet to inches and store in a variable. Print the different units of measure neatly on the screen. CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C #include <stdio.h> int main(void) { int inches, feet, fathoms; fathoms = 7; feet = 6 * fathoms; inches = 12 * feet; printf(“%d fathoms\n”, fathoms); printf(“%d feet\n”, feet); printf(“%d inches\n”, inches); return 0; } CSCE 206 Struectured Programming in C

Dissection of the Program In order to use printf(), we need to include the standard header file stdio.h. int inches, feet, fathoms;: A declaration. The variables are declared to be of type int (integer), one of the fundamental types in C. All variables in a program must be declared before they can be used. Declarations end with a semicolon. CSCE 206 Struectured Programming in C

Dissection of the Program fathoms = 7;: This is an assignment statement. = is the basic assignment operator in C. The value of the expression on the right side of = is assigned to the variable on the left side. *: multiplication operator. printf(“%d fathoms\n”, fathoms);: printf() has two arguments. The first argument is always a string, called the control string. %d is called a format that causes the value of the expression in the second argument. CSCE 206 Struectured Programming in C

General Form of a Simple Program preprocessing directives int main(void) { declarations statements } CSCE 206 Struectured Programming in C

Declarations and Statements The declarations must come before the statements. They tell the compiler what kind of data can be stored in each of the variables. The statements in the program carry out the desired computations and display information on the screen. CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C Identifiers A variable name (identifier) consists of letters, digits and underscores. It may not begin with a digit. Identifiers should be chosen to reflect their use in the program. They can serve as documentation. Make the program more readable. Certain keywords (reserved words) cannot be used as identifiers. (char, int, for, while, etc.) See p. 46 for all keywords in C. CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C Expressions Expressions typically are found on the right side of assignment operators and as arguments to functions. Simplest: constants (e.g. 6), a variable Combinations of operators with variables and constants Binary arithmetic operators: +, -, *, /, % Integer division: the result is an integer 29/5 = %: Modulus operator. The remainder after division 29%5= CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C char Type character type #include <stdio.h> int main(void) { char c; c = ‘A’; printf(“%c\n”, c); return 0; } CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C Floating Types 3 floating types: float, double, long double used to manipulate real numbers (or floating numbers). integer 7 ≠ floating 7.0 CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C Example #include <stdio.h> int main(void) { float x, y; x = 1.0; y = 2.0; printf(“The sum is %f\n”, x+y); return 0; } CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C Initialization When variables are declared, they may also be initialized. char c = ‘A’; int i = 1; CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C #define and #include #include <filename>: The preprocessor looks for the file only in standard places (typically /usr/include). #include “filename”: A search for the file is made first in the current directory and then in other system-dependent places. #define LIMIT 10: The preprocessor changes all occurrences of LIMIT to 10. CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C printf() and scanf() printf() is used for printing formatted output. scanf() is used for reading formatted input. Both have a list of arguments. control_string and other_arguments CSCE 206 Struectured Programming in C

printf() Conversion Characters How the corresponding argument is printed c character d decimal integer e floating-point number in scientific notation f floating-point number g e-format or f-format, whichever is shorter s string CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C Example printf(“Get set: %s %d %f %c%c\n”, “one”, 2, 3.33, ‘G’, ‘0’); CSCE 206 Struectured Programming in C

scanf() Conversion Characters How characters in the input stream are converted c character d decimal integer f floating-point number (float) lf floating-point number (double) Lf floating-point number (long double) s string CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C Example char first, middle, last; int age; scanf(“%c%c%c%d”, &first, &middle, &last, &age); CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C while Statement Performs a repetitive action. Its general form is while (expression) statement CSCE 206 Struectured Programming in C

CSCE 206 Struectured Programming in C Example #include <stdio.h> int main(void) { int i = 1, sum = 0; while (i <= 10) { sum = sum + i; i = i + 1; } printf(“Sum = %d\n”, sum); return 0; CSCE 206 Struectured Programming in C