Arithmetic: Basic calculations.
Relational: Compare values.
Logical: Combine conditions.
Bitwise: Manipulate bits.
Assignment: Assign values.
Increment/Decrement: Modify by 1.
Conditional: Simplify if-else.
Special Operators: Miscellaneous operations.
In C programming, operators are symbols or keywords used to perform operations on variables and values. They are fundamental to manipulating data and controlling the flow of the program. C provides a rich set of operators that can be classified into several categories.
Arithmetic Operators:
These operators are used to perform basic arithmetic operations like addition, subtraction, multiplication, division, and modulus.
Syntax:
result = operand1 operator operand2;
List of Arithmetic Operators:
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
% (Modulus or Remainder)
Example:
int a = 10, b = 5, result;
result = a + b; // Addition
result = a - b; // Subtraction
result = a * b; // Multiplication
result = a / b; // Division
result = a % b; // Modulus
Relational Operators:
These operators are used to compare two values and return a boolean result (true or false).
List of Relational Operators:
== (Equal to)
!= (Not equal to)
> (Greater than)
< (Less than)
>= (Greater than or equal to)
<= (Less than or equal to)
Example:
int a = 10, b = 5;
if (a == b) {
printf("a is equal to b\n");
}
if (a != b) {
printf("a is not equal to b\n");
}
if (a > b) {
printf("a is greater than b\n");
}
Logical Operators:
These operators are used to combine multiple conditions or boolean expressions. The result is also a boolean value.
List of Logical Operators:
&& (Logical AND)
|| (Logical OR)
! (Logical NOT)
Example:
int a = 10, b = 5, c = 20;
if (a > b && c > a) { // Logical AND
printf("Both conditions are true\n");
}
if (a > b || a < c) { // Logical OR
printf("At least one condition is true\n");
}
if (!(a == b)) { // Logical NOT
printf("a is not equal to b\n");
}
Bitwise Operators:
These operators work on the binary representation of numbers and perform bit-by-bit operations.
List of Bitwise Operators:
& (Bitwise AND)
| (Bitwise OR)
^ (Bitwise XOR)
~ (Bitwise NOT)
<< (Left Shift)
>> (Right Shift)
Example:
int a = 5, b = 3;
printf("a & b: %d\n", a & b); // Bitwise AND
printf("a | b: %d\n", a | b); // Bitwise OR
printf("a ^ b: %d\n", a ^ b); // Bitwise XOR
printf("~a: %d\n", ~a); // Bitwise NOT
printf("a << 1: %d\n", a << 1); // Left Shift
printf("a >> 1: %d\n", a >> 1); // Right Shift
Assignment Operators:
These operators are used to assign values to variables.
List of Assignment Operators:
= (Simple assignment)
+= (Add and assign)
-= (Subtract and assign)
*= (Multiply and assign)
/= (Divide and assign)
%= (Modulus and assign)
&= (Bitwise AND and assign)
|= (Bitwise OR and assign)
^= (Bitwise XOR and assign)
<<= (Left shift and assign)
>>= (Right shift and assign)
Example:
int a = 5;
a += 3; // a = a + 3
a -= 2; // a = a - 2
a *= 2; // a = a * 2
a /= 2; // a = a / 2
a %= 3; // a = a % 3
Unary Operators:
These operators work with only one operand. They can be used to increment, decrement, or get the address of a variable, etc.
List of Unary Operators:
++ (Increment)
-- (Decrement)
& (Address-of operator)
* (Dereference operator)
- (Unary minus)
+ (Unary plus)
! (Logical NOT)
Example:
int a = 5;
printf("++a: %d\n", ++a); // Increment first, then print
printf("--a: %d\n", --a); // Decrement first, then print
printf("Address of a: %p\n", &a); // Address of variable a
printf("Value at a: %d\n", *&a); // Dereferencing the address of a
Ternary (Conditional) Operator:
The ternary operator is a shorthand for an if-else statement. It evaluates a condition and returns one of two values.
Syntax:
condition ? expression1 : expression2;
Example:
int a = 5, b = 10;
int result = (a > b) ? a : b; // If a is greater than b, result will be a, otherwise b
printf("The larger number is: %d\n", result);
Comma Operator:
The comma operator allows multiple expressions to be evaluated in a single statement, where each expression is evaluated from left to right.
Syntax:
expression1, expression2, expression3, ...;
Example:
int a, b;
a = (b = 5, b + 1); // First assigns b = 5, then evaluates b + 1, and assigns that to a
printf("a = %d, b = %d\n", a, b);
Sizeof Operator:
The sizeof operator is used to determine the size (in bytes) of a data type or a variable.
Syntax:
sizeof(data_type)
Example:
int a = 10;
printf("Size of a: %lu bytes\n", sizeof(a)); // Output the size of int variable a
printf("Size of int: %lu bytes\n", sizeof(int)); // Output the size of int data type
Pointer Operators:
Address-of Operator (&): Used to get the memory address of a variable.
Dereference Operator (*): Used to access the value at the address stored in a pointer.
Example:
int a = 10;
int *ptr = &a; // Pointer to a
printf("Address of a: %p\n", &a); // Address of variable a
printf("Value of a: %d\n", *ptr); // Dereference ptr to get the value of a
c
Copy code
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
printf("The sum is: %d\n", sum);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num1, num2, difference;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
difference = num1 - num2;
printf("The difference is: %d\n", difference);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num1, num2, product;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
product = num1 * num2;
printf("The product is: %d\n", product);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
float num1, num2, quotient;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
if (num2 != 0) {
quotient = num1 / num2;
printf("The quotient is: %.2f\n", quotient);
} else {
printf("Division by zero is not allowed.\n");
}
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num1, num2, remainder;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num2 != 0) {
remainder = num1 % num2;
printf("The remainder is: %d\n", remainder);
} else {
printf("Modulus by zero is not allowed.\n");
}
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Original value: %d\n", num);
printf("After increment: %d\n", ++num);
printf("After decrement: %d\n", --num);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num, addValue;
printf("Enter a number and a value to add: ");
scanf("%d %d", &num, &addValue);
num += addValue;
printf("The result after compound addition is: %d\n", num);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num, multiplyValue;
printf("Enter a number and a value to multiply: ");
scanf("%d %d", &num, &multiplyValue);
num *= multiplyValue;
printf("The result after compound multiplication is: %d\n", num);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Addition: %d\n", num1 + num2);
printf("Subtraction: %d\n", num1 - num2);
printf("Multiplication: %d\n", num1 * num2);
if (num2 != 0) {
printf("Division: %.2f\n", (float)num1 / num2);
printf("Modulus: %d\n", num1 % num2);
} else {
printf("Division and modulus by zero are not allowed.\n");
}
return 0;
}
c
Copy code
#include <stdio.h>
#include <math.h>
int main() {
double base, exponent, result;
printf("Enter base and exponent: ");
scanf("%lf %lf", &base, &exponent);
result = pow(base, exponent);
printf("%.2lf raised to the power %.2lf is: %.2lf\n", base, exponent, result);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 == num2)
printf("The numbers are equal.\n");
else
printf("The numbers are not equal.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 != num2)
printf("The numbers are not equal.\n");
else
printf("The numbers are equal.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 > num2)
printf("%d is greater than %d.\n", num1, num2);
else
printf("%d is not greater than %d.\n", num1, num2);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 < num2)
printf("%d is less than %d.\n", num1, num2);
else
printf("%d is not less than %d.\n", num1, num2);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 >= num2)
printf("%d is greater than or equal to %d.\n", num1, num2);
else
printf("%d is not greater than or equal to %d.\n", num1, num2);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 <= num2)
printf("%d is less than or equal to %d.\n", num1, num2);
else
printf("%d is not less than or equal to %d.\n", num1, num2);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 > num2 && num1 > num3)
printf("%d is the largest number.\n", num1);
else if (num2 > num3)
printf("%d is the largest number.\n", num2);
else
printf("%d is the largest number.\n", num3);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num, lower, upper;
printf("Enter a number, lower bound, and upper bound: ");
scanf("%d %d %d", &num, &lower, &upper);
if (num >= lower && num <= upper)
printf("%d is between %d and %d.\n", num, lower, upper);
else
printf("%d is not between %d and %d.\n", num, lower, upper);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if ((num1 > 0 && num2 < 0) || (num1 < 0 && num2 > 0))
printf("The numbers have opposite signs.\n");
else
printf("The numbers do not have opposite signs.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18)
printf("You are eligible to vote.\n");
else
printf("You are not eligible to vote.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 > 0 && num2 > 0)
printf("Both numbers are positive.\n");
else
printf("At least one number is not positive.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 < 0 || num2 < 0)
printf("At least one number is negative.\n");
else
printf("Both numbers are non-negative.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (!(num % 2))
printf("The number is even.\n");
else
printf("The number is odd.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0 && (num % 2 == 0 || num % 5 == 0))
printf("The number is positive and divisible by 2 or 5.\n");
else
printf("The condition is not satisfied.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (!(age < 18))
printf("You are eligible to vote.\n");
else
printf("You are not eligible to vote.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num, lower, upper;
printf("Enter a number, lower bound, and upper bound: ");
scanf("%d %d %d", &num, &lower, &upper);
if (num >= lower && num <= upper)
printf("%d is between %d and %d.\n", num, lower, upper);
else
printf("%d is not between %d and %d.\n", num, lower, upper);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int marks;
printf("Enter marks (0-100): ");
scanf("%d", &marks);
if (marks >= 0 && marks <= 100)
printf("The marks are valid.\n");
else
printf("Invalid marks.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int marksMath, marksScience;
printf("Enter marks for Math and Science: ");
scanf("%d %d", &marksMath, &marksScience);
if (marksMath >= 40 || marksScience >= 40)
printf("You passed at least one subject.\n");
else
printf("You failed in both subjects.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int age;
char citizen;
printf("Enter your age and citizenship (Y/N): ");
scanf("%d %c", &age, &citizen);
if (age >= 18 && (citizen == 'Y' || citizen == 'y'))
printf("You are eligible to vote.\n");
else
printf("You are not eligible to vote.\n");
return 0;
}
Here are 10 live C programs demonstrating various use cases of assignment operators (=, +=, -=, *=, /=, %=):
c
Copy code
c
Copy code
c
Copy code
c
Copy code
c
Copy code
c
Copy code
c
Copy code
c
Copy code
c
Copy code
c
Copy code
}
Here are 10 live examples of programs that demonstrate the use of the conditional (ternary) operator (? :) in C programming language:
c
Copy code
#include <stdio.h>
int main() {
int a, b, max;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
max = (a > b) ? a : b;
printf("The maximum number is: %d\n", max);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
(num % 2 == 0) ? printf("The number is even.\n") : printf("The number is odd.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
(num > 0) ? printf("The number is positive.\n") : (num < 0) ? printf("The number is negative.\n") : printf("The number is zero.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a, b, min;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
min = (a < b) ? a : b;
printf("The smallest number is: %d\n", min);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
(age >= 18) ? printf("You are eligible to vote.\n") : printf("You are not eligible to vote.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a, b, c, max;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
printf("The greatest number is: %d\n", max);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
((ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'))
? printf("The character is a vowel.\n")
: printf("The character is a consonant.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
(num % 5 == 0 && num % 11 == 0) ? printf("The number is divisible by 5 and 11.\n") : printf("The number is not divisible by 5 and 11.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num, absValue;
printf("Enter a number: ");
scanf("%d", &num);
absValue = (num < 0) ? -num : num;
printf("The absolute value is: %d\n", absValue);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
(marks >= 40) ? printf("You passed.\n") : printf("You failed.\n");
return 0;
}
Here are 10 live examples of programs that demonstrate the use of the conditional (ternary) operator (? :) in C programming language:
c
Copy code
#include <stdio.h>
int main() {
int a, b, max;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
max = (a > b) ? a : b;
printf("The maximum number is: %d\n", max);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
(num % 2 == 0) ? printf("The number is even.\n") : printf("The number is odd.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
(num > 0) ? printf("The number is positive.\n") : (num < 0) ? printf("The number is negative.\n") : printf("The number is zero.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a, b, min;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
min = (a < b) ? a : b;
printf("The smallest number is: %d\n", min);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
(age >= 18) ? printf("You are eligible to vote.\n") : printf("You are not eligible to vote.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a, b, c, max;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
printf("The greatest number is: %d\n", max);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf(" %c", &ch);
((ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U'))
? printf("The character is a vowel.\n")
: printf("The character is a consonant.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
(num % 5 == 0 && num % 11 == 0) ? printf("The number is divisible by 5 and 11.\n") : printf("The number is not divisible by 5 and 11.\n");
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int num, absValue;
printf("Enter a number: ");
scanf("%d", &num);
absValue = (num < 0) ? -num : num;
printf("The absolute value is: %d\n", absValue);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
(marks >= 40) ? printf("You passed.\n") : printf("You failed.\n");
return 0;
}
Special operators include operators like sizeof, comma operator (,), pointer-related operators (&, *), and member selection operators (., ->). Here are 10 live programs demonstrating their usage:
c
Copy code
#include <stdio.h>
int main() {
int a;
float b;
double c;
printf("Size of int: %zu bytes\n", sizeof(a));
printf("Size of float: %zu bytes\n", sizeof(b));
printf("Size of double: %zu bytes\n", sizeof(c));
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int arr[10];
printf("Size of array: %zu bytes\n", sizeof(arr));
printf("Number of elements in array: %zu\n", sizeof(arr) / sizeof(arr[0]));
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a, b, sum;
a = (b = 5, b + 10); // First assigns b = 5, then evaluates b + 10
sum = (a, b); // Evaluates a but assigns the value of b to sum
printf("a = %d, b = %d, sum = %d\n", a, b, sum);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a = 10;
printf("The address of a is: %p\n", (void*)&a);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a = 10;
int *ptr = &a; // Pointer pointing to a
printf("Value of a: %d\n", *ptr); // Dereferencing pointer
return 0;
}
c
Copy code
#include <stdio.h>
struct Point {
int x, y;
};
int main() {
struct Point p = {10, 20};
struct Point *ptr = &p;
printf("x = %d, y = %d\n", ptr->x, ptr->y);
return 0;
}
c
Copy code
#include <stdio.h>
struct Point {
int x, y;
};
int main() {
struct Point p = {10, 20};
printf("x = %d, y = %d\n", p.x, p.y);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr;
printf("Address of arr[0]: %p\n", (void*)ptr);
ptr++; // Increment pointer
printf("Address after increment: %p\n", (void*)ptr);
printf("Size of int: %zu bytes\n", sizeof(int));
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a = 10, b = 20, result;
result = (a > b) ? (b = a, b + 5) : (a = b, a - 5);
printf("Result: %d\n", result);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
char str[] = "Hello, World!";
printf("Size of string: %zu bytes\n", sizeof(str)); // Includes null terminator
printf("Length of string: %zu characters\n", sizeof(str) - 1);
return 0;
}
kc
Here are 10 live programs demonstrating the use of the address-of (&) and pointer dereference (*) operators in C:
c
Copy code
#include <stdio.h>
int main() {
int a = 10;
printf("The address of a is: %p\n", (void*)&a);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a = 20;
int *ptr = &a; // Pointer points to the address of a
printf("The value of a using pointer: %d\n", *ptr);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a = 30;
int *ptr = &a;
*ptr = 50; // Modify the value of a using pointer
printf("The updated value of a is: %d\n", a);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40};
int *ptr = arr; // Pointer to the first element of the array
printf("First element: %d\n", *ptr);
ptr++; // Move to the next element
printf("Second element: %d\n", *ptr);
return 0;
}
c
Copy code
#include <stdio.h>
void modifyValue(int *x) {
*x = 100; // Modify the value at the address pointed by x
}
int main() {
int a = 50;
printf("Before: a = %d\n", a);
modifyValue(&a);
printf("After: a = %d\n", a);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a = 42;
int *ptr = &a;
int **ptr2 = &ptr; // Pointer to pointer
printf("Value of a using ptr2: %d\n", **ptr2);
return 0;
}
c
Copy code
#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr = (int *)malloc(sizeof(int)); // Allocate memory
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}
*ptr = 55; // Assign value
printf("Value stored at allocated memory: %d\n", *ptr);
free(ptr); // Free memory
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = arr; // Pointer points to the first element of the array
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, *(ptr + i)); // Access array elements using pointer
}
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
const int a = 100;
const int *ptr = &a; // Pointer to a const variable
printf("Value of a: %d\n", *ptr);
// *ptr = 200; // Uncommenting this line will give an error since a is const
return 0;
}
c
Copy code
#include <stdio.h>
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a = 10, b = 20;
printf("Before swapping: a = %d, b = %d\n", a, b);
swap(&a, &b);
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a, b, c;
a = (b = 5, c = 10, b + c); // Multiple expressions, assigns the last value to a
printf("a = %d\n", a);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
for (int i = 0, j = 10; i < 5; i++, j--) {
printf("i = %d, j = %d\n", i, j);
}
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a = 10, b = 20, result;
result = (a < b, b > 15); // The result is the value of the last expression
printf("Result: %d\n", result); // 1 because b > 15 is true
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a, b, c;
a = b = (c = 30, c + 10); // c = 30, then c + 10 is assigned to a and b
printf("a = %d, b = %d, c = %d\n", a, b, c);
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int x = 10, y = 20;
return (printf("x = %d, y = %d\n", x, y), 0); // Executes printf and then returns 0
}
c
Copy code
#include <stdio.h>
int main() {
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of char: %zu bytes\n", sizeof(char));
printf("Size of float: %zu bytes\n", sizeof(float));
printf("Size of double: %zu bytes\n", sizeof(double));
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a = 10;
float b = 20.5;
printf("Size of variable a: %zu bytes\n", sizeof(a));
printf("Size of variable b: %zu bytes\n", sizeof(b));
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("Size of array: %zu bytes\n", sizeof(arr));
printf("Number of elements in array: %zu\n", sizeof(arr) / sizeof(arr[0]));
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int *ptr;
char *cptr;
printf("Size of int pointer: %zu bytes\n", sizeof(ptr));
printf("Size of char pointer: %zu bytes\n", sizeof(cptr));
return 0;
}
c
Copy code
#include <stdio.h>
int main() {
int a = 10, b = 20;
printf("Size of a + b: %zu bytes\n", sizeof(a + b)); // Evaluates the size of the result
printf("Size of (a > b): %zu bytes\n", sizeof(a > b)); // Evaluates the size of the comparison result
return 0;
}
Here are live examples of C programs demonstrating each category of operator in C programming:
int main() {
int a = 10, b = 3;
printf("Addition: %d\n", a + b); // Output: 13
printf("Subtraction: %d\n", a - b); // Output: 7
printf("Multiplication: %d\n", a * b); // Output: 30
printf("Division: %d\n", a / b); // Output: 3
printf("Modulus: %d\n", a % b); // Output: 1
return 0;
}
int main() {
int a = 5, b = 10;
printf("a == b: %d\n", a == b); // Output: 0 (false)
printf("a != b: %d\n", a != b); // Output: 1 (true)
printf("a < b: %d\n", a < b); // Output: 1 (true)
printf("a > b: %d\n", a > b); // Output: 0 (false)
printf("a <= b: %d\n", a <= b); // Output: 1 (true)
printf("a >= b: %d\n", a >= b); // Output: 0 (false)
return 0;
}
int main() {
int a = 5, b = 10;
printf("Logical AND (a > 0 && b > 0): %d\n", (a > 0) && (b > 0)); // Output: 1
printf("Logical OR (a > 0 || b < 0): %d\n", (a > 0) || (b < 0)); // Output: 1
printf("Logical NOT (!(a > 0)): %d\n", !(a > 0)); // Output: 0
return 0;
}
int main() {
int a = 5, b = 3;
printf("Bitwise AND (a & b): %d\n", a & b); // Output: 1
printf("Bitwise OR (a | b): %d\n", a | b); // Output: 7
printf("Bitwise XOR (a ^ b): %d\n", a ^ b); // Output: 6
printf("Bitwise NOT (~a): %d\n", ~a); // Output: -6
printf("Left shift (a << 1): %d\n", a << 1); // Output: 10
printf("Right shift (a >> 1): %d\n", a >> 1); // Output: 2
return 0;
}
int main() {
int a = 5, b = 3;
a += b; // a = a + b
printf("a += b: %d\n", a); // Output: 8
a -= b; // a = a - b
printf("a -= b: %d\n", a); // Output: 5
a *= b; // a = a * b
printf("a *= b: %d\n", a); // Output: 15
a /= b; // a = a / b
printf("a /= b: %d\n", a); // Output: 5
a %= b; // a = a % b
printf("a %%= b: %d\n", a); // Output: 2
return 0;
}
int main() {
int a = 5, b = 10;
int max = (a > b) ? a : b;
printf("Maximum: %d\n", max); // Output: 10
return 0;
}
int main() {
int a = 5;
printf("Initial a: %d\n", a); // Output: 5
printf("Post-increment: %d\n", a++); // Output: 5
printf("After post-increment, a: %d\n", a); // Output: 6
printf("Pre-increment: %d\n", ++a); // Output: 7
printf("Post-decrement: %d\n", a--); // Output: 7
printf("After post-decrement, a: %d\n", a); // Output: 6
printf("Pre-decrement: %d\n", --a); // Output: 5
return 0;
}
a) sizeof Operator
int main() {
int a = 5;
printf("Size of int: %zu bytes\n", sizeof(int)); // Output: size of int in bytes
printf("Size of variable a: %zu bytes\n", sizeof(a)); // Output: size of a in bytes
return 0;
}
b) Address (&) and Pointer Dereference (*) Operators
int main() {
int a = 5;
int *ptr = &a;
printf("Address of a: %p\n", (void*)&a); // Output: memory address of a
printf("Value of a using pointer: %d\n", *ptr); // Output: 5
return 0;
}
c) Comma Operator
int main() {
int a = (5, 10); // Sets a to 10, the last expression's value
int x, y, z;
x = 1, y = 2, z = 3; // Comma as a separator
printf("a: %d, x: %d, y: %d, z: %d\n", a, x, y, z); // Output: 10, 1, 2, 3
return 0;
}