C Programs
C Programs
1. #include <stdio.h>
2.
3. int main()
4. {
5. char ch;
6.
7. printf("Enter a character\n");
8. scanf("%c", &ch);
9.
10. // Checking both lower and upper case
11.
12. if (ch == 'a' || ch == 'A' || ch == 'e' || ch ==
'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' ||
ch == 'u' || ch == 'U')
13. printf("%c is a vowel.\n", ch);
14. else
15. printf("%c isn't a vowel.\n", ch);
16.
17. return 0;
18. }
C programming code
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, t, sum = 0, remainder;
6.
7. printf("Enter an integer\n");
8. scanf("%d", &n);
9.
10. t = n;
11.
12. while (t != 0)
13. {
14. remainder = t % 10;
15. sum = sum + remainder;
16. t = t / 10;
17. }
18.
19. printf("Sum of digits of %d = %d\n", n, sum);
20.
21. return 0;
22. }
C programming code
1. #include <stdio.h>
2.
3. int main() {
4. int a, b, x, y, t, gcd, lcm;
5.
6. printf("Enter two integers\n");
7. scanf("%d%d", &x, &y);
8.
9. a = x;
10. b = y;
11.
12. while (b != 0) {
13. t = b;
14. b = a % b;
15. a = t;
16. }
17.
18. gcd = a;
19. lcm = (x*y)/gcd;
20.
21. printf("Greatest common divisor of %d and %d =
%d\n", x, y, gcd);
22. printf("Least common multiple of %d and %d =
%d\n", x, y, lcm);
23.
24. return 0;
25. }
This code only prints binary of an integer, but we may wish to perform
operations on binary, so in the program below we are storing the binary in a
string. We create a function which returns a pointer to string which is the
binary of the number passed as an argument to the function.
Output of program:
Pascal triangle in C
1. #include <stdio.h>
2.
3. long factorial(int);
4.
5. int main()
6. {
7. int i, n, c;
8.
9. printf("Enter the number of rows you wish to see in
pascal triangle\n");
10. scanf("%d",&n);
11.
12. for (i = 0; i < n; i++)
13. {
14. for (c = 0; c <= (n - i - 2); c++)
15. printf(" ");
16.
17. for (c = 0 ; c <= i; c++)
18. printf("%ld
",factorial(i)/(factorial(c)*factorial(i-c)));
19.
20. printf("\n");
21. }
22.
23. return 0;
24. }
25.
26. long factorial(int n)
27. {
28. int c;
29. long result = 1;
30.
31. for (c = 1; c <= n; c++)
32. result = result*c;
33.
34. return result;
35. }
C programming code
1. #include <stdio.h>
2.
3. int main()
4. {
5. int n, i, c, a = 1;
6.
7. printf("Enter the number of rows of Floyd's triangle
to print\n");
8. scanf("%d", &n);
9.
10. for (i = 1; i <= n; i++)
11. {
12. for (c = 1; c <= i; c++)
13. {
14. printf("%d ",a);
15. a++;
16. }
17. printf("\n");
18. }
19.
20. return 0;
21. }
Output of program:
C programming code
1. #include <stdio.h>
2.
3. int main()
4. {
5. int first, second, *p, *q, sum;
6.
7. printf("Enter two integers to add\n");
8. scanf("%d%d", &first, &second);
9.
10. p = &first;
11. q = &second;
12.
13. sum = *p + *q;
14.
15. printf("Sum of the numbers = %d\n", sum);
16.
17. return 0;
18. }
Output of program:
C program to add numbers using call by reference
1. #include <stdio.h>
2.
3. long add(long *, long *);
4.
5. int main()
6. {
7. long first, second, *p, *q, sum;
8.
9. printf("Input two integers to add\n");
10. scanf("%ld%ld", &first, &second);
11.
12. sum = add(&first, &second);
13.
14. printf("(%ld) + (%ld) =
(%ld)\n", first, second, sum);
15.
16. return 0;
17. }
18.
19. long add(long *x, long *y) {
20. long sum;
21.
22. sum = *x + *y;
23.
24. return sum;
25. }
Linear search in C
Linear search in C programming: The following code implements linear
search (Searching algorithm) which is used to find whether a given number
is present in an array and if it is present then at what location it occurs. It is
also known as sequential search. It is straightforward and works as follows:
We keep on comparing each element with the element to search until it is
found or the list ends. Linear search in C language for multiple
occurrences and using function.
1. #include <stdio.h>
2.
3. int main()
4. {
5. int array[100], search, c, n, count = 0;
6.
7. printf("Enter number of elements in array\n");
8. scanf("%d", &n);
9.
10. printf("Enter %d numbers\n", n);
11.
12. for (c = 0; c < n; c++)
13. scanf("%d", &array[c]);
14.
15. printf("Enter a number to search\n");
16. scanf("%d", &search);
17.
18. for (c = 0; c < n; c++) {
19. if (array[c] == search) {
20. printf("%d is present at location
%d.\n", search, c+1);
21. count++;
22. }
23. }
24. if (count == 0)
25. printf("%d isn't present in the
array.\n", search);
26. else
27. printf("%d is present %d times in the
array.\n", search, count);
28.
29. return 0;
30. }
Output of code:
Binary search is faster than linear search, but the list should be sorted,
hashing is more rapid than binary search and perform searches in constant
time.
C programming code
1. #include <stdio.h>
2.
3. int main()
4. {
5. int array[100], position, c, n, value;
6.
7. printf("Enter number of elements in array\n");
8. scanf("%d", &n);
9.
10. printf("Enter %d elements\n", n);
11.
12. for (c = 0; c < n; c++)
13. scanf("%d", &array[c]);
14.
15. printf("Enter the location where you wish to
insert an element\n");
16. scanf("%d", &position);
17.
18. printf("Enter the value to insert\n");
19. scanf("%d", &value);
20.
21. for (c = n - 1; c >= position - 1; c--)
22. array[c+1] = array[c];
23.
24. array[position-1] = value;
25.
26. printf("Resultant array is\n");
27.
28. for (c = 0; c <= n; c++)
29. printf("%d\n", array[c]);
30.
31. return 0;
32. }
Output of program:
Output of program:
Best case complexity of insertion sort is O(n), average and the worst case
complexity is O(n2).
Selection sort in C
Selection sort in C: C program for selection sort to sort numbers. This code
implements selection sort algorithm to arrange numbers of an array in
ascending order. With a little modification, it will arrange numbers in
descending order.
Matrix addition in C
Matrix addition in C: C program to add two matrices, i.e., compute the sum
of two matrices and then print it. Firstly a user will be asked to enter the
order of matrix (number of rows and columns) and then two matrices. For
example, if a user input order as 2, 2, i.e., two rows and two columns and
matrices as
First matrix:
12
34
Second matrix:
45
-1 5
then the output of the program (Summation of the two matrices) is:
57
29
Matrices are frequently used in programming to represent graph data
structure, in solving equations and in many other ways.
1. #include <stdio.h>
2.
3. int main()
4. {
5. int m, n, c, d, first[10][10], second[10][10], sum[1
0][10];
6.
7. printf("Enter the number of rows and columns of
matrix\n");
8. scanf("%d%d", &m, &n);
9. printf("Enter the elements of first matrix\n");
10.
11. for (c = 0; c < m; c++)
12. for (d = 0; d < n; d++)
13. scanf("%d", &first[c][d]);
14.
15. printf("Enter the elements of second matrix\n");
16.
17. for (c = 0; c < m; c++)
18. for (d = 0 ; d < n; d++)
19. scanf("%d", &second[c][d]);
20.
21. printf("Sum of entered matrices:-\n");
22.
23. for (c = 0; c < m; c++) {
24. for (d = 0 ; d < n; d++) {
25. sum[c][d] = first[c][d] + second[c][d];
26. printf("%d\t", sum[c][d]);
27. }
28. printf("\n");
29. }
30.
31. return 0;
32. }
Output of matrix addition C program:
Similarly, we can create a program to subtract two matrices. You can also
create a function to perform addition of two matrices.
Transpose of a matrix in C
Transpose of a matrix in C language: This C program prints transpose of a
matrix. It is obtained by interchanging rows and columns of a matrix. For
example, consider the following 3 X 2 matrix:
12
34
56
Transpose of the matrix:
135
246
When we transpose a matrix then its order changes, but for a square matrix
it remains the same.
Output of program:
Output of program:
1. #include <stdio.h>
2.
3. int main()
4. {
5. char s[1000];
6. int c = 0;
7.
8. printf("Input a string\n");
9. gets(s);
10.
11. while (s[c] != '\0')
12. c++;
13.
14. printf("Length of the string: %d\n", c);
15.
16. return 0;
17. }
1. #include <stdio.h>
2.
3. int string_length(char*);
4.
5. int main()
6. {
7. char s[100];
8.
9. gets(s);
10.
11. printf("Length = %d\n", string_length(s));
12.
13. return 0;
14. }
15.
16. int string_length(char *s) {
17. static int c = 0;
18.
19. while (s[c] != '\0') {
20. c++;
21. string_length(s+1);
22. }
23.
24. return c;
25. }
1. #include <stdio.h>
2.
3. int compare_strings(char [], char []);
4.
5. int main()
6. {
7. char a[1000], b[1000];
8.
9. printf("Input a string\n");
10. gets(a);
11.
12. printf("Input a string\n");
13. gets(b);
14.
15. if (compare_strings(a, b) == 0)
16. printf("Equal strings.\n");
17. else
18. printf("Unequal strings.\n");
19.
20. return 0;
21. }
22.
23. int compare_strings(char a[], char b[])
24. {
25. int c = 0;
26.
27. while (a[c] == b[c]) {
28. if (a[c] == '\0' || b[c] == '\0')
29. break;
30. c++;
31. }
32.
33. if (a[c] == '\0' && b[c] == '\0')
34. return 0;
35. else
36. return -1;
37. }
1. #include<stdio.h>
2.
3. int compare_string(char*, char*);
4.
5. int main()
6. {
7. char first[1000], second[1000]:
8. int result;
9.
10. printf("Input a string\n");
11. gets(first);
12.
13. printf("Input a string\n");
14. gets(second);
15.
16. result = compare_string(first, second);
17.
18. if (result == 0)
19. printf("The strings are same.\n");
20. else
21. printf("The strings are different.\n");
22.
23. return 0;
24. }
25.
26. int compare_string(char *first, char *second) {
27. while (*first == *second) {
28. if (*first == '\0' || *second == '\0')
29. break;
30.
31. first++;
32. second++;
33. }
34.
35. if (*first == '\0' && *second == '\0')
36. return 0;
37. else
38. return -1;
39. }
1. #include <stdio.h>
2.
3. int main()
4. {
5. char s[1000], r[1000];
6. int begin, end, count = 0;
7.
8. printf("Input a string\n");
9. gets(s);
10.
11. // Calculating string length
12.
13. while (s[count] != '\0')
14. count++;
15.
16. end = count - 1;
17.
18. for (begin = 0; begin < count; begin++) {
19. r[begin] = s[end];
20. end--;
21. }
22.
23. r[begin] = '\0';
24.
25. printf("%s\n", r);
26.
27. return 0;
28. }
In the recursive method, we swap characters at the beginning and the end
of the string and then move towards the middle of the string. This way is
inefficient due to repeated function calls but useful in practicing recursion.
1. #include<stdio.h>
2.
3. int string_length(char*);
4. void reverse(char*);
5.
6. main()
7. {
8. char s[100];
9.
10. printf("Enter a string\n");
11. gets(s);
12.
13. reverse(s);
14.
15. printf("Reverse of the string is \"%s\".\n", s);
16.
17. return 0;
18. }
19.
20. void reverse(char *s)
21. {
22. int length, c;
23. char *begin, *end, temp;
24.
25. length = string_length(s);
26. begin = s;
27. end = s;
28.
29. for (c = 0; c < length - 1; c++)
30. end++;
31.
32. for (c = 0; c < length/2; c++)
33. {
34. temp = *end;
35. *end = *begin;
36. *begin = temp;
37.
38. begin++;
39. end--;
40. }
41. }
42.
43. int string_length(char *pointer)
44. {
45. int c = 0;
46.
47. while( *(pointer + c) != '\0' )
48. c++;
49.
50. return c;
51. }
C substring, substring in C
C substring: C program to find substring of a string and all substrings of a
string. A substring is itself a string that is part of a longer string. For
example, substrings of string "the" are "" (empty string), "t", "th", "the", "h",
"he" and "e". Header file "string.h" does not contain any library function to
find a substring directly.
C substring program
1. #include <stdio.h>
2.
3. int main()
4. {
5. char string[1000], sub[1000];
6. int position, length, c = 0;
7.
8. printf("Input a string\n");
9. gets(string);
10.
11. printf("Enter the position and length of
substring\n");
12. scanf("%d%d", &position, &length);
13.
14. while (c < length) {
15. sub[c] = string[position+c-1];
16. c++;
17. }
18. sub[c] = '\0';
19.
20. printf("Required substring is \"%s\"\n", sub);
21.
22. return 0;
23. }
1. #include <stdio.h>
2.
3. void substring(char [], char[], int, int);
4.
5. int main()
6. {
7. char string[1000], sub[1000];
8. int position, length, c = 0;
9.
10. printf("Input a string\n");
11. gets(string);
12.
13. printf("Enter the position and length of
substring\n");
14. scanf("%d%d", &position, &length);
15.
16. substring(string, sub, position, length);
17.
18. printf("Required substring is \"%s\"\n", sub);
19.
20. return 0;
21. }
22. //C substring function definition
23. void substring(char s[], char sub[], int p, int l)
{
24. int c = 0;
25.
26. while (c < l) {
27. sub[c] = s[p+c-1];
28. c++;
29. }
30. sub[c] = '\0';
31. }
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. char* substring(char*, int, int);
5.
6. int main()
7. {
8. char string[100], *pointer;
9. int position, length;
10.
11. printf("Input a string\n");
12. gets(string);
13.
14. printf("Enter the position and length of
substring\n");
15. scanf("%d%d",&position, &length);
16.
17. pointer = substring( string, position, length);
18.
19. printf("Required substring
is \"%s\"\n", pointer);
20.
21. free(pointer);
22.
23. return 0;
24. }
25.
26. /*C substring function: It returns a pointer to the
substring */
27.
28. char *substring(char *string, int position, int len
gth)
29. {
30. char *pointer;
31. int c;
32.
33. pointer = malloc(length+1);
34.
35. if (pointer == NULL)
36. {
37. printf("Unable to allocate memory.\n");
38. exit(1);
39. }
40.
41. for (c = 0 ; c < length ; c++)
42. {
43. *(pointer+c) = *(string+position-1);
44. string++;
45. }
46.
47. *(pointer+c) = '\0';
48.
49. return pointer;
50. }
C programming code
1. #include <stdio.h>
2. #include <string.h>
3.
4. int check_subsequence (char [], char[]);
5.
6. int main () {
7. int flag;
8. char s1[1000], s2[1000];
9.
10. printf("Input first string\n");
11. gets(s1);
12.
13. printf("Input second string\n");
14. gets(s2);
15.
16. /** Passing smaller length string first */
17.
18. if (strlen(s1) < strlen(s2))
19. flag = check_subsequence(s1, s2);
20. else
21. flag = check_subsequence(s2, s1);
22.
23. if (flag)
24. printf("YES\n");
25. else
26. printf("NO\n");
27.
28. return 0;
29. }
30.
31. int check_subsequence (char a[], char b[]) {
32. int c, d;
33.
34. c = d = 0;
35.
36. while (a[c] != '\0') {
37. while ((a[c] != b[d]) && b[d] != '\0') {
38. d++;
39. }
40. if (b[d] == '\0')
41. break;
42. d++;
43. c++;
44. }
45. if (a[c] == '\0')
46. return 1;
47. else
48. return 0;
49. }
"C programming"
There are two spaces in this string, so our program will print the string
"C programming." It will remove spaces when they occur more than one
time consecutively in string anywhere.
C programming code
1. #include <stdio.h>
2.
3. int main()
4. {
5. char text[1000], blank[1000];
6. int c = 0, d = 0;
7.
8. printf("Enter some text\n");
9. gets(text);
10.
11. while (text[c] != '\0') {
12. if (text[c] == ' ') {
13. int temp = c + 1;
14. if (text[temp] != '\0') {
15. while (text[temp] == '
' && text[temp] != '\0') {
16. if (text[temp] == ' ') {
17. c++;
18. }
19. temp++;
20. }
21. }
22. }
23. blank[d] = text[c];
24. c++;
25. d++;
26. }
27.
28. blank[d] = '\0';
29.
30. printf("Text after removing
blanks\n%s\n", blank);
31.
32. return 0;
33. }
If you want you can copy blank into text string so that original string is
modified.
Function strlwr in C
1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char string[1000];
7.
8. printf("Input a string to convert to lower case\n");
9. gets(string);
10.
11. printf("The string in lower case:
%s\n", strlwr(string));
12.
13. return 0;
14. }
Function strupr in C
1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char string[1000];
7.
8. printf("Input a string to convert to upper case\n");
9. gets(string);
10.
11. printf("The string in upper case:
%s\n", strupr(string));
12.
13. return 0;
14. }
1. #include <stdio.h>
2.
3. int main ()
4. {
5. int c = 0;
6. char ch, s[1000];
7.
8. printf("Input a string\n");
9. gets(s);
10.
11. while (s[c] != '\0') {
12. ch = s[c];
13. if (ch >= 'A' && ch <= 'Z')
14. s[c] = s[c] + 32;
15. else if (ch >= 'a' && ch <= 'z')
16. s[c] = s[c] - 32;
17. c++;
18. }
19.
20. printf("%s\n", s);
21.
22. return 0;
23. }
1. Input a string
2. abcdefghijklmnopqrstuvwxyz{0123456789}ABCDEFGHIJKLMNOPQ
RSTUVWXYZ
3. ABCDEFGHIJKLMNOPQRSTUVWXYZ{0123456789}abcdefghijklmnopq
rstuvwxyz
C programming code
1. #include <stdio.h>
2. #include <string.h>
3.
4. int main()
5. {
6. char string[100];
7. int c = 0, count[26] = {0}, x;
8.
9. printf("Enter a string\n");
10. gets(string);
11.
12. while (string[c] != '\0') {
13. /** Considering characters from 'a' to 'z' only
and ignoring others. */
14.
15. if (string[c] >= 'a' && string[c] <= 'z') {
16. x = string[c] - 'a';
17. count[x]++;
18. }
19.
20. c++;
21. }
22.
23. for (c = 0; c < 26; c++)
24. printf("%c occurs %d times in the
string.\n", c + 'a', count[c]);
25.
26. return 0;
27. }
Did you notice that the string in the output of the program contains every
alphabet at least once?
1. #include <stdio.h>
2. #include <string.h>
3.
4. void find_frequency(char [], int []);
5.
6. int main()
7. {
8. char string[100];
9. int c, count[26] = {0};
10.
11. printf("Input a string\n");
12. gets(string);
13.
14. find_frequency(string, count);
15.
16. printf("Character Count\n");
17.
18. for (c = 0 ; c < 26 ; c++)
19. printf("%c \t %d\n", c + 'a', count[c]);
20.
21. return 0;
22. }
23.
24. void find_frequency(char s[], int count[]) {
25. int c = 0;
26.
27. while (s[c] != '\0') {
28. if (s[c] >= 'a' && s[c] <= 'z' )
29. count[s[c]-'a']++;
30. c++;
31. }
32. }
Output of the program:
Anagram program in C
Anagram program in C: C program to check whether two strings are
anagrams or not, a string is assumed to consist of lower case alphabets
only. Two words are said to be anagrams of each other if the letters of one
word can be rearranged to form the other word. So, in anagram strings, all
characters occur the same number of times. For example, "abc" and "cab"
are anagram strings, as every character 'a,' 'b,' and 'c' occur the same
number of times (one time here) in both the strings. A user will input two
strings, and our algorithm counts how many times each character ('a' to 'z')
appear in both the strings and then compare their corresponding counts.
The frequency of an alphabet in a string is how many times that alphabet
appears in the string. For example, the frequency of 'm' in the string
"programming" is '2' as it is present two times in "programming."
C anagram program
1. #include <stdio.h>
2.
3. int check_anagram(char [], char []);
4.
5. int main()
6. {
7. char a[100], b[100];
8.
9. printf("Enter a string\n");
10. gets(a);
11.
12. printf("Enter a string\n");
13. gets(b);
14.
15. if (check_anagram(a, b) == 1)
16. printf("The strings are anagrams.\n");
17. else
18. printf("The strings aren't anagrams.\n");
19.
20. return 0;
21. }
22.
23. int check_anagram(char a[], char b[])
24. {
25. int first[26] = {0}, second[26] = {0}, c=0;
26.
27. // Calculating frequency of characters of first
string
28.
29. while (a[c] != '\0')
30. {
31. first[a[c]-'a']++;
32. c++;
33. }
34.
35. c = 0;
36.
37. while (b[c] != '\0')
38. {
39. second[b[c]-'a']++;
40. c++;
41. }
42.
43. // Comparing frequency of characters
44.
45. for (c = 0; c < 26; c++)
46. {
47. if (first[c] != second[c])
48. return 0;
49. }
50.
51. return 1;
52. }
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. int main()
5. {
6. char ch, file_name[25];
7. FILE *fp;
8.
9. printf("Enter name of a file you wish to see\n");
10. gets(file_name);
11.
12. fp = fopen(file_name, "r"); // read mode
13.
14. if (fp == NULL)
15. {
16. perror("Error while opening the file.\n");
17. exit(EXIT_FAILURE);
18. }
19.
20. printf("The contents of %s file
are:\n", file_name);
21.
22. while((ch = fgetc(fp)) != EOF)
23. printf("%c", ch);
24.
25. fclose(fp);
26. return 0;
27. }
There are blank lines present at the end of the file. In our program we have
opened only one file, you can open multiple files in a single program and in
different modes as required. File handling is essential when we wish to
store data permanently on a storage device. All variables and data of a
program are lost when it exits if that data is required later we need to store
it in a file.
C programming code
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. int main()
5. {
6. char ch, source_file[20], target_file[20];
7. FILE *source, *target;
8.
9. printf("Enter name of file to copy\n");
10. gets(source_file);
11.
12. source = fopen(source_file, "r");
13.
14. if (source == NULL)
15. {
16. printf("Press any key to exit...\n");
17. exit(EXIT_FAILURE);
18. }
19.
20. printf("Enter name of target file\n");
21. gets(target_file);
22.
23. target = fopen(target_file, "w");
24.
25. if (target == NULL)
26. {
27. fclose(source);
28. printf("Press any key to exit...\n");
29. exit(EXIT_FAILURE);
30. }
31.
32. while ((ch = fgetc(source)) != EOF)
33. fputc(ch, target);
34.
35. printf("File copied successfully.\n");
36.
37. fclose(source);
38. fclose(target);
39.
40. return 0;
41. }
Output of program:
C programming code
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. int main()
5. {
6. FILE *fs1, *fs2, *ft;
7.
8. char ch, file1[20], file2[20], file3[20];
9.
10. printf("Enter name of first file\n");
11. gets(file1);
12.
13. printf("Enter name of second file\n");
14. gets(file2);
15.
16. printf("Enter name of file which will store
contents of the two files\n");
17. gets(file3);
18.
19. fs1 = fopen(file1, "r");
20. fs2 = fopen(file2, "r");
21.
22. if(fs1 == NULL || fs2 == NULL)
23. {
24. perror("Error ");
25. printf("Press any key to exit...\n");
26. exit(EXIT_FAILURE);
27. }
28.
29. ft = fopen(file3, "w"); // Opening in write mode
30.
31. if(ft == NULL)
32. {
33. perror("Error ");
34. printf("Press any key to exit...\n");
35. exit(EXIT_FAILURE);
36. }
37.
38. while((ch = fgetc(fs1)) != EOF)
39. fputc(ch,ft);
40.
41. while((ch = fgetc(fs2)) != EOF)
42. fputc(ch,ft);
43.
44. printf("The two files were merged into %s file
successfully.\n", file3);
45.
46. fclose(fs1);
47. fclose(fs2);
48. fclose(ft);
49.
50. return 0;
51. }
Output of program:
Apparently, you will get a different output when you execute the program
on your computer.
C programming code
1. #include <stdio.h>
2.
3. int main()
4. {
5. int status;
6. char file_name[25];
7.
8. printf("Enter name of a file you wish to delete\n");
9. gets(file_name);
10.
11. status = remove(file_name);
12.
13. if (status == 0)
14. printf("%s file deleted
successfully.\n", file_name);
15. else
16. {
17. printf("Unable to delete the file\n");
18. perror("Following error occurred");
19. }
20.
21. return 0;
22. }
Output of program:
The deleted file doesn't go to trash or recycle bin, so you may not be able to
recover it. Deleted files can be recovered using specialized recovery
software, if they aren't overwritten on the storage medium.
This code works in Turbo C only because it supports dos.h header file.
C programming code
1. #include<stdlib.h>
2.
3. int main()
4. {
5. system("C:\\Windows\\System32\\ipconfig");
6.
7. return 0;
8. }
Download IP address program.
You can use various options while executing shutdown.exe, for example,
you can use /t option to specify the number of seconds after which
shutdown occurs.
Syntax: "shutdown /s /t x"; where x is the number of seconds after which
shutdown will occur.
1. #include <stdio.h>
2. #include <stdlib.h>
3.
4. int main()
5. {
6. char ch;
7.
8. printf("Do you want to shutdown your computer now
(y/n)\n");
9. scanf("%c", &ch);
10.
11. if (ch == 'y' || ch == 'Y')
12. system("C:\\WINDOWS\\System32\\shutdown -s");
13.
14. return 0;
15. }
If you are using Turbo C Compiler then execute your program from
command prompt or by opening the executable file from the folder.
Press F9 to build your executable file from source program. When you run
program from within the compiler by pressing Ctrl+F9 it may not work.
You need to be logged in as root user for this program to execute otherwise
you will get the message shutdown: Need to be root, now specifies that you
want to shut down immediately. '-P' option specifies you want to power off
your machine. You can specify minutes as:
shutdown -P "number of minutes."