C Practical 1
C Practical 1
01
Aim :-
Software :-
Dev-C++
Theory :-
The 'printf' statement is part of the C programming language and is used for
formatted output. It allows you to print information to the console with specified
formatting. The format string contains placeholders, such as '%d' for integers or '%s'
for strings, which are then replaced by the corresponding values provided as additional
arguments.
the 'printf' statement is part of the standard input/output library ('stdio.h'). It is used
to display formatted output on the console. Here's a simple theory for printing "Hello,
World!" using the ‘printf’ statement.
Source Code :-
include<stdio.h>
int main()
printf("Hello World!");
return 0;
Output :-
Experiment No. 02
Aim :-
Software :-
Dev-C++
Theory :-
#include<stdio.h>
int main()
int x,y ;
int a, b, c, d, e;
a = x + y;
b = x - y;
c = x * y;
d = var1 / var2;
e = var1 % var2;
return 0;
}
Output :-
Experiment No. 03
Aim :-
Software :-
Dev-C++
Theory :-
Relational operators are used to compare two values and determine the relationship
between them. These operators return a Boolean result, either true (1) or false (0).
Here are some common relational operator :
Source Code :-
#include<stdio.h>
int main()
int x, y;
scanf("%d%d", &x,&y);
return 0;
}
Output :-
Experiment No. 04
Aim :-
Software :-
Dev-C++
Theory :-
Logical operators are used to perform logical operations on boolean values (true or
false). This are often used in conditional statements and loops to control the flow of a
program based on certain conditions.
1. Logical AND ("&&"): The "&&" operator returns true if both of its operands
are true; otherwise, it returns false.
2. Logical OR ("||"): The "||" operator returns true if at least one of its operands is
true; it returns false only if both operands are false.
3. Logical NOT ("!"): The "!" operator is a unary operator that negates the truth
value of its operand. If the operand is true, ! makes it false; if the operand is false, !
makes it true.
Source Code :-
#include<stdio.h>
int main()
int x, y, a, b, c, d;
a = 20<30 || 40>50;
b = 62<96 || 70>54;
c = !(20>30);
d = !(20<30);
Output :-
Experiment No. 05
Aim :-
Software :-
Dev-C++
Theory :-
The bitwise operators deal with the bit. It means that given operands are converted into
the binary form and then manipulated bits according to the operations.
1. Bitwise AND “&” : Takes two binary numbers as operands and performs a
bitwise AND operation on each pair of corresponding bits.
5. Left Shift “<<”: Shifts the bits of a binary number to the left by a specified
number of positions. New bits are filled with zeros.
6. Right Shift “>>”: Shifts the bits of a binary number to the right by a specified
number of positions. The vacant positions are filled based on the type of shift.
Source Code :-
#include<stdio.h>
int main()
int a, b,c;
scanf("%d%d",&a,&b);
c = a | b;
c = a & b;
c = a ^ b;
c = a << b;
c = ~a;
return 0;
Output :-
Experiment No. 06
Aim :-
Software :-
Dev-C++
Theory :-
The unary minus operator is used to negate the value of an expression. When
applied to a positive value, it changes the sign to negative, and vice versa.
3. Address of Operator (&) :
The address-of operator returns the memory address of its operand. It is often
used with pointers to obtain the memory address of a variable.
4. Size of Operator (size of) :
The sizeof operator is used to determine the size, in bytes, of a data type or a
variable. It provides a compile-time constant value that represents the size of the
expression or type.
Source Code :-
#include <stdio.h>
int main()
int a,b;
scanf("%d",&a);
b = -a;
b = +a;
b = sizeof(a);
return 0;
}
Output :-
Unary increment and decrement operators are used to increase or decrease the value of
a variable by 1. There are two forms of these operators: pre-increment, post-increment,
pre-decrement, and post-decrement.
Pre-increment (“++var”) :
The value of the variable is incremented before its value is used in an expression.
Post-increment (“var++”) :
Pre-decrement (“--var”) :
The value of the variable is decremented before its value is used in an expression.
Post-decrement (“var--”) :
#include <stdio.h>
int main()
int a,b;
scanf("%d",&a);
scanf("%d",&a);
scanf("%d",&a);
scanf("%d",&a);
return 0;
}
Output :-
Experiment No. 07
Aim :-
Software :-
Dev-C++
Theory :-
The ternary conditional operator in C, often denoted as “? :”, provides a concise way to
express conditional statements.
syntax:
1. Condition Evaluation :
2. Branching :
If the condition evaluates to true, the expression immediately following the ‘?’ is
executed.
If the condition evaluates to false, the expression immediately following the ‘:’
is executed.
Source Code :-
#include <stdio.h>
int main()
int a, b, c;
a= 14<19?14:19;
b= 45>96?45:96;
printf("a= %d\n",a);
printf("b= %d\n",b);
return 0;
}
Output :-
Experiment No. 08
Aim :-
Software :-
Dev-C++
Theory :-
Implicit and explicit conversions in C involve the conversion of data from one type to
another. These conversions can occur automatically by the compiler or can be
explicitly specified by the programmer.
Implicit Conversion:
Automatic Nature: The compiler automatically converts one data type to another
when it determines that the conversion is safe and will not result in loss of data.
Widening Conversions: Implicit conversions typically involve widening conversions,
where there is no loss of precision or data. For example, converting an integer to a
floating-point number.
Explicit Conversion:
Manual Instruction: The programmer uses casting operators like (type) to instruct the
compiler to perform the conversion.
Source Code :-
#include<stdio.h>
int main()
int x = 19;
char y = 'A';
x = x + y;
float z = x + 1.0;
double a = 1.7;
return 0;
Output :-
Experiment No. 09
Aim :-
Software :-
Dev-C++
Function in C :
A function in C is a block of code that performs a specific task, designed to be reusable
and modular. Functions help organize programs and avoid redundancy.
Definition of a Function:
A function is a set of statements grouped together to perform a specific operation.
Functions take input, perform computations, and return an output.
Types of Functions:
1. Library Functions: Built-in functions (e.g., printf(), scanf(), sqrt()).
2. User-defined Functions: Custom functions created by the programmer.
Defining a Function:
return_type function_name(parameters)
{
// Function body
return value; // Optional
}
Example:
Function Declaration:
The function declaration (prototype) specifies the function's name, return type, and
parameters before it is used in the program.
Syntax:
return_type function_name(parameter_list);
Example:
Function Calling:
1. Call by Value:
Example:
void modify(int x) {
2. Call by Reference:
Example:
}
Source Code :-
#include <stdio.h>
// Function Declaration
int add(int a, int b); // User-defined function (Call by Value)
void modify(int *x); // User-defined function (Call by Reference)
int main() {
// Call by Value
int num1 = 5, num2 = 10;
int result = add(num1, num2); // Function calling (Call by Value)
printf("Sum of %d and %d (Call by Value): %d\n", num1, num2, result);
// Call by Reference
int num3 = 20;
printf("Before Modify (Call by Reference): %d\n", num3);
modify(&num3); // Function calling (Call by Reference)
printf("After Modify (Call by Reference): %d\n", num3);
return 0;
}
// Function Definition
int add(int a, int b) {
return a + b;
}
Output :-
Experiment No. 10
Aim :-
Software :-
Dev-C++
Theory :-
Definition of Recursion:
Types of Recursion:
1. Direct Recursion:
A function directly calls itself.
a. Tail Recursion:
The recursive call is the last operation in the function.
b. Head Recursion:
The recursive call is made before any computation in the function.
c. Linear Recursion:
The function makes only one recursive call per execution.
d. Tree Recursion:
The function makes multiple recursive calls, creating a branching
structure.
e. Nested Recursion:
A function's parameter includes a recursive function call (e.g.,
f(f(x))).
2. Indirect Recursion:
A function calls another function, which eventually calls the original function
back.
Source Code :-
#include <stdio.h>
void funcA(int n) {
if (n > 0) {
printf("%d ", n);
funcB(n - 1); // Indirect recursion via funcB
}
}
void funcB(int n) {
if (n > 0) {
printf("%d ", n);
funcA(n - 1); // Indirect recursion via funcA
}
}
int main() {
printf("Direct Recursion: ");
directRecursion(5);
return 0;
}
Output :-