SlideShare una empresa de Scribd logo
1 de 60
Faculty of Mechanical and Manufacturing




Prepared by Alish Ahmad Al-shahab
alishahmad.alshahab@gmail.com
◊   Understand and implement the basic
    structure of computer programming.

◊   Write a computer program using C
    programming language.

◊   Convert algorithm into computer
    program.
Program       Basic Syntax
Development    of Programming
Environment


         CHAPTER 2
                     Data Types
  Variable
Declarations
Involves translating high-level language (programming
language such as C,C++, Visual Basic, C#)


             Because computers do NOT
           understand high level language!




       00011101      01010101     11011111
Edit/Write

    Preprocess

             Compile

                       Link

                              Load

                                     Execute
o Types or creates program in an editor
                                          Editor or text
o Makes corrections if necessary          editor is a
                                          type of
o Saves or stores program on disk         program used
  such as C:                             for editing
  or A: etc.                             plain text
                                          files. E.g
                                          dev/turbo
Turbo C
 editor
 (free)
DEV
 C++
(free)
Select
Compile
Terms                     Description
Machine language   Binary number codes understood by a specific
                   CPU.
High-level         Machine-independent programming
language           language that combines algebraic
                   expressions and English symbols.
Source file        File containing a program written in a high-
                   level language; the input for compiler
Compiler           Software that translates a high-level language
                   program into machine language.
Linker             Software that combines object files and create
                   an executable machine language program.
1        Syntax Error
    Error occurred during compilation
    normally due to syntax problem


   Misplaced else.
   Declaration syntax error
   Undefined symbol „_main‟ in module.
   Statement missing in function main()
2   Logic Error
3   Run-time Error
C preprocessor directive   Program block
                              components:
main function              1. Preprocessor
{                             directive
 //Identifiers/Variables   2. Program body
 //C statements            3. Main function
}                          4. Identifiers/Variable
                           5. C statements
                           6. Comment
 Utility program which link files from compiler library to the
  program code.

 Must be included in the first line of a computer program.

 Must be started with the symbol #, otherwise syntax errors
  will be occurred.

 Two types of common preprocessor directive: #include and
  #define.
#include <header file> or #include “user defined files”

                            Example
           #include <stdio.h>
           #include <conio.h>
            #include “jam.h”
Called from
#include <stdio.h>   standard library
Consists of built-in functions


               Functions contains standard instructions

               Function will be called and linked to program
                via header file



 Header file         List of functions
stdio.h        printf(), scanf(),fflush(), dll
conio.h        clrscr(),putch().getc().dll
math.h         sqrt(),pow(), log(),dll
 Contain functions defined by programmer.


            Developed by expert programmers.




 List of header file and its
  Header file     List of user-defined
              function  functions
utama.h          cetak(),baca(),papar(),dll

kira.h           plus(),minus(), divide(),dll
Format:

#define “file name” or #define constant_name constant_value


                               Example
                 #define MAX 100
 The part in which the program code will be started to
  execute.
 Consists of main function, C statements and identifiers.
 Use { to start the program code and } to end the
  program code.


                       main function
                      { //identifiers
                      //C statements }
void main( )
              { …………..}

int main( )                  main( )
{                            {
  return 0;                    return 0;
}                            }
Write the most basic structures of
          C programming.


      #include <stdio.h>
      void main()
      {
      }
Instructions to be executed by computers
Every statements must be ended with semicolon

           Function             Declaration
          statement             statement

      Control          Types
                                    Input/Output
    statement                         statement
                 Compound
                 statement
Statement in program code that will be ignored by compiler
Differs in terms of colour : grey

  To increase
                                           To
   program                             document
  readability                          a program
                      Function
                                         As a
        To provide                      future
        additional                     referen
       information                        ces
Using references from any C book, fin
the following concepts (definition an

   A) Reserved Word
   B) Variable
   C) Constant
Standard/special word in standard library

Contain special meaning understood by compiler


                          Rules
 Case –sensitive
                                     Cannot be used as ide
 Must be written in
 small case                          or variables
Example:
              void                               int
Refer to the function that will not
                                       The acronym for integer
         return any value


      case      default      switch      break

     for       continue        float     double

           return while        if     do int
Representing particular name in programming
Store values to be used in programming
Refers to the storage in computer
Standard           Special built-in words
identifier         Referred as function name
                    which will called from C library



             printf() scanf()
             puts()    gets()
