PROGRAMMING IN C
CODE- 312303
Ms. Pooja Ghug
Lecturer Comp De
II. COURSE LEVEL LEARNING OUTCOMES (COS)
• CO1 - Develop C program using input - output functions and arithmetic
expressions
• CO2 - Develop C program involving branching and looping statements
• CO3 - Implement Arrays and structures using C programs
• CO4 - Develop C program using user-defined functions
• CO5 - Write C program using pointer
1.1 Fundamentals of algorithms: Notion of algorithm, Notations used for
assignment statements and basic control structures.
1.2 Introduction to ‘C’: General structure of ‘ C' program, Header file, ‘main ()’
function.
1.3 Fundamental constructs of ‘C’: Character set, tokens, keywords, Identifiers,
Constants - number constants, character constants, string constants, Variables.
Data types in ‘C’: Declaring variables, data type conversion.
1.4 Basic Input and Output functions: input and output statements using printf(),
scanf() functions.
1.5 Assignments and expressions: simple assignment statements, arithmetic
operators, shift operators, bitwise operators, size of operator.
1.1 Fundamentals of algorithms: Notion of algorithm
An algorithm is a finite set of step-by-step instructions used to solve a specific problem or perform a
computation.
Example:
Algorithm to add two numbers:
1. Start
2. Read A and B
3. Sum = A + B
4. Display Sum
5. Stop
👉 In short:
Algorithm = Input + Processing +
Output
Notations used for assignment statements and basic control structures.
Introduction to ‘C’: General structure of ‘ C' program, Header file, ‘main ()’ function.
General Structure of ‘C’ Program
The general structure of a C program shows the basic organization of a C program.
It consists of the following parts:
[Link] Section – Contains comments describing the program.
[Link] Section – Includes header files using #include.
[Link] Section – Defines constants using #define .
[Link] Declaration Section – Declares global variables.
[Link]() Function – Entry point of the program where execution starts.
[Link] Defined Functions – Functions written by the programmer (optional).
1. Header Files
Header files are external files containing declarations of functions, macros, and global
variables that are used within the program. They serve as a contract, telling the
compiler what functions (like printf for printing output) exist and how to use them.
•Purpose: To import necessary definitions for standard library functions.
•Syntax: #include <filename.h> (for standard library files)
or #include "filename.h" (for user-defined local files).
•Common Examples:
•<stdio.h>: Standard Input/Output (e.g., printf, scanf).
•<stdlib.h>: Standard Library (e.g., memory management, data conversion).
•<math.h>: Mathematical functions (e.g., sqrt, pow).
2. main() function
The main() function is the essential entry point for every C program.
Execution always begins inside this function, regardless of where other functions are
located in the source code.
•Mandatory: Every valid C program must have exactly one main() function.
•Return Type: It is typically defined as int main(). The integer return value (usually 0)
indicates to the operating system whether the program executed successfully
(0 usually means success, while non-zero values usually indicate an error).
•Structure within main():
•Declarations: Variables used within the function must be declared first.
•Statements: Instructions the program executes sequentially.
•Return Statement: return 0; is typically the last line to signal successful
completion.
1. Fundamental Constructs of ‘C’
A. Character Set
The character set is the set of characters that a C compiler recognizes .
• Letters: Uppercase A-Z, Lowercase a-z.
• Digits: [Link] Characters:
• Characters like ;, ,, . , +, -, *, /, etc.
• White Spaces: Blank space, horizontal tab, return, and new line .
C Tokens
A Token is the smallest individual unit in a C program. The compiler breaks a program into tokens to
understand the syntax.
Example: In int x = 10;, the tokens are int, x, =, 10, and ;.
C. Keywords
Keywords are reserved words that have a fixed meaning to the C compiler.
They cannot be used as variable names.
There are 32 standard keywords in C (e.g., int, float, if, else, while, return, void).
All keywords must be written in lowercase.
4. Identifiers
Identifiers are names given to variables, functions, and [Link] for identifiers:
Must start with a letter or underscore (_) Can contain letters, digits, and underscore No special
symbols or spaces allowed Keywords cannot be used as identifiers
Examples: Valid: sum, _total, marks1Invalid: 1sum, total-marks, int
Identifier Name Status Reason
Uses letters and
student_name Valid
underscores.
_temp Valid Starts with an underscore.
RollNo21 Valid Alphanumeric.
2ndYear Invalid Starts with a digit.
5. Constants
Constants are fixed values that do not change during program execution.
Types of Constants:
a) Numeric Constants
Integer constants: 10, -25, 0
Floating point constants: 3.14, -2.5, 0.01
b) Character Constants Single character enclosed in single quotes
Example: 'A', '9', '#‘
c) String Constants Group of characters enclosed in double quotes
Example: "Hello", "C Programming"
6. Variables
A variable is a named memory location used to store data that can change during execution.
Syntax - datatype variable_name; Example: int count; float average;
Data Types in ‘C’
Data types specify the type of data a variable can store
.
Data Type Description Example
int Integer numbers int a;
float Decimal numbers float x;
char Single character char ch;
double Large decimal numbers double d;
7. Declaring Variables
Variable declaration tells the compiler about:
• Name of variable
• Type of data stored
Syntax- datatype variable_name;
EX-
int a;
float salary;
char grade;
data type conversion.
Definition:
Data type conversion in C is the process of converting one data type into another
data type during program execution.
There are two types of conversion in C:
Implicit Conversion (automatically)
Explicit Conversion (manually)
1) Implicit Conversion (automatically)
Implicit Type Conversion (Automatic)Done automatically by the
compiler.
Occurs when expressions contain different data types
Lower-order data type is converted to higher-order data type.
Order (Low → High):
char → int → float → double
// Automatic conversion: int to float
float myFloat = 9;
printf("%f", myFloat); // 9.000000
Explicit Conversion
Explicit conversion is done manually by placing the type in parentheses () in front of the value.
Syntax- (type) expression;
int a = 5, b = 2;
int a = 10, b = 3; float x, y;
float c;
c = (float)a / b; // result = 3.3333 x = a / b; // Output: 2.0 (implicit)
y = (float)a / b; // Output: 2.5 (explicit)
Why Type Conversion is Needed
• To avoid loss of data
• To get accurate results
• To perform operations between different data types
Program 1: Implicit Type Conversion Program 2: Explicit Type Conversion (Type Casting)
#include <stdio.h>
#include <stdio.h>
int main()
{ int main()
int a = 10; {
float b = 5.5; int a = 5, b = 2;
float result; float result;
result = a + b; // int is converted to float result = (float)a / b; // explicit conversion
automatically
printf("Result = %f", result);
printf("Result = %f", result); return 0;
return 0; }
}
1.4 Basic Input and Output functions:
Input and Output (I/O) functions are used to accept data from the user and display output on the screen.
In C, basic I/O is performed using scanf() and printf() functions.
Both functions are defined in the header file <stdio.h>.
printf() Function (Output Statement) Definitionprintf() is used to display output on the screen.
Syntax- printf("format string", variable1, variable2, ...);
EX- int a = 10;
printf("Value of a = %d", a);
Common Format Specifiers
Data Type Format Specifier
int %d
float %f
char %c
string %s
double %lf
scanf() Function -(Input Statement)
Definition - scanf() is used to accept input from the user.
Syntax- scanf("format string", &variable);
EX - int a;
scanf("%d", &a); (& (address-of operator) is used to store input value in variable memory location.)
1.5 Assignments and Expressions in C
Assignment Statement
An assignment statement assigns a value or result of an expression to a variable.
Syntax: variable = expression;
EX - int a;
a = 10;
Simple Assignment Operator Symbol: = Assigns right-hand value to left-hand variable
EX- int x = 5;
Arithmetic Operators
Used to perform basic mathematical operations.
Operator Meaning Example
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
/ Division a/b
% Modulus a%b
EX- int a = 10, b = 3;
printf("%d", a % b);
Shift Operators
Used to shift bits left or right.
Operator Name Description
<< Left shift Shifts bits to left
>> Right shift Shifts bits to right
Ex- int a = 4; << → Left shift (multiply by 2)
printf("%d", a << 1); // Output: 8
>> → Right shift (divide by 2)
Bitwise Operators
Operate on bit-level data.
Operator Name
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise NOT
Ex-
int a = 5, b = 3;
printf("%d", a & b);
sizeof Operator
• Returns size of variable or data type in bytes
• Evaluated at compile time
Syntax - Ex-
sizeof(variable); int a;
sizeof(data_type); printf("%d", sizeof(a));
Practical 1. Install and study the C programming Environment.
Write an algorithm and draw flowchart for the C program to read two numbers and print those
two numbers
Write an algorithm and draw the flowchart for printing “Hello World “.
Practical No 2: Implement C programs using Constants and Variables.
Exercise
1. Ramesh’s basic
salary is input through
the keyboard. His
dearness allowance is
40% of
basic salary, and
house rent allowance
is 20% of basic salary.
Write a program to
calculate his gross
salary.
.
2. If the marks obtained by a student in five different subjects are input through the keyboard, find out
the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks
that can be obtained by a student in each subject is 100.
Practical Related Questions
1. The length & breadth of a
rectangle and radius of a circle
are input through the keyboard.
Write a program to calculate
the area & perimeter of the
rectangle, and the area &
circumference of the circle.
2. Two numbers are
input through the
keyboard into two
locations C and D.
Write a program to
interchange the
contents of C and
D.
Practical No. 3: Implement C programs using arithmetic operators to solve given arithmetic
operations.
[Link] to Fahrenheit conversion.
2. Area calculation of rectangle
3. Area calculation of circle with PI value constant and #define function.
XIV. Practical Related Questions
I. Write an error message given by C compiler during program compilation if you use %d to read float
variable
III. Evaluate the following expressions and show their hierarchy. G=big/2+big*4/big -big + abc / 3;
( abc =2.5, big=2, assume G to be an float)
Convert the following mathematical formula into appropriate C statements. X=-b
(b*b)24ac/2a
Practical No 4: Implement C programs using implicit and Explicit data type conversion.
1. Program to show Implicit Typecasting
✅ 2. Program to show Explicit Typecasting
Practical No.5 Write well commented C programs using formatted Input/ Output statements.
✅ 1. Program
using
formatted
input/output
statements
Uses printf()
and scanf().
✅ 2. Program with Comment
4. Write a C
program to read
integer,
character, float,
double values
from the user
and display it.
Mention different
format specifiers
used in program
in comment.
Write a C program to print the address of variable. mention format
specifier used in comment.