SlideShare a Scribd company logo
1 of 55
Computer Languages
• Machine Language – A collection of binary numbers
   – Not standardized. There is a different machine language for
     every processor family.
• Assembly Language - mnemonic codes that corresponds
  to machine language instructions.
   – Low level: Very close to the actual machine language.
• High-level Languages - Combine algebraic expressions
  and symbols from English
   – High Level : Very far away from the actual machine language
   – For example: Fortran, Cobol, C, Prolog, Pascal, C#, Perl, Java.




                                                                       1
Example of Computer
             Languages
                 char name[40];
C Source Code:   printf("Please enter your namen");
                 scanf("%s", name);
                 printf("Hello %s", name);

                 push    offset string "Please enter your namen"
                 (41364Ch)
                 call   dword ptr [__imp__printf (415194h)]
                 add     esp,4
                 lea    eax,[name]
                 push    eax
                 push    offset string "%s" (413648h)
Assembly Code:   call   dword ptr [__imp__scanf (41519Ch)]
                 add     esp,8
                 lea    eax,[name]
                 push    eax
                 push    offset string "Hello %s" (41363Ch)
                 call   dword ptr [__imp__printf (415194h)]
                 add     esp,8


                 68 4C 36 41 00 FF 15 94 51 41 00 83 C4 04 8D 45 D8
Machine Code:    50 68 48 36 41 00 FF 15 9C 51 41 00 83 C4 08 8D 45
                 D8 50 68 3C 36 41 00 FF 15 94 51 41 00 83 C4 08

                                                                      2
Compiler
• Compilation is the process of translating the source code
  (high-level) into executable code (machine level).
• Source file - A file containing the program code
    – A Compiler turns the Source File into an Object File
• Object file - a file containing machine language
  instructions
    – A Linker turns the Object File into an Executable
• Integrated Development Environment (IDE) - a program
  that combines simple word processing with a compiler,
  linker, loader, and often other development tools
    – For example, Eclipse or Visual Studio

                                                          3
C Programming

  An introduction
History
• The initial development of C occurred at
  AT&T Bell Labs between 1969 and 1973.
• It is developed in 1972 by Dennis Ritchie
• The origin of C is closely tied to the
  development of the Unix operating system,
• It was named "C" because many of its
  features were derived from an earlier
  language called "B", which according to
  Ken Thompson was a stripped-down
  version of the BCPL (Basic Combined
  Programming Language).
                                          5
Programming
• Programming - scheduling, or performing a task or an
  event.
• Computer Programming - creating a sequence of steps
  for a computer to follow in performing a task.
• Programming Language - a set of rules, symbols, and
  special words used to construct a computer program.
• Programming language rules consist of:
   – Rules of Syntax which specify how valid instructions are written
     in the language.
   – Rules of Semantics which determine the meaning of the
     instructions (what the computer will do).

                                                                    6
A SIMPLE C PROGRAM
The following program is written in the C
  programming language.

#include <stdio.h>
main()
{
printf ("Programming in C is easy.n");
}
Sample Program Output: Programming in C is easy.
_
                                                   7
A NOTE ABOUT C PROGRAMS
• In C, lowercase and uppercase characters
  are very important! All commands in C must
  be in lowercase.
• The C programs starting point is identified
  by the word main()
  – This informs the computer as to where the
    program actually starts.
• The brackets that follow the keyword main
  indicate that there are no arguments
  supplied to this program (this will be
  examined later on).
                                           8
Cont’d
• The two braces, { and }, signify the begin
  and end segments of the program.
• The purpose of the statment :
      #include <stdio.h>
   is to allow the use of the printf statement
  to provide program output.
• Text to be displayed by printf() must be
  enclosed in double quotes “ ”.
• The program has only one statement
  printf("Programming in C is easy.n")
                                                 9
Cont’d
• printf() is actually a function in C that is
  used for printing variables and text. Where
  text appears in double quotes "", it is
  printed without modification.

• There are some exceptions however. This
  has to do with the  and % characters.
  These characters are modifier's, and for
  the present the  followed by the n
  character represents a newline character.

                                             10
Cont’d

• Another important thing to
  remember is that all C
  statements are terminated
  by a semi-colon ;


                               11
Summary of major points
• program execution begins at main()
• keywords are written in lower-case
• statements are terminated with a semi-colon
• text strings are enclosed in double quotes
• C is case sensitive, use lower-case and try not
  to capitalise variable names
• n means position the cursor on the beginning of
  the next line
• printf() can be used to display text to the screen
• the curly braces { } define the beginning and
  end of a program block

                                                  12
CLASS EXERCISE 1
Q1.1. What is the output of following program ?

