Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.

Slides:



Advertisements
Similar presentations
Fundamental of C Programming Language and Basic Input / Output Function Lecture 2.
Advertisements

Principles of Programming Chapter 3 Fundamental of C Programming Language and Basic Input/Output Function 1 NI S1 2009/10.
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
1 ICS103 Programming in C Lecture 3: Introduction to C (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.
 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?
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Introduction to C Programming
Principles of Programming - NI Chapter 3 Fundamental of C Programming Language and Basic Input/Output Function.
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.
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.
CSEB114: Principle of Programming
A First Book of ANSI C Fourth Edition
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Yu Yuanming CSCI2100B Data Structures Tutorial 3
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
C Programming Lecture 4 : Variables , Data Types
C Tokens Identifiers Keywords Constants Operators Special symbols.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
CHAPTER 2 PART #3 INPUT - OUTPUT 1 st semester King Saud University College of Applied studies and Community Service Csc
Lecture #5 Introduction to C++
© 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.
Characters and tokens Characters are the basic building blocks in C program, equivalent to ‘letters’ in English language Includes every printable character.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
CSEB114: Principle of Programming Chapter 3: Fundamental of C Programming Language and Basic Input / Output Function.
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
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.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Introducing constants, variables and data types March 31.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
© 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.
 Lecture 2 Introducing C. Lecture overview  Operator:=  Functions: main(), printf()  Putting together a simple C program  Creating integer-valued.
Principles of Programming CSEB134 : BS/ CHAPTER Fundamentals of the C Programming Language.
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:
1 C Syntax and Semantics Dr. Sherif Mohamed Tawfik Lecture Two.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
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.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Lecture2.
Chapter 1.2 Introduction to C++ Programming
CSCE 206 Structured Programming in C
Chapter Topics The Basics of a C++ Program Data Types
Zhang Hongyi CSCI2100B Data Structures Tutorial 3
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2, Part I Introduction to C Programming
Basic Elements of C++.
Revision Lecture
ICS103 Programming in C Lecture 3: Introduction to C (2)
Getting Started with C.
Basic Elements of C++ Chapter 2.
DFC1023 PROBLEM SOLVING AND PROGRAM DESIGN
Lecture3.
Getting Started With Coding
Presentation transcript:

Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1

Principles of Programming 2 Entering, translating, and running a High-Level Language Program

Principles of Programming C Program Structure  An example of simple program in C #include int main(void) { printf(“I love programming\n”); printf(“You will love it too once ”); printf(“you know the trick\n”); return(0); } 3

Principles of Programming The output  The previous program will produce the following output on your screen I love programming You will love it too once you know the trick 4

Principles of Programming Preprocessor directives  a C program line begins with # provides an instruction to the C preprocessor  It is executed before the actual compilation is done.  Two most common directives :  #include  #define  In our example (#include ) identifies the header file for standard input and output needed by the printf(). 5

Principles of Programming Function main  Identify the start of the program  Every C program has a main ( )  'main' is a C keyword. We must not use it for any other variable. 6 int main(void) { return (0); }

Principles of Programming The curly braces { }  Identify a segment / body of a program  The start and end of a function  The start and end of the selection or repetition block.  Since the opening brace indicates the start of a segment with the closing brace indicating the end of a segment, there must be just as many opening braces as closing braces (this is a common mistake of beginners) 7

Principles of Programming Statement  A specification of an action to be taken by the computer as the program executes.  Each statement in C needs to be terminated with semicolon (;)  Example: #include int main(void) { printf(“I love programming\n”); printf(“You will love it too once ”); printf(“you know the trick\n”); return (0); } 8 statement

Principles of Programming Statement cont…  Statement has two parts :  Declaration  The part of the program that tells the compiler the names of memory cells in a program  Executable statements  Program lines that are converted to machine language instructions and executed by the computer 9

Principles of Programming C program skeleton  In short, the basic skeleton of a C program looks like this: #include int main(void) { statement(s); return(0); } 10 Preprocessor directivesFunction mainStart of segmentEnd of segment

Principles of Programming Input/Output Operations  Input operation  an instruction that copies data from an input device into memory  Output operation  an instruction that displays information stored in memory to the output devices (such as the monitor screen) 11

Principles of Programming Input/Output Functions  A C function that performs an input or output operation  A few functions that are pre-defined in the header file such as :  printf()  scanf()  getchar() & putchar() 12

Principles of Programming The printf function  Used to send data to the standard output (usually the monitor) to be printed according to specific format.  General format:  printf(“string literal”);  A sequence of any number of characters surrounded by double quotation marks.  printf(“format string”, variables);  Format string is a combination of text, conversion specifier and escape sequence. 13

Principles of Programming C Token  Tokens are a series of continuous characters that compilers treat as separate entities.  Tokens can be classified into: 1.Reserved words (also known as keywords) 2.Identifiers 3.Constants 4.String Literal 5.Punctuators 6.Operators 14

Principles of Programming Reserved Words  Keywords that identify language entities such as statements, data types, language attributes, etc.  Have special meaning to the compiler, cannot be used as identifiers (variable, function name) in our program.  Should be typed in lowercase.  Example: const, double, int, main, void, printf, while, for, else (etc..) 15

Principles of Programming Identifiers  Words used to represent certain program entities (variables, function names, etc).  Example:  int my_name;  my_name is an identifier used as a program variable  void CalculateTotal(int value)  CalculateTotal is an identifier used as a function name 16

Principles of Programming Rules for naming identifiers 17 RulesExample Can contain a mix of characters and numbers. However it cannot start with a number H2o First character must be a letter or underscoreNumber1; _area Can be of mixed cases including underscore character XsquAre my_num Cannot contain any arithmetic operatorsR*S+T … or any other punctuation Cannot be a C keyword/reserved wordstruct; printf; Cannot contain a spaceMy height … identifiers are case sensitiveTax != tax

Principles of Programming Variables  Variable  a name associated with a memory cell whose value can change  Variable Declaration: specifies the type of a variable  Example: int num;  Variable Definition: assigning a value to the declared variable  Example: num = 5; 18

Principles of Programming Basic Data Types  There are 4 basic data types :  int  float  double  char  int  used to declare numeric program variables of integer type  whole numbers, positive and negative  keyword: int int number; number = 12; 19

Principles of Programming Basic Data Types cont…  float  fractional parts, positive and negative  keyword: float float height; height = 1.72;  double  used to declare floating point variable of higher precision or higher range of numbers  exponential numbers, positive and negative  keyword: double double valuebig; valuebig = 12E-3; 20

Principles of Programming Basic Data Types cont…  char  equivalent to ‘letters’ in English language  Example of characters:  Numeric digits:  Lowercase/uppercase letters: a - z and A - Z  Space (blank)  Special characters:,. ; ? “ / ( ) [ ] { } * & % ^ etc  single character  keyword: char char my_letter; my_letter = 'U';  In addition, there are void, short, long, etc. 21 The declared character must be enclosed within a single quote!

Principles of Programming Constants  Entities that appear in the program code as fixed values.  Any attempt to modify a CONSTANT will result in error.  4 types of constants:  Integer constants  Positive or negative whole numbers with no fractional part  Example: const int MAX_NUM = 10; const int MIN_NUM = -90;  Floating-point constants (float or double)  Positive or negative decimal numbers with an integer part, a decimal point and a fractional part  Example: const double VAL = e2 ; (stands for x 10 2 ) 22

Principles of Programming Constants cont…  Character constants  A character enclosed in a single quotation mark  Example:  const char letter = ‘n’;  const char number = ‘1’;  printf(“%c”, ‘S’);  Output would be: S  Enumeration  Values are given as a list  Example: 23 enum Language { Malay, English, Arabic };