2 CP
2 CP
In C, data types specify the type of data that a variable can hold. The basic data types in C are:
float: Floa ng-point data type, used for numbers with decimal points.
float price = 19.99;
double: Double-precision floa ng-point data type, provides more precision than float.
double pi = 3.14159265359;
float: 32-bit floa ng-point.
double: 64-bit double-precision floa ng-point.
long double: Extended precision.
double price = 99.99;
Variables:
Variables are used to store and manipulate data in a program. They must be declared before
use, specifying their data type.
int main() {
int x; // Declara on
Constants:
Constants are values that do not change during the execu on of a program. They can be
defined using the const keyword.
const float PI = 3.1415;
Literals:
Literals are constant values that are used directly in the program. For example:
Integer literals: 10, 20
Floa ng-point literals: 3.14, 2.0
Character literals: 'A', 'b'
Storage Classes:
Storage classes in C determine the scope and life me of variables. The main storage classes
are:
auto: The default storage class for local variables. The variable is automa cally created and
destroyed.
register: Similar to auto, but suggests the compiler to store the variable in a register for faster
access.
sta c: The variable retains its value between func on calls. It has a file scope if declared
outside a func on.
extern: Informs the compiler that the variable is defined elsewhere. Used for global variables.
Operators:
Operators in C are symbols that perform opera ons on variables and values. They can be
categorized as:
Arithme c Operators: +, -, *, /, %
int result = 10 + 5; // result is 15
int main() {
int a = 5, b = 10;
prin ("Before Swap: a = %d, b = %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
int main() {
int num;
prin ("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0)
prin ("%d is even.\n", num);
else
prin ("%d is odd.\n", num);
return 0;
}
int main() {
float radius, area;
prin ("Enter the radius of the circle: ");
scanf("%f", &radius);
4. Declare a constant named PI and a variable named radius. Calculate the circumference
using the formula: circumference = 2 * PI * radius.
#include <stdio.h>
int main() {
const float PI = 3.14;
float radius, circumference;
circumference = 2 * PI * radius;
5. Write a program to perform bitwise AND, OR, and XOR opera ons on two integers.
#include <stdio.h>
int main() {
int a = 12, b = 25;
int main() {
int year;
prin ("Enter a year: ");
scanf("%d", &year);
return 0;
}
int main() {
int num, sum = 0;
prin ("Enter a posi ve integer: ");
scanf("%d", &num);
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
int main() {
int num;
prin ("Enter a number: ");
scanf("%d", &num);
9. Write a program that ini alizes an array of integers and finds the sum and average of
its elements.
#include <stdio.h>
int main() {
int numbers[] = {2, 4, 6, 8, 10};
int sum = 0, size = sizeof(numbers) / sizeof(numbers[0]);
return 0;
}
10. Write a program to simulate a basic login system using logical operators.
#include <stdio.h>
int main() {
char username[] = "user123";
char password[] = "pass456";
char inputUsername[20];
char inputPassword[20];
return 0;
}
11. Write a program to find the maximum of two numbers using the condi onal (ternary)
operator.
#include <stdio.h>
int main() {
int a, b;
prin ("Enter two numbers: ");
scanf("%d %d", &a, &b);
12. Write a program to find the length of a string without using the `strlen` func on.
#include <stdio.h>
int main() {
char text[] = "Hello, World!";
prin ("Length of the string: %d\n", stringLength(text));
return 0;
}
13. Write a program to le -shi and right-shi an integer by a specified number of
posi ons.
#include <stdio.h>
int main() {
int num, shi ;
return 0;
}
14. Define a structure called `Person` with a ributes `name` and `age`. Create an array of
persons and print their details.
#include <stdio.h>
struct Person {
char name[50];
int age;
};
int main() {
struct Person people[3] = {{"Alice", 25}, {"Bob", 30}, {"Charlie", 22}};
return 0;
}
15. Create a union called `Value` that can store either an integer or a float. Write a
program to demonstrate its usage.
#include <stdio.h>
union Value {
int intValue;
float floatValue;
};
int main() {
union Value data;
data.intValue = 10;
data.floatValue = 3.14;
prin ("Float Value: %f\n", data.floatValue);
return 0;
}