c

Perfect Number In C

吃可爱长大的小学妹 提交于 2021-02-18 12:48:05
问题 I need to write a C program to find the Perfect Number.. main() { int n=1000,sum = 0; for(int num = 1; num <= n; num++) { sum = 0; for(int i = 1; i < num; i++) { if(!(num%i)) { sum+=i; } } if(sum == num) printf("\n%d",num); } } if(!(num%i)) - This is d line I do not understand. If there is any other simple method do please suggest me 回答1: if(!(num%i)) simply means if( (num%i) == 0 ) 回答2: If you are looking for a more efficient way to find perfect numbers, you might want to read the Wikipedia

how to read from stdout in C

若如初见. 提交于 2021-02-18 12:45:09
问题 I need to write a C program ( myprogram ) which checks output of other programs. It should basically work like this: ./otherprogram | ./myprogram But I could not find how to read line-by-line from stdout (or the pipe), and then write all this to stdout . 回答1: Create an executable using: #include <stdio.h> int main() { char line[BUFSIZ]; while ( fgets(line, BUFSIZ, stdin) != NULL ) { // Do something with the line of text } } Then you can pipe the output of any program to it, read the contents

When do we need to clear the scanf buffer?

你离开我真会死。 提交于 2021-02-18 12:44:18
问题 I always thought that the "'\n' in buffer" issue only occurs when we are reading characters, however, I stumbled upon this issue with the following code: int main(int argc, char** argv){ int height = 0, width = 0; while(height < 1 || height > 10|| width < 1 || width > 15){ printf("Please insert the height(1~10) and width(1~15) of the parallelogram (integers): "); if(scanf("%d %d", &height, &width) != 2){ height = width = 0; } } return 0; } As above, I'm only reading integers with scanf, but

When do we need to clear the scanf buffer?

喜你入骨 提交于 2021-02-18 12:44:08
问题 I always thought that the "'\n' in buffer" issue only occurs when we are reading characters, however, I stumbled upon this issue with the following code: int main(int argc, char** argv){ int height = 0, width = 0; while(height < 1 || height > 10|| width < 1 || width > 15){ printf("Please insert the height(1~10) and width(1~15) of the parallelogram (integers): "); if(scanf("%d %d", &height, &width) != 2){ height = width = 0; } } return 0; } As above, I'm only reading integers with scanf, but

Which bi-directional ZeroMQ pattern should I use for multiple clients connecting to a single server?

与世无争的帅哥 提交于 2021-02-18 12:33:05
问题 I have multiple ( 1000s ) of clients connecting to a single server and sending some log data. The server analyses the data and responds, if necessary. PUB / SUB is one directional (monitor example ). REQ / REP cannot identify the peer and reply specifically to a peer ( it is more for ACK and such only ). I need to register these clients, identify them and be able to respond after analysis of their logs. With sockets, I have the client sockets, after accept and I can respond using that socket

Eigenvalues calculations in C-within-R codes

天大地大妈咪最大 提交于 2021-02-18 12:28:28
问题 I am writing R code which uses compiled C code. From "Writing R Extensions" document, I learned there are many R executable/DLL that can be called from C code. The header file ‘Rmath.h’ lists many functions that are available and its source codes are listed on the this website: http://svn.r-project.org/R/trunk/src/nmath/ I need to calculate singular value decomposition of many matrices, however I do not find subroutines which does this on the above website. (So I am assuming that Rmath.h does

Writing an EXT4 file system in C?

一个人想着一个人 提交于 2021-02-18 12:19:13
问题 This may sound noobish, especially as I'm ( as you may have guessed ) trying to write an Operating System. At the moment I'm stuck on trying to make a file system. What I want is a similar file system as Linux Ubuntu which is EXT4 ( at least mine is ). I want to try and also either write it in C. Any idea's on how I can go about this? And/or any tutorials that you might have found that may help me ( I have tried searching with no luck ) :L Thanks in advance! Jamie. 回答1: Really smart and

AVX 4-bit integers

[亡魂溺海] 提交于 2021-02-18 12:12:32
问题 I need to perform the following operation: w[i] = scale * v[i] + point scale and point are fixed, whereas v[] is a vector of 4-bit integers. I need to compute w[] for the arbitrary input vector v[] and I want to speed up the process using AVX intrinsics. However, v[i] is a vector of 4-bit integers. The question is how to perform operations on 4-bit integers using intrinsics? I could use 8-bit integers and perform operations that way, but is there a way to do the following: [a,b] + [c,d] = [a

How to get function address by name?

夙愿已清 提交于 2021-02-18 12:11:16
问题 I'd like to get function's address by name. For example, currently I am using dlsym : unsigned long get_func_addr(const char *func_name) { return (unsigned long)dlsym(NULL, func_name); } However, dlsym only works for extern function. It won't work for static function. I know there could multiple static functions with same name in different files. But I need to at least get one static function's address with the name. Sometime static function will be inlned. But it's OK if C file is compiled

how can i store an int array into string [closed]

瘦欲@ 提交于 2021-02-18 12:10:58
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 5 years ago . Improve this question I have an integer array: int a[5]={5,21,456,1,3} I need to store these number into char array so that the char array will have some thing like this: char *s="52145613"; Is there any library function in c for this? 回答1: sprintf do what you need. Little example