Name given to the declaration of
User-defined     data to be used in program
                Refer to the storage name
 identifier     Store data values/result/output



     Constant     Type           Variable
RULES

Identifiers name can only consists of name, number
and underscore
Identifiers name cannot be started with numbers
Symbol cannot be used in identifier name
Cannot contains spaces between two identifiers
name
Identifiers name should be unique
Identifiers is not case sensitive
WHY?
 Invalid identifiers
   8Century       BIT 1033           Two*four

              ‘Sixsense’      void
WHY?
                       WHY?               WHY?
Name which used to store data value
         Refer to name of one cell in computer
          storage
         Contants value is fixed

How to give
 name to a       Follow identifiers rules
 constant
  value?
Declaration format:       1
const data_type const_name = const_value;




                                                Constant
Reserved word       const   float pi = 3.142;    Value
Declaration format:   2
#define const_name const_value;



                            #define pi 3.142;
      Reserved word
Example of constant:
      #define minimum 0;
      #define MAX 100;

      const int counter = 100;
      const char alphabet = ‘J’;
      const float value = 4.5;
Name which used to store data/input
                 value
                Refer to the name of one cell in computer
                 storage
                Variable’s value can be modified/changed
                 during execution

Declaration Format:

             data_type variable_name;
Declaration Example

      Declaration of a variable
      number of integer data
      type.
       Declaration of a variable
       weight of
       floating point data type.

   Declaration of a variable
   alphabet
   of character data type.
Variable/constant declaration example

  //Variable and constant declration
  #include <stdio.h>

  int number;
  float weight;       Variable declaration
  void main()
  {
       const float pi =3.142;   Constant declaration
      int bilangan;
      float berat;       Variable declaration
      char abjad;
      }
//Variable and constant declaration
#include <stdio.h>

const float pi=3.142;

void main()
{
     int bilangan, bil, bilang;
     float berat, kg;
     char A;
     }
Method to give/assign value to variable


        Interactive                 Initialization




                              Assign value to variable
                               during declaration.
   Input/enter data
    through input devices     Assign value to variable.

 Use input statement         Use assign symbol, “=“
      (scanf/gets)
Assigning value to variables


#include <stdio.h>

void main()
{
                             Initialize a variable
     int number = 10;
     float weight;
     weight = 60.00;                         Interactive
    printf(“Enter the value of number :”);
    scanf(“%d”,&number);

    number = 50.00;            Initialize a variable
   }
Represents types of data can be stored in computer
Types of data to be stored and used in
 programming should be
 informed to the compiler/system


                      Types
     Integer                         Character
                      Floating
                        point
Represents any round number with +/-
              values.
Integer      Divided into short and long integer.
             Reserved word for integer – int
             Valid until 5 places of integer number.


                 Example:
age is used to represent the age of students
between 18 and 25 years old. The declaration
for the
variable is as follow: int age;
Floating     Represents any floating point numbers +/-
 number      Reserved word– double /float




                      Example:
  height is used to represent the student’s height
  between 150 cm and 180 cm. The declaration for the
  variable is as follow:

                      float height;
Character          Represents character data.
                   Reserved word – char



                  Example:
  gender is used to represent the gender of a
  student. The declaration for the variable is
  as follow:
                    char gender;
Determine whether the following identifiers is
valid or invalid. Give reason for invalid cases.
1)    Parit Raja
2)    20thCentury
3)    int
4)    INTEGER
5)    _BMW2003
6)    Reservedword
7)    BIT1033
8)    markah_pelajar
9)    jam*kredit
10)   printf
Write a suitable variable declaration for each
of the following statement:
i.      Salary of an employee
ii.     Student’s mark for programming subject
iii.    ATM pin number
iv.     Phone number
 v.       Price of one item
 vi.      Bus seat number
 vii.     Student name
Based on the following problem, determine the
appropriate
variables can be declared:


Given the value of x is 10 and a is 12,
find the result of the following equation:

         y = 2x + a - 6
Based on the following problem, determine the appropriate
       variables can be declared:



Mrs Leeya needs to determine her students grade for
programming subject based on the mark scored during
final examination. The ‘A’ grade will be given if the
Mark scored is between 85 to 100. If a student has scored
90 marks, what is the grade should Mrs Leeya
give to the student?
Based on the following problem, determine the appropriate
variables can be declared:



