Introduction to C
Programming
Lecture 2
1
Topics to be covered
 Introduction to C - Father of C, History of C
 Use of C
 How a Computer Program Works
 Understanding “HelloWorld.c”
 Free Format
 Programming Style
 Statement
 Naming Entities in C
 C Keywords
 Storage Locations
 Variables
 Data Types in C
 ASCII Encoding Scheme

2
Father of C
 Largely in 1972, Dennis Ritchie devised the C programming language
(which, in case you’re wondering, was the successor to an earlier B
language), and he and Thompson used it the following year to develop the
fourth edition of Unix. (Ritchie also used it to write what has become the
standard, first program any beginning coder in any language comes up with,
“Hello, world!”.)
3
History of C
 Martin Richards designed BCPL in 1966
BCPL - Basic Combined Programming Language
 This influenced B programming language by Ken Thompson with
contributions from Dennis Ritchie in 1969.
 Then Dennis Ritchie came up with the C language in 1972.
 BCPL and B is no longer in use. But C is still in use since 1972.
4
 During the late 1970s and 1980s, versions of C were implemented for a wide variety
computer vendors
These various versions of C give rise to a portability problem.
 Thus American National Standards Institute (ANSI) standardized C in 1989
ANSI C or C89 was the result
In 1990, the ANSI C standard was adopted by the International Organization for
Standardization (ISO)
ANSI/ISO C or C90 was the result
With the introduction of C++, C language was again extended by ISO.
C99 was the result.
5
History of C Cont.
How does a Computer Program Works
 Programs are executed by the CPU. Since the CPU can only understand
machine code programs need to be converted into machine code.
 Following software tools convert high level programming code into
machine code,
Assemblers
Compilers
Interpreters
6
Translator
Program
Source
Program
Object
Code
Software Tools for Conversion
 Assemblers
Translate statement from assembly language to machine language
Generally use table look-up techniques
 Interpreter
An Interpreter converts your program to machine code one
instruction at a time.
 Compiler
A compiler converts your entire program to machine code at one go .
After compilation you have an executable file(Object code) in
machine code.
e.g. an EXE file
7
Your First C Program
8
Include Information about
standard Library
Define a Function Named “main”
that receives no argument value
Main calls library function “printf”
to print this sequence of
characters.
“n” represents the newline
character.
9
 #include <stdio.h>
 Known as the “Preprocessor Command”.
 Tells the compiler information about the printf() output routine that is
used later in the program.
 int main() - main is a special name that indicates precisely where the
