Introduction to Computers and
Programming (CSC103)
Lecture 03
Introduction to C Language
 C is developed at AT&T’s Bell Laboratories of USA in
1972
 Designed and written by Dennis Ritchie
 Gained popularity and support in late 1970
 Most of the Operating Systems are written in C
 American National Standards Institute (ANSI) formed an
ANSI C committee X3J11 in 1983
 In 1990, first official ANSI standard definition was
published
 Why C?Why not C++, Java or C#?
 First have to learn basic learning elements of a language
2
Programming
 Computers are dumb machines;They are told what to do
 e.g. How to add two numbers, how to find the average etc
 Basic operations of a computer system form instruction
set
 Solving a problem
 Express the solution in set of instructions
 Program: collection of instructions to solve a specific
problem
 Algorithm: the approach or method that is used to solve a
problem.
3
 For example:A program that tests if a number is even or
odd
 Program:The set of statements that solves the problem
 Algorithm:The method that is used to test if a number is even
or odd
 To solve a problem, first express the solution in terms of
an algorithm
 Then develop a program that implements that algorithm
4
 Algorithm for solving even and odd number problem:
1) Get the number
2) Divide the number by two (2)
3) If remainder is zero (0), the number is even
4) Otherwise, the number is odd
 This algorithm is then implemented in a specific language
like C, C++, Java,Visual Basic etc
5
Learning English Language vs C Language
6
C Character Set
 Valid alphabets, numbers and special symbols allowed in C
7
My First C Program – with a problem
main()
{
printf("My First C Program");
}
Output:
Compilation Error
8
My First C Program – correction
#include<stdio.h>
void main()
{
printf("My First C Program");
}
Output
My First C Program
9
Compilation & Execution
 Edit
 Program is first typed into a file
 A text editor like notepad is used
 Save the file with a valid name along with .c extension e.g. prog1.c
 This program is known as source program
 Preprocessor
 Removes comments
 Handles directives for source file inclusions, definitions etc
 Compile
 It examines each program statement in the source program
 Checks the syntax and semantics of the language
 Any error is notified to the user
 Correct the error and restart the compilation process
 Two types of errors
 Syntactic errors (when an invalid statement is written in program)
 Semantic errors (logical error)
10
 If there is no error, the compiler translates each
statement into assembly language form.
 Then the assembly language statements are translated
into machine instructions using another program called
assembler
 Sometimes assembler is part of the compiler
 The assembler translated each assembly language
statement into a binary format known as object code
 Object code is written into another file having extension of
”obj” e.g. prog1.obj
 Now the program is ready to be linked
11
 Link
 The purpose of linking is to get the program into final form for
execution
 It links other programs if the compiler have used before
 Programs from library are also linked
 The process of compiling and linking is called building
 The final linked file, called executable object code is stored in
another file
 with extension “.exe”
 Execute
 To subsequently execute the program type the name of the
executable file
 Loading is the process of placing the program in computer’s memory
and initiating it’s execution
12
Constants, Variables and Keywords
 The alphabets, digits and special symbols when properly
combined form constants, variables and keywords
 A constant is an entity that doesn’t change
 A variable is an entity that may change
 In a program, we do different calculations
 The results of these calculations are stored in computers
memory
 To make the retrieval of these values easy, these memory
locations are given names
 Since the values stored in these memory locations may change
 The names given to these memory locations are called variables
13
Example
 For example 3 is stored in a memory location
 The name of this memory location is x
 Then we assign a new value 5 to this memory location
 This will overwrite the earlier value 3, as a memory
location can hold one value at a time
 Since the memory location x can hold different values at
different times
 x is known as a variable
 Value 3 or 5 do not change, hence are known as
constants
14
Types of C Constants
 C constants can be divided into two major categories
 Primary Constants
 Secondary Constants
15
Rules for Integer Constants
 An integer constant must have at least one digit
 It must not have a decimal point
 It can be either positive or negative
 If no sign precedes an integer constant it is assumed to be
positive
 No commas or blanks are allowed within an integer
constant
 The allowable range for constants is -32768 to 32767
 e.g. 430, +635, -4090
16
Rules for Real or Float Constants
 The float constants could be written in two forms
 Fractional form
 Exponential form
Following are the rules for float constants
 A float constant must have at least one digit
 It must have a decimal point
 It could be either positive or negative
 Default sign is positive
 No commas or blanks are allowed within a float constant
 e.g. +534.63, 834.0, -27.50