#include <stdio.h>
main()
{
printf("Programming in C is easy.n");
printf("And so is Pascal.n");

}
                                             13
ANSWER 1.1


Programming in C is easy.
And so is Pascal.
_




                            14
Process of Program execution
  The diagram of program execution is given on next
  Page:----




                                                      15
Fig 1.12
Entering,
Translating,
and Running
a High-Level
Language
Program




               16
Q1.2 What is the output of following program ?


#include <stdio.h>
main()
 {
printf("The black dog was big. ");
printf("The cow jumped over the moon.n");
}


                                             17
ANSWER 1.2


The black dog was big. The cow jumped over the moon.
_




                                                  18
Q1.3 Try and work out what the following
 program displays,

#include <stdio.h>
main()
{
printf("Hello...n..oh myn...when do i stop?n");
}


                                                     19
ANSWER 1.3


Hello...
..oh my
...when do i stop?
 _

                      20
KEYWORDS
• Keywords are words reserved by C so
  they cannot be used as variables.
                  Keywords
   auto      double   int        struct
   break     else     long       switch
   case      enum     register   typedef
   char      extern   return     union
   const     float    short      unsigned
   continue for       signed     void
   default   goto     sizeof     volatile
   do        if       static     while
                                            21
Variables & Data Types
• Declaring a variable reserves enough
  bytes in memory to store value of the
  declared type.




                                      22
Cont’d
• C provides the programmer with FOUR
  basic data types
• These are:
   – integer
   – character
   – float
   – double


                                        23
Cont’d

• The basic format for declaring variables is
       data_type var, var, ... ;
where data_type is one of the four basic
 types, an integer, character, float, or
 double type.




                                            24
DATA TYPE`2              Q2q                         `q2w2


Q1                 2wSZ                      2 bytes(-32768 ,32767)

Long int           Integer quantity          4 bytes

float              A number containing a     4 bytes
                   decimal point             (1E-37 to 1E+37)

char               Any character             1 byte    (-128 to 127)




double             A number containing a     8 bytes
                   decimal point but here we
                   can have more significant
                   figures.




                                                                       25
Cont’d
• User defined variables must be declared
  before they can be used in a program.
• Get into the habit of declaring variables
  using lowercase characters. Remember
  that C is case sensitive, so even though the
  two variables listed below have the same
  name, they are considered different
  variables in C.
       – sum
       – Sum
                                            26
Cont’d
• The declaration of variables is done after the
   opening brace of main(),
e.g.
#include <stdio.h>
main()
 {
int sum;
sum = 500 + 15;
printf("The sum of 500 and 15 is %dn", sum);
}
                                                   27
General Form of printf() function
printf(“<format string>”,<list of variables”>);
<format string> can contain
        %d for printing integer values.
        %f for printing real values.
        %c for printing character values.




                                                  28
scanf() function

scanf(“format specifier”,& list of vaiables)

Again format specifier is same as in case of printf().

And & is address of operator.




                                                         29
Some of the formatters for printf are

1. Cursor Control Formatters
    n new line
    t   tab




                                    30
Cont’d
2. Variable Formatters
    %d       decimal integer
    %c       character
    %s       string or character array
    %f       float
    %e       double


                                         31
CLASS EXERCISE 2
Q2.1 What is the output of the following program?
#include <stdio.h>
main()
 {
int value1, value2, sum;
value1 = 35;
value2 = 18;
sum = value1 + value2;
printf("The sum of %d and %d is %dn", value1, value2, sum);
}
                                                          32
ANSWER 2.1


The sum of 35 and 18 is 53
_




                             33
MORE ABOUT VARIABLES
• Variables must begin with a character or
  underscore, and may be followed by any
  combination of characters, underscores, or the
  digits 0 - 9.
• The following is a list of valid variable names:
      summary
      exit_flag
       I
       Jerry7
      Number_of_moves
      _valid_flag
                                                     34
Cont’d
• You should ensure that you use
  meaningful names for your variables. The
  reasons for this are:
  – meaningful names for variables are self
    documenting (see what they do at a
    glance)
  – they are easier to understand
  – there is no correlation with the amount of
    space used in the .EXE file
  – makes programs easier to read

                                                 35
CLASS EXERCISE 3

Q3.1 Why are the variables in the
 following list invalid,

•   value$sum
•   exit flag
•   3lotsofmoney
•   char

                                    36
ANSWER 3.1

•   value$sum contains a $
•   exit flag contains a space
•   3lotsofmoney begins with a digit
•   char is a reserved keyword




                                       37
COMMENTS
• The addition of comments inside programs
  is desirable.
