3. Road Map
• Generations of Computers and Languages
• Organization of Computers-Online Lecture
• Number Systems Conversion
• Logical Analysis and Thinking
Introduction to
Computers
• Structure of C Program & Compilation and Linking Process
• Variables and Datatypes
• Managing Input and Output statements and Operators
• Decision and Looping Statements
C Programming
Basics
• Creation and Usages
• 1D and 2 D arrys
• String Functions
• Matrix operations
Arrays and
Strings
• Declaration and Definitions of Functions
• Passing Arguments
• Recursion
• Pointers & Pointer Arithmetic
Functions and
Pointers
• Need of Structure and Unions
• Declaration and Definition
• Storage classes
• Preprocessor Directives
Structures and
Unions
CSEG1001 Computer Programming
4. LECTURE #8
STRUCTURE OF C PROGRAM ..COMPILATIONAND
LINKING
Instructor
Dhiviya Rose J . Asst. Prof. Senior Scale
School of Computer Science and Engineering | UPES
CSEG1001 Computer Programming
5. Steps in C compilation
• Compiling a C program is a multi-stage process
• It contains four separate stages:
• Traditional C compilers orchestrate this process by
invoking other programs to handle each stage.
CSEG1001 Computer Programming
Preprocessing Compilation Assembly Linking
10. Documentation
• Comments
• provide clarity to the C source code
• allows others to better understand
• helps in debugging the code.
• Two types
• Single Line //
• Multiline /* any text */
CSEG1001 Computer Programming
11. Hello World in C
// This is my first Program
/* Written for Engineering students of
Petroleum University */
#include <stdio.h>
void main()
{
printf(“Hello”);
}
11
Instructor: Dhiviya Rose J , AP-Sr. Scale | CIT
12. Preprocessing
• First stage of compilation
• Lines starting with a # character are interpreted by the
preprocessor as preprocessor commands.
• This language is used to reduce repetition in source code
• Print the result of the preprocessing stage, pass the -E
option to cc:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
CSEG1001 Computer Programming
13. Function / Block
• Definition: Function/Procedure/Sub-routine
• Set of Instructions that are executed to achieve a particular task
• Code reusability
• Types
• Built-in Functions (System Defined)
• E.g. printf(), scanf()
• User-defined Functions
13
Instructor: Dhiviya Rose J , AP-Sr. Scale | CIT
14. Main() function
main() is always the first function called in a program
execution.
void main( void ) or int main()
{ … {….
void indicates that the function takes no arguments
int indicates that the function returns an integer value
CSEG1001 Computer Programming
19. Variables
• Naming a Variable
• Must be a valid identifier.
• Must not be a keyword
• Names are case sensitive.
• Variables are identified by only first 32 characters.
• length
• lowerLimit lower_limit
• incomeTax income_tax
20. Variable Declaration / Definition
• Declaring/Definition a Variable
• Declaration announces the data type of a variable and allocates
appropriate memory location.
• Each variable used must be declared.
• A form of a declaration statement is
data-type var1, var2,…;
Examples
int sum = 0;
char t1 = ‘a’;
float epsilon = 1.44;
21. Variable Definition and Initialization
If not initialized it will take a garbage value.
CSEG1001 Computer Programming
22. 22
Global and Local Variables
• Global Variables
• These variables are
declared outside all
functions.
• Life time of a global
variable is the entire
execution period of the
program.
• Can be accessed by any
function defined below the
declaration, in a file.
• Local Variables
• These variables are
declared inside some
functions.
• Life time of a local
variable is the entire
execution period of the
function in which it is
defined.
• Cannot be accessed by any
other function.
• In general variables
declared inside a block are
accessible only in that
block.
24. Constants
• The value will not change wherever declared
• Declared using const keyword
• Eg.
const float pi=3.14;
CSEG1001 Computer Programming
25. LECTURE #10
INPUTAND OUTPUT STATEMENTS
Instructor
Dhiviya Rose J . Asst. Prof. Senior Scale
School of Computer Science and Engineering | UPES
CSEG1001 Computer Programming
26. OUTPUT STATEMENT - printf()
• printf() function
• Output to Standard Output
• Prototype definition available in stdio.h
26
Instructor: Dhiviya Rose J , AP-Sr. Scale | CIT
27. • Case 1: printing only text
Flowchart code Output
printf(“Hello”); Hello
• Case 2: printing only value of a variable
printf(“%d” , area); 120
• Case 3: printing text and a value
printf(“The value of area is %d”,area); The value of area is 120
Print Hello
Print area
OUTPUT STATEMENT - printf()
28. INPUT STATEMENT – scanf()
• Case 1: getting 1 input value
scanf( “%d”, &r);
• Case 2: getting 2 input value
scanf( “%d %d ”, &l, &b);
• Case 3: getting 3 input value
scanf( “%d %d %d ”, &a, &b ,&c);
29. • scanf() function
• Input from Standard Input
• Prototype definition available in stdio.h
• Format Specifier(%d)
• Address-of Operator (&)
• Provides the memory address of the input variable were the input value is to
be stored
29
Instructor: Dhiviya Rose J , AP-Sr. Scale | CIT
30. Format Specifiers in C
• Used in association with printf() and scanf()
CSEG1001 Computer Programming
31. ONLINE LECTURE #11
OPERATORS IN C
Instructor
Dhiviya Rose J . Asst. Prof. Senior Scale
School of Computer Science and Engineering | UPES
CSEG1001 Computer Programming
37. LECTURE #12
DECISION STRUCTURES IN C
Instructor
Dhiviya Rose J . Asst. Prof. Senior Scale
School of Computer Science and Engineering | UPES
CSEG1001 Computer Programming
38. List of Decision Making Structures in C
•If structure
•If Else structure
•Nested If structure
•Switch Structures
•Conditional Operator/
Terniary operator
CSEG1001 Computer Programming
39. Converting a if block to C program
CSEG1001 Computer Programming
if(condition)
{
//true statements
}
40. Check if entered number is positive
CSEG1001 Computer Programming
41. Converting a if-else block to C program
CSEG1001 Computer Programming
if(condition)
{
//true statements
}
else
{
//false statements
}
49. EXPERIMENT NO – 4
Control Statements in C Language
List of lab works:
1. Write a program to accept 3 numbers and find the greatest of them, using
if…….else statements.
2. Write a program to find the biggest of 3 numbers using conditional operator/ternary
operator?
3. Write a program to check whether the roots of a quadratic equation are real or
imaginary?
4. Program to find the average of students marks, if average<50 then result is ‘FAIL’
otherwise print the grade as pass /first class/distinction.
5. A book and stationary store decides to give its customers 10% discount on a
purchase greater than 10,000/-. The program should accept the quantity
purchased the price of the items and then calculate the amount payable. Further
based on the total amount, appropriate discount should be given and final payable
amount should be displayed.
6. Write a program to accept a number and display “Sunday/Monday/Tuesday…..”
Based on the number. (hint: if 1 is input then “Sunday”, if 2 is input then
“Monday”…..) using switch case.
7. Read the minutes from the keyboard and find out the no. of hours, mins , days ?
(Ex 1210 mins are displayed as 0 days,20 hrs,10 min)
CSEG1001 Computer Programming
50. LECTURE #13
LOOP STRUCTURES IN C
Instructor
Dhiviya Rose J . Asst. Prof. Senior Scale
School of Computer Science and Engineering | UPES
CSEG1001 Computer Programming
51. Introduction - Loop Statements
• Helps in executing a statement in flowchart repeatedly
• Loop statements – block of statement executed more
than one time
• How many times the loop statements are executed is
managed by a counter variable
• Counter Variable Initialization
• Eg. C=0
• Counter Variable Increment/Decrement
• Eg. C++, c=c+1,c=c+3
• Counter Variable Condition Check
• Eg. C<3
CSEG1001 Computer Programming
52. List of Loop Structures in C
• While
• Do….While
• For
CSEG1001 Computer Programming
63. EXPERIMENT NO – 5
Loop Statements in C Language
List of lab works:
1. Write a program to print half pyramid using *.
2. Write a C program to print all natural numbers from 1-n
using while loop.
3. Write a C program to find the sum of all even numbers
between 1 to n using do-while loop.
4. Write a C program to print multiplication table of any
number using for loop.
CSEG1001 Computer Programming