Overview of C. C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
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.
1 CS 201 Introduction to C (1) Debzani Deb. 2 Outline Overview of C General form of a C program C Language Elements.
Software Development Method & C Language Elements H&K Chapter 1-2
Introduction to Computing Lecture 01: Introduction to C Introduction to Computing Lecture 01: Introduction to C Assist.Prof.Dr. Nükhet ÖZBEK Ege University.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Structure of a C program
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.
Topic 2 – Introduction to the C Programming Language.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Chapter 2: Introduction to 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.
Ping Zhang 10/08/2010.  You can get data from the user (input) and display information to the user (output).  However, you must include the library.
A First Book of ANSI C Fourth Edition
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
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.
(1 - 2) C Language Elements H&K Chapter 2 Instructor - Andrew S. O’Fallon CptS 121 (August 28, 2015) Washington State University.
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Week 1 Algorithmization and Programming Languages.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Introduction to Programming
ELE118 Introduction to Programming
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
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.
CISC105 – General Computer Science Class 2 – 6/7/2006.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
A.Abhari CPS1251 Topic 2: C Overview C Language Elements Variable Declaration and Data Types Statement Execution C Program Layout Formatting Output Interactive.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to 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.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Numbers in ‘C’ Two general categories: Integers Floats
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Basic Elements of C++.
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Lecture2.
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.
INPUT & OUTPUT scanf & printf.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2: Introduction to C++.
WEEK-2.
Presentation transcript:

Overview of C

C—a high-level programming language developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories. We will discuss: –the elements of a C program –the types of data that can be processed by C. –C statements for –performing computations, –for entering data, and –for displaying results. Topics to Be Covered

An algorithm An algorithm that is implemented in C language

What is the program about? There are two parts in the code: –Preprocessor –Function main 1. C language elements

