//C Program to hibernate Windows #includevoid main() { system("shutdown /h"); }
Search This Blog
Showing posts with label How to. Show all posts
Showing posts with label How to. Show all posts
C Program to Hibernate Windows
Do you want to hibernate your Windows computer from c? This is a c program to hibernate Windows operating System. If you execute this c program, it will hibernate your Windows computer instantly. The following is the c code to hibernate Windows:
C Program to Run Applications to Open a File in Windows
In this post, we will see how to run an executable to open a file. This will tell you open a file with specified application. This can be used like the open with option in operating systems. To open a file, most applications, are launched with the path to the file as command line argument (command line parameter). The following c program will open paint with a command-line argument. mspaint command opens paint. Or you can use "C:\\Windows\\System 32\\mspaint.exe". The exepected command-line argument to paint is the path to an image file. Then the paint application opens that file as image if possible.
#includeThe above c program will open the image flower.jpg using paint application in Windowsvoid main() { system("C:\\Windows\\System32\\mspaint.exe \"C:\\Users\\Shareef\\Desktop\\flower.jpg\""); }
C Program to Launch or Execute an Application With Command Line Parameters
In this post, we will see how to run an executable with command line arguments. This will tell you how to execute any application with command-line arguments using c code. The following c program will open paint with a command-line argument. mspaint command open paint. Or you can use "C:\\Windows\\System 32\\mspaint.exe". The exepected command-line argument to paint is the path to an image file. Then the paint application opene that file as image if possible.
//How to run applications with command line arguments in c #includevoid main() { system("C:\\Windows\\System32\\mspaint.exe \"C:\\Users\\Shareef\\Desktop\\flower.jpg\""); }
C Program to Schedule a Shutdown - How to Schedule Shutdown in C
You can schedule shutdown in Windows or Linux using C program. We will see how to schedule a shutdown using a c program. To schedule a shutdown in Windows, we execute the Windows command shutdown /s /t sec (where sec is time delay before shutdown in seconds). In Linux, we execute the command shutdown -h +600. The c programs to schedule a shutdown in Windows and Linux are given separately.
C Program to Shutdown Computer - How to Shutdown Computer Using C Code
Computer can be shutdown using c program. But the commands are different in Windows and Linux. The system() function in c can be used to execute system commands ( Windows command or Linux command ). To shutdown the computer using c program, we execute the appropriate shutdown command using the system() function.
How to Execute Windows Commands in C
You may have used Windows commands like ipconfig, netstat, type, set, mkdir, cd etc in cmd (DOS Prompt or Command Prompt). You can execute these commands from a C program. This post will tell you how to execute Windows commands (cmd commands) from c program. To execute different Windows commands, you can use the system() function c language. The system() function is used to invoke command processor to execute a given command. The command which is to be executed is passed as argument to the function. An example c program with system() function is shown below.
Making of Tetris Game in C
![]() |
| Block types in tetris |
To make the tetris game, we first make a two dimensional array. An element in this integer array is a single block (one of the four blocks in each block type) in the game. We set all the elements to zero initially to show that the matrix is empty. We may call each element a cell. So to represent a Z block, we need four cells. In the matrix, to represent a falling block (eg: a Z block) we set corresponding elements' values to 1. We move these 1's in the 2d integer array downwards to simulate falling. Then when it reaches bottom, these 1's are changed to 2's. This is to denote that these cell cannot be moved further. Again, we start another random block from the top (first row).
How to Get Time and Date in C - C Program to Read and Display Time and Date
In this post, we will discuss how to read system time (current time) and date in c. To get system time and date, we use the time.h header file. It contains functions related to time and date. The function time() reads the current time and stores in the variable given as parameter. For example, if we call time(now), then the variable now is assigned the current date and time value. This parameter should be of type time_t. A sample code is:
time_t now; time ( &now );
Note that we should pass the reference to a variable of type time_t. But, the value stored by the function time() contains all information including date and time. So, the value appears to be an unintelligible random number. There fore, to extract hours, minutes, seconds, year, month, day of month etc from this value, we should convert into an object of structure struct tm. The function localtime() converts the value for us. This conversion to date and time can be done as follows:
Using System calls in C Program to rename a Directory in Linux
C Program to rename a directory in Unix or Linux operating systems using system call rename().
#include<stdio.h>
main()
{
char s[10],d[10];
printf("Enter source Dir Name:\n");
scanf("%s",s);
printf("Enter New Dir Name:\n");
scanf("%s",d);
if(rename(s,d)==-1)
perror("Can't be changed\n");
else
printf("%s is changed to %s\n\n",s,d);
}
#include<stdio.h>
main()
{
char s[10],d[10];
printf("Enter source Dir Name:\n");
scanf("%s",s);
printf("Enter New Dir Name:\n");
scanf("%s",d);
if(rename(s,d)==-1)
perror("Can't be changed\n");
else
printf("%s is changed to %s\n\n",s,d);
}
Creating A Process in Linux (UNIX) - fork() Example Program
The system call fork() is used to create a new process in UNIX based operating systems and Linux systems. The fork() system call creates a child process when called from a parent process. Unix will make an exact copy of the parent's address space and give it to the child. Therefore, the parent and child processes have separate address spaces. Here is a C program which uses the fork() system call to create a process during execution. The program is commented well for better understanding.
Program
/*The program (parent process) reads an array from the user.
It sorts it in descending order.
The child process created by parent process sorts it ascending order
*/
#include<stdlib.h>
#include<stdio.h>
int main()
{
int cpid;
int temp,a[100],n,i,j;
printf("\nEnter number of elements : ");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\n");
cpid=fork(); /*Creating child process using fork() system call */
if(cpid<0)
{
printf("Cannot create a new process\n");
exit(0);
}
if(cpid != 0)
{
/*fork returns processid of newly created process.
Therefore, the value of integer variable cpid in parent process is the process id of child process.
This block (if block) is executed only by the parent because the value of cpid is zero in child process. We use the value of variable cpid to decide which process (parent or child) should execute which part of code*/
printf("\nThis is parent with id: %d\n",getpid());
/* The UNIX system call getpid() return the process id of process being executed.*/
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]<a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Parent sorted the array in descending order\n");
}else
{
/*This block (else block) is executed only by the child process because value of cpid is zero only for child process.*/
printf("\nThis is child with id: %d \n",getpid());
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Child sorted the array in ascending order\n");
}
/*The following part of code is executed by both the processes because it is not restricted using the value of cpid.*/
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
return 0;
}
Output:
Enter number of elements : 5
Enter elements
8
6
10
2
12
This is parent with id: 4261
Parent sorted the array in descending order
12
10
8
6
2
This is child with id: 4262
Child sorted the array in ascending order
2
6
8
10
12
Program
/*The program (parent process) reads an array from the user.
It sorts it in descending order.
The child process created by parent process sorts it ascending order
*/
#include<stdlib.h>
#include<stdio.h>
int main()
{
int cpid;
int temp,a[100],n,i,j;
printf("\nEnter number of elements : ");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\n");
cpid=fork(); /*Creating child process using fork() system call */
if(cpid<0)
{
printf("Cannot create a new process\n");
exit(0);
}
if(cpid != 0)
{
/*fork returns processid of newly created process.
Therefore, the value of integer variable cpid in parent process is the process id of child process.
This block (if block) is executed only by the parent because the value of cpid is zero in child process. We use the value of variable cpid to decide which process (parent or child) should execute which part of code*/
printf("\nThis is parent with id: %d\n",getpid());
/* The UNIX system call getpid() return the process id of process being executed.*/
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]<a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Parent sorted the array in descending order\n");
}else
{
/*This block (else block) is executed only by the child process because value of cpid is zero only for child process.*/
printf("\nThis is child with id: %d \n",getpid());
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Child sorted the array in ascending order\n");
}
/*The following part of code is executed by both the processes because it is not restricted using the value of cpid.*/
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
return 0;
}
Output:
Enter number of elements : 5
Enter elements
8
6
10
2
12
This is parent with id: 4261
Parent sorted the array in descending order
12
10
8
6
2
This is child with id: 4262
Child sorted the array in ascending order
2
6
8
10
12
![]() |
| Output of the program |
C Program To Represent Directed or Undirected Graph Using Adjacency Matrix
C program to represent directed or undirected graph using Adjacency matrix.
#include<stdio.h>
#include<conio.h>
#define max 20
int adj[max][max]; //Adjacency matrix
int n; //Denotes number of nodes in the graph
void main()
{
int max_edges,i,j,origin,destin;
char graph_type;
printf("Enter number of nodes : ");
scanf("%d",&n);
printf("Enter type of graph, directed or undirected.\n");
printf("Enter d for directed and u for undirected:\n ");
fflush(stdin);
scanf("%c",&graph_type);
if(graph_type=='u')
max_edges=n*(n-1)/2;
else
max_edges=n*(n-1);
for(i=1;i<=max_edges;i++)
{
printf("Enter edge %d( 0 0 to quit ) : ",i);
scanf("%d %d",&origin,&destin);
if( (origin==0) && (destin==0) )
break;
if( origin > n || destin > n || origin<=0 || destin<=0)
{
printf("Invalid edge!\n");
i--;
}
else
{
adj[origin][destin]=1;
if( graph_type=='u')
adj[destin][origin]=1;
}
}
printf("The adjacency matrix is :\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%4d",adj[i][j]);
printf("\n");
}
getch();
}
Subscribe to:
Posts (Atom)

