Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <lib.h>    
#include <stdio.h>

int main(void) {
    int  nr_procs;
    printf("processes: %d\n",nr_procs);
    return 0;
}


What I have tried:

I have tried this code but as a result i get:
processes: 134244732
----------but instead i have to take this result--------------
processes: 112
Have you have any ideas why it prints me this big number?
Posted
Updated 20-Apr-16 14:07pm
v2

It prints a huge number because you never initialized the nr_procs variable. It'll be whatever happens to be in memory allocated for that variable when the code is run.
int nr_procs = 0;


You also never tried to get a list of processes from anywhere so I don't know what you really expect this code to do other than print a meaningless number.
 
Share this answer
 
Comments
Member 12474105 20-Apr-16 17:00pm    
sorry i am new in the topic .i want to print the number of running processes
Dave Kreskowiak 20-Apr-16 18:18pm    
OK, so where's your code to get the list of processes? This will be dependent on the O/S you're running the code on.
Member 12474105 20-Apr-16 18:24pm    
i am running 3.2.1 minix,do you mean that i have to write some code fot this.can i use nr_procs to get the number of running processes?
Dave Kreskowiak 20-Apr-16 18:51pm    
YES you have to write the code for it! nr_procs is just a variable you declared as an integer. How the hell is the system supposed to know that you want the number of processes in that variable???

I've never even seen a minix machine, so you're going to have to dig through Google to get some sense of what the code is going to look like. I'd start by Googling for "minix get list of processes".

Quote:
Have you have any ideas why it prints me this big number?
Yes, your program print what you requested, but you requested wrong.
Quote:
C++
int  nr_procs;
Here you declare an uninitialized variable, you don't put a value in it, and then you print the random value that is in it.
You forgot to feed the variable with a value.
You need
C++
int  nr_procs;
nr_procs= something;
printf("processes: %d\n",nr_procs);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900