Chapter 2 : Overview of C By Suraya Alias. /*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }

Slides:



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

Computer Programming w/ Eng. Applications
Lecture 2 Introduction to C Programming
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
TDBA66, VT-03 Lecture - Ch. 21 A complete C-program Display Fig. 2.1 and comment on different things such as Preprocessor directives Header files Identifiers.
1 CS 201 Introduction to C (1) Debzani Deb. 2 Outline Overview of C General form of a C program C Language Elements.
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.
Lab 5 rC language Elements rVariables Declarations and Data Types rGeneral Form of a C Program rArithmetic Expressions rFormatting Numbers rExercises Note:
Topic 2 – Introduction to the C Programming Language.
1 Lecture 2  Input-Process-Output  The Hello-world program  A Feet-to-inches program  Variables, expressions, assignments & initialization  printf()
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
 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.
1 CS 201 Introduction to C (2) Debzani Deb. 2 Overview C Arithmetic Expressions Formatting Numbers in Program Output Interactive Mode, Batch Mode, and.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
Introduction to C Programming
CISC 105 – Topic 2 Last Topic Review Name the principle components of a computer. What advantage does assembly language have over machine language? What.
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.
Chapter 2 Getting Started in C Programming
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.
Chapter 2 Overview of C Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Pengantar C/C++. 2 Outline  C overview  C language elements  Variable declarations and data types  Executable statements  General form of a C program.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
© 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.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
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.
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.
1 ELE118 lecture 3 Dr. Mehmet Demirer Dr. Seniha Esen Yuksel.
© 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?
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
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
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
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.
CSCE 206 Structured Programming in C
Chapter 2 - Introduction to C Programming
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Chapter 2 - Introduction to C Programming
Lecture2.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
INPUT & OUTPUT scanf & printf.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Lecture3.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Computer Science II CS132/601* Lecture #C-1.
Presentation transcript:

Chapter 2 : Overview of C By Suraya Alias

/*The classic HelloWorld */ #include int main(void) { printf(“Hello World!!"); return 0; }

 /*The classic HelloWorld */ These are comments, starts with /* ends with */, // is used to comment out per line.  #include Lines begin with # is the preprocessing directives which communicates with the processor. #include means to include standard header file that has definitions such as printf and scanf. The other preprocessor directive is #define, such as #define HOUR 60, which means that the constant macro HOUR has the value of 60.  Syntax : #define NAME value

 int main (void) The main function is where the execution begins. The braces { } enclose the main function body, which contains declaration and executable statements. The keyword int indicates that the main function returns an integer value 0 after the execution ends. The keyword void means the main function takes no argument The function body has two parts : declaration and executable statements. Every declarations and statements ends with semicolon ; in C programming

 Reserved words a word that has special meaning in C Such as int, void, double and return  Standard Identifiers also has special meaning such as printf and scanf  User Defined Identifiers user can define their own identifiers such as HOUR, KM_PER_MILE An identifier must consist only letters, digit and underscore An identifier cannot begin with a digit A C reserved word cannot be used as an identifier  Lowercase and uppercase identifiers does make a different in C. The names Rate and rate and RATE is considered as different identifiers.

 Variables a memory cell used to store value and can be changed as the programs executes  Variable Declaration statement that tells the compiler the variable name and the value stored in the variable.  The Variable declarations starts with an identifier followed by the variable name such as : int count; double x, y, x; char w;  Data types a set of values and operations that can be performed on those values Data type int – to represent integers in C, such as 77 Data type double – a real number that is separated by decimal point, such as and 0.09 Data type char – represents an individual character value such as a letter, digit or special symbol. Such as ‘A’, ‘7’, ‘:’

1-7

 The printf function Used for printing formatted output and prompting message printf(“Please enter a number>”) printf(“That equals %f kilometers.\n ”, kms); That equals kilometers %f is the placeholder which is replaced by the value from kms A placeholder always begins with the symbol %  The scanf function Used to read formatted input scanf(“%lf”, &miles); The format “%lf” is the placeholder that tells scanf what kind of data to copy to variable miles The name of each variable is preceded by & character

1-12

PlaceholderVariable typeFunction used %cCharprintf/scanf %dIntprintf/scanf %fDoublePrintf %lfDoublescanf Multiple placeholder printf (“Hi %c%c%c – you will be %d years old today \n”, letter_1, letter_2, letter_3, age); Will display as Hi BOB – you will be 32 years old today

1-14

1-15

Arithmetic OperatorMeaningExamples +Addition5 + 2 is 7, =7.0 -Subtraction5-2 is 3, is 2.0 *Multiplication5*2 is 10, 5.0*2.0 is 10.0 /Division5/2 is 2, 5.0/2.0 is 2.5 %remainder5%2 is 1 Rules for evaluating Expression 1)Parentheses rules 1)Solve or evaluate expression in bracket first 2)Operator Precedence Rules 1)Unary + - 2)* / % 3)Binary + - 3)Associativity Rules 1)For same level, left to right

1-18

1-19

1-20

1-21

1-22

1-23

 Formatting values of Type int By using field width printf(“Results: %3d meters = %4d ft. %2d in. \n”, meters, feet, inches); Results : 21 meters= 68 ft. 11 in.  Formatting values of Type double To display the value of x to an accuracy of 2 decimal place we can use the placeholder %6.2f Example : x is will be displayed as

 Interactive mode A mode where user responds to prompt by entering data  Batch mode Programs scans data from data file

1-26

 Using the FILE * command FILE *inp, /* pointer to input file*/ *outp; /* pointer to output file*/ /* Open the input and output files. */ inp = fopen("D:\\distance.txt", "r"); outp = fopen("D:\\distanceout.txt", "w"); /* Get and echo the distance in miles. */ fscanf(inp, "%lf", &miles); fprintf(outp, "The distance in miles is %.2f.\n", miles); /* Display the distance in kilometers. */ fprintf(outp, "That equals %.2f kilometers.\n", kms); /* Close files. */ fclose(inp); fclose(outp);

1-28

1-29

1-30

1-31

1-32