A box has height, width and length.
Calculate the volume of a box.
Based on the following problem, determine the appropriate
  variables can be declared:


Uncle Degawan wants to buy 5 tins of paint from
Cinda’s shop. The price of each tin of the paint is
RM 15.60. Calculate the price which Uncle Degawan
have
to pay for all the tin of paints he bought.

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
CProgrammingTutorial
CProgrammingTutorialCProgrammingTutorial
CProgrammingTutorial
 
Chap 2 c++
Chap 2 c++Chap 2 c++
Chap 2 c++
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Basic C Programming language
Basic C Programming languageBasic C Programming language
Basic C Programming language
 
The smartpath information systems c pro
The smartpath information systems c proThe smartpath information systems c pro
The smartpath information systems c pro
 
Introduction of c_language
Introduction of c_languageIntroduction of c_language
Introduction of c_language
 
Presentation c++
Presentation c++Presentation c++
Presentation c++
 
OOP Poster Presentation
OOP Poster PresentationOOP Poster Presentation
OOP Poster Presentation
 
Introduction to C Programming - I
Introduction to C Programming - I Introduction to C Programming - I
Introduction to C Programming - I
 
Learning c - An extensive guide to learn the C Language
Learning c - An extensive guide to learn the C LanguageLearning c - An extensive guide to learn the C Language
Learning c - An extensive guide to learn the C Language
 
Basics of C programming
Basics of C programmingBasics of C programming
Basics of C programming
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
C programming part1
C programming part1C programming part1
C programming part1
 
C tutorial
C tutorialC tutorial
C tutorial
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C Language
C LanguageC Language
C Language
 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
 
C language (Collected By Dushmanta)
C language  (Collected By Dushmanta)C language  (Collected By Dushmanta)
C language (Collected By Dushmanta)
 

Destacado

Tugas Fisika ; Gerak Melingkar
Tugas Fisika ; Gerak MelingkarTugas Fisika ; Gerak Melingkar
Tugas Fisika ; Gerak MelingkarMarcella Claudia
 
Emc procedure quarterly reviews
Emc procedure quarterly reviewsEmc procedure quarterly reviews
Emc procedure quarterly reviewsAndreaaaNinaaa1
 
English Presentation
English PresentationEnglish Presentation
English Presentationmajhat
 
i-CONTROL Company Profile
i-CONTROL Company Profilei-CONTROL Company Profile
i-CONTROL Company ProfileJasmine leung
 

Destacado (9)

project2
project2project2
project2
 
proj2
proj2proj2
proj2
 
Tugas Fisika ; Gerak Melingkar
Tugas Fisika ; Gerak MelingkarTugas Fisika ; Gerak Melingkar
Tugas Fisika ; Gerak Melingkar
 
Emc procedure quarterly reviews
Emc procedure quarterly reviewsEmc procedure quarterly reviews
Emc procedure quarterly reviews
 
Spa blog
Spa blogSpa blog
Spa blog
 
Spa blog
Spa blogSpa blog
Spa blog
 
He 210 teachback
He 210 teachbackHe 210 teachback
He 210 teachback
 
English Presentation
English PresentationEnglish Presentation
English Presentation
 
i-CONTROL Company Profile
i-CONTROL Company Profilei-CONTROL Company Profile
i-CONTROL Company Profile
 

Similar a Chapter 2

Similar a Chapter 2 (20)

Chap 2 structure of c programming dti2143
Chap 2  structure of c programming dti2143Chap 2  structure of c programming dti2143
Chap 2 structure of c programming dti2143
 
Unit-2.pptx
Unit-2.pptxUnit-2.pptx
Unit-2.pptx
 
Chapter3
Chapter3Chapter3
Chapter3
 
(Lect. 2 & 3) Introduction to C.ppt
(Lect. 2 & 3) Introduction to C.ppt(Lect. 2 & 3) Introduction to C.ppt
(Lect. 2 & 3) Introduction to C.ppt
 
C Theory
C TheoryC Theory
C Theory
 
Unit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptxUnit-1 (introduction to c language).pptx
Unit-1 (introduction to c language).pptx
 
Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_
 
Introduction to C Unit 1
Introduction to C Unit 1Introduction to C Unit 1
Introduction to C Unit 1
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
 
