Introduction to the C Language

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

Lecture 2 Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
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 –
Structure of a C program
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
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.
Basic Elements of C++ Chapter 2.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Introduction to Programming
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Computer Science: A Structured Programming Approach Using C1 2-7 Input/Output Although our programs have implicitly shown how to print messages, we have.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Numbers in ‘C’ Two general categories: Integers Floats
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Topics Designing a Program Input, Processing, and Output
Val Manes Department of Math & Computer Science
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Chapter 7 Text Input/Output Objectives
Chapter 7 Text Input/Output Objectives
Chapter 2 - Introduction to C Programming
Topic: Python’s building blocks -> Variables, Values, and Types
Chapter 7 Text Input/Output Objectives
Basic Elements of C++.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 - Introduction to C Programming
Topics discussed in this section:
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Introduction to the C Language
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
Introduction to the C Language
Chapter 10 Programming Fundamentals with JavaScript
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
2.1 Parts of a C++ Program.
Programming Funamental slides
Programming Funamental slides
Topics Introduction Hardware and Software How Computers Store Data
Topics discussed in this section:
Topics discussed in this section:
Chapter 2: Introduction to C++.
WEEK-2.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Variables in C Topics Naming Variables Declaring Variables
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
Variables and Constants
Presentation transcript:

Introduction to the C Language Chapter 1 Introduction to the C Language Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C program. ❏ To introduce the include preprocessor command. ❏ To be able to create good identifiers for objects in a program. ❏ To be able to list, describe, and use the C basic data types. ❏ To be able to create and use variables and constants. ❏ To understand input and output concepts. ❏ To be able to use simple input and output statements. ❏ To understand the software engineering role. Computer Science: A Structured Programming Approach Using C

1-1 Background C is a structured programming language. It is considered a high-level language because it allows the programmer to concentrate on the problem at hand and not worry about the machine that the program will be using. While many languages claim to be machine independent, C is one of the closest to achieving that goal. That is another reason why it is used by software developers whose applications have to run on many different hardware platforms. Computer Science: A Structured Programming Approach Using C

FIGURE 1-1 Taxonomy of the C Language Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 1-2 C Programs It's time to write your first C program! This section will take you through all the basic parts of a C program so that you will be able to write it. Topics discussed in this section: Structure of a C Program Your First C Program Comments The Greeting Program Computer Science: A Structured Programming Approach Using C

FIGURE 1-2 Structure of a C Program Computer Science: A Structured Programming Approach Using C

FIGURE 1-3 The Greeting Program Computer Science: A Structured Programming Approach Using C

FIGURE 1-4 Examples of Block Comments Computer Science: A Structured Programming Approach Using C

FIGURE 1-5 Examples of Line Comments Computer Science: A Structured Programming Approach Using C

FIGURE 1-6 Nested Block Comments Are Invalid Computer Science: A Structured Programming Approach Using C

PROGRAM 1-1 The Greeting Program Computer Science: A Structured Programming Approach Using C

1-3 Identifiers One feature present in all computer languages is the identifier. Identifiers allow us to name data and other objects in the program. Each identified object in the computer is stored at a unique address. If we didn’t have identifiers that we could use to symbolically represent data locations, we would have to know and use object’s addresses. Instead, we simply give data identifiers and let the compiler keep track of where they are physically located. Computer Science: A Structured Programming Approach Using C

Table 1-1 Rules for Identifiers Computer Science: A Structured Programming Approach Using C

Note An identifier must start with a letter or underscore: it may not have a space or a hyphen. Computer Science: A Structured Programming Approach Using C

C is a case-sensitive language. Note C is a case-sensitive language. Computer Science: A Structured Programming Approach Using C

Examples of Valid and Invalid Names Table 1-2 Examples of Valid and Invalid Names Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 1-4 Types A type defines a set of values and a set of operations that can be applied on those values. For example, a light switch can be compared to a computer type. It has a set of two values, on and off. Only two operations can be applied to a light switch: turn-on and turn-off. Topics discussed in this section: Void Type Integral Type Floating-Point Types Computer Science: A Structured Programming Approach Using C

FIGURE 1-7 Data Types Computer Science: A Structured Programming Approach Using C

FIGURE 1-8 Character Types Computer Science: A Structured Programming Approach Using C

FIGURE 1-9 Integer Types Computer Science: A Structured Programming Approach Using C

sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long) Note sizeof (short) ≤ sizeof (int) ≤ sizeof (long) ≤ sizeof (long long) Computer Science: A Structured Programming Approach Using C

Typical Integer Sizes and Values for Signed Integers Table 1-3 Typical Integer Sizes and Values for Signed Integers Computer Science: A Structured Programming Approach Using C

FIGURE 1-10 Floating-point Types Computer Science: A Structured Programming Approach Using C

sizeof (float) ≤ sizeof (double) ≤ sizeof (long double) Note sizeof (float) ≤ sizeof (double) ≤ sizeof (long double) Computer Science: A Structured Programming Approach Using C

Table 1-4 Type Summary Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 1-5 Variables Variables are named memory locations that have a type, such as integer or character, which is inherited from their type. The type determines the values that a variable may contain and the operations that may be used with its values. Topics discussed in this section: Variable Declaration Variable Initialization Computer Science: A Structured Programming Approach Using C

FIGURE 1-11 Variables Computer Science: A Structured Programming Approach Using C

Examples of Variable Declarations and Definitions Table 1-5 Examples of Variable Declarations and Definitions Computer Science: A Structured Programming Approach Using C

FIGURE 1-12 Variable Initialization Computer Science: A Structured Programming Approach Using C

Note When a variable is defined, it is not initialized. We must initialize any variable requiring prescribed data when the function starts. Computer Science: A Structured Programming Approach Using C

Print Sum of Three Numbers PROGRAM 1-2 Print Sum of Three Numbers Computer Science: A Structured Programming Approach Using C

Print Sum of Three Numbers (continued) PROGRAM 1-2 Print Sum of Three Numbers (continued) Computer Science: A Structured Programming Approach Using C

Print Sum of Three Numbers (continued) PROGRAM 1-2 Print Sum of Three Numbers (continued) Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 1-6 Constants Constants are data values that cannot be changed during the execution of a program. Like variables, constants have a type. In this section, we discuss Boolean, character, integer, real, complex, and string constants. Topics discussed in this section: Constant Representation Coding Constants Computer Science: A Structured Programming Approach Using C

A character constant is enclosed in single quotes. Note A character constant is enclosed in single quotes. Computer Science: A Structured Programming Approach Using C

Symbolic Names for Control Characters Table 1-6 Symbolic Names for Control Characters Computer Science: A Structured Programming Approach Using C

Examples of Integer Constants Table 1-7 Examples of Integer Constants Computer Science: A Structured Programming Approach Using C

Examples of Real Constants Table 1-8 Examples of Real Constants Computer Science: A Structured Programming Approach Using C

Note The two components of a complex constant must be of the same precision, that is, if the real part is type double, then the imaginary part must also be type double. Computer Science: A Structured Programming Approach Using C

Examples of Complex Constants Table 1-9 Examples of Complex Constants Computer Science: A Structured Programming Approach Using C

FIGURE 1-13 Some Strings Computer Science: A Structured Programming Approach Using C

FIGURE 1-14 Null Characters and Null Strings Computer Science: A Structured Programming Approach Using C

Note Use single quotes for character constants. Use double quotes for string constants. Computer Science: A Structured Programming Approach Using C

PROGRAM 1-3 Memory Constants Computer Science: A Structured Programming Approach Using C

Memory Constants (continued) PROGRAM 1-3 Memory Constants (continued) Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 1-7 Input/Output Although our programs have implicitly shown how to print messages, we have not formally discussed how we use C facilities to input and output data. We devote two chapters, Chapter 7 and 13, to fully explain the C input/output facilities and how to use them. In this section, we describe simple input and output formatting. Topics discussed in this section: Streams Formatting Input/Output Computer Science: A Structured Programming Approach Using C

for a text stream; a monitor is a destination Note A terminal keyboard and monitor can be associated only with a text stream. A keyboard is a source for a text stream; a monitor is a destination for a text stream. Computer Science: A Structured Programming Approach Using C

FIGURE 1-15 Stream Physical Devices Computer Science: A Structured Programming Approach Using C

FIGURE 1-16 Output Formatting Concept Computer Science: A Structured Programming Approach Using C

FIGURE 1-17 Output Stream Formatting Example Computer Science: A Structured Programming Approach Using C

FIGURE 1-18 Conversion Specification Computer Science: A Structured Programming Approach Using C

Format Codes for Output Table 1-10 Format Codes for Output Computer Science: A Structured Programming Approach Using C

Flag Formatting Options Table 1-11 Flag Formatting Options Computer Science: A Structured Programming Approach Using C

FIGURE 1-19 Formatting Text from an Input Stream Computer Science: A Structured Programming Approach Using C

FIGURE 1-20 Input Stream Formatting Example Computer Science: A Structured Programming Approach Using C

FIGURE 1-21 Conversion Specification Computer Science: A Structured Programming Approach Using C

scanf requires variable addresses in the address list. Note scanf requires variable addresses in the address list. Computer Science: A Structured Programming Approach Using C

Table 1-12 scanf Rules Computer Science: A Structured Programming Approach Using C

1-8 Programming Examples In this section, we show some programming example to emphasize the ideas and concepts we have discussed about input/output. Computer Science: A Structured Programming Approach Using C

A Program That Prints “Nothing!” Computer Science: A Structured Programming Approach Using C

Demonstrate Printing Boolean Constants PROGRAM 1-5 Demonstrate Printing Boolean Constants Computer Science: A Structured Programming Approach Using C

Demonstrate Printing Boolean Constants (continued) PROGRAM 1-5 Demonstrate Printing Boolean Constants (continued) Computer Science: A Structured Programming Approach Using C

Print Value of Selected Characters PROGRAM 1-6 Print Value of Selected Characters Computer Science: A Structured Programming Approach Using C

Print Value of Selected Characters (continued) PROGRAM 1-6 Print Value of Selected Characters (continued) Computer Science: A Structured Programming Approach Using C

Print Value of Selected Characters (continued) PROGRAM 1-6 Print Value of Selected Characters (continued) Computer Science: A Structured Programming Approach Using C

Print Value of Selected Characters (continued) PROGRAM 1-6 Print Value of Selected Characters (continued) Computer Science: A Structured Programming Approach Using C

Calculate a Circle’s Area and Circumference PROGRAM 1-7 Calculate a Circle’s Area and Circumference Computer Science: A Structured Programming Approach Using C

Calculate a Circle’s Area and Circumference (continued) PROGRAM 1-7 Calculate a Circle’s Area and Circumference (continued) Computer Science: A Structured Programming Approach Using C

FIGURE 1-22 Output Specifications for Inventory Report Computer Science: A Structured Programming Approach Using C

A Sample Inventory Report PROGRAM 1-8 A Sample Inventory Report Computer Science: A Structured Programming Approach Using C

A Sample Inventory Report (continued) PROGRAM 1-8 A Sample Inventory Report (continued) Computer Science: A Structured Programming Approach Using C

FIGURE 1-23 Complex Number Attributes Computer Science: A Structured Programming Approach Using C

Print Complex Number Attributes PROGRAM 1-9 Print Complex Number Attributes Computer Science: A Structured Programming Approach Using C

Print Complex Number Attributes (continued) PROGRAM 1-9 Print Complex Number Attributes (continued) Computer Science: A Structured Programming Approach Using C

Complex Number Arithmetic PROGRAM 1-10 Complex Number Arithmetic Computer Science: A Structured Programming Approach Using C

Complex Number Arithmetic (continued) PROGRAM 1-10 Complex Number Arithmetic (continued) Computer Science: A Structured Programming Approach Using C

Complex Number Arithmetic (continued) PROGRAM 1-10 Complex Number Arithmetic (continued) Computer Science: A Structured Programming Approach Using C

Topics discussed in this section: 1-9 Software Engineering Although this chapter introduces only a few programming concepts, there is still much to be said from a software engineering point of view. We will discuss the concepts of program documentation, data naming, and data hiding. Topics discussed in this section: Program Documentation Data Names Data Hiding Computer Science: A Structured Programming Approach Using C

Sample of General Program Documentation Computer Science: A Structured Programming Approach Using C

Examples of Good and Poor Data Names Table 1-13 Examples of Good and Poor Data Names Computer Science: A Structured Programming Approach Using C

Examples of Defined Constants Table 1-14 Examples of Defined Constants Computer Science: A Structured Programming Approach Using C

No variables are to be placed in the global area of a program. Note Programming Standard No variables are to be placed in the global area of a program. Computer Science: A Structured Programming Approach Using C