C Programming Basics and Examples
C Programming Basics and Examples
net/projects/embarcadero-devcpp/
int main()
{
char ch;
return 0;
}
//%zu → "print an unsigned decimal integer of type size_t".
[Link] for short
// C program to take input from user and print for short
#include <stdio.h>
#include <limits.h>
int main()
{
short num;
int main()
{
int number;
return 0;
}
[Link] for long
// C program to take input from user and print for long
#include <stdio.h>
#include <limits.h>
int main()
{
long num;
return 0;
}
[Link] for long long
// C program to take input from user and print for long long
#include <stdio.h>
#include <limits.h>
int main()
{
long longnum;
return 0;
}
[Link] for float
// C program to take input from user and print for float
#include <stdio.h>
#include <float.h>
int main()
{
float num;
return 0;
}
[Link] for double
// C program to take input from user and print for double
#include <stdio.h>
#include <float.h>
int main()
{
double num;
return 0;
}
[Link] for long double
// C program to take input from user and print for long double
#include <stdio.h>
#include <float.h>
int main()
{
long double num;
return 0;
}
11.// C program to illustrate syntax error
#include<stdio.h>
void main()
{
int x = 10;
int y = 15;
printf("%d", (x, y)) // semicolon missed
}
Output:
error: expected ';' before '}' token
Output:
warning: division by zero [-Wdiv-by-zero] div = n/0;
Output:
the remainder value is 5
int main() {
greet(); // ❌ error: implicit declaration / undefined reference
return 0;
}
fp = fopen("[Link]", "w");
fprintf(fp, "%d %d", 10, 20);
fclose(fp);
fp = fopen("[Link]", "r");
fscanf(fp, "%d %d", &x, &y);
fclose(fp);
if (num> 0) {
printf("The number is positive.\n");
}
return 0;
}
Input:
5
Output:
The number is positive.
59. if-else Statement
Program:
#include <stdio.h>
intmain() {
int age;
printf("Enter age: ");
scanf("%d", &age);
if (num>= 0) {
if (num % 2 == 0) {
printf("The number is positive and even.\n");
} else {
printf("The number is positive and odd.\n");
}
} else {
printf("The number is negative.\n");
}
return 0;
}
Input:
8
Output:
The number is positive and even
Input:
-5
Output:
The number is negative.
switch (choice) {
case 1:
printf("You chose Addition.\n");
break;
case 2:
printf("You chose Subtraction.\n");
break;
case 3:
printf("You chose Multiplication.\n");
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
Input:
2
Output:
You chose Subtraction.
63. while Loop
Program:
#include <stdio.h>
intmain() {
inti = 1;
while (i<= 5) {
printf("%d ", i);
i++;
}
return 0;
}
Output:
12345
64. for Loop
Program:
#include <stdio.h>
intmain() {
for (inti = 1; i<= 5; i++) {
printf("%d ", i);
}
return 0;
}
Output:
12345
65. do-while Loop
Program:
#include <stdio.h>
intmain() {
inti = 1;
do {
printf("%d ", i);
i++;
} while (i<= 5);
return 0;
}
Output:
12345
66. Goto Statement
Program:
#include <stdio.h>
intmain() {
intnum = 1;
start:
if (num<= 5) {
printf("%d ", num);
num++;
goto start;
}
return 0;
}
Output:
12345
67. break Statement
Program:
#include <stdio.h>
intmain() {
for (inti = 1; i<= 10; i++) {
if (i == 6)
break;
printf("%d ", i);
}
return 0;
}
Output:
12345
68. continue Statement
Program:
#include <stdio.h>
intmain() {
for (inti = 1; i<= 5; i++) {
if (i == 3)
continue;
printf("%d ", i);
}
return 0;
}
Output:
1245
69. Nested Loops
Program:
#include <stdio.h>
intmain() {
for (inti = 1; i<= 3; i++) {
for (int j = 1; j <= 3; j++) {
printf("%d,%d ", i, j);
}
printf("\n");
}
return 0;
}
Output:
1,1 1,2 1,3
2,1 2,2 2,3
3,1 3,2 3,3
70. Rectangle Pattern of Stars
Program Code:
#include <stdio.h>
intmain() {
int rows = 3, cols = 5;
intmain() {
int n = 5;
intmain() {
int n = 5;
intmain() {
int n = 4;
intmain() {
inti = 1;
while (i<= 3) {
int j = 1;
while (j <= 4) {
printf("%d ", j);
j++;
}
printf("\n");
i++;
}
return 0;
}
Sample Output:
1234
1234
1234
#include <stdio.h>
int main() {
for (inti = 1; i<= 3; i++) {
int j = 1;
while (j <= 3) {
printf("%d ", j);
j++;
}
printf("\n");
}
return 0;
}
o/p:
123
123
123
[Link] loop inside while loop
#include <stdio.h>
int main() {
inti = 1;
while (i<= 3) {
for (int j = 1; j <= 3; j++) {
printf("%d ", j);
}
printf("\n");
i++;
}
return 0;
}
o/p:
123
123
123
#include <stdio.h>
int main() {
inti = 1;
do {
for (int j = 1; j <= 3; j++) {
printf("%d ", j);
}
printf("\n");
i++;
} while (i<= 3);
return 0;
}
o/p:
123
123
123
intmain() {
int n = 5;
for (inti = 1; i<= n; i++) {
for (int space = 1; space <= n - i; space++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Sample Output:
*
**
***
****
*****
82. Inverted Pyramid Pattern
Program Code:
#include <stdio.h>
intmain() {
int n = 5;
for (inti = n; i>= 1; i--) {
for (int space = 1; space <= n - i; space++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Sample Output:
*****
****
***
**
*
83. Diamond Pattern
Program Code:
#include <stdio.h>
int main() {
int n = 5;
intmain() {
int n = 5;
for (inti = 1; i<= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == 1 || i == n || j == 1 || j == n)
printf("* ");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Sample Output:
*****
* *
* *
* *
*****
85. Hollow Pyramid Pattern
Program Code:
#include <stdio.h>
int main() {
int n = 5;
for (inti = 1; i<= n; i++) {
for (int space = 1; space <= n - i; space++) {
printf(" ");
}
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i || i == n)
printf("* ");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Sample Output:
*
**
* *
* *
*****
86. Hollow Diamond Pattern
Program Code:
#include <stdio.h>
int main() {
int n = 5;
int main() {
int n = 5;
int main() {
int n = 5;
int main() {
int n = 5;
int temp = n;
while(temp != 0) {
temp /= 10;
count++;
}
temp = n;
while(temp != 0) {
rem = temp % 10;
result += pow(rem, count);
temp /= 10;
}
if(result == original)
printf("Armstrong Number\n");
else
printf("Not Armstrong Number\n");
return 0;
}
Input / Output Example:
Input: 153
Output: Armstrong Number
97. Multiplication Table
Program Code:
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
for(int i = 1; i <= 10; i++)
printf("%d x %d = %d\n", n, i, n * i);
return 0;
}
Input / Output Example:
Input: 5
Output: Table of 5 from 1 to 10
98. Sum of Digits
Program Code:
#include <stdio.h>
int main() {
int n, sum = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n != 0) {
rem = n % 10;
sum += rem;
n /= 10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}
Input / Output Example:
Input: 1234
Output: 10
99. Count Digits
Program Code:
#include <stdio.h>
int main() {
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
do {
count++;
n /= 10;
} while(n != 0);
printf("Number of digits = %d\n", count);
return 0;
}
Input / Output Example:
Input: 5678
Output: 4
100. Power of a Number
Program Code:
#include <stdio.h>
int main() {
int base, exp;
long long result = 1;
printf("Enter base and exponent: ");
scanf("%d %d", &base, &exp);
for(int i = 1; i <= exp; i++)
result *= base;
printf("%d^%d = %lld\n", base, exp, result);
return 0;
}
Input / Output Example:
Input: 2 5
Output: 2^5 = 32
101. Factor Count of a Number
Program Code:
#include <stdio.h>
int main() {
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
if(n % i == 0)
count++;
}
printf("Number of factors = %d\n", count);
return 0;
}
Input / Output Example:
Input: 12
Output: 6
102. Print Even Numbers
Program Code:
#include <stdio.h>
int main() {
int n;
printf("Enter limit: ");
scanf("%d", &n);
printf("Even numbers: ");
for(int i = 2; i <= n; i += 2)
printf("%d ", i);
return 0;
}
Input / Output Example:
Input: 10
Output: 2 4 6 8 10
103. Print Odd Numbers
Program Code:
#include <stdio.h>
int main() {
int n;
printf("Enter limit: ");
scanf("%d", &n);
printf("Odd numbers: ");
for(int i = 1; i <= n; i += 2)
printf("%d ", i);
return 0;
}
Input / Output Example:
Input: 9
Output: 1 3 5 7 9
104. Sum of Even and Odd Numbers Separately
Program Code:
#include <stdio.h>
int main() {
int n, evenSum = 0, oddSum = 0;
printf("Enter n: ");
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
if(i % 2 == 0)
evenSum += i;
else
oddSum += i;
}
printf("Sum of even numbers = %d\n", evenSum);
printf("Sum of odd numbers = %d\n", oddSum);
return 0;
}
Input / Output Example:
Input: 10
Output: Sum of even numbers = 30
Sum of odd numbers = 25
[Link] declaration
#include<stdio.h>
void main()
{
int a[]={25,23,21,42};
int i;
for(i=0;i<4;i++)
{
printf("a[%d]=%d\n",i,a[i]);
printf("adress=%u\n",&a[i]);
}
}
Output:
a[0]=25
adress=6684208
a[1]=23
adress=6684212
a[2]=21
adress=6684216
a[3]=42
adress=6684220
#include<stdio.h>
void main()
{
int i,number[10]={1,5,8};
float num[5]={0.5,15.8,-10};
char name[8]={'n','i','k'};
for(i=0;i<10;i++)
{
printf("number[%d] = %d\n",i,number[i]);
}
for(i=0;i<5;i++)
{
printf("num[%d] = %f\n",i,num[i]);
}
for(i=0;i<8;i++)
{
printf("name[%d] = %c\n",i,name[i]);
}
}
Output:
number[0] = 1
number[1] = 5
number[2] = 8
number[3] = 0
number[4] = 0
number[5] = 0
number[6] = 0
number[7] = 0
number[8] = 0
number[9] = 0
num[0] = 0.500000
num[1] = 15.800000
num[2] = -10.000000
num[3] = 0.000000
num[4] = 0.000000
name[0] = n
name[1] = i
name[2] = k
name[3] =
name[4] =
name[5] =
name[6] =
name[7] =
#include<stdio.h>
void main()
{
int arr[5], i;
printf("enter array elements:");
for(i = 0; i < 5; i++)
{
scanf("%d", &arr[i]);
}
printf("\nPrinting elements of the array: \n\n");
for(i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
}
Output:
enter array elements:1 6 3 2 9
16329
#include<stdio.h>
void main()
{
int sum=0, avg, i, marks[10];
for(i=0;i<10;i++)
{
printf("Enter marks:");
scanf("%d",&marks[i]);
}
for(i=0;i<10;i++)
{
sum=sum+ marks[i];
}
avg=sum/10;
printf("\nAverage marks= %d\n", avg);
}
Output:
Enter marks:5
Enter marks:2
Enter marks:8
Enter marks:21
Enter marks:67
Enter marks:32
Enter marks:25
Enter marks:90
Enter marks:32
Enter marks:55
Average marks= 33
#include<stdio.h>
void main()
{
int a[]={25,23,21,42};
int i;
for(i=0;i<4;i++)
{
printf("address=%u, ",&a[i]);
printf("a[%d]=%d %d %d %d \n",i,a[i], *(a+i), *(i+a), i[a]);
}
}
Output:
address=6684208, a[0]=25 25 25 25
address=6684212, a[1]=23 23 23 23
address=6684216, a[2]=21 21 21 21
address=6684220, a[3]=42 42 42 42
#include<stdio.h>
void main()
{
int a[50], i, n, large, small;
printf("How many elements:");
scanf("%d",&n);
[Link] to initialize Two dimensional array, print the array elements and its respective
address
#include<stdio.h>
void main()
{
int students[4][2]={101,88,102,97,103,65,104,85};
int i,j;
for(i=0;i<4;i++)
for(j=0;j<2;j++)
{
printf("students[%d][%d] = %d", i,j,students[i][j]);
printf(" address = %u\n", &students[i][j]);
}
}
Output:
students[0][0] = 101 address = 6684192
students[0][1] = 88 address = 6684196
students[1][0] = 102 address = 6684200
students[1][1] = 97 address = 6684204
students[2][0] = 103 address = 6684208
students[2][1] = 65 address = 6684212
students[3][0] = 104 address = 6684216
students[3][1] = 85 address = 6684220
#include<stdio.h>
void main()
{
int arr[10][10], row, col, i, j;
printf("Enter number of row for Array (max 10) : ");
scanf("%d",&row);
printf("Enter number of column for Array (max 10) : ");
scanf("%d",&col);
printf("Now Enter %d*%d Array Elements : ",row, col);
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("The Array is :\n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
Output:
Enter number of row for Array (max 10) : 5
Enter number of column for Array (max 10) : 2
Now Enter 5*2 Array Elements : 1 2 3 4 5 6 7 8 9 10
The Array is :
1 2
3 4
5 6
7 8
9 10
[Link] to show how 2-D array is stored
#include<stdio.h>
void main()
{
int s[4][2]={101,88,102,76,103,54,104,46};
printf("Base Address = %u\n",s);
printf("zeroth 1-d address = %u\n",s[0]);
printf("first 1-d address = %u\n",s[1]);
printf("second 1-d address = %u\n",s[2]);
printf("third 1-d address = %u\n",s[3]);
}
Output:
Base Address = 6684208
zeroth 1-d address = 6684208
first 1-d address = 6684216
second 1-d address = 6684224
third 1-d address = 6684232
Matrix 1:
1 2 3
4 5 6
7 8 9
Matrix 2:
1 2 3
4 5 6
7 8 9
sum matrix:
2 4 6
8 10 12
14 16 18
#include<stdio.h>
#include<stdlib.h>//for exit() function
void main()
{
int mat1[5][5], mat2[5][5], mat3[5][5], i, j, k, m, n, p, q;
printf("Enter first matrix dimensions: ");
scanf("%d %d",&m, &n);
printf("Enter second matrix dimensions: ");
scanf("%d %d", &p, &q);
if(n!=p)
{
printf("Enter valid dimensions\n");
exit(0);
}
printf("Enter first matrix (%d*%d) : ",m,n);
for(i=0; i<m; i++)
for(j=0; j<n; j++)
scanf("%d",&mat1[i][j]);
Matrix 1:
1 1 1
1 1 1
1 1 1
Matrix 2:
2 2 2
2 2 2
2 2 2
Multiplication matrix:
6 6 6
6 6 6
6 6 6
#include<stdio.h>
void main()
{
int mat1[3][3], mat2[3][3], i, j;
printf("Enter first matrix element (3*3) : ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("\nMatrix 1:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%2d ",mat1[i][j]);
}
printf("\n");
}
//Transpose of matrix 1
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
mat2[i][j]=mat1[j][i];
}
}
//print transpose matrix
printf("\nTranspose Matrix:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%2d ",mat2[i][j]);
}
printf("\n");
}
}
Matrix 1:
1 2 3
1 2 3
1 2 3
Transpose Matrix:
1 1 1
2 2 2
3 3 3
Matrix 1:
1 2
3 4
5 6
Transpose Matrix:
1 3 5
2 4 6
118. program for Trace of matrices
#include<stdio.h>
void main()
{
int mat1[3][3], i, j, sum;
sum=0;
printf("Enter first matrix element (3*3) : ");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
scanf("%d",&mat1[i][j]);
}
}
printf("\nMatrix 1:\n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%2d ",mat1[i][j]);
}
printf("\n");
}
//Trace of matrix 1
for(i=0; i<3; i++)
{
sum=sum+mat1[i][i];
}
//print trace matrix
printf("\nTrace of Matrix1 = %d\n", sum);
}
Output:
Enter first matrix element (3*3) : 1 2 3 4 5 6 7 8 9
Matrix 1:
1 2 3
4 5 6
7 8 9
Trace of Matrix1 = 15
#include <stdio.h>
#include <math.h>
void main ()
{
int array[10][10];
int i,j,m,n,sum,normal;
sum=0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the elements of the matrix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
printf("\nMatrix :\n");
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
printf("%2d ",array[i][j]);
sum=sum+ array[i][j] * array[i][j]; // calculate sum of square of elementsl
}
printf("\n");
}
normal = sqrt(sum);
printf("The normal of the matrix is = %d\n", normal);
}
Output:
Enter the order of the matrix
33
Enter the elements of the matrix
123456789
Matrix :
1 2 3
4 5 6
7 8 9
The normal of the matrix is = 16
HELLO
Enter your name: krishna
Your name is krishna.
122.A program to show how gets() and puts() functions are used
#include<stdio.h>
void main()
{
char name[20];
printf("Enter your name: ");
gets(name);
puts("Hello!");
puts(name);
}
Output:
Enter your name: venkata krishna rao
Hello!
venkata krishna rao
int main() {
char str[100];
int i, count = 0;
return 0;
}
Output:
Enter a string: Hello World
Number of vowels in the string: 3
int main() {
char str[100];
int i;
int a = 0, e = 0, i_count = 0, o = 0, u = 0;
printf("\nVowel frequencies:\n");
printf("A or a: %d\n", a);
printf("E or e: %d\n", e);
printf("I or i: %d\n", i_count);
printf("O or o: %d\n", o);
printf("U or u: %d\n", u);
return 0;
}
Output:
Enter a string: hello world this is great
Vowel frequencies:
A or a: 1
E or e: 2
I or i: 2
O or o: 2
U or u: 0
Output:
6 names:
ram
sham
priya
ramya
raju
ravi
Enter some name:
krishna
ramesh
rajesh
3 names
krishna
ramesh
rajesh
126. Count the Number of Vowels, Consonants, and Digits
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int vowels = 0, consonants = 0, digits = 0, i;
#include <stdio.h>
#include <string.h>
int main() {
char str[100], rev[100];
int i, len;
len = strlen(str);
for (i = 0; i <len; i++)
rev[i] = str[len - i - 1];
rev[len] = '\0';
#include<stdio.h>
#include<string.h>
int main() {
char str[100];
int i, len, flag = 0;
printf("Enter a string: ");
gets(str);
len = strlen(str);
for (i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
flag = 1;
break;
}
}
if (flag == 0)
printf("The string is a palindrome.\n");
else
printf("The string is not a palindrome.\n");
return 0;
}
Output:
Enter a string: abcdcba
The string is a palindrome.
#include <stdio.h>
int main() {
char str[100];
int i;
printf("Enter a string: ");
gets(str);
for (i = 0; str[i] != '\0'; i++) {
// If character is lowercase (a–z)
if (str[i] >= 'a' && str[i] <= 'z') {
str[i] = str[i] - 32; // Convert to uppercase (ASCII difference = 32)
}
// If character is uppercase (A–Z)
else if (str[i] >= 'A' && str[i] <= 'Z') {
str[i] = str[i] + 32; // Convert to lowercase
}
}
printf("Converted string: %s", str);
return 0;
}
OUTPUT:
Enter a string: abcABC
Converted string: ABCabc
#include <stdio.h>
void main() {
int arr[2][2][2] = { { {1, 2}, {3, 4} }, { {5, 6}, {7, 8} } };
for (int i = 0; i < 2; i++) {
printf("block %d\n",i+1);
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
printf("arr[%d][%d][%d] = %d\t", i, j, k, arr[i][j][k]);
} printf("\n");
} printf("\n\n\n");
}
}
block 1
arr[0][0][0] = 1 arr[0][0][1] = 2
arr[0][1][0] = 3 arr[0][1][1] = 4
block 2
arr[1][0][0] = 5 arr[1][0][1] = 6
arr[1][1][0] = 7 arr[1][1][1] = 8
#include <stdio.h>
int main() {
int num = 123;
float pi = 3.1415926;
char str[] = "Hello";
// 2. Precision specifier
printf("2. Precision specifier:\n");
printf("Float default: |%f|\n", pi);
printf("Float with 2 decimals: |%.2f|\n", pi);
printf("String with precision 3: |%.3s|\n", str); // only first 3 chars printed
printf("\n");
// 3. Left Justification
printf("3. Left Justification:\n");
printf("Right justified (default): |%10d|\n", num);
printf("Left justified: |%-10d|\n", num);
printf("Left justified string: |%-10s|\n", str);
return 0;
}
Output:
1. Field width specification:
Without width: |123|
With width 10: | 123|
2. Precision specifier:
Float default: |3.141593|
Float with 2 decimals: |3.14|
String with precision 3: |Hel|
3. Left Justification:
Right justified (default): | 123|
Left justified: |123 |
Left justified string: |Hello |
[Link] for strcmp,strcpy,strcat
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
char str3[50];
int n = 3;
printf("Initial Strings:\n");
printf("str1 = %s\n", str1);
printf("str2 = %s\n\n", str2);
// strcat(str1, str2)
strcat(str1, str2);
printf("After strcat(str1, str2): %s\n", str1);
// Reset str1
strcpy(str1, "Hello");
// strncat(str1, str2, n)
strncat(str1, str2, n);
printf("After strncat(str1, str2, %d): %s\n", n, str1);
// strcpy(str3, str2)
strcpy(str3, str2);
printf("After strcpy(str3, str2): %s\n", str3);
// strncpy(str3, str2, n)
strncpy(str3, "GoodMorning", n);
str3[n] = '\0'; // Add null terminator manually
printf("After strncpy(str3, \"GoodMorning\", %d): %s\n", n, str3);
printf("str 1 - %s \t str 2 - %s \t str 3 - %s\n",str1,str2,str3);
// strlen(str1)
printf("Length of str1 = %lu\n", strlen(str1));
// strcmp(str1, str2)
if (strcmp(str1, str2) == 0)
printf("strcmp(str1, str2): Strings are equal\n");
else
printf("strcmp(str1, str2): Strings are not equal\n");
// strncmp(str1, str2, n)
if (strncmp(str1, str2, n) == 0)
printf("strncmp(str1, str2, %d): First %d characters are equal\n", n, n);
else
printf("strncmp(str1, str2, %d): First %d characters are not equal\n", n, n);
if (strcmpi(str1, str2) == 0)
printf("strcmpi(str1, str2): Strings are equal (case-insensitive)\n");
else
printf("strcmpi(str1, str2): Strings are not equal (case-insensitive)\n");
if (strcasecmp(str1, str2) == 0)
printf("strcasecmp(str1, str2): Strings are equal (case-insensitive)\n");
else
printf("strcasecmp(str1, str2): Strings are not equal (case-insensitive)\n");
return 0;
}
Output:
Initial Strings:
str1 = Hello
str2 = World
#include <stdio.h>
#include <string.h>
#include <stdlib.h> // for strdup()
int main() {
char str1[100] = "Hello World, Welcome to C Programming world";
char str2[50] = "World";
char ch = 'o';
char *ptr;
// strchr(str1, ch)
ptr = strchr(str1, ch);
if (ptr != NULL)
printf("strchr(str1, '%c') = %s\n", ch, ptr);
else
printf("Character '%c' not found using strchr.\n", ch);
// strrchr(str1, ch)
ptr = strrchr(str1, ch);
if (ptr != NULL)
printf("strrchr(str1, '%c') = %s\n", ch, ptr);
else
printf("Character '%c' not found using strrchr.\n", ch);
// strstr(str1, str2)
ptr = strstr(str1, str2);
if (ptr != NULL)
printf("strstr(str1, \"%s\") = %s\n", str2, ptr);
else
printf("Substring \"%s\" not found using strstr.\n", str2);
// strdup(str1) – duplicates a string (returns a new copy in heap)
char *copy = strdup(str1);
if (copy != NULL)
printf("strdup(str1) = %s\n", copy);
// strlwr(str1) – converts to lowercase (non-standard, works in Turbo C / Windows)
strlwr(str1);
printf("After strlwr(str1): %s\n", str1);
return 0;
}
Output:
Original String: Hello World, Welcome to C Programming world
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char str1[100] = "HelloWorld";
char str2[50] = "Welcome";
char temp[100];
char delim[] = " ,.-";
char *token;
int n = 5;
strrev(temp);
printf("After strrev(str1): %s\n", temp);
strset(temp, '*');
printf("After strset(str1, '*'): %s\n", temp);
// strnset(str1, ch, n) – sets first n characters to ch (non-standard)
strcpy(temp, str1);
str1 = abcdef123
str2 = abcxyz
strspn(str1, str2) = 3
#include<stdio.h>
int main( )
{
char line[150];
int i, v=0, c=0, ch=0, d=0, s=0, o=0;
printf("Enter a line of string:\n");
gets(line);
for(i=0; line[i]!='\0';++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
++v;
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
++c;
else if(line[i]>='0'&&c<='9')
++d;
else if (line[i]==' ')
++s;
}
printf("Vowels: %d", v);
printf("\nConsonants: %d", c);
printf("\nDigits: %d", d);
printf("\nWhite spaces: %d", s);
return 0;
}
Output:
Enter a line of string:
n v krishna rao
Vowels: 4
Consonants: 8
Digits: 0
White spaces: 3
int main()
{
int add = sum(10, 30); // function call
printf("Sum is : %d", add);
return 0;
}
return 0;
}
/* function definition to swap the values */
void swap(int x, int y)
{
int temp; /* local variable definition */
printf("in subfunction before swap, value of a : %d b : %d\n", x,y );
#include <stdio.h>
/* function declaration */
void swap(int *x, int *y);
void main( )
{
/* local variable definition */
int a = 100,b = 200;
printf("in main function Before swap, value of a : %d b : %d\n", a,b );
/* calling a function to swap the values.
* &a indicates pointer to a i.e. address of variable a and
* &b indicates pointer to b i.e. address of variable b.
*/
swap(&a, &b);
printf("in main function After swap, value of a : %d b : %d\n", a,b );
}
/* function definition to swap the values */
void swap (int *x, int *y)
{
int temp;/* local variable definition */
printf("in subfunction before swap, value of a : %d b : %d\n", *x,*y );
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put temp into y */
printf("in subfunction After swap, value of a : %d b : %d\n", *x,*y );
#define MAX 10
// Function prototypes
void inputMatrix(int matrix[MAX][MAX], int rows, int cols);
void displayMatrix(int matrix[MAX][MAX], int rows, int cols);
void addMatrices(int a[MAX][MAX], int b[MAX][MAX], int r, int c);
void multiplyMatrices(int a[MAX][MAX], int b[MAX][MAX], int r1, int c1, int r2, int c2);
void transposeMatrix(int a[MAX][MAX], int r, int c);
void traceMatrix(int a[MAX][MAX], int n);
int main() {
int choice;
int a[MAX][MAX], b[MAX][MAX];
int r1, c1, r2, c2, n;
switch (choice) {
case 1:
printf("\nEnter rows and columns of matrices: ");
scanf("%d %d", &r1, &c1);
printf("Enter elements of Matrix A:\n");
inputMatrix(a, r1, c1);
printf("Enter elements of Matrix B:\n");
inputMatrix(b, r1, c1);
addMatrices(a, b, r1, c1);
break;
case 2:
printf("\nEnter rows and columns of Matrix A: ");
scanf("%d %d", &r1, &c1);
printf("Enter elements of Matrix A:\n");
inputMatrix(a, r1, c1);
if (c1 != r2) {
printf("Matrix multiplication not possible!\n");
break;
}
printf("Enter elements of Matrix B:\n");
inputMatrix(b, r2, c2);
multiplyMatrices(a, b, r1, c1, r2, c2);
break;
case 3:
printf("\nEnter rows and columns of Matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter elements of Matrix:\n");
inputMatrix(a, r1, c1);
transposeMatrix(a, r1, c1);
break;
case 4:
printf("\nEnter order of square matrix: ");
scanf("%d", &n);
printf("Enter elements of Matrix:\n");
inputMatrix(a, n, n);
traceMatrix(a, n);
break;
default:
printf("Invalid choice!\n");
}
return 0;
}
#include <stdio.h>
// Function declaration
void greet();
int main() {
greet(); // Function call
return 0;
}
// Function definition
void greet() {
printf("Hello! Welcome to C programming.\n");
}
// Function declaration
void displaySum(int a, int b);
int main() {
int x = 5, y = 10;
displaySum(x, y); // Function call with arguments
return 0;
}
// Function definition
void displaySum(int a, int b) {
int sum = a + b;
printf("Sum = %d\n", sum);
}
#include <stdio.h>
// Function declaration
int getNumber();
int main() {
int num = getNumber(); // Function call and storing return value
printf("You entered: %d\n", num);
return 0;
}
// Function definition
int getNumber() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
return n;
}
#include <stdio.h>
// Function declaration
int multiply(int a, int b);
int main() {
int x = 4, y = 6;
int result = multiply(x, y); // Function call
printf("Product = %d\n", result);
return 0;
}
// Function definition
int multiply(int a, int b) {
return a * b;
}
#include <stdio.h>
int main() {
int a = 5;
int b = 10;
return 0;
}
#include <stdio.h>
void findAge() {
// the age variable is not accessible outside the function findAge()
// as it is having local scope to the function i.e. function scope
int age = 18;
printf("Age is %d", age);
}
int main() {
#include <stdio.h>
int main() {
int sum = findSum(3, 5);
printf("Sum of 3 and 5 is %d", sum);
return 0;
}
#include <stdio.h>
// global variables having file scope
int A = 10;
double B = 9.8;
{
// all variables declared inside have block scope
// int A, int C, int sum, double B and double Area
// variables have block scope
double Area = PI * radius * radius;
int A = 99;
double B = 10.97;
int C = 101;
int sum = sumOfTwoNumbers(A, C);
printf("Block Scope Variables:\nA : %d, B : %lf, C : %d, sum : %d, Area : %lf\n", A, B, C, sum,
Area);
}
// we can't use C and sum variables here
// (outside the block scope)
return 0;
}
#include<stdio.h>
int main()
{
add();
add();
add();add();
}
void add()
{
static int x=0;
x++;
printf("%d ",x);
}
[Link] STORAGE(write in Record)
#include<stdio.h>
int main()
{
register int x=2;
printf("%d",x);
return 0;
}
/*C program to search the given key value in array elements using Linear searching
technique. */
/*************************************************************************/
#include<stdio.h>
int main()
{
int a[100], n, i, j, flag=0, key;
printf("Enter Number of elements in array: \n");
scanf("%d", &n);
printf("Enter %d Array Elements:\n", n);
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("Enter Key value to search\n");
scanf("%d", &key);
for(i=0;i<n;i++)
{
if(key==a[i])
{
flag=1;
break;
}
}
if(flag==1)
printf("%d found at position %d\n",key,i+1);
else
printf("Sorry! %d not found in given elements\n",key);
return 0;
}
Output:
Enter Number of elements in array:
5
Enter 5 Array Elements:
25137
Enter Key value to search
1
1 found at position 3
#include<stdio.h>
int main() {
int arr[50], n, key, pos;
if(pos != -1)
printf("Element found at index %d\n", pos);
else
printf("Element not found\n");
return 0;
}
Output:
[Link] Search with recursive functions (write in observation)
#include <stdio.h>
if(arr[index] == key)
return index;
int main() {
int arr[50], n, key, pos;
if(pos != -1)
printf("Element found at index %d\n", pos);
else
printf("Element not found\n");
return 0;
}
Output:
Algorithm:
/*C program to search the given key value in array elements using Binary searching
technique.
***********************************************************************/
#include<stdio.h>
int main()
{
int a[100], n, i, high, low, mid, key;
printf("Enter Number of elements in array: \n");
scanf("%d", &n);
printf("Enter %d Array Elements in sorted order:\n", n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter Key value to search\n");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high){
mid=(low+high)/2;
if(key==a[mid])
{
printf("%d found at position %d\n",key,mid+1);
break;
}
else if(key>a[mid])
low=mid+1;
else
high=mid-1;
}
if(low>high)
printf ("Sorry! %d not found in given elements\n", key);
return 0;
}
Output:
if(arr[mid] == key)
return mid;
else if(arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main() {
int arr[50], n, key, pos;
if(pos != -1)
printf("Element found at index %d\n", pos);
else
printf("Element not found\n");
return 0;
}
Output:
int main() {
int arr[50], n, key, pos;
if(pos != -1)
printf("Element found at index %d\n", pos);
else
printf("Element not found\n");
return 0;
}
Output:
/*C program to sort the given array elements using bubble sort */
/**************************************************************/
#include<stdio.h>
int main( )
{
int a[100], n, i, j, temp;
printf("Enter Number of elements in array: \n");
scanf("%d", &n);
printf("Enter %d Array Elements:\n",n);
for(i=0;i<n; i++)
scanf("%d", &a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("Sorted order of given elements is:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n");
return 0;
}
Output:
int main() {
int arr[50], n;
return 0;
}
Output:
bubbleSortRec(arr, n - 1);
}
int main() {
int arr[50], n;
bubbleSortRec(arr, n);
return 0;
}
Output:
[Link] sort (write in Record)
Algorithm:
The algorithm consists of two main loops and the swap operation.
Step 1: Read the elements
Step 2: Outer Loop (Passes)
Iterate from the first element (index i=0) up to the second to last element (index i <
n-1) of the array, where n is the total number of elements.
This loop divides the array into two parts: a sorted subarray (to the left of i) and an
unsorted subarray (from i to the end).
In each pass i, we find the smallest element in the unsorted subarray A[i..n-1].
Step3: Inner Loop (Finding the Minimum)
Inside the outer loop, initialize a variable, say min_index, to the starting index of the
unsorted part, i. This assumes the current element A[i] is the minimum.
Iterate from the next element (j = i+1) up to the last element (j < n) of the array.
Compare the element A[j] with the element at the current minimum index
A[min_index].
If A[j] is smaller than A[min_index], update min_index to j.
After the inner loop finishes, min_index will hold the index of the smallest element
in the unsorted subarray A[i..n-1].
Step 4: Swap
Swap the element at the current position A[i] with the smallest element found at
A[min_index].
This effectively moves the smallest element from the unsorted part to its correct
position in the sorted part.
Step 5: print the sorted elements
#include <stdio.h>
int main() {
int arr[50], n;
selectionSort(arr, n);
return 0;
}
Output:
#include <stdio.h>
int main() {
int arr[50], n;
selectionSortRec(arr, 0, n);
return 0;
}
Output:
#include <stdio.h>
int main() {
int a[100], n, i;
return 0;
}
Output: