1.
%d – Integer (Decimal)
Program:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num); // Reading integer input from the user
printf("You entered: %d\n", num); // Printing the integer entered by the
user
return 0;
}
Explanation:
%d is used to input and output integer values (both positive and negative).
scanf("%d", &num) takes the integer input from the user.
printf("%d", num) prints the integer value.
Output:
Enter an integer: 5
You entered: 5
2. %f – Floating-point Numbers
Program:
#include <stdio.h>
int main() {
float num;
printf("Enter a floating-point number: ");
scanf("%f", &num); // Reading floating-point number input
printf("You entered: %f\n", num); // Printing the floating-point number
return 0;
}
Explanation:
%f is used for floating-point numbers (float).
scanf("%f", &num) reads a floating-point number.
printf("%f", num) prints the floating-point number.
Output:
Enter a floating-point number: 5.3
You entered: 5.300000
3. %lf – Double (Floating-point)
Program:
#include <stdio.h>
int main() {
double num;
printf("Enter a double: ");
scanf("%lf", &num); // Reading double precision floating-point number
printf("You entered: %lf\n", num); // Printing the double precision
floating-point number
return 0;
}
Explanation:
%lf is used for double precision floating-point numbers.
scanf("%lf", &num) reads the double value.
printf("%lf", num) prints the double value.
Output:
Enter a double: 3.14159
You entered: 3.141590
4. %c – Character
Program:
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch); // Reading a single character
printf("You entered: %c\n", ch); // Printing the character entered
return 0;
}
Explanation:
%c is used to input and output single characters.
scanf("%c", &ch) takes the character input from the user.
printf("%c", ch) prints the character.
Output:
Enter a character: A
You entered: A
5. %s – String
Program:
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str); // Reading a string (no spaces)
printf("You entered: %s\n", str); // Printing the string
return 0;
}
Explanation:
%s is used to input and output strings.
scanf("%s", str) reads a single word (no spaces) as input.
printf("%s", str) prints the string.
Output:
Enter a string: Hello
You entered: Hello
6. %x / %X – Hexadecimal (Lowercase/Uppercase)
Program:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num); // Reading integer input
printf("Hexadecimal (lowercase): %x\n", num); // Printing in lowercase
hexadecimal format
printf("Hexadecimal (uppercase): %X\n", num); // Printing in uppercase
hexadecimal format
return 0;
}
Explanation:
%x prints the hexadecimal value in lowercase.
%X prints the hexadecimal value in uppercase.
scanf("%d", &num) reads an integer.
printf("%x", num) prints the integer in lowercase hexadecimal.
printf("%X", num) prints the integer in uppercase hexadecimal.
Output:
Enter an integer: 255
Hexadecimal (lowercase): ff
Hexadecimal (uppercase): FF
7. %o – Octal
Program:
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num); // Reading an integer
printf("Octal format: %o\n", num); // Printing the number in octal format
return 0;
}
Explanation:
%o is used for octal representation of integers (base 8).
scanf("%d", &num) reads an integer.
printf("%o", num) prints the number in octal format.
Output:
Enter an integer: 10
Octal format: 12
8. %e / %E – Exponential Notation
Program:
#include <stdio.h>
int main() {
float num;
printf("Enter a floating-point number: ");
scanf("%f", &num); // Reading floating-point number
printf("Scientific notation (lowercase): %e\n", num); // Printing in
scientific notation (lowercase)
printf("Scientific notation (uppercase): %E\n", num); // Printing in
scientific notation (uppercase)
return 0;
}
Explanation:
%e prints the number in scientific notation (lowercase 'e').
%E prints the number in scientific notation (uppercase 'E').
scanf("%f", &num) reads a floating-point number.
printf("%e", num) prints the number in scientific notation (lowercase).
printf("%E", num) prints the number in scientific notation (uppercase).
Output:
Enter a floating-point number: 1234.567
Scientific notation (lowercase): 1.234567e+03
Scientific notation (uppercase): 1.234567E+03
9. %g / %G – Shortened Exponential or Floating-point
Program:
#include <stdio.h>
int main() {
double num;
printf("Enter a floating-point number: ");
scanf("%lf", &num); // Reading a floating-point number
printf("Shortened (lowercase): %g\n", num); // Printing the number in the
shortest form (lowercase)
printf("Shortened (uppercase): %G\n", num); // Printing the number in the
shortest form (uppercase)
return 0;
}
Explanation:
%g prints the number in the shortest form, either scientific or regular floating-point
format.
%G does the same but prints the scientific notation in uppercase.
scanf("%lf", &num) reads a floating-point number as double.
printf("%g", num) prints the number in the shortest format (either scientific or
regular).
printf("%G", num) prints the number in the shortest format (with uppercase scientific
notation if needed).
Output:
Enter a floating-point number: 0.000123456
Shortened (lowercase): 1.23456e-04
Shortened (uppercase): 1.23456E-04
10. %p – Pointer Address
Program:
#include <stdio.h>
int main() {
int num = 10;
int *ptr = # // Pointer holding the address of num
printf("Address of num: %p\n", (void*)ptr); // Printing the memory
address stored in ptr
return 0;
}
Explanation:
%p is used to print the memory address stored in a pointer.
(void*)ptr is cast to a void* to ensure portability across compilers.
printf("%p", (void*)ptr) prints the memory address of the pointer.
Output:
Address of num: 0x7ffee4b8fbbc
11. %ld – Long Integer
Program:
#include <stdio.h>
int main() {
long int num;
printf("Enter a long integer: ");
scanf("%ld", &num); // Reading long integer
printf("You entered: %ld\n", num); // Printing long integer
return 0;
}
Explanation:
%ld is used for long integers.
scanf("%ld", &num) reads a long integer.
printf("%ld", num) prints the long integer.
Output:
Enter a long integer: 1234567890
You entered: 1234567890
12. %llu – Unsigned Long Long
Program:
#include <stdio.h>
int main() {
unsigned long long num;
printf("Enter an unsigned long long integer: ");
scanf("%llu", &num); // Reading unsigned long long integer
printf("You entered: %llu\n", num); // Printing unsigned long long
integer
return 0;
}
Explanation:
%llu is used for unsigned long long integers, which are large positive integers.
scanf("%llu", &num) reads an unsigned long long integer.
printf("%llu", num) prints the unsigned long long integer.
Output:
Enter an unsigned long long integer: 1234567890123456789
You entered: 1234567890123456789
13. %% – Literal Percent Sign
Program:
#include <stdio.h>
int main() {
printf("This is 100%% correct!\n"); // Printing the literal percentage
sign
return 0;
}
Explanation:
%% is used to print a literal percent sign.
printf("100%% correct!") prints 100% correct!.
Output:
This is 100% correct!
OPERATORS PROGRAMS
1. Program to Swap Two Bits
Program:
#include <stdio.h>
int main() {
unsigned int num = 5; // Example number (binary: 0101)
int pos1 = 0, pos2 = 2;
unsigned int bit1 = (num >> pos1) & 1;
unsigned int bit2 = (num >> pos2) & 1;
// If the bits are different, swap them
if (bit1 != bit2) {
num ^= (1 << pos1) | (1 << pos2); // Toggle the bits at pos1 and pos2
}
printf("Number after swapping bits: %u\n", num);
return 0;
}
Explanation:
We shift the bits at the given positions (pos1, pos2) using the bitwise right shift (>>).
We check if the bits at those positions are different. If they are, we toggle (flip) the bits
using the XOR operator (^).
The final number is printed after swapping the bits.
Output:
Number after swapping bits: 1
(Original number: 5 or 0101 in binary, after swapping bit positions 0 and 2: 1 or 0001 in binary)
2. Program to Swap Two Words/Nibbles of a Byte
Program:
#include <stdio.h>
int main() {
unsigned char byte = 0xAB; // Example byte (binary: 10101011)
unsigned char nibble1 = (byte >> 4) & 0x0F; // Extract the first nibble
unsigned char nibble2 = byte & 0x0F; // Extract the second nibble
unsigned char swapped = (nibble2 << 4) | nibble1; // Swap the nibbles
printf("Original byte: 0x%X\n", byte);
printf("After swapping nibbles: 0x%X\n", swapped);
return 0;
}
Explanation:
The byte is split into two 4-bit nibbles.
The nibbles are swapped by shifting and using the bitwise OR (|).
Output:
Original byte: 0xAB
After swapping nibbles: 0xBA
3. C Program to Swap Two Numbers Without Using a Third Variable
Program:
#include <stdio.h>
int main() {
int a = 5, b = 10;
printf("Before swapping: a = %d, b = %d\n", a, b);
// Swapping logic without using third variable
a = a + b; // a now holds the sum of a and b
b = a - b; // b gets the original value of a
a = a - b; // a gets the original value of b
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Explanation:
We swap the values by using arithmetic operations: addition and subtraction. This avoids
the need for a third variable.
Output:
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
4. C Program to Convert Temperature from Celsius to Fahrenheit and Vice
Versa
Program:
#include <stdio.h>
int main() {
float celsius, fahrenheit;
// Celsius to Fahrenheit
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9/5) + 32;
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
// Fahrenheit to Celsius
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5/9;
printf("Temperature in Celsius: %.2f\n", celsius);
return 0;
}
Explanation:
To convert from Celsius to Fahrenheit: F = (C * 9/5) + 32
To convert from Fahrenheit to Celsius: C = (F - 32) * 5/9
Output:
Enter temperature in Celsius: 25
Temperature in Fahrenheit: 77.00
Enter temperature in Fahrenheit: 77
Temperature in Celsius: 25.00
5. C Program to Calculate Compound Interest and Simple Interest
Program:
#include <stdio.h>
#include <math.h>
int main() {
float principal, rate, time, simpleInterest, compoundInterest;
// Input values
printf("Enter principal amount: ");
scanf("%f", &principal);
printf("Enter rate of interest: ");
scanf("%f", &rate);
printf("Enter time period: ");
scanf("%f", &time);
// Simple Interest formula: SI = P * R * T / 100
simpleInterest = (principal * rate * time) / 100;
// Compound Interest formula: CI = P * (1 + R/100)^T - P
compoundInterest = principal * pow((1 + rate / 100), time) - principal;
// Output results
printf("Simple Interest: %.2f\n", simpleInterest);
printf("Compound Interest: %.2f\n", compoundInterest);
return 0;
}
Explanation:
Simple interest is calculated using the formula: SI = (P * R * T) / 100
Compound interest is calculated using the formula: CI = P * (1 + R/100)^T - P
Output:
Enter principal amount: 1000
Enter rate of interest: 5
Enter time period: 2
Simple Interest: 100.00
Compound Interest: 102.50
6. C Program to Convert Specified Days into Years, Weeks, and Days
Program:
#include <stdio.h>
int main() {
int days, years, weeks;
printf("Enter number of days: ");
scanf("%d", &days);
years = days / 365;
days = days % 365;
weeks = days / 7;
days = days % 7;
printf("%d days = %d years, %d weeks, %d days\n", days, years, weeks,
days);
return 0;
}
Explanation:
First, calculate the years by dividing the total days by 365.
Then, calculate weeks by dividing the remaining days by 7.
The remaining days are calculated using the modulo operation.
Output:
Enter number of days: 400
400 days = 1 years, 10 weeks, 5 days
7. C Program to Calculate the Distance Between Two Points
Program:
#include <stdio.h>
#include <math.h>
int main() {
float x1, y1, x2, y2, distance;
printf("Enter coordinates of point 1 (x1, y1): ");
scanf("%f %f", &x1, &y1);
printf("Enter coordinates of point 2 (x2, y2): ");
scanf("%f %f", &x2, &y2);
distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2)); // Using distance
formula
printf("Distance between the points: %.2f\n", distance);
return 0;
}
Explanation:
The distance between two points (x1, y1) and (x2, y2) is calculated using the distance
formula:
d = sqrt((x2 - x1)^2 + (y2 - y1)^2)
Output:
Enter coordinates of point 1 (x1, y1): 3 4
Enter coordinates of point 2 (x2, y2): 6 8
Distance between the points: 5.00
8. What will be the output of the following program?
#include <stdio.h>
void main()
{
printf("value is = %d",(10++));
}
Explanation:
10++ is invalid because 10 is a constant and you can't increment a constant value.
This program will give a compilation error.
Output:
Compilation Error
9. What will be the output of the following program?
#include <stdio.h>
void main()
{
int x = 10;
x += (x++) + (++x) + x;
printf("%d", x);
}
Explanation:
x++ increments x but returns the old value (10).
++x increments x before using it (12).
The final result is 10 + 12 + 12 = 34. Then, x += 34 results in x = 10 + 34 = 44.
Output:
44
10. What will be the output of the following program?
#include <stdio.h>
void main()
{
int x;
x = (printf("AA") || printf("BB"));
printf("%d", x);
printf("\n");
x = (printf("AA") && printf("BB"));
printf("%d", x);
}
Explanation:
|| (logical OR) stops after printing "AA" because it's a true condition.
&& (logical AND) prints both "AA" and "BB", but the result of x is 1 because the second
printf evaluates to true.
Output:
AA1
AABB1
11. What will be the output of the following program?
#include <stdio.h>
void main()
{
int a = 3, b = 2;
a = a == b == 0;
printf("%d, %d", a, b);
}
Explanation:
a == b is evaluated first as a comparison, which is false (0), then 0 == 0 is evaluated
as true (1).
So, a = 1, and b = 2 remains unchanged.
Output:
1, 2
12. What will be the output of the following program?
#include <stdio.h>
void main() {
int intVar = 20, x;
x = ++intVar, intVar++, ++intVar;
printf("Value of intVar = %d, x = %d", intVar, x);
}
Explanation:
++intVar increments intVar to 21.
intVar++ increments intVar to 22, but returns 21 for assignment to x.
++intVar increments intVar to 23.
The final values are intVar = 23 and x = 21.
Output:
Value of intVar = 23, x = 21