0% found this document useful (0 votes)
21 views

2 CP

The document discusses various data types, variables, constants, literals, storage classes, operators and programming concepts in C language. It provides definitions and examples for basic data types like int, float, char etc. It also explains concepts like variables, constants, literals, operators and provides programming questions with solutions to demonstrate various C programming concepts.

Uploaded by

John Philip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

2 CP

The document discusses various data types, variables, constants, literals, storage classes, operators and programming concepts in C language. It provides definitions and examples for basic data types like int, float, char etc. It also explains concepts like variables, constants, literals, operators and provides programming questions with solutions to demonstrate various C programming concepts.

Uploaded by

John Philip
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Data Types:

In C, data types specify the type of data that a variable can hold. The basic data types in C are:

int: Integer data type, used for whole numbers.


int age = 25;
short int: Typically 16 bits.
int: Minimum 16 bits, usually 32 bits.
long int: Minimum 32 bits.
long long int: Minimum 64 bits.
long long int bigNumber = 1234567890123456LL;

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;

char: Character data type, used for single characters.


char grade = 'A';
char: Typically 8 bits.
signed char: Can represent both posi ve and nega ve values.
unsigned char: Represents only non-nega ve values.

_Bool: Boolean data type, represents true or false values.


_Bool isPassed = 1; // true

void: Represents the absence of a type or no value.


void func onWithoutReturnValue() {
// Some code here

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

x = 10; // Ini aliza on


return 0;
}

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

Rela onal Operators: ==, !=, <, >, <=, >=


int x = 5, y = 10;
if (x < y) {
// Code executes if x is less than y
}

Logical Operators: && (AND), || (OR), ! (NOT)


int a = 1, b = 0;
if (a && b) {

// Code executes if both a and b are true


}

Assignment Operators: =, +=, -=, *=, /=


x += 5; // Equivalent to x = x + 5

Increment/Decrement Operators: ++, --


int count = 10;
count++; // Increment by 1
Bitwise Operators: &, |, ^, <<, >>
int a = 5, b = 3;
int result = a & b; // Bitwise AND

Condi onal (Ternary) Operator: condi on ? expr1 : expr2


int max = (a > b) ? a : b; // If a is greater than b, max is a; otherwise, max is b.

sizeof Operator: Returns the size of a data type or object.


int size = sizeof(int); // size is the size of an integer in bytes

Programming ques ons:


1. Write a program to swap the values of two variables without using a third variable.
#include <stdio.h>

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;

prin ("A er Swap: a = %d, b = %d\n", a, b);


return 0;
}

2. Write a program to check if a given integer is even or odd


#include <stdio.h>

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;
}

3. Write a program to calculate the area of a circle using the radius.


#include <stdio.h>

int main() {
float radius, area;
prin ("Enter the radius of the circle: ");
scanf("%f", &radius);

area = 3.14 * radius * radius;

prin ("Area of the circle: %f\n", area);


return 0;
}

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;

prin ("Enter the radius of the circle: ");


scanf("%f", &radius);

circumference = 2 * PI * radius;

prin ("Circumference of the circle: %f\n", circumference);


return 0;
}

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;

prin ("Bitwise AND: %d\n", a & b);


prin ("Bitwise OR: %d\n", a | b);
prin ("Bitwise XOR: %d\n", a ^ b);
return 0;
}

6. Write a program to check whether a given year is a leap year or not.


#include <stdio.h>

int main() {
int year;
prin ("Enter a year: ");
scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))


prin ("%d is a leap year.\n", year);
else
prin ("%d is not a leap year.\n", year);

return 0;
}

7. Write a program to calculate the sum of digits of a posi ve integer.


#include <stdio.h>

int main() {
int num, sum = 0;
prin ("Enter a posi ve integer: ");
scanf("%d", &num);

while (num > 0) {


sum += num % 10;
num /= 10;
}

prin ("Sum of digits: %d\n", sum);


return 0;
}

8. Write a program to calculate the factorial of a given number.


#include <stdio.h>

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);

prin ("Factorial of %d: %d\n", num, factorial(num));


return 0;
}

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]);

for (int i = 0; i < size; ++i) {


sum += numbers[i];
}

float average = (float)sum / size;

prin ("Sum: %d\n", sum);


prin ("Average: %f\n", average);

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];

prin ("Enter username: ");


scanf("%s", inputUsername);
prin ("Enter password: ");
scanf("%s", inputPassword);

if (strcmp(username, inputUsername) == 0 && strcmp(password, inputPassword) ==


0)
prin ("Login successful!\n");
else
prin ("Login failed!\n");

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);

int max = (a > b) ? a : b;

prin ("Maximum: %d\n", max);


return 0;
}

12. Write a program to find the length of a string without using the `strlen` func on.
#include <stdio.h>

int stringLength(char str[]) {


int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}

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 ;

prin ("Enter an integer: ");


scanf("%d", &num);

prin ("Enter the number of posi ons to shi : ");


scanf("%d", &shi );

prin ("Le -shi ed result: %d\n", num << shi );


prin ("Right-shi ed result: %d\n", 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}};

for (int i = 0; i < 3; ++i) {


prin ("Person %d: %s, %d years old\n", i + 1, people[i].name, people[i].age);
}

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;

prin ("Integer Value: %d\n", data.intValue);

data.floatValue = 3.14;
prin ("Float Value: %f\n", data.floatValue);

return 0;
}

You might also like