17
Cont…
 In exponential form, the float constant is represented in two
parts.The part appearing before e is called mantissa, whereas
the part after e is called exponent
Following are the rules for exponential form of float constant
 The mantissa and exponential parts should be separated by e
 The mantissa part may have a positive or negative sign
 Default sign of mantissa part is positive
 The exponent must have at least one digit, which must be a
positive or negative integer. Default sign is positive
 Range of float constant in exponential form is -3.4e38 to
3.4e38 (which is 3.4 x 1038)
 e.g. 363e5,+4.2e7, 9.4e-4
18
Rules for Character Constants
 A character constant is a single alphabet, a single digit or
a single special symbol enclosed within single inverted
commas
 Both inverted commas should point to the left
 For example, ’A’ is a valid character constant whereas ‘A’
is not
 The maximum length of a character constant can be 1
character
 e.g.‘A’,‘e’,‘5’
19
Types of C Variables
 Variable names are given to a location in memory
 These locations can contain integer, float or character
constants
 Types of variables depends on the types of constants that
it can handle
 A particular type of variable can hold only the same type
of constant
 e.g. an integer variable can hold only an integer constant,
a float variable can hold a float constant and a character
variable can hold a character constant
20
Constructing a Variable Name
 Constructing the variable names of all types the same set
of rules apply. These rules are given below:
 A variable name is any combination of 1 to 31 alphabets, digits
or underscores
 Some compilers allow variable names of length 247 characters
 Do not create unnecessary long variable names
 The first character in the variable name must be an alphabet or
underscore
 No commas or blanks are allowed within a variable name
 No special characters other than underscore can be used
 e.g. si_int, age_max, value80
21
 C compiler distinguish between the variable names by
making it compulsory to declare the type of any variable
you want to use in a program
 Following are some type declaration examples
int sum;
float salary;
char name;
22
Addition Example
#include<stdio.h>
void main()
{
int sum;
sum = 30 + 40;
printf("The sum of 30 and 40 is %dn", sum);
}
23
C Keywords
 Keywords are the words whose meaning has already been explained to the
C compiler
 The keywords cannot be used as variable names
 The keywords are also called “Reserved words”
 There are 32 keywords available in C
24

