Problem Solving Using C NVG
Problem Solving Using C NVG
ALGOL BCPL B C
3
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
WHY SHOULD I LEARN C PROGRAMMING ?
➢ Foundation for Other Modern Languages
➢ System Level Programming
➢ Embedded Systems Development
➢ Core to Software Development
➢ Scientific and Engineering Libraries
4
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
FEATURES
OF C
LANGUAGE
5
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
6
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
ALGORITHM
7
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
EXAMPLE
ALGORITHM ADD TWO NUMBERS
Step 0 : START
Step 1 : INPUT first number into variable A
Step 2 : INPUT second number into variable B
Step 3 : COMPUTE SUM = A + B
Step 4 : DISPLAY SUM
Step 5 : END
8
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
PSEUDOCODE
It is a means to represent an algorithm in coded form.
9
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
EXAMPLE
PSEUDOCODE SUM OF TWO NUMBERS
SUM_TWO {
PRINT “Enter a value for number A : “
SCAN A
PRINT “Enter a value for number B : “
SCAN B
SUM A + B
PRINT “The sum is : “, SUM
10
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
PSEUDOCODE
ADVANTAGES DISADVANTAGES
12
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
NOTATIONS USED IN FLOWCHART
SYMBOL
NAME
Terminal Symbol
Process Box
Decision Box
Arrows
Connector
Off-Page Connector
13
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
EXAMPLE
Start
Input A
FLOWCHART Input B
ADD TWO NUMBERS
SUM = A + B
Display SUM
End
14
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
FLOWCHART
ADVANTAGES DISADVANTAGES
2. INTERPRETER
➢ Translates line by line
➢ Detects errors in current line
16
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
THE C COMPILER
➢ The source code written in the source file is the human-readable version
of your program
➢ The compiler compiles the source code into final executable program
17
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
18
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
STRUCTURE OF A C PROGRAM
19
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
C PROGRAM STRUCTURE
A C program basically consists of the following parts
➢ Preprocessor Commands
➢ Functions
➢ Variables
➢ Comments
20
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
COMMENTS
➢ Used to explain code, make it more readable
➢ Prevent execution when testing alternative code
➢ Comments can be classified as follows :
■ Single - line
○ Starts with two forward slashes - //
■ Multi - line
○ Starts with /* and ends with */
21
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
EXAMPLE OF COMMENTS
➢ // This is a single line comment
printf(“HELLO WORLD !”);
➢ /*
This is a multi line comment :
This code will print the words HELLO WORLD ! to the screen
*/
printf(“HELLO WORLD!”);
22
CHARACTER SET
➢ Set of all valid characters that can be used in source program
23
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
CHARACTER SET INCLUDES
1. Alphabetical Characters
➢ Uppercase Letters : A - Z
➢ Lowercase Letters : a - z
2. Digits
➢ 0-9
24
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
CHARACTER SET INCLUDES
3. Special Characters
?, @, [, `, ], ^, _, `, {, |, }, ~
4. Whitespaces
➢ Blank Space
➢ New Line
➢ Tab 25
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
ASCII
AMERICAN STANDARD CODE FOR INFORMATION INTERCHANGE
27
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
KEYWORDS IN C
There are 32 keywords in C Language
28
PCTE INSTITUTE OF ENGINEERING & TECHNOLOGY
IDENTIFIERS IN C
➢ Names used to identify variables, functions, etc.
○ Case-sensitive
1. _count 6. float
2. highScore 7. float_number_1
3. total_value_ 8. sum#
4. _number_ 9. number 1
32
PRIMARY DATA TYPES
1. INT
➢ Used to store integers ( both positive and negative )
➢ Each int variable takes upto 4 bytes of memory ( 32 bits )
➢ Int can be classified as :
a. Signed
○ Range : -2,147,483,648 to 2,147,483,647
b. Unsigned :
○ Range : 0 to 4,294,967,295
➢ short int ( 2 bytes )
➢ long int ( 8 bytes ) 33
PRIMARY DATA TYPES
2. FLOAT
➢ Used to store single precision floating-point numbers (decimals)
➢ Size : 4 bytes of memory ( 32 bits )
➢ Precision : 6 - 7 decimal digits
3. DOUBLE
➢ Size : 8 bytes
➢ Precision : 15 - 16 decimal digits
➢ long double ( 10, 12 or 16 bytes )
34
PRIMARY DATA TYPES
4. CHAR
➢ Used to store a single character
➢ Each char variable takes upto 1 byte of memory ( 8 bits )
➢ Char can be classified as :
a. Signed
○ Range : -128 to 127
b. Unsigned :
○ Range : 0 to 255
35
VARIABLES IN C
➢ It can be reused
36
TYPES OF VARIABLES IN C
1. LOCAL VARIABLES
or block
37
TYPES OF VARIABLES IN C
2. GLOBAL VARIABLES
execution
38
TYPES OF VARIABLES IN C
3. STATIC VARIABLES
default
40
TYPES OF VARIABLES IN C
5. EXTERNAL VARIABLES
keyword
6. REGISTER VARIABLES
42
CONSTANTS IN C
43
SYMBOLIC CONSTANTS IN C
➢ Eg #define PI 3.14159 ;
44
OPERATORS IN C
➢ Symbols that tell the compiler to perform specific operations
2. Relational Operators
6. Conditional Operators
3. Logical Operators
7. Bitwise Operators
4. Assignment Operators
46
ARITHMETIC OPERATORS
1. + (Addition) : Adds two operands
2. - (Subtraction) : Subtracts the second operand from the first
3. * (Multiplication) : Multiplies two operands
4. / (Division) : Divides first operand by the second
The result is an integer if both operands are integers
5. % ( Modulus ) : Returns the remainder of the division
47
RELATIONAL OPERATORS
These operators compare two operands (values) and return either
‘ true ’ ( 1 ) or ‘ false ‘ ( 0 )
48
RELATIONAL OPERATORS
These operators compare two operands (values) and return either
‘ true ’ ( 1 ) or ‘ false ‘ ( 0 )
6. <= ( Less than or equal to ) : Checks if first operand is less than or equal
to
second
49
LOGICAL OPERATORS
These operators are used to combine multiple conditions.
They are essential for decision making based on multiple criteria.
50
ASSIGNMENT OPERATORS
These operators are used to assign the value or result of an expression to a variable.
2. += : Adds right operand to left and assigns result to the left operand
3. -= : Subtracts right operand from left and assigns result to left operand
4. *= : Multiplies right operand with left and assigns result to the left operand
5. /= : Divides left operand by right and assigns result to the left operand
6. %= : Takes modulus of left operand with right and assigns result to the left operand
51
INCREMENT & DECREMENT
OPERATORS
1. ++ ( INCREMENT ) : Increase the value of the operand by 1
i. Pre-increment (++a)
ii. Post-increment(a++)
i. Pre-increment (--a)
ii. Post-increment(a--)
52
CONDITIONAL OPERATORS
This operator allows you to make decision within a single line of code.
1. & ( Bitwise AND ) : Perform AND operation on corresponding bits of two integers
4. ~ ( Bitwise NOT ) : Flips all the bits of an integer, turning ‘1’ to ‘0’ and vice versa
5. << ( Left Shift ) : Shifts the bits of a number to the left by specified no. of positions
6. >> ( Right Shift ) : Shifts the bits of a number to the right by specified no. of positions
54
QUIZ
Determine whether the following statements are True or False.
10. The / operator performs integer division when both operands are integers.
56
LIBRARY FUNCTIONS
➢ Pre-defined functions provided by C libraries