Are commands that give instructions to C preprocessor, whose job is to modify the text of a C program before it is compiled. Begins with a number symbol (#) Two most common directives : –#include –#define C language explicitly defines only a small number of operations. Many actions that are necessary in a computer program are not defined directly by C. Instead, every C implementation contains collections of useful functions and symbols called libraries  library_name.h Preprocessor directives

#include directive gives a program access to a library. This directive causes the preprocessor to insert definitions from a standard header file into a program before compilation. The directive notifies the preprocessor that some names used in the program (such as scanf and printf ) are found in the standard header file. #include

This directive instructs the preprocessor to replace each occurrence of KMS_PER_MILE by before compilation begins. –Associates the constant macro KMS_PER_MILE with the meaning –constant macro a name that is replaced by a particular constant value before the program is sent to the compiler. –An executing C program cannot change the value of a name defined as a constant macro. #define

The text on the right of each preprocessor directive, starting with /* and ending with */, is a comment. Comments provide supplementary information making it easier for us to understand the program, but comments are ignored by the C preprocessor and compiler. Comments

Syntax: #include

Syntax: #define

Every C program has a main function. Function main marks the beginning of the main function where program execution begins. The remaining lines of the program –form the body of the function is enclosed in braces { }.

Function main

Some example of reserved words in C: Reserved words are identifiers from standard libraries, and names for memory cells. All the reserved words appear in lowercase ; they have special meaning in C and cannot be used for other purposes. A complete list of ANSI C reserved words is found in Appendix E. Table 2.1 describes the reserved words in Fig 2.1. Reserved Words

Notice that main is not a reserved word. However, this is a fairly technical distinction, and for practical purposes you are advised to treat, main as if it were reserved as well.

Other words in Fig. 2.1 are identifiers that come in two varieties: – standard – user-defined. Standard identifiers have special meaning in C. –In Fig. 2.1, the standard identifiers printf and scanf are names of operations defined in the standard input/output library. User-defined identifiers –We choose our own identifiers to name memory cells that will hold data and program results and to name operations. – Example: KMS_PER_MILE, is the name of a constant macro. Identifiers

You have some freedom in selecting identifiers. The syntax rules and some valid identifiers follow. 1.must consist only of letters, digits, and underscores. 2.cannot begin with a digit. 3.Cannot use C reserved words as an identifier. 4.An identifier defined in a C standard library should not be redefined. Example of valid identifiers: letter_1, letter_2, inches, cent, CENT_PER_INCH, Hello, variable

Table 2.2 shows some invalid identifiers.

Case sensitive -- C compiler considers uppercase and lowercase letters as different identifiers. Adopting a consistent pattern in the way you use uppercase and lowercase letters is helpful to the readers of your programs. –All reserved words in C and names of all standard library functions use only lowercase letters. –One style that has been widely adopted in industry uses all uppercase letters in the names of constant macros. –For other identifiers we use all lowercase letters. Uppercase and lowercase letters

A program that “looks good” is easier to read and understand than one that is sloppy. Most programs will be examined or studied by someone other than the original programmers. In industry, programmers spend considerably more time on program maintenance (that is, updating and modifying the program) than they do on its original design or coding. A program that is neatly stated and whose meaning is clear makes everyone’s job simpler.

Meaningful name for a user-defined identifier, –Example: salary better than s or sal. If an identifier consists of two or more words, placing the underscore character ( _ ) between words will improve the readability of the name –Example: dollars_per_hour better than dollarsperhour. Pick long enough identifier to convey your meaning, but avoid excessively long names (it will more likely make a typing error) –Example: lbs_per_sq_in better than pounds_per_square_inch. Avoid two names that are different only in their use of uppercase and lowercase letters. –Example: LARGE and large Not to use two names that differ only in the presence or absence of an underscore –Example: xcoord and x_coord.

2. Variable Declarations and Data Types

a name associated with a memory cell whose value can change, used to store a program’s input data and its computational results. variable declarations in a C program communicate to the C compiler –the names of all variables used in a program –what kind of information will be stored in each variable and –how that information will be represented in memory. Example of variable declarations: Variable and variable declaration

give the names of two variables ( miles, kms ) –used to store real numbers. –Note: C ignores the comments on the right of each line describing the usage of each variable. C requires you to declare every variable used in a program

A data type of an item specifies operations on that item. –Example: C uses standard data types – double as abstractions for real numbers – int as integers. Data type

int data type is used to represent integers (whole numbers) in C. Because of the finite size of a memory cell, not all integers can be represented by type int. ANSI C specifies range of int : −32767 through You can –store an integer in a type int variable –perform common arithmetic operations (add, subtract, multiply, and divide), and compare two integers. Data Type int

A real number has an integral part and a fractional part that are separated by a decimal point. In C, the data type double is used to represent real numbers (for example, , , ). You can –store a real number in a type double variable, –perform common arithmetic operations (add, subtract, multiply, and divide), and compare them. In normal scientific notation, real number 1.23x10 5 is equivalent to –In C scientific notation, we write this number as 1.23e5 or 1.23E5. Data Type double

Operations involving integers are faster than those involving numbers of type double. Less storage space is needed to store type int values. Also, operations with integers are always precise, whereas some loss of accuracy or round-off error may occur when dealing with type double numbers. why having more than one numeric type is necessary?

represents an individual character value — a letter, a digit, or a special symbol. Each type char value is enclosed in apostrophes (single quotes) as shown here. 'A' 'z' '2' '9' '*' ':' ' ' A character is represented in memory as an integer. –The value stored is determined by the code used by your C compiler. – ASCII code (American Standard Code for Information Interchange) is the most common. Data type char

An assignment statement stores a value or a computational result in a variable,and is used to perform most arithmetic operations in a program. In C, = is the assignment operator. Example: kms = KMS_PER_MILE * miles; –This instruction assigns a value to the variable kms. 3. Executable statements Assignment statement

sum = sum + item; new_x = x; –copies the value of variable x into variable new_x new_x = -x; –get the value of x, negate that value, and store the result in new_x. next_letter = 'A'; Examples:

Input operation is data transfer from the outside world into memory. Output operation is displaying program results to the program user. All input/output operations in C are performed by special program units called input/output functions  supplied as part of the C standard input/output library to which we gain access through the preprocessor directive #include 3. Executable statements Input/Output Operations and Functions

Output function printf displays a line of program output. A function call consists of two parts: the function name and the function arguments, enclosed in parentheses. –The arguments for printf consist of a format string (in quotes) and a print list (the variable kms ). Output function printf

%f is a placeholder, marks the display position for a type double variable. Other data type placeholders.

prompt (prompting message) a message displayed to indicate what data to enter and in what form. Displaying Prompts

copy data into the variable miles. & operator tells the scanf function where to find each variable into which it is to store a new value. Example: scanf("%c%c%c", &letter_1, &letter_2, &letter_3); Input function scanf

Transfers control from your program to the operating system. Value in parentheses, 0, is considered the result of function main ’s execution, and it indicates that your program executed without error. The return Statement

One possible solution /*C program for area of circle*/ #include #define PI int main() { double radius,area; printf("Radius: "); scanf("%lf", &radius); area = PI * radius * radius; printf(“Area of the circle is %f\n", area); return (0); } Note, variable declarations are inside the function main

Program Style –Spaces in Programs –Comments in Programs, describe –purpose of the program, –use of identifiers, –purpose of each program step. Comments are part of the program documentation. 4. General form of a C program

Self check

Formatting values of type integers –simply add a number between % and the d of the %d placeholder in the printf format string. –This number specifies the field width —the number of columns to use for the display of the value. –Example: 5. Formatting numbers in program output

Formatting values of type double –we must indicate both total field width needed and the number of decimal places desired. 5. Formatting numbers in program output