• These may be added to C programs by
  enclosing them as follows:
     /* bla bla bla bla bla bla */
• Note that the /* opens the comment field
  and */ closes the comment field.
  Comments may span multiple lines.
                                             38
Cont’d
• Comments may not be nested one inside
  another. For e.g.
  /* this is a comment. /* this comment is
  inside */ wrong */
• In the above example, the first occurrence
  of */ closes the comment statement for the
  entire line, meaning that the text wrong is
  interpreted as a C statement or variable,
  and in this example, generates an error.
                                            39
What Comments Are Used For

• Documentation of variables and their
  usage
• Explaining difficult sections of code
• Describes the program, author, date,
  modification changes, revisions etc
• Copyrighting


                                          40
PREPROCESSOR
          STATEMENTS
• Note that preprocessor statements begin
  with a # symbol, and are NOT terminated
  by a semi-colon.
• Preprocessor statements are handled by
  the compiler (or preprocessor) before the
  program is actually compiled.
• In general, preprocessor constants are
  written in UPPERCASE
                                              41
Cont’d
• The define statement is used to make
  programs more readable.
• Consider the following examples:
  #define TRUE 1
  #define FALSE 0
  #define NULL 0
  #define AND &
  #define OR |
  #define EQUALS ==
                                         42
HEADER FILES
• Header files contain definitions of
  functions and variables which can be
  incorporated into any C program by using
  the pre-processor #include statement.
• Standard header files are provided with
  each compiler, and cover a range of
  areas, string handling, mathematical, data
  conversion, printing and reading of
  variables.

                                          43
Cont’d
• To use any of the standard functions, the
  appropriate header file should be included.
• This is done at the beginning of the C
  source file. For example, to use the
  function printf() in a program, the line
             #include <stdio.h>
  should be at the beginning of the source
  file, because the definition for printf() is
  found in the file stdio.h
                                            44
Cont’d
• All header files have the extension .h and
  generally reside in the /include subdirectory.
• The use of angle brackets <> informs the
  compiler to search the compilers include
  directory for the specified file.
• The use of the double quotes “ " around the
  filename inform the compiler to search in the
  current directory for the specified file.
                #include "mydec.h"


                                                   45
C character set

