21CSS101J - Unit1 - Updated 2
21CSS101J - Unit1 - Updated 2
Kattankulathur
SCHOOLF OF COMPUTING
21CSS101J – PROGRAMMING FOR PROBLEM SOLVING
Unit : I
Session title: Evolution and Problem Solving
Session Outcome:
By the end of this session, learners will be able to:
i)Exhibit knowledge regarding evolution of programming languages
ii) Highlight the steps involved to solve a problem with the help of computer
• Programming has its origin in the 19th century, when the first “programmable” looms
and player piano scrolls were developed.
• This followed the punch cards encoded data in 20th century that used to direct the
mechanical processing. In the 1930s and early 1940s lambda calculus remained the
influential in language design.
• In 1940’s the modern computers have been introduced and programming languages
were developed.
• Write Program
• Choose a programming and convert the pseudocode into a program
• https://www.youtube.com/watch?v=YZV8Zv_YW7I
• https://www.youtube.com/watch?v=M4d3FXu9-3I
• https://www.youtube.com/watch?v=iHNLEWa7Hew
Unit : I
Session title: Algorithms and Pseudocode
Session Outcome:
By the end of this session, learners will be able to:
Think and evolve with a logic to construct an algorithm and pseudocode that can be converted into a program.
• Algorithms are crucial for data processing and decision-making in computer science.
• A well-designed algorithm can significantly reduce computation time and resource usage.
● It uses the structures of programming languages but is not meant for execution.
Benefits of Pseudocode
● Pseudocode enhances readability and understanding of algorithms for non-programmers.
Unit : I
Session title: Comments and structure of C program
Session Outcome:
By the end of this session, learners will be able to:
Know how to use the comments and how to write the structure of c program effectively
• The comments in C are human-readable explanations or notes in the source code of a C program.
• A comment makes the program easier to read and understand. These are the statements that are not
executed by the compiler or an interpreter.
• It is considered to be a good practice to document our code using comments.
• A person reading a large code will be bemused if comments are not provided about details of the
program.
• C Comments are a way to make a code more readable by providing more descriptions.
• C Comments can be used to prevent the execution of some parts of the code.
07-08-2024 21CSS101J PROGRAMMING FOR PROBLEM SOLVING - UNIT II 22
Types of comments in C
• Single-line comment
• Multi-line comment
/*Comment starts
continues
continues
.
.
.
Comment ends*/
/* C program to illustrate
use of
multi-line comment */
#include <stdio.h>
int main(void)
{
/*
This is a
multi-line comment
*/
/*
This comment contains some code which
will not be executed.
printf("Code enclosed in Comment");
*/
printf("Welcome to GeeksforGeeks");
return 0;
}
Output: Welcome to GeeksforGeeks
Definition
A preprocessor directive in C is any statement that begins with the "#" symbol. The #define is a
preprocessor compiler directive used to create constants. In simple terms, #define basically allows
the macro definition, which allows the use of constants in our code.
#define BORN 2000
Code:
/** //Documentation
* file: age.c
* author: you
* description: program to find our age.
*/
#include <stdio.h> //Link
#define BORN 2000 //Definition
int age(int current); //Global Declaration
int main(void) //Main() Function
{
int current = 2021;
printf("Age: %d", age(current));
return 0;
}
int age(int current) { //Subprograms
return current - BORN;
}
30
SRM INSTITUTE OF SCIENCE AND TECHNOLOGY
Kattankulathur
SCHOOLF OF COMPUTING
21CSS101J – PROGRAMMING FOR PROBLEM SOLVING
Unit :I
Session title: Input and output statements, Variables and
Identifiers, Constants, Keywords - Values, Names, Scope, Binding
Session Outcome:
By the end of this session, learners will be able to:
i)Write programs using various forms of Input and output statements, Variables and identifiers,
Constants, Keywords - Values, Names, Scope, Binding .
ii)Demonstrate the differences and appropriate use cases for each type of input / output
.
statements
Syntax
printf(“%x”, variableOfxType);
Character:
syntax
Input: scanf("%s", stringVariable);
Output: printf("%s", stringVariable);
Syntax
data_type variable_name = value; // defining single variable or
data_type variable_name1, variable_name2; // defining multiple variable
Here,
• data_type: Type of data that a variable can store.
• variable_name: Name of the variable given by the user.
• value: value assigned to the variable by the user.
Example
int var; // integer variable
char a; // character variable
float fff; // float variables
07-08-2024 21CSS101J PROGRAMMING FOR PROBLEM SOLVING - UNIT II 4
2
Variables
There are 3 aspects of defining a variable:
1.Variable Declaration
2.Variable Definition
3.Variable Initialization
C Variable Declaration
Variable declaration in C tells the compiler about the
existence of the variable with the given name and data
type.When the variable is declared, an entry in symbol
table is created and memory will be allocated at the time
of initialization of the variable.
https://www.programiz.com/c-programming/online-compiler/
07-08-2024 21CSS101J PROGRAMMING FOR PROBLEM SOLVING - UNIT II 4
3
Variables
C Variable Initialization
Initialization of a variable is the process where the user assigns some
meaningful value to the variable when creating the variable.
Example
int var = 10;
07-08-2024 21CSS101J PROGRAMMING FOR PROBLEM SOLVING - UNIT II 4
5
Rules for Naming Variables in C
You can assign any name to the variable as long as
it follows the following rules:
1.A variable name must only contain alphabets, digits,
and underscore.
2.A variable name must start with an alphabet or an
underscore only. It cannot start with a digit.
3.No white space is allowed within the variable name.
4.A variable name must not be any reserved word or
keyword.
As its lifetime is till the end of the program, it can retain its value for multiple
function calls
https://www.programiz.com/c-programming/online-compiler/
In the above example, we can see that the local variable will always print the same
value whenever the function will be called whereas the static variable will print the
incremented value in each function call.
Syntax
register data_type variable_name = initial_value;
NOTE: We cannot get the address of the register variable using addressof (&)
operator because they are stored in the CPU register. The compiler will throw an
error if we try to get the address of register variable.
•
07-08-2024 21CSS101J PROGRAMMING FOR PROBLEM SOLVING - UNIT II 7
0
Local Scope in C
int main() {
int num1 = 10;
int num2 = 5;
Guess printf("The sum of %d and %d is:
%d", num1, num2, num1 +
the num2);
return 0;
output… }
• #include <stdio.h>
• int main() {
float radius = 3.5;
float area = 3.14159 * radius *
Guess radius;
printf("The area of the circle with
the radius %.2f is: %.2f", radius, area);
output… }
return 0;
• void main()
• { int number ;// number is a variable name
float amount;
• number= 100;
• amount = 30.75 + 75.35;
Guess • printf(“%d\n”, number); // this function
contain 2 argument the %d tells the compiler
the that the value of second argument number
output… should be printed as a decimal integer
• printf(“%5.2f\n”, amount); // %5.2f tells the
compiler that the output must be floating
point, with five place in all and two places to
the right of the decimal point.
07-08-2024
• }
21CSS101J PROGRAMMING FOR PROBLEM SOLVING - UNIT I 76
}
#include<stdio.h>
void main()
{ int a,b;
• float c;
• printf(" Enter the value of a \n");
Guess • scanf("%d",&a);
• printf(" Enter the value of b \n");
the • scanf("%d",&b);
output… • c=a*b;
• printf("The sum of two numbers are: %5.3f ",c);
/*printf("Hello world \n");
• printf("my first program");
• return (0);*/ }
07-08-2024 21CSS101J PROGRAMMING FOR PROBLEM SOLVING - UNIT I 77
}
• #include<stdio.h>
• void main()
• {
• char a;
Guess • printf("Enter the a:\n");
the • scanf("%c",&a);
output… • printf("You have entered: %c", a); }
• #include<stdio.h>
• void main()
• {
• char a[100];
• printf("Enter a:\n");
Guess • scanf("%s",&a);
the • printf("You name is: %s\n", a); //will read only
the text before space
output… • }
• O/P
• Enter a: Welcome to Python
07-08-2024
• You name is: Welcome
21CSS101J PROGRAMMING FOR PROBLEM SOLVING - UNIT I 79
#include <stdio.h> // driver code
int main()
{
float a = 12.67;
printf("Using %%f: %f\n", a);
printf("Using %%e: %e\n", a);
Guess printf("Using %%E, %E", a);
return 0;
the }
Using %f: 12.670000
output… Using %e: 1.267000e+01
Using %E, 1.267000E+01
Unit : I
Session title: Storage Classes
Session Outcome:
By the end of this session, learners will be able to:
i) To understand the different storage classes in C and their impact on variable scope, visibility, and lifetime.
ii) To demonstrates the use of auto, register, static, and extern storage classes through practical examples.
SCHOOL OF COMPUTING
21CSS101J – PROGRAMMING FOR PROBLEM SOLVING
Unit : I
Session title: Numeric Data types: Integer and Floating Point Non Numeric
Data Types: Char & String-L Value & R Value in Expression
Session Outcome:
By the end of this session, learners will be able to:
i)Write programs using various numerical data types
Mainly 2 Types
I. Numeric Data Types: Numeric data types are used to represent numerical values.
II. Non-Numeric Data types: Non-numeric data types are used to represent characters,
text, logical values, and other non-numerical information.
L value and R value refer to the left and right sides of the assignment operator.
An L value refers to an expression that represents a memory location. An L value means expression
which can be placed on the left-hand side of the assignment operator. An expression which has memory
location.
An examples of L values include variables, array elements, and deferential pointers.
An R value refers to an expression that represents a value that is stored at some location.
It can appear on the right-hand side of the assignment operator.
An examples of R value include literals (e.g numbers and characters), the results of arithmetic operations,
and function return values.
L value and R value in expression
Note:
L value can be used as an R value , but an R-value cannot be used as an L value.
The unary operator ‘&’ can be used to get the address of L value.
The address of operator ‘&’ cannot be applied to R value.
int main() {
int x = 10; // 'x' is an l-value
int y = x; // 'x' is an r-value on the right side of the assignment, and 'y' is an l-value
int* ptr = &x; // '&' operator gets the address of 'x', which is an l-value
// The following lines would result in compilation errors:
// int* ptr2 = &10; // Error: Cannot take the address of an r-value
// &x = 20; // Error: 'x' is an l-value, but the left side of the assignment operator expects an l-value
return 0;
}
Reference
• https://data-flair.training/blogs/numeric-data-types-in-c/#google_
vignette
• https://www.geeksforgeeks.org/data-types-in-c/
Unit: I
Session Title: Increment & Decrement Operators
Session Outcome:
By the end of this session, learners will be able to:
i)Write programs using Increment and Decrement operators, and understanding the power of this operators
In C programming, operators are symbols that perform operations on variables and values.
They are classified into different categories based on the type of operations they perform.
1. Arithmetic Operator
2. Relational Operator
3. Logical Operator
6. Bitwise Operator
7. Comma Operator
8. Sizeof Operator
07-08-2024 21CSS101J PROGRAMMING FOR PROBLEM SOLVING - UNIT II 108
Operators in C
1.Arithmetic Operator:-
Arithmetic operators are used to perform arithmetic operations between two operands.
== Equal to a == b
!= Not equal to a != b
> Greater than a>b
< Less than a<b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b
In C, logical operators are used to combine two or more conditions/expressions and return either true
(1) or false (0) based on the evaluation.
The three logical operators are:
1. Logical AND (&&): Returns true if both conditions are true.
2. Logical OR ( | | ): Returns true if at least one of the conditions is true.
3. Logical NOT (!): Reverses the logical state of its operand (true becomes false, false becomes
true).
Operator Description Example
&& Logical AND a && b
`|| Logical OR ` a || b
! Logical NOT !a
The assignment operators are used to assign the value of the right expression to the
left operand.
Operator Description Example
= Assignment a=b
The bitwise operators perform bit by bit operation on the values of the two operands.
The sizeof is a unary operator that returns the size of data (constants, variables, array,
structure, etc).
Syntax: sizeof(variable);
Pre Increment (++variable): Increases the value of the variable first, then returns the updated
value.
Post Increment (variable++): Returns the current value of the variable, then increases it by 1.
Example:
int a = 5;
Pre Decrement (- -variable): Decreases the value of the variable first, then returns the updated
value.
Post Decrement (variable - -): Returns the current value of the variable, then decreases it by 1.
Example:
int b = 5;
Output
#include <stdio.h>
int main()
{
output…
printf("c=%d \n", c++);
printf("d=%d \n", d--);
return 0;
}
sizeof operator
• The sizeof is a unary operator that returns the size of data (constants,
variables, array, structure, etc).
output… char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
return 0;
07-08-2024 }
21CSS101J PROGRAMMING FOR PROBLEM SOLVING - UNIT II 150
SRM INSTITUTE OF SCIENCE AND TECHNOLOGY
Kattankulathur
SCHOOLF OF COMPUTING
21CSS101J – PROGRAMMING FOR PROBLEM SOLVING
Unit : I
Session title: Operators
Session Outcome:
By the end of this session, learners will be able to:
i) To understand the types of operators and to understand how to use it while coding.
ii) It demonstrates the examples how to use operators
• Arithmetic Operator
• Relational Operator
• Logical Operator
1. Not
2. And
3. Or
Unit : I
Session title: Operator Precedence - Expressions with pre / post increment
operator
Session Outcome:
By the end of this session, learners will be able to:
i)Write programs using the associativity property of an operator.
ii)Demonstrate the operators of the same priority are evaluated either from left to right or from right to left, depending on the level .
Prepared By: Dr.Savaridaasan.P, Assistant Professor/ NWC.
Dr.Maranco.M, , Assistant Professor/ NWC.
05/11/2025 21CSS101J PROGRAMMING FOR PROBLEM SOLVING - UNIT I 168
Operator Precedence
(pre)++var_name;
Example:
https://tinyurl.com/3cub6edw
var_name -- (Post);
Example:
https://tinyurl.com/5n7dv3uh
(pre)--var_name;
Example:
https://tinyurl.com/3rc6m3t3
var_name++ (post);
Example:
https://tinyurl.com/mr3ftrf5
• https://www.geeksforgeeks.org/increment-and-decrement-
operators-in-c/