question
stringlengths 24
425
| answer
stringlengths 5
405
|
---|---|
```
Min Profile Cycles [215914]
Max Profile Cycles [934625]
Max Profile [23]
Max Profile Count [4]
```
How to print all these console output into some .txt file in c so that whenever i will call that function it will print in txt file instead of printing in console.
| as your tag is C, you can fopen() in append-mode a file and instead of using printf() you use fprintf() (and fwrite() and similar) with this filehandle.
|
How to I give the user the ability to name the output .txt file? Here is all I have right now.
```
FILE *f = fopen("output.txt", "a");
```
| You can read the input from user and append.txt
```
char fileName[30];
// ...
scanf("%25s", fileName); // max 25 characters because .txt have 4 (25+4 = 29)
strcat(fileName, ".txt"); // append .txt extension
// ...
FILE *f = fopen(fileName, "a");
```
|
Actually, I know it's necessary to free the memory allocated with malloc(), but I wonder if I can know there is some memory left being not collected if I forget to collect it with free().
| Valgrind would be your best bet
http://valgrind.org/
|
Actually, I know it's necessary to free the memory allocated with malloc(), but I wonder if I can know there is some memory left being not collected if I forget to collect it with free().
| Valgrind would be your best bet
http://valgrind.org/
|
How can I determine the length, in seconds, of the Ogg Vorbis stream with libvorbis and/or libvorbisfile?
As far as I can tell, there's nothing in thevorbis_infostructure that tells me the length of the stream.
| Using the lower level API, you can do this withov_time_total.
|
I have a 16 bit address 0-15, I need to check at value of bit corresponding to their position in C
for example at 15 place i have 1, 14 has 0, and soo on!
|1|0|0|0|1|0|1|0|1|1|1|1|1|0|1|0|
I was thinking to create 16 new addresses with all zero and the position i am looking
| Use a macro similar to this:
```
#define CHECK_BIT(x, i) ( ((x) & (1 << (i)) != 0 ) )
```
CHECK_BIT(x, i)will returntrueif the i'th bit ofxis one, false otherwise.
|
I am confused about this example:
```
typedef int32_t voltage_dc_estimate_t[1];
```
Everything is OK but that[1]at the end of the type definition confuse me. Could someone please help me to understand that situation?
| [1]means an array of1element.
voltage_dc_estimate_tis a type of an array of1element of typeint32_t.
|
I'm trying to use #ifndef as below.
```
#ifndef MACRO1 || #ifndef MACRO2
....
#endif
```
I already tried:
```
#ifndef (MACRO1 || MACRO2)
..
#endif
```
But for both cases am getting below error
error: extra tokens at end of #ifndef directive
| Use the#ifpreprocessor directive instead:
```
#if !defined(MACRO1) || !defined(MACRO2)
```
|
I have:
```
const char *ptr = "int";
```
If I have to declare usingptr:
```
int a;
```
ptrcan point to anything arbitrarychar,string,<anythihg>.
I have to delcare a variable of that type whatptris pointing at.
Is it possible?
| No. C++ is not an interpreted language."int"has a meaning to the compiler, but at runtime there's nothing which understands"int".
|
I have:
```
const char *ptr = "int";
```
If I have to declare usingptr:
```
int a;
```
ptrcan point to anything arbitrarychar,string,<anythihg>.
I have to delcare a variable of that type whatptris pointing at.
Is it possible?
| No. C++ is not an interpreted language."int"has a meaning to the compiler, but at runtime there's nothing which understands"int".
|
I have:
```
const char *ptr = "int";
```
If I have to declare usingptr:
```
int a;
```
ptrcan point to anything arbitrarychar,string,<anythihg>.
I have to delcare a variable of that type whatptris pointing at.
Is it possible?
| No. C++ is not an interpreted language."int"has a meaning to the compiler, but at runtime there's nothing which understands"int".
|
I know how to find Big O for "for loops" and nested "for loops", but what happens when we have two for loops, not nested but two separate for loops in the same function.
| It just gets added.
See for e.g. :
```
for(i=0;i<n;i++)
//statements
for(i=0;i<m;i++)
//statements
```
So the total complexity is O(m+n).
lets say m=3n then
its O(4n) which is O(n) only .
let m = n^2
then its O(n^2+n) which is O(n^2)
|
How do I define a 2D array where the first dimension is already defined with MAX_VALUES. I wish to be able to allocate memory to the second dimension. Below is what I have attempted. I want an array with A[1000][mallocd]
```
#define MAX_VALUES 1000
int
main(){
int n= 10;
int *A[MAX_VALUES] = malloc(10 * sizeof(int));
}
```
| Try this
```
int (*A)[n] = malloc(MAX_VALUES * sizeof(*A));
```
It will allocate contiguous 2D array.
|
I need to know what's the address from which/bin/lsprogram execution starts (after dynamic linker fix the environment, what address gets the control?) I need it in hexadecimal (0xNNN...) or decimal format
| That would be platform dependent. For a recent system, that should actually be randomized.
|
I have to insert uncompressed data in between the compressed data bytes. Type 0 header in zlib allows me to do that. But How can i do that ? any clues ?
| There is no type 0 allowed in a zlib header. There is a stored block type in the deflate format used within a zlib stream. zlib will automatically use stored blocks if they are smaller than the same data compressed.
|
I have a question related to bit-fields in C. Here I have such a structure:
```
struct Register
{
int bit:1;
};
int main(void)
{
struct Register bit = {1};
printf("\nbit = %d", bit.bit);
return 0;
}
```
Can you please explain me why do I get:
bit = -1
| If you're working with bitfields, you should useunsigned int.signed intis a problem for bit-fields.
|
I need to know what's the address from which/bin/lsprogram execution starts (after dynamic linker fix the environment, what address gets the control?) I need it in hexadecimal (0xNNN...) or decimal format
| That would be platform dependent. For a recent system, that should actually be randomized.
|
I am now starting to learn C. I would like to know why I get the warning. I declared a bidimensional char array, but why the character"d"is not allowed?
```
char array[3][3] = {{1,"d",3},{3,2,1},{2,1,3}};
```
| Replace "d" with 'd'
'd' is a character
"d" is a string
|
I am now starting to learn C. I would like to know why I get the warning. I declared a bidimensional char array, but why the character"d"is not allowed?
```
char array[3][3] = {{1,"d",3},{3,2,1},{2,1,3}};
```
| Replace "d" with 'd'
'd' is a character
"d" is a string
|
On the CPU, I have an array of positions and colors:
```
[ x0, y0, z0, col0, x1, y1, z1, col1 ...]
```
On the device, I have a 3D texture. Is there any efficient way to modify that texture so that positionx0, y0, z0's color becomes col0 and so on?
| glTexSubimage3d will work if you can arrange your data into cubes. Doing it one texel at a time would be miserable though.
https://www.opengl.org/sdk/docs/man3/xhtml/glTexSubImage3D.xml
|
I tried includingstring.handstdlib.h, but still get undefined reference compile errors.
This leads me to conclude that it is in a different library that I didn't include. Where is it?
I am using gcc compiler - the code is written in Windows, but is going to be compiled and run on a unix server.
| strcat_scan be found instring.has of C 2011. Generally speaking, only microsoft has implemented these alternative functions.
|
On the CPU, I have an array of positions and colors:
```
[ x0, y0, z0, col0, x1, y1, z1, col1 ...]
```
On the device, I have a 3D texture. Is there any efficient way to modify that texture so that positionx0, y0, z0's color becomes col0 and so on?
| glTexSubimage3d will work if you can arrange your data into cubes. Doing it one texel at a time would be miserable though.
https://www.opengl.org/sdk/docs/man3/xhtml/glTexSubImage3D.xml
|
I tried includingstring.handstdlib.h, but still get undefined reference compile errors.
This leads me to conclude that it is in a different library that I didn't include. Where is it?
I am using gcc compiler - the code is written in Windows, but is going to be compiled and run on a unix server.
| strcat_scan be found instring.has of C 2011. Generally speaking, only microsoft has implemented these alternative functions.
|
Consider the C code below:
```
#include <stdio.h>
int main()
{
int i = 012;
printf("%d", i);
}
```
On O/P the value ofiis printed as 10. I know (012)8= (10)10. But I'm confused about how C storesintvariable values. Can anybody explain it to me?
| They are stored in binary, you can use many representations to define them, but in the end it's the binary representation that is used.
|
I have a set of points in a 2D plane. I need to detect only the points that belong to the shape that borns from the union of this subset of points in order to cover the largest area: that is, the boundary points.
The following figure is an example:
The red points are those ones I need to detect.
| What you need is calledConvex hull.A lot of algorithms existto calculate it.
|
I have a set of points in a 2D plane. I need to detect only the points that belong to the shape that borns from the union of this subset of points in order to cover the largest area: that is, the boundary points.
The following figure is an example:
The red points are those ones I need to detect.
| What you need is calledConvex hull.A lot of algorithms existto calculate it.
|
I disassembled the following C call
```
exit(EXIT_SUCCESS);
```
and found out that it calls the exit procedure at an assembly level
```
call exit
```
Where can I find the source code for this procedure so that I can study it and find out exactly what is happening?
| In theC library of your system. Which then eventually invokes youroperating system.
The links are for linux with glibc, you forgot to mention your environment.
|
I disassembled the following C call
```
exit(EXIT_SUCCESS);
```
and found out that it calls the exit procedure at an assembly level
```
call exit
```
Where can I find the source code for this procedure so that I can study it and find out exactly what is happening?
| In theC library of your system. Which then eventually invokes youroperating system.
The links are for linux with glibc, you forgot to mention your environment.
|
so I have a string with the following value
[7, 8]
Now I wan't to store each value (only the numbers) in a 2D array [1][2].I'm pretty sure using sscanf is the best way to do this, but how do I tell sccanf to only read the integers?
Thanks in advance
| ```
sscanf("[%d,%d]", Array[a][b], Array[c][d]);
```
|
Whats difference in the working of
```
while(i++<100)
```
and
```
while(i++<100);
```
Is this correct that in 1st case,iincreases until its value reach to99but in 2nd case,;is an error that's why nothing is going to happen?
| No:
while(i++<100);is a loop with a empty command (does nothing untiliis 100), (there is no compilation error).
Andwhile(i++<100) {commands}is a same loop but does someting.
|
so I have a string with the following value
[7, 8]
Now I wan't to store each value (only the numbers) in a 2D array [1][2].I'm pretty sure using sscanf is the best way to do this, but how do I tell sccanf to only read the integers?
Thanks in advance
| ```
sscanf("[%d,%d]", Array[a][b], Array[c][d]);
```
|
Whats difference in the working of
```
while(i++<100)
```
and
```
while(i++<100);
```
Is this correct that in 1st case,iincreases until its value reach to99but in 2nd case,;is an error that's why nothing is going to happen?
| No:
while(i++<100);is a loop with a empty command (does nothing untiliis 100), (there is no compilation error).
Andwhile(i++<100) {commands}is a same loop but does someting.
|
How do I that? I don't want to use the Visual Studio IDE, yet I want to use the provided compiler (cl.exe) and the Developer Command Prompt for VS2013.
| I used to do it to via command line
```
cl /EHsc /MD main.cpp /Fetest.exe /I F:\C++_Libraries\SDL2\SDL2-2.0.3\include /link /LIBPATH:F:\C++_Libraries\SDL2\SDL2-2.0.3\lib\x86 SDL2.lib SDL2main.lib /SUBSYSTEM:CONSOLE
```
|
Please explain the difference between
```
char* str = "Hello";
```
And
```
char* str = {"Hello"};
```
| ISO 9899-1990 6.5.7("Initialization") says :
An array of character type may be initialized by a character string
literal, optionally enclosed in braces.
|
Please explain the difference between
```
char* str = "Hello";
```
And
```
char* str = {"Hello"};
```
| ISO 9899-1990 6.5.7("Initialization") says :
An array of character type may be initialized by a character string
literal, optionally enclosed in braces.
|
Create file using C in Ubuntu.
I've tried that code and it doesn't work.
```
#include<stdio.h>
#include<string.h>
int main()
{
char a[50];
char command[150];
printf("Enter The File's Name");
gets(a);
strcpy("touch ");
strcat("a");
system(command);
return 0;
}
```
| Your strcpy should bestrcpy(command, "touch")and your strcat should bestrcat(command, a)
But there are much better ways to create an empty file...
|
Why doesn't this work?
```
printf("%d \n\n\n\n", atoi("11110010100"));
```
it outputs -1774891788... I just want it outputted as it is. It seems to work just fine if the number is a bit smaller.
| atoireturns anint. You pass a string which contains a number bigger than whatint(in your implementation) can hold. So, you have an integer overflow.
To print the maximum value anintcan hold, includelimits.hand printINT_MAX.
|
Newbie question: why can't I getprintfto output the binary number as it is, instead of converting it?
```
int gpa = 01011001;
printf("pure: %i \n", gpa);
```
gives the output: 266753
How can I get it to output 01011001 or just 1011001?
| 0preceding an integer literal is hold for octa-decimal number. Decimal equivalent of01011001is266753. Remove that preceding0to print as it is.
|
Create file using C in Ubuntu.
I've tried that code and it doesn't work.
```
#include<stdio.h>
#include<string.h>
int main()
{
char a[50];
char command[150];
printf("Enter The File's Name");
gets(a);
strcpy("touch ");
strcat("a");
system(command);
return 0;
}
```
| Your strcpy should bestrcpy(command, "touch")and your strcat should bestrcat(command, a)
But there are much better ways to create an empty file...
|
I am using linux Lite. I have installed code Blocks. When i am creating any new project then it is working correctly. it compiling codes. But When any empty c file is creating and trying to compile then it is not working.Showing message that permission denied. I have checked the compiler setting for empty file. it is GNU GCC Compiler. How can i solve this?
| It would be happen due to root access. run code-blocks as root user and try it would be work.
|
I need to edit the values of an existing ListView row (i.e change the values of an Item and its Subitems).
I need to do that programmatically and not by clicking on the ListView item at runtime to rename it.
| just useLVM_SETITEMas documented.
|
```
#include<stdio.h>
void main ()
{
int a=4;
const int *p=&a;
*p--;
}
```
in the above line it means that we can not change the value a via p, so in decrement statement it should give an error but it is not giving the error. can anyone explain why??
| *p--decrementspnot contents ofp.
If you do(*p)--you will get compilation error
```
error: decrement of read-only location ‘*p’
```
|
I know compiler does Structure padding because CPU can read one word at time ( Word size depends on architecture 16/32/64 ) but I am not able to understand padding behavior for below example.
```
struct pad
{
char ch;
};
```
Size of "struct pad" comes as 1 Byte ( But According to padding concept I suspected 4 byte ( 32 bit architecture ).
| A char type can be accessed efficiently on any address boundary, so no padding is necessary.
|
I'm trying to do thisswprintf(hexResult, L"%I64X", result);from javascript, is this possible? Such thatCityHash64('C:\Program Files (x86)\Mozilla Firefox')is encoded asE7CF176E110C211B.
Thanks
| JavaScript numbers have atoStringmethod which takes a radix. UsingtoString(16)will get you the number in hex. Is this what you're looking for?
|
How can I delete the first digit of a given number using a user defined function (Suppose the function isDELTOP) ?
For Ex.,
```
x=DELTOP(1748);
```
I want the value ofxas748.
Would anybody tell me how to write this function easily ?
| A C++11 one-liner would be
```
int DELTOP(int x) {
return std::stoi(std::to_string(x).substr(1));
}
```
|
Does something like TextBox exist in ncurses?
Requirements to TextBox:
ability to adequate handle KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT, KEY_BACKSPACE, KEY_DC and other.adequate transfer lines.save, if you need, text in char* buffer.
| ncursesitself provides lower-level functions from which a "textbox" can be built. For instance, thedialogandcdklibraries use (n)curses to provide these features.
|
Does something like TextBox exist in ncurses?
Requirements to TextBox:
ability to adequate handle KEY_DOWN, KEY_UP, KEY_LEFT, KEY_RIGHT, KEY_BACKSPACE, KEY_DC and other.adequate transfer lines.save, if you need, text in char* buffer.
| ncursesitself provides lower-level functions from which a "textbox" can be built. For instance, thedialogandcdklibraries use (n)curses to provide these features.
|
At the third line I get "Expected a declaration" for instr[0xB8][256]
```
void funcB8 ();
void (*instr[256][257])();
instr[0xB8][256] = &funcB8;
```
| You can initialize the array at a specific index outside a function:
```
void (*instr[256][257])() = { [0xB8][256] = &funcB8 };
```
or assign a value to the array, but to do that you have to do that in a function
|
So, I'm trying to write some JNA code to work with Unix sockets, and I'm trying to reference AF_UNIX for sockets. I looked through some of the gcc included files, but I couldn't find anything.
| It may be platform-dependent, which is why you should use the define. It should be insys/socket.h.
|
I'm writing a netfilter module and need to store current time value somewhere inside the module to calculate difference in time. What is the best way to do that?
| I didn't understood your question properly. Because you are not provided any piece of code. Any way you can keep your variable either indevice structure of module, or you can declare variable simply as aGlobal variable.
|
The following C code is apparently O(N) (according to my practice exam). However, I am not sure as to why it is O(N) and not O(Something*Something).
```
void doit(int N) {
while (N) {
for (int j = 0; j < N; j += 1) {
}
N = N / 2;
}
}
```
Anyone care to give me some insight to this problem?
Thanks in advance!
| Because N + N/2 + N/4 + ... = 2N.
|
I just started programming in C. And I don't really understand the following code:
```
printf("%zu",i);
```
or instead of%zuwhat are the other things that I can write (I know that they depend on the type of i) and which one is for what?
| It's a format modifier forsiz_tandsize_tis unsigned.
```
printf("%zu\n", x); // print unsigned decimal
printf("%zx\n", x); // print hexadecimal
printf("%zd\n", y); // print signed decimal
```
|
I just started programming in C. And I don't really understand the following code:
```
printf("%zu",i);
```
or instead of%zuwhat are the other things that I can write (I know that they depend on the type of i) and which one is for what?
| It's a format modifier forsiz_tandsize_tis unsigned.
```
printf("%zu\n", x); // print unsigned decimal
printf("%zx\n", x); // print hexadecimal
printf("%zd\n", y); // print signed decimal
```
|
I want to be able to do this in visual studio:
```
struct A
{
int x;
};
struct B
{
int y;
struct A;
};
struct B b;
b.x //...
```
How can I do this? In gcc you use -fms-extensions to compile it
| Youjust do it.MSVS supports this as an extension.
|
Basically, I can't figure this out, I want my C program to store the entire plaintext of a batch program then insert in a file and then run.
I finished my program, but holding the contents is my problem. How do I insert the code in a string and make it ignore ALL special characters like %s \ etc?
| You have to escape special characters with a \, you can escape backslash itself with another backslash (i.e.\\).
|
Basically, I can't figure this out, I want my C program to store the entire plaintext of a batch program then insert in a file and then run.
I finished my program, but holding the contents is my problem. How do I insert the code in a string and make it ignore ALL special characters like %s \ etc?
| You have to escape special characters with a \, you can escape backslash itself with another backslash (i.e.\\).
|
In C, we can build a debug version or a release version of the binary files (the object files and the executable). How can we do this in Go?
| In Go, it isn't typical to have a debug version or a release version.
By default,go buildcombines symbol and debug info with binary files. However, you can remove the symbol and debug info withgo build -ldflags "-s -w".
|
In C, we can build a debug version or a release version of the binary files (the object files and the executable). How can we do this in Go?
| In Go, it isn't typical to have a debug version or a release version.
By default,go buildcombines symbol and debug info with binary files. However, you can remove the symbol and debug info withgo build -ldflags "-s -w".
|
Is it possible to load akernel extension(kext) through aC APIinstead of using thesystem()call?
| Yes, you want theKextManagerAPI. In particular,KextManagerLoadKextWithIdentifier()is probably the function you'll want to use to load an installed kext, versusKextManagerLoadKextWithURL()if you want to load a kext that is not in /System/Library/Extensions or /Library/Extensions but e.g. in an .app bundle.
|
This question already has answers here:Fill with variable number of ones(2 answers)Closed8 years ago.
I have a number in range 1-8.
I want to convert it to
```
0b00000001 // 1
0b00000011 // 2
0b00000111 // 3
0b00001111 // 4
0b00011111 // 5
0b00111111 // 6
0b01111111 // 7
0b11111111 // 8
```
Can it be done without a switch or a for loop?
Ideally with a simple formula.
| What you are looking for is
```
(1 << v) - 1
```
|
This question already has answers here:Fill with variable number of ones(2 answers)Closed8 years ago.
I have a number in range 1-8.
I want to convert it to
```
0b00000001 // 1
0b00000011 // 2
0b00000111 // 3
0b00001111 // 4
0b00011111 // 5
0b00111111 // 6
0b01111111 // 7
0b11111111 // 8
```
Can it be done without a switch or a for loop?
Ideally with a simple formula.
| What you are looking for is
```
(1 << v) - 1
```
|
This is a piece of myCcode:
```
for (i=2; i<sequence_length+1; i++)
{
int tab_length = Newton(sequence_length, i);
start_tab[tab_length] = make_for_num(i, sequence_length, start_tab, tab_length);
}
```
My goal is to use updatedstart_tabvariable for every next step of forloop. Output of each iteration should be an input for the next one, but it isn't. How can I fix that?
| You could use a recursive function.
|
This is a piece of myCcode:
```
for (i=2; i<sequence_length+1; i++)
{
int tab_length = Newton(sequence_length, i);
start_tab[tab_length] = make_for_num(i, sequence_length, start_tab, tab_length);
}
```
My goal is to use updatedstart_tabvariable for every next step of forloop. Output of each iteration should be an input for the next one, but it isn't. How can I fix that?
| You could use a recursive function.
|
Hello I am developing a simple protection for a Cocoa Mac Os X application for independent distribution. What would be the best place to store some secret hidden files on the user's disk to keep track of demo expiration ? also how to protect in case a user monitors the file system activity with tools such as FSEventer ? Thanks
| Store it as a generic password in the user's keychain.
Keychain Services Programming Guide
|
I am wrapping c library for Python using Cython.I have pointers to callable Python objects in my C code to call them back later from C code.How to ensure a callback is not garbage collected or deleted if I only have address.
| Just__Pyx_INCREF(obj)the object you're holding -- this ensures it won't go away!Doremember to__Pyx_DECREF(obj)once you're done with it -- and, seehttps://github.com/cython/cython/wiki/refcountingfor details!-)
|
I was going throughPthreads Programmingby Bradford Nichols, on Page number177I noticed this code:
```
static char work_buffer[100];
void reverse_string(in_str)
char *in_str;
{
...
...
}
```
What is the third line of code doing here?
| Third line states that the parameterin_starofreverse_stringis of typechar *. Its an old K&R style of parameter declaration.
|
I was going throughPthreads Programmingby Bradford Nichols, on Page number177I noticed this code:
```
static char work_buffer[100];
void reverse_string(in_str)
char *in_str;
{
...
...
}
```
What is the third line of code doing here?
| Third line states that the parameterin_starofreverse_stringis of typechar *. Its an old K&R style of parameter declaration.
|
I'm studying operating systems and I encountered both the terms ISR and interrupt handler. Are they two words for the same mechanism? If not, what is the difference?
| There is no difference in Interrupt handler and ISR.Wikisays that:
In computer systems programming,an interrupt handler, also known as an interrupt service routine or ISR, is a callback function [...]
|
I'm telling other people thatcc -DFOOis just the same ascc -DFOO=1but I'm not quite confident if all compilers support this. So is it a standard for C compilers?
(As inspired by the accepted answer, found the recent POSIXc99standard, 2016 edition. Just for reference.)
| It's not a standard for C compilers, though it is a standard for POSIX-compliant systems. Seethe cc manual.
|
I have one Tcp/Ip with ssl server enabled with epoll so it can handle ten thousand connections simultaneously. When i tried to test the server,i created thousand clients using thread. when i tried to connect with the server,after certain time connect() return ETIMEOUT. How to overcome this error?
| you could change the socket attributes for recv and xmit timeout values Probably the same way you set the keepalive attribute
|
How could I modify the line bellow in ANSI C without using theunsignedmark?
```
unsigned int x, y, z; // unsigned variables should not be used
/*... some operations where x, y and z gets values between 0x0 and 0xFFFFFFFF ... */
x = (unsigned int)-(int)(y * z); // line to modify
```
| You can directly apply negation to an unsigned value. The result will be reduced modulo 2^N, which is what your existing code does anyway.
```
x = -(y * z);
```
|
I have a big project. I want to find which all .cpp/.h files includefile1.hin them. Is there any shortcut key in Eclipse CDT to do this?
| I'd suggest the Include Browser view. You can drag a file in there and you get a nice tree which shows all files that include your file, including indirect includes.
|
i have a functionisEmpty()and i want to replace it with with thisEmpty().
```
int isEmpty(char *s)
{ return s[0]=='\0';
}
```
Should i replace it like this?
```
#define isEmpty() Empty()
```
| Given you have a functionisEmptyas defined, you need to pass a parameter to it:
```
#define Empty(s) isEmpty(s)
```
But you might want to achieve it without any function:
```
#define Empty(s) ((s)[0]=='\0')
```
|
I have a big project. I want to find which all .cpp/.h files includefile1.hin them. Is there any shortcut key in Eclipse CDT to do this?
| I'd suggest the Include Browser view. You can drag a file in there and you get a nice tree which shows all files that include your file, including indirect includes.
|
let's say there is a (void)function I define that prints some numbers and I want to check if it prints the correct content. I'm thinking writing the content to a file and read it back and check if it is what I wanted. But I dont know how since the function is void and returns nothing.
| I just found out I can use freopen() to write data into a file from stdout. My bad for not making the question more clear.
|
I want to test an element forstructureif he is empty or not
but when (ex.) theG[s].a=0andG[s].b=2he never go into theif,, any help ?
```
if (G[s].a != NULL && G[s].b != NULL)
```
| re write your logic with positive logic instead of using not null.
"not (A or B)" is the same as "(not A) and (not B)".
|
I saw some code like this:
```
enum
{
FIRST_DAY = 0
};
```
I know the grammar allows only one member in the enum, but in which case we define it like this? and what is the reason?
| I can only guess, but in C this was an idiom to define named integer constants without resorting to using #define.
|
Suppose I write a program in C/C++ and create an array of certain size. I want to keep that permanently even if I switch off the computer and later access it. Is there a way to do that? If so do let me know and also after saving it, how to access it.
| Save the data to a file and load it on programm start.
|
I saw some code like this:
```
enum
{
FIRST_DAY = 0
};
```
I know the grammar allows only one member in the enum, but in which case we define it like this? and what is the reason?
| I can only guess, but in C this was an idiom to define named integer constants without resorting to using #define.
|
Suppose I write a program in C/C++ and create an array of certain size. I want to keep that permanently even if I switch off the computer and later access it. Is there a way to do that? If so do let me know and also after saving it, how to access it.
| Save the data to a file and load it on programm start.
|
I want to quickly make a static linked list, with as little code as possible, which is very readable and without clutter. How do i accomplish this elegantly?
something like
1 -> 2 -> 3 -> 4 -> NULL
| ```
struct node {int x; struct node *next;};
#define cons(x,next) (struct node[]){{x,next}}
struct node *head = cons(1, cons(2, cons(3, cons(4, NULL))));
```
|
Is there a way to compute the result of((UINT_MAX+1)/x)*x-1in C without resorting tounsigned long(wherexisunsigned int)?
(respective "without resorting tounsigned long long" depending on architecture.)
| It is rather simple arithmetic:
```
((UINT_MAX + 1) / x) * x - 1 =
((UINT_MAX - x + x + 1) / x) * x - 1 =
((UINT_MAX - x + 1) / x + 1) * x - 1 =
(UINT_MAX - x + 1) / x) * x + (x - 1)
```
|
Is there a way to compute the result of((UINT_MAX+1)/x)*x-1in C without resorting tounsigned long(wherexisunsigned int)?
(respective "without resorting tounsigned long long" depending on architecture.)
| It is rather simple arithmetic:
```
((UINT_MAX + 1) / x) * x - 1 =
((UINT_MAX - x + x + 1) / x) * x - 1 =
((UINT_MAX - x + 1) / x + 1) * x - 1 =
(UINT_MAX - x + 1) / x) * x + (x - 1)
```
|
So I declared a variable as an integer and initialized it to 0. When I tried printing the value of that variable, it gave me an incredibly high number. int
```
x=0;
printf("%d", &x);
```
This is what I did. Am I doing anything wrong? Thanks in advance.
| The operator '&' represent the address of that variable. we need, the actual value of that variable use like this...
```
printf("%d",x);
```
|
What is the reason to invoke theprepare handle callbackand thecheck handle callbackin the libuv event loop?
| The i/o operations happen between those 2, so you may want to perform some operation before blocking for i/o (prepare handles), and after i/o (check handles).
This diagram illustrates how the libuv event loop operates internally:http://docs.libuv.org/en/v1.x/design.html#the-i-o-loop
|
What is the reason to invoke theprepare handle callbackand thecheck handle callbackin the libuv event loop?
| The i/o operations happen between those 2, so you may want to perform some operation before blocking for i/o (prepare handles), and after i/o (check handles).
This diagram illustrates how the libuv event loop operates internally:http://docs.libuv.org/en/v1.x/design.html#the-i-o-loop
|
As the title suggests, what are names ofletoh16,letoh32,betoh16, andbetoh32on Linux? I have foundhtonl, but that doesn't let me choose 16 bit or 32 bit, and it is also only to big endianess.
| The functions are called the following on Linux:
letoh16is calledle16tohletoh32is calledle32tohbetoh16is calledbe16tohbetoh32is calledbe32toh
|
how do I add Arduino libraries to Eclipse Project? I just started playing with Intel Edison and have a Grove developer kit with an LCD that I can't use, because the library is missing.
| If you go to Project->Properties->C/C++ Build->Settings
You can add a library under the appropriate compiler with -l and you can include directories for headers with -I under C/C++ General->Paths and Symbols under the includes tab.
|
how do I add Arduino libraries to Eclipse Project? I just started playing with Intel Edison and have a Grove developer kit with an LCD that I can't use, because the library is missing.
| If you go to Project->Properties->C/C++ Build->Settings
You can add a library under the appropriate compiler with -l and you can include directories for headers with -I under C/C++ General->Paths and Symbols under the includes tab.
|
printf stops printing at the first \0 it meets.
Is there a way to force it to continue, for example if my string contains more characters after \0.
| If you know the length of your string asncharacters, you can output it usingfwrite:
```
if (n && fwrite(str, 1, n, stdout) != n) {
/* Error handling. */
}
```
|
I am confused between theif/elseand#if/#else/#endifconstructs.
What are the differences between them?In which specific situations should I use each of them?
| if(...)andelse(...)conditions are evaluated at runtime.#if,#elseare evaluated before compile time by the preprocessor.
|
Why won't this program run?
```
#include<stdio.h>
int main()
{
printf("%f\n", log(36.0));
return 0;
}
```
| You have to include
```
#include<math.h>
```
This is because math.h is a header file in the standard library of C programming language designed for basic mathematical operations.
|
Why won't this program run?
```
#include<stdio.h>
int main()
{
printf("%f\n", log(36.0));
return 0;
}
```
| You have to include
```
#include<math.h>
```
This is because math.h is a header file in the standard library of C programming language designed for basic mathematical operations.
|
Why won't this program run?
```
#include<stdio.h>
int main()
{
printf("%f\n", log(36.0));
return 0;
}
```
| You have to include
```
#include<math.h>
```
This is because math.h is a header file in the standard library of C programming language designed for basic mathematical operations.
|
I think that the C programming language should be more clear with regards to syntax for certain elements.
I have tried to solve this with an emulator but it was to slow.
```
typedef unsigned long into uli;
uli i, j;
unsigned long int x, y;
```
| Yes they are. The C programming language contains various discrepancies but this question is relatively simple. The terms "uli" and "unsigned long int" are the exact same thing.
|
I want to scan a file for 001.120 float or 220.550 or 123.125 etc., and floats with three digits, and three decimal digits, how do I do this, like is it %03.3f?
| No, it's just%f. It doesn't need to know the specifics because it can deduce them.
|
I was debugging my syscall and I found that dmesg prints so many lines which made me couldn't see all of myprintk.
Is it possible toprintkto a file?
| Usetrace_printk()instead ofprintk()
And your prints should be in ftrace buffers.
```
$ cd /sys/kernel/debug/tracing
$ cat trace
```
Referhttp://june-takeout.blogspot.dk/2010/01/debugging-kernel-using-ftrace-part-1.htmland see section "Using trace_printk()"
|
I was debugging my syscall and I found that dmesg prints so many lines which made me couldn't see all of myprintk.
Is it possible toprintkto a file?
| Usetrace_printk()instead ofprintk()
And your prints should be in ftrace buffers.
```
$ cd /sys/kernel/debug/tracing
$ cat trace
```
Referhttp://june-takeout.blogspot.dk/2010/01/debugging-kernel-using-ftrace-part-1.htmland see section "Using trace_printk()"
|
I am studying pointers for c and this placement of*really really confuses me.
I understand whatint *ais..
But what is
```
a = malloc(n * sizeof(int) )
```
I understand what above is doing but why is*in the middle of by itself?? This part really confuse me so please someone can explain to me?
| unary*means dereferencing and binary*means multiplication.
|
I am studying pointers for c and this placement of*really really confuses me.
I understand whatint *ais..
But what is
```
a = malloc(n * sizeof(int) )
```
I understand what above is doing but why is*in the middle of by itself?? This part really confuse me so please someone can explain to me?
| unary*means dereferencing and binary*means multiplication.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.