The only characters required by the C Programming
             Language are as follows:
                       A-Z
                       a -z
                       0-9
                 space . , : ; ' $ "
               # % & ! _ {} [] () < > |
                      +-/*=




                                                     46
                                                    46
Variables, Constants and
              Keywords
Variables:-It is a name given to a memory
 location to store a value of a data that a
 program is working on.




                                              47
Rules for constructing Variable
               names
(1) A variable name is any combination of 1
  to 31 alphabets, digits or underscores but
  there is some restriction on the length of
  variables that depends on compiler to
  compiles. Do not create unnecessarily
  long variable names as it adds to your
  effort.
(2) The first character in the variable name
  must be an alphabet or underscore.

                                               48
Continued
(3) No commas or blanks are allowed within
  a variable name.
(4) No special symbol other than an
  underscore can be used in a variable
  name.




                                         49
Keywords:
• Keywords are words reserved by C so
  they cannot be used as variables.
        Keyw ord s
        auto         double   int        struct
        break        else     long       switch
        case         enum     register   typedef
        char         extern   return     union
        const        float    short      unsigned
        continue     for      signed     void
        default      goto     sizeof     volatile
        do           if       static     while

                                                     50
                                                    50
Data Types.
Type of value that a variable can hold is
 called data type of a variable.




                                            51
continued

DATA TYPE   DESCRIPTION              MEMORY
                                     REQUIRMENT
int         Integer quantity         2 bytes(-32767
                                     32767)

Long int    Integer quantity         4 bytes

float       A number containing a    4 bytes
            decimal point            (1E-37 to
                                     1E+37)
char        Any character            1 byte    (-127 to 127)

double      A number containing a    8 bytes
            decimal point but here
            we can have more                                    52
                                                               52
            significant figures.
Declaring variable
• In order to use a variable in a C program, you
  must first declare it.
• You declare a variable with a declaration.
• int x=5;
• As in the example shown above, a declaration of
  a variable consists of the variable’s type followed
  by the variable’s name and then a semicolon.
  There must be whitespace between the type and
  the name.
• It is also possible to declare multiple variables
  on one line, as we’ll see later.
                                                     53
                                                    53
Constants
• A constant is an entity that does’t change
  its value while a variable is an entity that
  may change.




                                                 54
Types of C constants
(a) Primary constants. i.e. integer, real and
  character constants.
(b) Secondary constants. i.e. Array, pointer,
  structure, union and enum etc.




                                                55

More Related Content

What's hot

What's hot (20)

C basics
C   basicsC   basics
C basics
 
C language (Collected By Dushmanta)
C language  (Collected By Dushmanta)C language  (Collected By Dushmanta)
C language (Collected By Dushmanta)
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
C program
C programC program
C program
 
Discussing Fundamentals of C
Discussing Fundamentals of CDiscussing Fundamentals of C
Discussing Fundamentals of C
 
C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1C Programming Language Step by Step Part 1
C Programming Language Step by Step Part 1
 
Introduction to C Programming
Introduction to C ProgrammingIntroduction to C Programming
Introduction to C Programming
 
C PROGRAMMING
C PROGRAMMINGC PROGRAMMING
C PROGRAMMING
 
Unit 2 introduction to c programming
Unit 2   introduction to c programmingUnit 2   introduction to c programming
Unit 2 introduction to c programming
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
C programming basics
C  programming basicsC  programming basics
C programming basics
 
introduction to C programming (C)
introduction to C programming (C)introduction to C programming (C)
introduction to C programming (C)
 
best notes in c language
best notes in c languagebest notes in c language
best notes in c language
 
Unit ii
Unit   iiUnit   ii
Unit ii
 
Unit 1 c - all topics
Unit 1   c - all topicsUnit 1   c - all topics
Unit 1 c - all topics
 
The smartpath information systems c pro
The smartpath information systems c proThe smartpath information systems c pro
The smartpath information systems c pro
 
C LANGUAGE - BESTECH SOLUTIONS
C LANGUAGE - BESTECH SOLUTIONSC LANGUAGE - BESTECH SOLUTIONS
C LANGUAGE - BESTECH SOLUTIONS
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 

Viewers also liked

Smart Sync Teacher Orientation
Smart Sync Teacher OrientationSmart Sync Teacher Orientation
Smart Sync Teacher Orientationclunes
 
重构——关于可读性、原则和模式
重构——关于可读性、原则和模式重构——关于可读性、原则和模式
重构——关于可读性、原则和模式cnfi
 
Quantum computing quant ph-9708022
Quantum computing quant ph-9708022Quantum computing quant ph-9708022
Quantum computing quant ph-9708022Tien Nguyen
 
与程序员们谈谈产品和设计
与程序员们谈谈产品和设计与程序员们谈谈产品和设计
与程序员们谈谈产品和设计Tian Li
 
Newton’s laws project
Newton’s laws projectNewton’s laws project
Newton’s laws projectancientwarrior
 
Cmp104 lec 6 computer lang
Cmp104 lec 6 computer langCmp104 lec 6 computer lang
Cmp104 lec 6 computer langkapil078
 
Elementary education in bulgaria and at 32 sou
Elementary education in bulgaria and at 32 souElementary education in bulgaria and at 32 sou
Elementary education in bulgaria and at 32 souLeni Anachkova
 
Low secondary department of 32nd sou
Low secondary department of 32nd souLow secondary department of 32nd sou
Low secondary department of 32nd souLeni Anachkova
 
Smart Sync Teacher Orientation
Smart Sync Teacher OrientationSmart Sync Teacher Orientation
Smart Sync Teacher Orientationclunes
 
Y combo
Y comboY combo
Y combocnfi
 
Garage cafe keynote peak ji_no_video
Garage cafe keynote peak ji_no_videoGarage cafe keynote peak ji_no_video
Garage cafe keynote peak ji_no_videoTian Li
 
E learning presentation in e-service
E learning presentation in e-serviceE learning presentation in e-service
E learning presentation in e-serviceMuzamil Kazmi
 
Node js feat pegasus
Node js feat pegasusNode js feat pegasus
Node js feat pegasuscnfi
 
Cmp104 lec 1
Cmp104 lec 1Cmp104 lec 1
Cmp104 lec 1kapil078
 
Cmp104 lec 3 component of computer
Cmp104 lec 3 component of computerCmp104 lec 3 component of computer
Cmp104 lec 3 component of computerkapil078
 

Viewers also liked (18)

Smart Sync Teacher Orientation
Smart Sync Teacher OrientationSmart Sync Teacher Orientation
Smart Sync Teacher Orientation
 
重构——关于可读性、原则和模式
重构——关于可读性、原则和模式重构——关于可读性、原则和模式
重构——关于可读性、原则和模式
 
Quantum computing quant ph-9708022
Quantum computing quant ph-9708022Quantum computing quant ph-9708022
Quantum computing quant ph-9708022
 
与程序员们谈谈产品和设计
与程序员们谈谈产品和设计与程序员们谈谈产品和设计
与程序员们谈谈产品和设计
 
Newton’s laws project
Newton’s laws projectNewton’s laws project
Newton’s laws project
 
Cmp104 lec 6 computer lang
Cmp104 lec 6 computer langCmp104 lec 6 computer lang
Cmp104 lec 6 computer lang
 
Elementary education in bulgaria and at 32 sou
Elementary education in bulgaria and at 32 souElementary education in bulgaria and at 32 sou
Elementary education in bulgaria and at 32 sou
 
Java1
Java1Java1
Java1
 
Low secondary department of 32nd sou
Low secondary department of 32nd souLow secondary department of 32nd sou
Low secondary department of 32nd sou
 
Mbjh
MbjhMbjh
Mbjh
 
Smart Sync Teacher Orientation
Smart Sync Teacher OrientationSmart Sync Teacher Orientation
Smart Sync Teacher Orientation
 
Y combo
Y comboY combo
Y combo
 
Garage cafe keynote peak ji_no_video
Garage cafe keynote peak ji_no_videoGarage cafe keynote peak ji_no_video
Garage cafe keynote peak ji_no_video
 
E learning presentation in e-service
E learning presentation in e-serviceE learning presentation in e-service
E learning presentation in e-service
 
Node js feat pegasus
Node js feat pegasusNode js feat pegasus
Node js feat pegasus
 
Cmp104 lec 1
Cmp104 lec 1Cmp104 lec 1
Cmp104 lec 1
 
Cmp104 lec 3 component of computer
Cmp104 lec 3 component of computerCmp104 lec 3 component of computer
Cmp104 lec 3 component of computer
 
Illegal logging
Illegal loggingIllegal logging
Illegal logging
 

Similar to cmp104 lec 8

c_pro_introduction.pptx
c_pro_introduction.pptxc_pro_introduction.pptx
c_pro_introduction.pptxRohitRaj744272
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.pptManiMala75
 
490450755-Chapter-2.ppt
490450755-Chapter-2.ppt490450755-Chapter-2.ppt
490450755-Chapter-2.pptManiMala75
 
Chapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptChapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptANISHYAPIT
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programmingAlpana Gupta
 
424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).ppt424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).pptadvRajatSharma
 
What is turbo c and how it works
What is turbo c and how it worksWhat is turbo c and how it works
What is turbo c and how it worksMark John Lado, MIT
 
C prog ppt
C prog pptC prog ppt
C prog pptxinoe
 
Programming Fundamentals and basic knowledge
Programming Fundamentals and basic knowledge Programming Fundamentals and basic knowledge
Programming Fundamentals and basic knowledge imtiazalijoono
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.pptUdhayaKumar175069
 
Survey of programming language getting started in C
Survey of programming language getting started in CSurvey of programming language getting started in C
Survey of programming language getting started in Cummeafruz
 
270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functions270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functionsray143eddie
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.pptAlefya1
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.pptJoshCasas1
 
1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computerShankar Gangaju
 
Chapter3
Chapter3Chapter3
Chapter3Kamran
 

Similar to cmp104 lec 8 (20)

c_pro_introduction.pptx
c_pro_introduction.pptxc_pro_introduction.pptx
c_pro_introduction.pptx
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
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 notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
Chapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this pptChapter-2 edited on Programming in Can refer this ppt
Chapter-2 edited on Programming in Can refer this ppt
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Presentation 2.ppt
Presentation 2.pptPresentation 2.ppt
Presentation 2.ppt
 
424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).ppt424769021-1-First-C-Program-1-ppt (1).ppt
424769021-1-First-C-Program-1-ppt (1).ppt
 
What is turbo c and how it works
What is turbo c and how it worksWhat is turbo c and how it works
What is turbo c and how it works
 
C prog ppt
C prog pptC prog ppt
C prog ppt
 
Programming Fundamentals and basic knowledge
Programming Fundamentals and basic knowledge Programming Fundamentals and basic knowledge
Programming Fundamentals and basic knowledge
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 
Survey of programming language getting started in C
Survey of programming language getting started in CSurvey of programming language getting started in C
Survey of programming language getting started in C
 
270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functions270 1 c_intro_up_to_functions
270 1 c_intro_up_to_functions
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 
C programming
C programmingC programming
C programming
 
270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt270_1_CIntro_Up_To_Functions.ppt
270_1_CIntro_Up_To_Functions.ppt
 
1. introduction to computer
1. introduction to computer1. introduction to computer
1. introduction to computer
 
Chapter3
Chapter3Chapter3
Chapter3
 

More from kapil078

12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop kapil078
 
11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage classkapil078
 
Btech 1st yr midterm 1st
Btech 1st yr midterm 1stBtech 1st yr midterm 1st
Btech 1st yr midterm 1stkapil078
 
Cmp104 lec 7 algorithm and flowcharts
Cmp104 lec 7 algorithm and flowchartsCmp104 lec 7 algorithm and flowcharts
Cmp104 lec 7 algorithm and flowchartskapil078
 
Cmp104 lec 2 number system
Cmp104 lec 2 number systemCmp104 lec 2 number system
Cmp104 lec 2 number systemkapil078
 
Cmp104 lec 6 computer lang
Cmp104 lec 6 computer langCmp104 lec 6 computer lang
Cmp104 lec 6 computer langkapil078
 
Cmp104 lec 4 types of computer
Cmp104 lec 4 types of computerCmp104 lec 4 types of computer
Cmp104 lec 4 types of computerkapil078
 

More from kapil078 (10)

12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage class
 
Lec 10
Lec 10Lec 10
Lec 10
 
Btech 1st yr midterm 1st
Btech 1st yr midterm 1stBtech 1st yr midterm 1st
Btech 1st yr midterm 1st
 
Lec9
Lec9Lec9
Lec9
 
Cmp 104
Cmp 104Cmp 104
Cmp 104
 
Cmp104 lec 7 algorithm and flowcharts
Cmp104 lec 7 algorithm and flowchartsCmp104 lec 7 algorithm and flowcharts
Cmp104 lec 7 algorithm and flowcharts
 
Cmp104 lec 2 number system
Cmp104 lec 2 number systemCmp104 lec 2 number system
Cmp104 lec 2 number system
 
Cmp104 lec 6 computer lang
Cmp104 lec 6 computer langCmp104 lec 6 computer lang
Cmp104 lec 6 computer lang
 
Cmp104 lec 4 types of computer
Cmp104 lec 4 types of computerCmp104 lec 4 types of computer
Cmp104 lec 4 types of computer
 

Recently uploaded

Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxAneriPatwari
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 

Recently uploaded (20)

Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
CHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptxCHEST Proprioceptive neuromuscular facilitation.pptx
CHEST Proprioceptive neuromuscular facilitation.pptx
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 

cmp104 lec 8

  • 1. Computer Languages • Machine Language – A collection of binary numbers – Not standardized. There is a different machine language for every processor family. • Assembly Language - mnemonic codes that corresponds to machine language instructions. – Low level: Very close to the actual machine language. • High-level Languages - Combine algebraic expressions and symbols from English – High Level : Very far away from the actual machine language – For example: Fortran, Cobol, C, Prolog, Pascal, C#, Perl, Java. 1
  • 2. Example of Computer Languages char name[40]; C Source Code: printf("Please enter your namen"); scanf("%s", name); printf("Hello %s", name); push offset string "Please enter your namen" (41364Ch) call dword ptr [__imp__printf (415194h)] add esp,4 lea eax,[name] push eax push offset string "%s" (413648h) Assembly Code: call dword ptr [__imp__scanf (41519Ch)] add esp,8 lea eax,[name] push eax push offset string "Hello %s" (41363Ch) call dword ptr [__imp__printf (415194h)] add esp,8 68 4C 36 41 00 FF 15 94 51 41 00 83 C4 04 8D 45 D8 Machine Code: 50 68 48 36 41 00 FF 15 9C 51 41 00 83 C4 08 8D 45 D8 50 68 3C 36 41 00 FF 15 94 51 41 00 83 C4 08 2
  • 3. Compiler • Compilation is the process of translating the source code (high-level) into executable code (machine level). • Source file - A file containing the program code – A Compiler turns the Source File into an Object File • Object file - a file containing machine language instructions – A Linker turns the Object File into an Executable • Integrated Development Environment (IDE) - a program that combines simple word processing with a compiler, linker, loader, and often other development tools – For example, Eclipse or Visual Studio 3
  • 4. C Programming An introduction
  • 5. History • The initial development of C occurred at AT&T Bell Labs between 1969 and 1973. • It is developed in 1972 by Dennis Ritchie • The origin of C is closely tied to the development of the Unix operating system, • It was named "C" because many of its features were derived from an earlier language called "B", which according to Ken Thompson was a stripped-down version of the BCPL (Basic Combined Programming Language). 5
  • 6. Programming • Programming - scheduling, or performing a task or an event. • Computer Programming - creating a sequence of steps for a computer to follow in performing a task. • Programming Language - a set of rules, symbols, and special words used to construct a computer program. • Programming language rules consist of: – Rules of Syntax which specify how valid instructions are written in the language. – Rules of Semantics which determine the meaning of the instructions (what the computer will do). 6
  • 7. A SIMPLE C PROGRAM The following program is written in the C programming language. #include <stdio.h> main() { printf ("Programming in C is easy.n"); } Sample Program Output: Programming in C is easy. _ 7
  • 8. A NOTE ABOUT C PROGRAMS • In C, lowercase and uppercase characters are very important! All commands in C must be in lowercase. • The C programs starting point is identified by the word main() – This informs the computer as to where the program actually starts. • The brackets that follow the keyword main indicate that there are no arguments supplied to this program (this will be examined later on). 8
  • 9. Cont’d • The two braces, { and }, signify the begin and end segments of the program. • The purpose of the statment : #include <stdio.h> is to allow the use of the printf statement to provide program output. • Text to be displayed by printf() must be enclosed in double quotes “ ”. • The program has only one statement printf("Programming in C is easy.n") 9
  • 10. Cont’d • printf() is actually a function in C that is used for printing variables and text. Where text appears in double quotes "", it is printed without modification. • There are some exceptions however. This has to do with the and % characters. These characters are modifier's, and for the present the followed by the n character represents a newline character. 10
  • 11. Cont’d • Another important thing to remember is that all C statements are terminated by a semi-colon ; 11
  • 12. Summary of major points • program execution begins at main() • keywords are written in lower-case • statements are terminated with a semi-colon • text strings are enclosed in double quotes • C is case sensitive, use lower-case and try not to capitalise variable names • n means position the cursor on the beginning of the next line • printf() can be used to display text to the screen • the curly braces { } define the beginning and end of a program block 12
  • 13. CLASS EXERCISE 1 Q1.1. What is the output of following program ? #include <stdio.h> main() { printf("Programming in C is easy.n"); printf("And so is Pascal.n"); } 13
  • 14. ANSWER 1.1 Programming in C is easy. And so is Pascal. _ 14
  • 15. Process of Program execution The diagram of program execution is given on next Page:---- 15
  • 16. Fig 1.12 Entering, Translating, and Running a High-Level Language Program 16
  • 17. Q1.2 What is the output of following program ? #include <stdio.h> main() { printf("The black dog was big. "); printf("The cow jumped over the moon.n"); } 17
  • 18. ANSWER 1.2 The black dog was big. The cow jumped over the moon. _ 18
  • 19. Q1.3 Try and work out what the following program displays, #include <stdio.h> main() { printf("Hello...n..oh myn...when do i stop?n"); } 19
  • 21. KEYWORDS • Keywords are words reserved by C so they cannot be used as variables. Keywords auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while 21
  • 22. Variables & Data Types • Declaring a variable reserves enough bytes in memory to store value of the declared type. 22
  • 23. Cont’d • C provides the programmer with FOUR basic data types • These are: – integer – character – float – double 23
  • 24. Cont’d • The basic format for declaring variables is data_type var, var, ... ; where data_type is one of the four basic types, an integer, character, float, or double type. 24
  • 25. DATA TYPE`2 Q2q `q2w2 Q1 2wSZ 2 bytes(-32768 ,32767) Long int Integer quantity 4 bytes float A number containing a 4 bytes decimal point (1E-37 to 1E+37) char Any character 1 byte (-128 to 127) double A number containing a 8 bytes decimal point but here we can have more significant figures. 25
  • 26. Cont’d • User defined variables must be declared before they can be used in a program. • Get into the habit of declaring variables using lowercase characters. Remember that C is case sensitive, so even though the two variables listed below have the same name, they are considered different variables in C. – sum – Sum 26
  • 27. Cont’d • The declaration of variables is done after the opening brace of main(), e.g. #include <stdio.h> main() { int sum; sum = 500 + 15; printf("The sum of 500 and 15 is %dn", sum); } 27
  • 28. General Form of printf() function printf(“<format string>”,<list of variables”>); <format string> can contain %d for printing integer values. %f for printing real values. %c for printing character values. 28
  • 29. scanf() function scanf(“format specifier”,& list of vaiables) Again format specifier is same as in case of printf(). And & is address of operator. 29
  • 30. Some of the formatters for printf are 1. Cursor Control Formatters n new line t tab 30
  • 31. Cont’d 2. Variable Formatters %d decimal integer %c character %s string or character array %f float %e double 31
  • 32. CLASS EXERCISE 2 Q2.1 What is the output of the following program? #include <stdio.h> main() { int value1, value2, sum; value1 = 35; value2 = 18; sum = value1 + value2; printf("The sum of %d and %d is %dn", value1, value2, sum); } 32
  • 33. ANSWER 2.1 The sum of 35 and 18 is 53 _ 33
  • 34. MORE ABOUT VARIABLES • Variables must begin with a character or underscore, and may be followed by any combination of characters, underscores, or the digits 0 - 9. • The following is a list of valid variable names: summary exit_flag I Jerry7 Number_of_moves _valid_flag 34
  • 35. Cont’d • You should ensure that you use meaningful names for your variables. The reasons for this are: – meaningful names for variables are self documenting (see what they do at a glance) – they are easier to understand – there is no correlation with the amount of space used in the .EXE file – makes programs easier to read 35
  • 36. CLASS EXERCISE 3 Q3.1 Why are the variables in the following list invalid, • value$sum • exit flag • 3lotsofmoney • char 36
  • 37. ANSWER 3.1 • value$sum contains a $ • exit flag contains a space • 3lotsofmoney begins with a digit • char is a reserved keyword 37
  • 38. COMMENTS • The addition of comments inside programs is desirable. • These may be added to C programs by enclosing them as follows: /* bla bla bla bla bla bla */ • Note that the /* opens the comment field and */ closes the comment field. Comments may span multiple lines. 38
  • 39. Cont’d • Comments may not be nested one inside another. For e.g. /* this is a comment. /* this comment is inside */ wrong */ • In the above example, the first occurrence of */ closes the comment statement for the entire line, meaning that the text wrong is interpreted as a C statement or variable, and in this example, generates an error. 39
  • 40. What Comments Are Used For • Documentation of variables and their usage • Explaining difficult sections of code • Describes the program, author, date, modification changes, revisions etc • Copyrighting 40
  • 41. PREPROCESSOR STATEMENTS • Note that preprocessor statements begin with a # symbol, and are NOT terminated by a semi-colon. • Preprocessor statements are handled by the compiler (or preprocessor) before the program is actually compiled. • In general, preprocessor constants are written in UPPERCASE 41
  • 42. Cont’d • The define statement is used to make programs more readable. • Consider the following examples: #define TRUE 1 #define FALSE 0 #define NULL 0 #define AND & #define OR | #define EQUALS == 42
  • 43. HEADER FILES • Header files contain definitions of functions and variables which can be incorporated into any C program by using the pre-processor #include statement. • Standard header files are provided with each compiler, and cover a range of areas, string handling, mathematical, data conversion, printing and reading of variables. 43
  • 44. Cont’d • To use any of the standard functions, the appropriate header file should be included. • This is done at the beginning of the C source file. For example, to use the function printf() in a program, the line #include <stdio.h> should be at the beginning of the source file, because the definition for printf() is found in the file stdio.h 44
  • 45. Cont’d • All header files have the extension .h and generally reside in the /include subdirectory. • The use of angle brackets <> informs the compiler to search the compilers include directory for the specified file. • The use of the double quotes “ " around the filename inform the compiler to search in the current directory for the specified file. #include "mydec.h" 45
  • 46. C character set The only characters required by the C Programming Language are as follows: A-Z a -z 0-9 space . , : ; ' $ " # % & ! _ {} [] () < > | +-/*= 46 46
  • 47. Variables, Constants and Keywords Variables:-It is a name given to a memory location to store a value of a data that a program is working on. 47
  • 48. Rules for constructing Variable names (1) A variable name is any combination of 1 to 31 alphabets, digits or underscores but there is some restriction on the length of variables that depends on compiler to compiles. Do not create unnecessarily long variable names as it adds to your effort. (2) The first character in the variable name must be an alphabet or underscore. 48
  • 49. Continued (3) No commas or blanks are allowed within a variable name. (4) No special symbol other than an underscore can be used in a variable name. 49
  • 50. Keywords: • Keywords are words reserved by C so they cannot be used as variables. Keyw ord s auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while 50 50
  • 51. Data Types. Type of value that a variable can hold is called data type of a variable. 51
  • 52. continued DATA TYPE DESCRIPTION MEMORY REQUIRMENT int Integer quantity 2 bytes(-32767 32767) Long int Integer quantity 4 bytes float A number containing a 4 bytes decimal point (1E-37 to 1E+37) char Any character 1 byte (-127 to 127) double A number containing a 8 bytes decimal point but here we can have more 52 52 significant figures.
  • 53. Declaring variable • In order to use a variable in a C program, you must first declare it. • You declare a variable with a declaration. • int x=5; • As in the example shown above, a declaration of a variable consists of the variable’s type followed by the variable’s name and then a semicolon. There must be whitespace between the type and the name. • It is also possible to declare multiple variables on one line, as we’ll see later. 53 53
  • 54. Constants • A constant is an entity that does’t change its value while a variable is an entity that may change. 54
  • 55. Types of C constants (a) Primary constants. i.e. integer, real and character constants. (b) Secondary constants. i.e. Array, pointer, structure, union and enum etc. 55