program is to begin execution.
 This main function returns an integer value.
 You begin a function with { and ends with } .
 printf (“Hello World !!n"); - The printf() function is called.
 The printf() routine is a function in the C library that simply prints or
displays its argument on your screen.
Your First C Program
 Hello World !! is the argument in this call
 n prints a new line
 Every statement in C must finish with a ;
 return 0; - says to finish execution of main, and return to the system a status
value of 0.
You can use any integer here. Zero is used by convention to indicate that
the program completed successfully—that is, without running into any
errors.
Different numbers can be used to indicate different types of error
10
Your First C Program
Free Format
 A language has free format if statements can appear anywhere on one or
several lines.
 Blank lines are ignored. Extra spaces are ignored.
 Multiple statements can be placed on the same line.
 Statements can be broken over more than one line anywhere other than in
the middle of a string literal, operator, or identifier.
11
Program Style
 Use blank lines to separate parts of the program.
 Use indentation to align curly braces and match the opening and closing
braces by placing them under each other.
Note: When using the vi editor, use 3 spaces for every indentation.
 Use blank spaces to separate the parts of a statement to make it easier to
read.
12
Naming Entities in C
 Identifiers are used to name entities in C.
 Entities that need names
 Functions and methods
 Storage locations
Variables
Constants
 Objects
 User defined types
13
Keywords
 A reserved word is a word which has a special meaning in C and cannot be
redefined by the programmer.
Examples:
int float delete else if this try void for while
 A standard identifier or keyword is a word which has a defined meaning in C
but its use can be changed by the programmer.
Examples:
main sqrt sqr abs
14
C Keywords
There are totally 32(Thirty Two) keywords used in a C programming
15
Case Sensitive Languages
 A programming language is case sensitive if the language considers two
identifiers to be different if they consist of the same characters in the same
order except for variations in upper and lower case.
 Name name NAME would be three different identifiers in a language
which is case sensitive.
 C is case sensitive.
16
Which of the following list can be used as
identifiers?
 name
 Number Of Values
 Tax_Rate
 DistanceInFeet
 2BeOrNot2Be
 Number3
 Tax%
 for
17
Storage Locations
 Storage locations in memory can be used to hold data for a program.
 A storage location has:
Value - the information stored at that location.
Type - the type of data stored at that location.
Address - the number which says where the storage location is in memory.
Name - an identifier that a program associates with that storage location.
18
66
Num
3560
int
Variables
The symbolic name (identifier) for a storage location which can change
its value is a variable.
Each location in memory has a memory address, which is a number. This long
number is inconvenient to use when we want to access the memory location.
We give a human understandable name to refer to this number
e.g. age, quantity
The C compiler maps this name to the memory address number.
 At a given time one value can be stored under the variable.
 The value can vary when the statements in the program are executed.
19
20
# include <stdio.h>
int Main (){
int Num1,Num2, Ans;
Num1= 12;
Num2 = 15;
Ans = Num1 + Num2;
printf(Ans) ;
}
Rules For Making Variables
 First character should be letter or underscore.
 Keywords are not allowed to use as a variable name.
 White space is not allowed.
 C is case sensitive i.e. UPPER and lower case are significant.
 Only underscore, special symbol is allowed between two characters.
 The length of identifier may be up to 31 characters but only the first 8
characters are significant by compiler.
 (Note: Some compilers allow variable names whose length may be up to 247
characters. But, it is recommended to use maximum 31 characters in variable
name. Large variable name leads to occur errors.)
21
Variable Names contd.
 Do not use special characters such as $, &, * etc. in variable names
 Do not use key words in C language as variable names
Example: main, printf, return
 Use short but meaningful names
22
Display “ Hello world”
23
Data Types
 You need to specify what type of data is to be stored in a memory
location.
 This is because we must instruct how much memory should be reserved
by the program to store the value of a variable.
 The amount of memory needed depends on the maximum of the value we
need to store in the variable.
24
Data Types in C
 C has the following basic built-in data types.
int
float
double
Char
 In addition, there are a number of qualifiers that can be applied to these
basic types.
short
long
signed
unsigned
25
Data Type Bytes Range
short int 2 -32,768 -> +32,767
unsigned short int 2 0 -> +65,535
unsigned int 4 0 -> +4,294,967,295
int 4 -2,147,483,648 -> +2,147,483,647
long int 4 -2,147,483,648 -> +2,147,483,647
signed char 1 -128 -> +127
unsigned char 1 0 -> +255
float 4 Real number to approx. 6 sig. figs
double 8 Real number to approx. 15 sig. figs
long double 12 Real number to approx. 33 sig. figs
26
Data Types in C
 You can use the sizeof operator to see the number of bytes allocated to a data
type
27
#include <stdio.h>
int main()
{
printf("The size of char is %d byte n",sizeof(char));
printf("The size of int is %d bytes n",sizeof(int));
printf("The size of float is %d bytes n",sizeof(float));
printf("The size of double is %d bytes n",sizeof(double));
return(0);
}
Data Types in C
Variable Declaration and Initialization
 To store the values used in a program variables must be declared
Example:
int pounds; float kilos;
char Letter;
double Height;
 The variable must be initialized before it is used in a program statement.
Example:
pounds = 12;
 You can initialize a variable at the same line you declare it
Example: int pounds = 12;
28
Integer Data
 An integer is a whole number without a decimal point or a fractional part.
 In mathematics, the integers form an infinite set.
 The computer has only a finite amount of storage to represent an integer,
therefore there is a maximum and a minimum integer that can be stored in
the computer.
29
30
Floating Point or Real Numbers
 Numbers which contain a decimal point and a fractional part are called
floating point or real numbers.
 In mathematics, the real numbers are infinite in both size and density.
 Because the computer uses a finite amount of storage to represent a real
number, real numbers in the computer are not infinite, in size nor density.
31
Floating Point Numbers
 Floating point numbers have a wider range of numbers than integers.
 Floating point numbers are stored as a fractional part(mantissa) and an
exponent.
 Many floating point numbers can not be represented exactly in the
computer. The representation error, can be magnified by numeric
computations.
32
Exponential Notation
 Exponential notation represents a floating point number as a
decimal fraction times a power of 10.
 With a being a decimal fraction and n an integer, the exponential
notation aen represents a x 10n.
 For example, 1.645e2 is 1.645 x 102 or 164.5
33
Representing Numeric Values in C
 Examples of integers are:
462 –39 31285
 Examples of real numbers are:
-21.73 15.0 -85. 6.252e-3
 We can specify the real data type to use for real constants using a
suffix.
f for float e.g. x = 12.5f
L for long double e.g. y = 3.52L
When no suffix is specified double is used.
34
Scanf function
 The scanf function reads formatted input from the keyboard. When user
enters, it is stored in the variable.
35
Integer input/ Output
36
37
Numeric Data Types
 Integers
short int - 2 bytes
int - implementation dependent (2 or 4 bytes)
long int - 4 bytes
 Real Numbers
float - generally 4 bytes
double - at least 4 bytes, generally 8 bytes
long double - at least as large as double, may be larger
38
The Character Data Type
 Character constants are enclosed within single quotes.
 String constants are enclosed within double quotes.
 Characters are one byte (8 bits) of storage.
 Strings use one more byte of storage than the number of characters in
the string.
 Examples of characters:
‘A’ ‘@’ ‘7’ ‘v’ ‘.’ ‘’’ ‘ ’
 Examples of strings:
“Today” “2000” “486-2687” “Be happy!”
39
40
Addition program in C
41
Output
42
Multiply two floating numbers
43
Write C programs for following
 C program for output your name
 C program for add two numbers and get the answer to another
variable
 Program to calculate the average mark from 3 subject marks.
44
ASCII Encoding Scheme
 The American Code for Information Interchange (ASCII) encoding
scheme is a system of assigning a number to a character.
 Most computers store characters using the ASCII encoding scheme.
Some mainframe computers use a different encoding scheme called
EBCDIC.
45
ASCII Representation
46
Character Representation
47
Escape Sequences
 Character combinations consisting of a backslash () followed by a letter
or by a combination of digits are called "escape sequences."
48
49
Quick Quiz – What is the data type of each
value below?
 47
 “Alpha”
 -35
 55.6d
 “#”
 ‘7’
 ‘+’
 “4.69”
 46.5f
50
Constants in C
 Constants are storage elements used in a program whose
value dose not change at runtime.
Example PI, Kilo grams in a pound..
 There are two methods to define constants in C
As a statement
const float pi = 3.14159;
As a preprocessor directive
#define PI 3.14159;
51
Summary
 This lecture was mainly focused on
 Statement
 Naming Entities in C
 C Keywords
 Case Sensitive Languages
 Storage Locations
 Variables
 Data Types in C
 ASCII Encoding Scheme
 Escape Sequences
 Constants in C
52

Lec 02 Introduction to C Programming.pptx

  • 1.
  • 2.
    Topics to becovered  Introduction to C - Father of C, History of C  Use of C  How a Computer Program Works  Understanding “HelloWorld.c”  Free Format  Programming Style  Statement  Naming Entities in C  C Keywords  Storage Locations  Variables  Data Types in C  ASCII Encoding Scheme  2
  • 3.
    Father of C Largely in 1972, Dennis Ritchie devised the C programming language (which, in case you’re wondering, was the successor to an earlier B language), and he and Thompson used it the following year to develop the fourth edition of Unix. (Ritchie also used it to write what has become the standard, first program any beginning coder in any language comes up with, “Hello, world!”.) 3
  • 4.
    History of C Martin Richards designed BCPL in 1966 BCPL - Basic Combined Programming Language  This influenced B programming language by Ken Thompson with contributions from Dennis Ritchie in 1969.  Then Dennis Ritchie came up with the C language in 1972.  BCPL and B is no longer in use. But C is still in use since 1972. 4
  • 5.
     During thelate 1970s and 1980s, versions of C were implemented for a wide variety computer vendors These various versions of C give rise to a portability problem.  Thus American National Standards Institute (ANSI) standardized C in 1989 ANSI C or C89 was the result In 1990, the ANSI C standard was adopted by the International Organization for Standardization (ISO) ANSI/ISO C or C90 was the result With the introduction of C++, C language was again extended by ISO. C99 was the result. 5 History of C Cont.
  • 6.
    How does aComputer Program Works  Programs are executed by the CPU. Since the CPU can only understand machine code programs need to be converted into machine code.  Following software tools convert high level programming code into machine code, Assemblers Compilers Interpreters 6 Translator Program Source Program Object Code
  • 7.
    Software Tools forConversion  Assemblers Translate statement from assembly language to machine language Generally use table look-up techniques  Interpreter An Interpreter converts your program to machine code one instruction at a time.  Compiler A compiler converts your entire program to machine code at one go . After compilation you have an executable file(Object code) in machine code. e.g. an EXE file 7
  • 8.
    Your First CProgram 8 Include Information about standard Library Define a Function Named “main” that receives no argument value Main calls library function “printf” to print this sequence of characters. “n” represents the newline character.
  • 9.
    9  #include <stdio.h> Known as the “Preprocessor Command”.  Tells the compiler information about the printf() output routine that is used later in the program.  int main() - main is a special name that indicates precisely where the program is to begin execution.  This main function returns an integer value.  You begin a function with { and ends with } .  printf (“Hello World !!n"); - The printf() function is called.  The printf() routine is a function in the C library that simply prints or displays its argument on your screen. Your First C Program
  • 10.
     Hello World!! is the argument in this call  n prints a new line  Every statement in C must finish with a ;  return 0; - says to finish execution of main, and return to the system a status value of 0. You can use any integer here. Zero is used by convention to indicate that the program completed successfully—that is, without running into any errors. Different numbers can be used to indicate different types of error 10 Your First C Program
  • 11.
    Free Format  Alanguage has free format if statements can appear anywhere on one or several lines.  Blank lines are ignored. Extra spaces are ignored.  Multiple statements can be placed on the same line.  Statements can be broken over more than one line anywhere other than in the middle of a string literal, operator, or identifier. 11
  • 12.
    Program Style  Useblank lines to separate parts of the program.  Use indentation to align curly braces and match the opening and closing braces by placing them under each other. Note: When using the vi editor, use 3 spaces for every indentation.  Use blank spaces to separate the parts of a statement to make it easier to read. 12
  • 13.
    Naming Entities inC  Identifiers are used to name entities in C.  Entities that need names  Functions and methods  Storage locations Variables Constants  Objects  User defined types 13
  • 14.
    Keywords  A reservedword is a word which has a special meaning in C and cannot be redefined by the programmer. Examples: int float delete else if this try void for while  A standard identifier or keyword is a word which has a defined meaning in C but its use can be changed by the programmer. Examples: main sqrt sqr abs 14
  • 15.
    C Keywords There aretotally 32(Thirty Two) keywords used in a C programming 15
  • 16.
    Case Sensitive Languages A programming language is case sensitive if the language considers two identifiers to be different if they consist of the same characters in the same order except for variations in upper and lower case.  Name name NAME would be three different identifiers in a language which is case sensitive.  C is case sensitive. 16
  • 17.
    Which of thefollowing list can be used as identifiers?  name  Number Of Values  Tax_Rate  DistanceInFeet  2BeOrNot2Be  Number3  Tax%  for 17
  • 18.
    Storage Locations  Storagelocations in memory can be used to hold data for a program.  A storage location has: Value - the information stored at that location. Type - the type of data stored at that location. Address - the number which says where the storage location is in memory. Name - an identifier that a program associates with that storage location. 18 66 Num 3560 int
  • 19.
    Variables The symbolic name(identifier) for a storage location which can change its value is a variable. Each location in memory has a memory address, which is a number. This long number is inconvenient to use when we want to access the memory location. We give a human understandable name to refer to this number e.g. age, quantity The C compiler maps this name to the memory address number.  At a given time one value can be stored under the variable.  The value can vary when the statements in the program are executed. 19
  • 20.
    20 # include <stdio.h> intMain (){ int Num1,Num2, Ans; Num1= 12; Num2 = 15; Ans = Num1 + Num2; printf(Ans) ; }
  • 21.
    Rules For MakingVariables  First character should be letter or underscore.  Keywords are not allowed to use as a variable name.  White space is not allowed.  C is case sensitive i.e. UPPER and lower case are significant.  Only underscore, special symbol is allowed between two characters.  The length of identifier may be up to 31 characters but only the first 8 characters are significant by compiler.  (Note: Some compilers allow variable names whose length may be up to 247 characters. But, it is recommended to use maximum 31 characters in variable name. Large variable name leads to occur errors.) 21
  • 22.
    Variable Names contd. Do not use special characters such as $, &, * etc. in variable names  Do not use key words in C language as variable names Example: main, printf, return  Use short but meaningful names 22
  • 23.
    Display “ Helloworld” 23
  • 24.
    Data Types  Youneed to specify what type of data is to be stored in a memory location.  This is because we must instruct how much memory should be reserved by the program to store the value of a variable.  The amount of memory needed depends on the maximum of the value we need to store in the variable. 24
  • 25.
    Data Types inC  C has the following basic built-in data types. int float double Char  In addition, there are a number of qualifiers that can be applied to these basic types. short long signed unsigned 25
  • 26.
    Data Type BytesRange short int 2 -32,768 -> +32,767 unsigned short int 2 0 -> +65,535 unsigned int 4 0 -> +4,294,967,295 int 4 -2,147,483,648 -> +2,147,483,647 long int 4 -2,147,483,648 -> +2,147,483,647 signed char 1 -128 -> +127 unsigned char 1 0 -> +255 float 4 Real number to approx. 6 sig. figs double 8 Real number to approx. 15 sig. figs long double 12 Real number to approx. 33 sig. figs 26 Data Types in C
  • 27.
     You canuse the sizeof operator to see the number of bytes allocated to a data type 27 #include <stdio.h> int main() { printf("The size of char is %d byte n",sizeof(char)); printf("The size of int is %d bytes n",sizeof(int)); printf("The size of float is %d bytes n",sizeof(float)); printf("The size of double is %d bytes n",sizeof(double)); return(0); } Data Types in C
  • 28.
    Variable Declaration andInitialization  To store the values used in a program variables must be declared Example: int pounds; float kilos; char Letter; double Height;  The variable must be initialized before it is used in a program statement. Example: pounds = 12;  You can initialize a variable at the same line you declare it Example: int pounds = 12; 28
  • 29.
    Integer Data  Aninteger is a whole number without a decimal point or a fractional part.  In mathematics, the integers form an infinite set.  The computer has only a finite amount of storage to represent an integer, therefore there is a maximum and a minimum integer that can be stored in the computer. 29
  • 30.
  • 31.
    Floating Point orReal Numbers  Numbers which contain a decimal point and a fractional part are called floating point or real numbers.  In mathematics, the real numbers are infinite in both size and density.  Because the computer uses a finite amount of storage to represent a real number, real numbers in the computer are not infinite, in size nor density. 31
  • 32.
    Floating Point Numbers Floating point numbers have a wider range of numbers than integers.  Floating point numbers are stored as a fractional part(mantissa) and an exponent.  Many floating point numbers can not be represented exactly in the computer. The representation error, can be magnified by numeric computations. 32
  • 33.
    Exponential Notation  Exponentialnotation represents a floating point number as a decimal fraction times a power of 10.  With a being a decimal fraction and n an integer, the exponential notation aen represents a x 10n.  For example, 1.645e2 is 1.645 x 102 or 164.5 33
  • 34.
    Representing Numeric Valuesin C  Examples of integers are: 462 –39 31285  Examples of real numbers are: -21.73 15.0 -85. 6.252e-3  We can specify the real data type to use for real constants using a suffix. f for float e.g. x = 12.5f L for long double e.g. y = 3.52L When no suffix is specified double is used. 34
  • 35.
    Scanf function  Thescanf function reads formatted input from the keyboard. When user enters, it is stored in the variable. 35
  • 36.
  • 37.
  • 38.
    Numeric Data Types Integers short int - 2 bytes int - implementation dependent (2 or 4 bytes) long int - 4 bytes  Real Numbers float - generally 4 bytes double - at least 4 bytes, generally 8 bytes long double - at least as large as double, may be larger 38
  • 39.
    The Character DataType  Character constants are enclosed within single quotes.  String constants are enclosed within double quotes.  Characters are one byte (8 bits) of storage.  Strings use one more byte of storage than the number of characters in the string.  Examples of characters: ‘A’ ‘@’ ‘7’ ‘v’ ‘.’ ‘’’ ‘ ’  Examples of strings: “Today” “2000” “486-2687” “Be happy!” 39
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
    Write C programsfor following  C program for output your name  C program for add two numbers and get the answer to another variable  Program to calculate the average mark from 3 subject marks. 44
  • 45.
    ASCII Encoding Scheme The American Code for Information Interchange (ASCII) encoding scheme is a system of assigning a number to a character.  Most computers store characters using the ASCII encoding scheme. Some mainframe computers use a different encoding scheme called EBCDIC. 45
  • 46.
  • 47.
  • 48.
    Escape Sequences  Charactercombinations consisting of a backslash () followed by a letter or by a combination of digits are called "escape sequences." 48
  • 49.
  • 50.
    Quick Quiz –What is the data type of each value below?  47  “Alpha”  -35  55.6d  “#”  ‘7’  ‘+’  “4.69”  46.5f 50
  • 51.
    Constants in C Constants are storage elements used in a program whose value dose not change at runtime. Example PI, Kilo grams in a pound..  There are two methods to define constants in C As a statement const float pi = 3.14159; As a preprocessor directive #define PI 3.14159; 51
  • 52.
    Summary  This lecturewas mainly focused on  Statement  Naming Entities in C  C Keywords  Case Sensitive Languages  Storage Locations  Variables  Data Types in C  ASCII Encoding Scheme  Escape Sequences  Constants in C 52

Editor's Notes

  • #8 Explain argument About a code Block {} Regarding the RETURN line : It is not compulsory. If you add return, you need to make as “int main”
  • #14 Identifier is the name of a variable that is made up from combination of alphabets, digits and underscore.
  • #19 􀂃 The symbolic name (identifier) for a storage location which can change its value is a variable. 􀂃 Variables must be declared, before they can be given a value. When you declare a variable you specify its name and type. 􀂃 The declaration allocates the storage location of the appropriate size and associates the name and data type with that location.
  • #21 Using Camel Format
  • #24 Mainly 3 in C -Primary *Ex-Int, bool, double, char -Secondary *Ex-Arrays, Pointers, Structures -User defined *Ex-Syntax: typedef <type> <identifier>; like typedef int number