Introduction to the
C Programming Language
Introduction to C 1
 Software Life Cycle
 Problem statement
 Clear and unambiguous
 Problem analysis and design
 Analyse the method to solve the problem. Develop
 an algorithm. Represent it as flowchart or
 pseudocode
 Analysis of the algorithm
 Check for correctness and required amount of time
 and memory to complete the task
 Coding
Introduction to C 2
 Software Life Cycle ...
 Documentation
 Write explanations within the program so that
 anyone can understand the working, use of
 variables, etc. and should be able to modify it.
 Testing
 Give several test inputs and check if output is as
 specified. Detects inconsistencies in the program
 Maintenance
 Depending on the application, the program may
 require changes and updates.
Introduction to C 3
 Algorithms
 An algorithm is well defined, finite set of computational instructions
 that accomplishes a particular task, which may or may not have inputs
 and produces some value or a set of values as output. In addition all
 algorihtms must satisfy the following criteria:
 Zero or more quantities are externally supplied: inputs
 At least one quantity is produced: output
 Each instruction is clear and unambigious: definiteness
 No uncertainity in the instruction
 The algorithm terminates after a finite number of steps: finiteness
 Must terminate after finite number of steps and reasonable time
 For every input instance, it halts with the correct output: correct
Introduction to C 4
 Algorithm: Example
Algorithm: Largest
This algorithm computes the largest of three numbers. The variables used are:
x, y, z : type integer
big : storing the value of the largest number, type integer
Step-1 : [Input the three numbers]
 read x, y, z
Step-2: [Compute the largest of three numbers]
 big = x;
 if(y > big) big=y
 if(z > big) big=z
Step-3: [write the largest number]
 write (big)
Step-4: [Finished]
 exit
Introduction to C 5
 Flowcharts
Introduction to C 6
 Flowchart: Example
 To find average of three numbers
Introduction to C 7
 Flochart: Example
 To find average of three numbers
Introduction to C 8
 Pseudocode
 English like statements describing the algorithm
 Written in structured manner using indentation
 Example: to find maximum of n given numbers
Algorithm Max(A,n)
/*A is an array of size n, index starts from 1*/
{
 result = A[i];
 for i=2 to n do
 if A[i] > result then result := A[i];
 return result
}
Introduction to C 9
 First C program
/* hello.c: hello world program, first example */
#include <stdio.h>
void main()
{
 printf(Hello World\n);
 printf(Bye.);
}
Introduction to C 10
 First C program
 Comment
/* hello.c: hello world program, first example */
 Preprocessor directive
#include <stdio.h>
 Function Name
void main()
{
 printf(Hello World\n); Body of the
 function main
 printf(Bye.);
}
Introduction to C 11
 First C program
/* hello.c: hello world program, first example */
#include <stdio.h>
 $> gcc firstCprog.c
 $> ./a.out
void main()
{
 printf(Hello World\n);
 printf(Bye.);
}
 Hello World
 Bye.
Introduction to C 12
 First C program
/* hello.c: hello world Statement enclosed in curly
program, first example */
 braces form a compound
 statement
#include <stdio.h>
 2 statements in braces after
 main() form the function main()
void main()
{ void: Does not return value
 printf(Hello World\n);
 printf(Bye.);
 main(): special function in C
} main is executed first among
 all functions in a file
 Introduction to C 13
 Preprocessor directive
 #include <stdio.h>
 stdio.h is a file supplied by the C compiler which has
 information about various functions used for input-output
 The function printf() is available in stdio.h
 Lines in program starting with # are preprocessor directives
 Before the compiler, the program is preprocessed
 During this it includes the contents of stdio.h in the program
 The function printf() is precompiled and kept in the C
 library. When your program calls this function, the linker will
 take it from the library and include in the executable
Introduction to C 14
 Case sensitiveness
 Upper and Lower case are recognised
 differently
 Following are different functions:
 printf(Hello World. \n);
 Printf(Hello World. \n); /* error */
 PRINTF(Hello World. \n); /* error */
Introduction to C 15
 Standard input and output devices
 The printf function outputs the text on standard output
 device.
 Usually it is the screen
 We can re-direct the output to a file
 $> a.out > outputMessages.txt
 Here all statements will go to file outputMessages.txt
 Output redirection operator: >
 User input using scanf can be done from the keyboard or
 from a file.
 $> a.out < inputStrings.txt
 Input redirection operator: <
Introduction to C 16
 Statement separation
 Separated by semicolons
 Can write multiple statements in one line
 printf(Hello World \n); printf(Bye.);
 Can also split a statement across lines
 printf(
 Hello World \n);
 printf(
 Bye.);
 C uses semincolon (;) to separate them
