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
CSEG1003 Programming for Problem
Solving
4. Online Lecture
• Problem Formulation
• Problem Solving
• Introduction to C
CSEG1003 Programming for Problem
Solving
5. LECTURE #8
STRUCTURE OF C PROGRAM ..COMPILATIONAND
LINKING
Instructor
Dhiviya Rose J . Asst. Prof. Senior Scale
School of Computer Science and Engineering | UPES
CSEG1003 Programming for Problem
Solving
8. Structured Blocks -> Functions
• Solves a large problem
• divides the problem into smaller
modules called functions
• each have particular responsibility.
• The program which solves the
entire problem is a collection of
such functions.
• Types
• Built-in Functions (Predefined)
• E.g. printf(), scanf()
• User-defined Functions
CSEG1003 Programming for Problem
Solving
10. Example Program 2
#include<stdio.h> PREPROCESSOR DIRECTIVE
void myline(); FUNCTION DECLARATION
void main()
{
printf("Hello");
myline(); FUNCTION CALL
}
void myline()
{
printf("this is my function"); FUNCTION DEFINITION
}
10Instructor: Dhiviya Rose J , AP-Sr. Scale | CIT
11. Functions Example 3
CSEG1003 Programming for Problem
Solving
OUTPUT ?????
Function Call ????
Function definition???
Function Declaration????
12. Main Function
CSEG1003 Programming for Problem
Solving
• When system is executed it calls the main function
• Entry point of any program
• Can have void main(){……}
• where it return type is void
• Or can be int main(){……… return 0;}
• where it returns an integer.
13. Steps in C compilation
CSEG1003 Programming for Problem Solving
14. Files in a C program
CSEG1003 Programming for Problem
Solving
Binary Format
a.out ./ <obj file>
17. Documentation
• Comments
• provide clarity to the C source code and allows others to better
understand
• helps in debugging the code.
• Two types
• Single Line //
• Multiline /* any text */
// This is my first Program
/* Written for Engineering students of Petroleum University for the batch
2018-1019 students */
#include <stdio.h>
void main()
{
printf(“Hello”);
}
CSEG1003 Programming for Problem
Solving
18. 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 gcc:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
CSEG1003 Programming for Problem
Solving
20. LECTURE #9
VARIABLES, CONSTANTS, DATATYPESAND
OPERATORS
Instructor
Dhiviya Rose J . Asst. Prof. Senior Scale
School of Computer Science and Engineering | UPES
CSEG1003 Programming for Problem
Solving
21. Knowledge Checks
#include <stdio.h>
void solveMeFirst( )
{
int num1,num2;
scanf("%d %d",&num1,&num2);
int sum;
sum = num1+num2;
printf("%d",sum);
}
void main()
{
________________Fill me
}
CSEG1003 Programming for Problem
Solving
22. Knowledge Check
• C Program Specification
• 3 user defined functions
• F1 -> prints “I am F1”
• F2 -> prints “I am F2”
• F3 -> prints “I am F3”
• Required Output
I am F2
I am F3
I am F1
I am F2
CSEG1003 Programming for Problem
Solving
23. Keywords
• C has a set of 32 reserved words often known as
keywords.
• All keywords are basically a sequence of characters
that have a fixed meaning.
• By convention all keywords must be written in
lowercase (small) letters.
• Example: for, while, do-while, auto break, case, char,
continue, do, double, else, enum, extern, float, goto,
if, int, long, register, return, short, signed, sizeof,
static, struct, switch, typedef, union, unsigned, void,
volatile
CSEG1003 Programming for Problem
Solving
24. Identifiers
• Identifiers are names given to program elements such as
variables, arrays and functions.
• Rules for forming identifier name
• it cannot include any special characters or punctuation marks (like #,
$, ^, ?, ., etc) except the underscore"_".
• There cannot be two successive underscores
• Keywords cannot be used as identifiers
• The names are case sensitive. So, example, “FIRST” is different from
“first” and “First”.
• It must begin with an alphabet or an underscore.
• It can be of any reasonable length. Though it should not contain more
than 31 characters.
• Example: roll_number, marks, name, SAP_ID, COURSE,
SEM
CSEG1003 Programming for Problem
Solving
25. Variables
• Meaningful name given to the data storage location in
computer memory.
• C language supports two basic kinds of variables.
• Numeric variables can be used to store either integer values or
floating point values.
• While an integer value is a whole numbers without a fraction part or
decimal point, a floating point number, can have a decimal point in
them.
• Numeric values may also be associated with modifiers like short, long,
signed and unsigned.
• By default, C automatically a numeric variable signed..
• Character variables can include any letter from the alphabet or from
the ASCII chart and numbers 0 – 9 that are put between single
quotes.
CSEG1003 Programming for Problem
Solving
27. Knowledge Check - Output????
CSEG1003 Programming for Problem
Solving
28. Knowledge Check - Output????
CSEG1003 Programming for Problem
Solving
29. DataType in C language
CSEG1003 Programming for Problem
Solving
30. 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;
CSEG1003 Programming for Problem
Solving
31. Possible ways of Variable Declaration
CSEG1003 Programming for Problem
Solving
35. 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.
CSEG1003 Programming for Problem Solving
37. Constants
• Constants are identifiers whose value does not
change.
• To declare a constant, precede the normal variable
declaration with const keyword and assign it a value.
For example,
const float pi = 3.14;
• Another way to designate a constant is to use the pre-
processor command define.
#define PI 3.14159
CSEG1003 Programming for Problem
Solving
38. Operators in C
• C language supports a lot of operators to be used in
expressions. Includes
• Arithmetic operators
• Relational Operators
• Equality Operators
• Logical Operators
• Unary Operators
• Conditional Operators
• Bitwise Operators
• Assignment operators
• Comma Operator
• Sizeof Operator
CSEG1003 Programming for Problem
Solving
40. Logical and Unary
• Logical operators
• Logical AND (&&)
• Logical OR (||)
• Logical NOT (!).
• As in case of arithmetic expressions, the logical
expressions are evaluated from left to right.
• Unary operators
• act on single operands.
• Three unary operators
• unary minus(-)
• Increment(++)
• Decrement operators(--)
CSEG1003 Programming for Problem
Solving
41. Conditional Operator
• (?:) is just like an if .. else statement
• Syntax of the conditional operator is
exp1 ? exp2 : exp3
• Eg.
large = ( a > b) ? a : b
• Conditional operator is also known as ternary
operator as it is neither a unary nor a binary operator;
it takes three operands.
CSEG1003 Programming for Problem
Solving
42. Bitwise and Bitwise Shift Operators
• Bitwise operators perform operations at bit level.
• bitwise AND operator (&)
• bitwise OR operator (|)
• bitwise NOT (~)
• bitwise XOR operator (^)
• In bitwise shift operations, the digits are moved, or shifted,
to the left or right.
• Left Shift unsigned (<<)
int x = 11000101;
x << 2 = 00010100
• Right arithmetic shift (>>)
int x = 11000101;
x >> 2 = 00110001
CSEG1003 Programming for Problem
Solving
44. Assignment, Comma Operator
CSEG1003 Programming for Problem
Solving
equal sign (=) is the fundamental assignment operator, C also supports
other assignment operators that provide shorthand ways to represent
common variable assignments.
Comma separated operands when chained together are evaluated in left-
to-right sequence with the right-most value yielding the result of the
expression.
45. Sizeof operator
• sizeof is a unary operator used to calculate the sizes
of data types.
• sizeof(char) returns 1, that is the size of a character
data type.
int a = 10;
int result;
result = sizeof(a);
then result = 2,
CSEG1003 Programming for Problem
Solving
47. LECTURE #10
INPUTAND OUTPUT STATEMENTS
Instructor
Dhiviya Rose J . Asst. Prof. Senior Scale
School of Computer Science and Engineering | UPES
CSEG1003 Programming for Problem
Solving
48. OUTPUT STATEMENT - printf()
• printf() function
• Output to Standard Output
• Prototype definition available in stdio.h
CSEG1003 Programming for Problem Solving
49. • 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()
CSEG1003 Programming for Problem
Solving
50. 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);
CSEG1003 Programming for Problem
Solving
51. • 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
CSEG1003 Programming for Problem Solving
55. List of Decision Making Structures in C
•If structure
•If Else structure
•Nested If structure
•Switch Structures
•Conditional Operator/
Terniary operator
CSEG1003 Programming for Problem
Solving
56. Converting a if block to C program
CSEG1003 Programming for Problem
Solving
if(condition)
{
//true statements
}
57. Check if entered number is positive
CSEG1003 Programming for Problem
Solving
58. Converting a if-else block to C program
CSEG1003 Programming for Problem
Solving
if(condition)
{
//true statements
}
else
{
//false statements
}
59. Example: If Else Statement
CSEG1003 Programming for Problem
Solving
60. Knowledge Checks – Implement – If… Else
CSEG1003 Programming for Problem
Solving
61. Nested IF
CSEG1003 Programming for Problem
Solving
if(condition 1)
{
//true statements
}
else if(condition 2)
{
//statements
}
else if(condition 3)
{
//statements
}
else
{
//statements
}
64. Switch Case
• Keywords used switch, case, default, break
• An value is passed as the input
• Switch(value)
• Evaluates with an value in each case
• Case 1:
• Case ‘A’:
• Default:
• Break is used to get exit from switch block
• Break;
CSEG1003 Programming for Problem
Solving
65. Convert Switch to C program
CSEG1003 Programming for Problem
Solving
66. Switch case Vs Nested IF
CSEG1003 Programming for Problem
Solving
69. 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)
CSEG1003 Programming for Problem
Solving
71. LECTURE #13
LOOP STRUCTURES IN C
Instructor
Dhiviya Rose J . Asst. Prof. Senior Scale
School of Computer Science and Engineering | UPES
CSEG1003 Programming for Problem
Solving
72. 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
CSEG1003 Programming for Problem
Solving
73. List of Loop Structures in C
• While
• Do….While
• For
CSEG1003 Programming for Problem
Solving
85. 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.
CSEG1003 Programming for Problem
Solving
88. 1. I = scanf("%d%d",&a,&b); value of I will be
2. I = sizeof(float); the value of I will be
3. i=printf("%d",20000); then i=?
4. i=printf("upes"); then i= ?
5. i=scanf("%d",&a); then i=?
CSEG1003 Programming for Problem
Solving
89. 6. i=printf("%d",20000); then i=?
7. printf("%d",printf("%d",345)); the output of the
statement will be ?
8. printf(“%d”,’A’);
9. printf("%o",12); the output of the statement will be ?
10. printf("%d",2&2); the output of the statement will
be ?
11. printf("%.2f",78.34567);
CSEG1003 Programming for Problem
Solving
95. Question 5
void anyFun(int a)
{
if(a==0)
return;
printf("Value is not Zero.");
}
void main()
{
anyFun(0);
}
96. Question 6
#include <stdio.h>
void fun(int a,int b)
{
a+=10;
b+=10;
}
int main()
{
int val1=10;
int val2=10;
printf("nValue before function calling : %d, %d",val1,val2);
fun(val1,val2);
printf("nValue after function calling : %d, %d",val1,val2);
return 0;
}
97. Question 7
#include <stdio.h>
void fun1(void)
{
void fun2(void); /*another function declaration*/
printf("nI am fun1");
fun2();
}
int main()
{
fun1();
return 0;
}
void fun2(void)
{
printf("nI am fun2");
}