Introduction to Computer and Programming - Lecture 03

  • 1.
    Introduction to Computersand Programming (CSC103) Lecture 03
  • 2.
    Introduction to CLanguage  C is developed at AT&T’s Bell Laboratories of USA in 1972  Designed and written by Dennis Ritchie  Gained popularity and support in late 1970  Most of the Operating Systems are written in C  American National Standards Institute (ANSI) formed an ANSI C committee X3J11 in 1983  In 1990, first official ANSI standard definition was published  Why C?Why not C++, Java or C#?  First have to learn basic learning elements of a language 2
  • 3.
    Programming  Computers aredumb machines;They are told what to do  e.g. How to add two numbers, how to find the average etc  Basic operations of a computer system form instruction set  Solving a problem  Express the solution in set of instructions  Program: collection of instructions to solve a specific problem  Algorithm: the approach or method that is used to solve a problem. 3
  • 4.
     For example:Aprogram that tests if a number is even or odd  Program:The set of statements that solves the problem  Algorithm:The method that is used to test if a number is even or odd  To solve a problem, first express the solution in terms of an algorithm  Then develop a program that implements that algorithm 4
  • 5.
     Algorithm forsolving even and odd number problem: 1) Get the number 2) Divide the number by two (2) 3) If remainder is zero (0), the number is even 4) Otherwise, the number is odd  This algorithm is then implemented in a specific language like C, C++, Java,Visual Basic etc 5
  • 6.
  • 7.
    C Character Set Valid alphabets, numbers and special symbols allowed in C 7
  • 8.
    My First CProgram – with a problem main() { printf("My First C Program"); } Output: Compilation Error 8
  • 9.
    My First CProgram – correction #include<stdio.h> void main() { printf("My First C Program"); } Output My First C Program 9
  • 10.
    Compilation & Execution Edit  Program is first typed into a file  A text editor like notepad is used  Save the file with a valid name along with .c extension e.g. prog1.c  This program is known as source program  Preprocessor  Removes comments  Handles directives for source file inclusions, definitions etc  Compile  It examines each program statement in the source program  Checks the syntax and semantics of the language  Any error is notified to the user  Correct the error and restart the compilation process  Two types of errors  Syntactic errors (when an invalid statement is written in program)  Semantic errors (logical error) 10
  • 11.
     If thereis no error, the compiler translates each statement into assembly language form.  Then the assembly language statements are translated into machine instructions using another program called assembler  Sometimes assembler is part of the compiler  The assembler translated each assembly language statement into a binary format known as object code  Object code is written into another file having extension of ”obj” e.g. prog1.obj  Now the program is ready to be linked 11
  • 12.
     Link  Thepurpose of linking is to get the program into final form for execution  It links other programs if the compiler have used before  Programs from library are also linked  The process of compiling and linking is called building  The final linked file, called executable object code is stored in another file  with extension “.exe”  Execute  To subsequently execute the program type the name of the executable file  Loading is the process of placing the program in computer’s memory and initiating it’s execution 12
  • 13.
    Constants, Variables andKeywords  The alphabets, digits and special symbols when properly combined form constants, variables and keywords  A constant is an entity that doesn’t change  A variable is an entity that may change  In a program, we do different calculations  The results of these calculations are stored in computers memory  To make the retrieval of these values easy, these memory locations are given names  Since the values stored in these memory locations may change  The names given to these memory locations are called variables 13
  • 14.
    Example  For example3 is stored in a memory location  The name of this memory location is x  Then we assign a new value 5 to this memory location  This will overwrite the earlier value 3, as a memory location can hold one value at a time  Since the memory location x can hold different values at different times  x is known as a variable  Value 3 or 5 do not change, hence are known as constants 14
  • 15.
    Types of CConstants  C constants can be divided into two major categories  Primary Constants  Secondary Constants 15
  • 16.
    Rules for IntegerConstants  An integer constant must have at least one digit  It must not have a decimal point  It can be either positive or negative  If no sign precedes an integer constant it is assumed to be positive  No commas or blanks are allowed within an integer constant  The allowable range for constants is -32768 to 32767  e.g. 430, +635, -4090 16
  • 17.
    Rules for Realor Float Constants  The float constants could be written in two forms  Fractional form  Exponential form Following are the rules for float constants  A float constant must have at least one digit  It must have a decimal point  It could be either positive or negative  Default sign is positive  No commas or blanks are allowed within a float constant  e.g. +534.63, 834.0, -27.50 17
  • 18.
    Cont…  In exponentialform, the float constant is represented in two parts.The part appearing before e is called mantissa, whereas the part after e is called exponent Following are the rules for exponential form of float constant  The mantissa and exponential parts should be separated by e  The mantissa part may have a positive or negative sign  Default sign of mantissa part is positive  The exponent must have at least one digit, which must be a positive or negative integer. Default sign is positive  Range of float constant in exponential form is -3.4e38 to 3.4e38 (which is 3.4 x 1038)  e.g. 363e5,+4.2e7, 9.4e-4 18
  • 19.
    Rules for CharacterConstants  A character constant is a single alphabet, a single digit or a single special symbol enclosed within single inverted commas  Both inverted commas should point to the left  For example, ’A’ is a valid character constant whereas ‘A’ is not  The maximum length of a character constant can be 1 character  e.g.‘A’,‘e’,‘5’ 19
  • 20.
    Types of CVariables  Variable names are given to a location in memory  These locations can contain integer, float or character constants  Types of variables depends on the types of constants that it can handle  A particular type of variable can hold only the same type of constant  e.g. an integer variable can hold only an integer constant, a float variable can hold a float constant and a character variable can hold a character constant 20
  • 21.
    Constructing a VariableName  Constructing the variable names of all types the same set of rules apply. These rules are given below:  A variable name is any combination of 1 to 31 alphabets, digits or underscores  Some compilers allow variable names of length 247 characters  Do not create unnecessary long variable names  The first character in the variable name must be an alphabet or underscore  No commas or blanks are allowed within a variable name  No special characters other than underscore can be used  e.g. si_int, age_max, value80 21
  • 22.
     C compilerdistinguish between the variable names by making it compulsory to declare the type of any variable you want to use in a program  Following are some type declaration examples int sum; float salary; char name; 22
  • 23.
    Addition Example #include<stdio.h> void main() { intsum; sum = 30 + 40; printf("The sum of 30 and 40 is %dn", sum); } 23
  • 24.
    C Keywords  Keywordsare the words whose meaning has already been explained to the C compiler  The keywords cannot be used as variable names  The keywords are also called “Reserved words”  There are 32 keywords available in C 24