Basics of C porgramming
Basics of C porgrammingBasics of C porgramming
Basics of C porgramming
 
Cp week _2.
Cp week _2.Cp week _2.
Cp week _2.
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
 
1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computer
 
C Programming UNIT 1.pptx
C Programming  UNIT 1.pptxC Programming  UNIT 1.pptx
C Programming UNIT 1.pptx
 
C language ppt
C language pptC language ppt
C language ppt
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.ppt
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.ppt
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
Introduction%20C.pptx
Introduction%20C.pptxIntroduction%20C.pptx
Introduction%20C.pptx
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 

Último

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 

Último (20)

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

Chapter 2

  • 1. Faculty of Mechanical and Manufacturing Prepared by Alish Ahmad Al-shahab alishahmad.alshahab@gmail.com
  • 2. Understand and implement the basic structure of computer programming. ◊ Write a computer program using C programming language. ◊ Convert algorithm into computer program.
  • 3. Program Basic Syntax Development of Programming Environment CHAPTER 2 Data Types Variable Declarations
  • 4. Involves translating high-level language (programming language such as C,C++, Visual Basic, C#) Because computers do NOT understand high level language! 00011101 01010101 11011111
  • 5. Edit/Write Preprocess Compile Link Load Execute
  • 6. o Types or creates program in an editor Editor or text o Makes corrections if necessary editor is a type of o Saves or stores program on disk program used such as C: for editing or A: etc. plain text files. E.g dev/turbo
  • 7. Turbo C editor (free)
  • 9.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15. Terms Description Machine language Binary number codes understood by a specific CPU. High-level Machine-independent programming language language that combines algebraic expressions and English symbols. Source file File containing a program written in a high- level language; the input for compiler Compiler Software that translates a high-level language program into machine language. Linker Software that combines object files and create an executable machine language program.
  • 16.
  • 17. 1 Syntax Error Error occurred during compilation normally due to syntax problem  Misplaced else.  Declaration syntax error  Undefined symbol „_main‟ in module.  Statement missing in function main()
  • 18. 2 Logic Error
  • 19.
  • 20. 3 Run-time Error
  • 21. C preprocessor directive Program block components: main function 1. Preprocessor { directive //Identifiers/Variables 2. Program body //C statements 3. Main function } 4. Identifiers/Variable 5. C statements 6. Comment
  • 22.  Utility program which link files from compiler library to the program code.  Must be included in the first line of a computer program.  Must be started with the symbol #, otherwise syntax errors will be occurred.  Two types of common preprocessor directive: #include and #define.
  • 23. #include <header file> or #include “user defined files” Example #include <stdio.h> #include <conio.h> #include “jam.h”
  • 24. Called from #include <stdio.h> standard library
  • 25. Consists of built-in functions Functions contains standard instructions Function will be called and linked to program via header file Header file List of functions stdio.h printf(), scanf(),fflush(), dll conio.h clrscr(),putch().getc().dll math.h sqrt(),pow(), log(),dll
  • 26.  Contain functions defined by programmer.  Developed by expert programmers. List of header file and its Header file List of user-defined function functions utama.h cetak(),baca(),papar(),dll kira.h plus(),minus(), divide(),dll
  • 27. Format: #define “file name” or #define constant_name constant_value Example #define MAX 100
  • 28.  The part in which the program code will be started to execute.  Consists of main function, C statements and identifiers.  Use { to start the program code and } to end the program code. main function { //identifiers //C statements }
  • 29. void main( ) { …………..} int main( ) main( ) { { return 0; return 0; } }
  • 30. Write the most basic structures of C programming. #include <stdio.h> void main() { }
  • 31. Instructions to be executed by computers Every statements must be ended with semicolon Function Declaration statement statement Control Types Input/Output statement statement Compound statement
  • 32. Statement in program code that will be ignored by compiler Differs in terms of colour : grey To increase To program document readability a program Function As a To provide future additional referen information ces
  • 33. Using references from any C book, fin the following concepts (definition an A) Reserved Word B) Variable C) Constant
  • 34. Standard/special word in standard library Contain special meaning understood by compiler Rules Case –sensitive Cannot be used as ide Must be written in small case or variables
  • 35. Example: void int Refer to the function that will not The acronym for integer return any value case default switch break for continue float double return while if do int
  • 36. Representing particular name in programming Store values to be used in programming Refers to the storage in computer
  • 37. Standard Special built-in words identifier Referred as function name which will called from C library printf() scanf() puts() gets()
  • 38. Name given to the declaration of User-defined data to be used in program Refer to the storage name identifier Store data values/result/output Constant Type Variable
  • 39. RULES Identifiers name can only consists of name, number and underscore Identifiers name cannot be started with numbers Symbol cannot be used in identifier name Cannot contains spaces between two identifiers name Identifiers name should be unique Identifiers is not case sensitive
  • 40. WHY? Invalid identifiers 8Century BIT 1033 Two*four ‘Sixsense’ void WHY? WHY? WHY?
  • 41. Name which used to store data value Refer to name of one cell in computer storage Contants value is fixed How to give name to a Follow identifiers rules constant value?
  • 42. Declaration format: 1 const data_type const_name = const_value; Constant Reserved word const float pi = 3.142; Value
  • 43. Declaration format: 2 #define const_name const_value; #define pi 3.142; Reserved word
  • 44. Example of constant: #define minimum 0; #define MAX 100; const int counter = 100; const char alphabet = ‘J’; const float value = 4.5;
  • 45. Name which used to store data/input value Refer to the name of one cell in computer storage Variable’s value can be modified/changed during execution Declaration Format: data_type variable_name;
  • 46. Declaration Example Declaration of a variable number of integer data type. Declaration of a variable weight of floating point data type. Declaration of a variable alphabet of character data type.
  • 47. Variable/constant declaration example //Variable and constant declration #include <stdio.h> int number; float weight; Variable declaration void main() { const float pi =3.142; Constant declaration int bilangan; float berat; Variable declaration char abjad; }
  • 48. //Variable and constant declaration #include <stdio.h> const float pi=3.142; void main() { int bilangan, bil, bilang; float berat, kg; char A; }
  • 49. Method to give/assign value to variable Interactive Initialization  Assign value to variable during declaration.  Input/enter data through input devices  Assign value to variable.  Use input statement  Use assign symbol, “=“ (scanf/gets)
  • 50. Assigning value to variables #include <stdio.h> void main() { Initialize a variable int number = 10; float weight; weight = 60.00; Interactive printf(“Enter the value of number :”); scanf(“%d”,&number); number = 50.00; Initialize a variable }
  • 51. Represents types of data can be stored in computer Types of data to be stored and used in programming should be informed to the compiler/system Types Integer Character Floating point
  • 52. Represents any round number with +/- values. Integer Divided into short and long integer. Reserved word for integer – int Valid until 5 places of integer number. Example: age is used to represent the age of students between 18 and 25 years old. The declaration for the variable is as follow: int age;
  • 53. Floating Represents any floating point numbers +/- number Reserved word– double /float Example: height is used to represent the student’s height between 150 cm and 180 cm. The declaration for the variable is as follow: float height;
  • 54. Character Represents character data. Reserved word – char Example: gender is used to represent the gender of a student. The declaration for the variable is as follow: char gender;
  • 55. Determine whether the following identifiers is valid or invalid. Give reason for invalid cases. 1) Parit Raja 2) 20thCentury 3) int 4) INTEGER 5) _BMW2003 6) Reservedword 7) BIT1033 8) markah_pelajar 9) jam*kredit 10) printf
  • 56. Write a suitable variable declaration for each of the following statement: i. Salary of an employee ii. Student’s mark for programming subject iii. ATM pin number iv. Phone number v. Price of one item vi. Bus seat number vii. Student name
  • 57. Based on the following problem, determine the appropriate variables can be declared: Given the value of x is 10 and a is 12, find the result of the following equation: y = 2x + a - 6
  • 58. Based on the following problem, determine the appropriate variables can be declared: Mrs Leeya needs to determine her students grade for programming subject based on the mark scored during final examination. The ‘A’ grade will be given if the Mark scored is between 85 to 100. If a student has scored 90 marks, what is the grade should Mrs Leeya give to the student?
  • 59. Based on the following problem, determine the appropriate variables can be declared: A box has height, width and length. Calculate the volume of a box.
  • 60. Based on the following problem, determine the appropriate variables can be declared: Uncle Degawan wants to buy 5 tins of paint from Cinda’s shop. The price of each tin of the paint is RM 15.60. Calculate the price which Uncle Degawan have to pay for all the tin of paints he bought.