Introduction to C 17
Variables and Data types
 Character Set
 Alphabets
 A to Z
 a to z
 Digits
 0 to 9
 Special characters
 Next slide lists all
 White space characters
 Blank spaces, formfeed, newline, horizontal tab,
 carriage return, vertical tab
 Special characters
, comma & ampersand
. period ^ caret
; semicolon * asterisk
: colon - minus sign
? question mark + plus sign
' apostrophe < opening angular bracket or less than sign
" quotation mark > closing angular bracket or greater than sign
! exclamation mark ( left parenthesis
| vertical bar ) right parenthesis
/ slash [ left square bracket
\ backslash ] right square bracket
~ tilde { left brace
_ under score } right brace
$ doller sign % percent sign
# number sign
 What is a variable?
 A variable is a storage unit, which sets a space in memory to
 hold a value and can take different values at different times
 during program execution.
 Rules to construct variable names:
 A variable name may consists of letters, digits and the underscore ( _ )
 characters.
 A variable name must begin with a letter. Some systems allow to start
 the variable name with underscore as first character.
 ANSI standard recognizes a length of 31 characters for a variable
 name. However the length should not be normally more than any
 combination of eight alphabets, digits and underscores.
 Case sensitive: Uppercase and lowercase are significant. That is the
 variable Totamt is not the same as totamt and TOTAMT.
 Variable name must not be a C reserved word (keyword).
 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
 Constants
 A specific quantity that does not change its
 value during the execution of a program
 Types
 Integer Constants
 Real Constants
 Single Character Constants
 String Constants
 Rules for integer constants
 An integer constant refers to a sequence of digits. The three types of
 integers are decimal, octal and hexadecimal
 No embedded blanks, commas and non-numeric character are
 permitted within an integer constant.
 An integer constant must contain one digit
 Decimal integers consist set of digits, 0 to 9 without any decimal point
 and can be preceded by an optional +ve or -ve sign.
 An octal integer constant contains any combination of digits between
 0 and 7 with a leading 0.
 A hexadecimal constant contains any combination of digits between 0
 and 9 and can also hold alphabets between A and F or a and f with
 prefix 0x or 0X. Alphabet 'A' or 'a' represents number 10 and 'F' or 'f'
 represents 15.
 The largest integer value for 16-bit machine is 32767 and
 2147483647 for 32-bit machine.
 Rules for real constants
 The real constant is a number that may have a fractional part.
 It could be either +ve or -ve.
 No embedded blanks, commas and non-numeric character are
 permitted within a real constant.
 A real number may also be expressed in exponential notation.
 An exponent is an integer number with an optional plus or
 minus sign. Exponential is useful for representing the number
 which is vary large or very small in magnitude.
 Examples of constants
241 Decimal Integer 047 Octal Integer
-973 Decimal Integer 053 Octal Integer
0 Decimal Integer 0X59 Hexadecimal Integer
+4177 Decimal Integer 0x47F Hexadecimal Integer
Real constants
0.0045 -.71 +45.203 0.45e3
-0.547 0.78e-4 337. 2.79E4
.478. -4.69E-4
 Data types
 Four fundamental data types
Type Range of values Description
Char (Characters) -128 to 127 A single byte (8 bits) can
 store one character type
 data
Int (Integers, whole -32768 to 32767 To represent a whole
numbers) number in the specified
 range
Float (Floating point, real 3.4e-38 to 3.4e+38 Single precision floating
numbers) point
Double (Double) 1.7e-308 to 1.7e+308 Double-precision floating
 point
 Data types ...
 Char
 8 bits
 Qualifier
 Signed: -128 to +127
 Unsigned: 0 to 255
 Int
 short int, int, long int in both signed and unsigned
 Signed uses 1 bit for sign and 15 bits for magnitude of a 16-bit number
 Float
 32 bits with 6 digit precision
 For more accuracy use double
 Double
 64 bit number with 14 digits precision
 For more precision use long double with 80 bits
 Data types: size and range
 Type Size(bits) Range
char or signed char 8 -128 to 127
unsigned char 8 0 to 255
int or signed int 16 -32768 to 32767
unsigned int 16 0 to 65535
short int 8 -128 to 127
signed int 8 -128 to 127
unsigned short int 8 0 to 255
long int 32 -2147483648 to 2147483647
signed long int 32 -2147483648 to 2147483647
unsigned long int 32 0 to 4294967295
float 32 3.4E-38 TO 3.4E+38
double 64 1.7E-308 TO 1.7E+308
long double 80 3.4E-4932 TO 1.1E+4932