question
stringlengths
24
425
answer
stringlengths
5
405
Given a list of URLs, such as a/b/e/f, b/c/d/k/g, s/e/c/d, how to match an input URL to the one in the list, for example, an input c/d should be matched to s/e/c/d, not b/c/d/k/g
Why not b/c/d/k/g? Are the "Url"s simply strings? If so simply search it using strstr or one of its derivative (wcsstr, _mbsstr, _mbsstr_l).
Whenever any question is asked, and a reference text is needed, I never seeMSDN C++ Language Referencebeing referred. I was browsing through it and I personally feel that it is extremely well written. Is there some specific reason it is not used as often as a standard? Is it because it contains some VC++ specific features?
I believe it's because those of us referencing a reference reference the actual standard itself.
Given a list of URLs, such as a/b/e/f, b/c/d/k/g, s/e/c/d, how to match an input URL to the one in the list, for example, an input c/d should be matched to s/e/c/d, not b/c/d/k/g
Why not b/c/d/k/g? Are the "Url"s simply strings? If so simply search it using strstr or one of its derivative (wcsstr, _mbsstr, _mbsstr_l).
Given a list of URLs, such as a/b/e/f, b/c/d/k/g, s/e/c/d, how to match an input URL to the one in the list, for example, an input c/d should be matched to s/e/c/d, not b/c/d/k/g
Why not b/c/d/k/g? Are the "Url"s simply strings? If so simply search it using strstr or one of its derivative (wcsstr, _mbsstr, _mbsstr_l).
if an array of size n has only 3 values 0 ,1 and 2 (repeated any number of times) what is the best way to sort them. best indicates complexity. consider space and time complexity both
Count the occurences of each number and afterward fill the array with the correct counts, this isO(n)
I get an "undefined reference to 'typeof'"-error compiling and linking this: ``` #include <stdio.h> #include <stdlib.h> #include <stddef.h> int main() { typeof(5); return 0; } ``` Version of gcc is 4.3.3, command line is "gcc.exe -std=c99 1.c -o 1.exe".
By passing the option-std=c99to GCC you've asked it to compile according to the C99 standard, which doesn't support thetypeofkeyword. You may want to use-std=gnu99instead.
I need to work with a binary number. I tried writing: ``` const char x = 00010000; ``` But it didn't work. I know that I can use a hexadecimal number that has the same value as00010000, but I want to know if there is a type in C++ for binary numbers, and if there isn't, is there another solution for my problem?
If you are using GCC then you can usea GCC extension(which is included inthe C++14 standard) for this: ``` int x = 0b00010000; ```
Is there a C or C++ compatible library for reading and writing Java class files? I.e. something like Apache BCEL.
None that I'm aware of. However, you can call Java from C++. That will let you use BCEL from C++. If you're on one of gcj's supported platforms, you could try using it to compile BCEL to native code.
Can we have a nested function in C? What is the use of nested functions? If they exist in C does their implementation differ from compiler to compiler?
You cannot define a function within another function in standard C. You candeclarea function inside of a function, but it's not a nested function. gcc hasa language extension that allows nested functions. They are nonstandard, and as such are entirely compiler-dependent.
it is possible to display a picture on a window created by winmain i mean using GDI, i want do create a window that captures my whole screen i have done that by using GDI but unable to show an image on it how can i do so?
There arelotsofgoodtutorialson theweb.
String.h I found this while searching aboutstring.h. Where can I find the source code of functions defined instring.h? i.e [say] source code of ``` void *memcpy(void *, const void *, size_t); ``` Also I want to ask how to make your functions as an interface with its implmentation hidden from developer asstring.hfunctions has.
Here's memmovefrom Ohloh's code search.
Is there any way to find out the list of undefined symbols in windows static library compiled using VC6? I know the nm command will list all the symbols in linux and symbols with a tag of "U" can be identified as undefined. But, how to do it Windows?
You can use dumpbin.exe /symbols on the .lib file. That lists all the symbols used, the external ones have the "External" prefix.
I have two pointers, ``` char *str1; int *str2; ``` If I look at the size of both the pointers let’s assume ``` str1=4 bytes str2=4 bytes ``` str1++ will increment by 1 byte, but if str2++ it will increment 4 bytes. What is the concept behind this?
Simple, in the provided scenario: char is 1 byte longint (in your platform) is 4 bytes long The++operator increments the pointer by the size of the pointed type.
Quick c question: How to know the length of a char* foo[]? Thanks.
You can't. Not without knowing something about what is inside of the pointers, or storing that data ahead of time.
I'm implementing some code generators, i would like to know if there's any way in C, if a variable has already been declared ? I was trying to find something using the preprocessor but without any success... thanks.
C is strictly static, you can't "lookup" if a variable has already been declared. If you are creating a code generator, why not read lines of code and see what's been declared?
How do I inputDEADBEEFand outputDEADBEEFas four byte arrays?
``` void hexconvert( char *text, unsigned char bytes[] ) { int i; int temp; for( i = 0; i < 4; ++i ) { sscanf( text + 2 * i, "%2x", &temp ); bytes[i] = temp; } } ```
What does the last function argument mean in C language? Please, point to documentation where I can read about it. ``` void parse_options( int argc, char **argv, const OptionDef *options, void (* parse_arg_function)(const char*) ) ``` Thanks.
This is apointer to a functionthat takes aconst char*and returnsvoid. For more information, seehere.
Is Test Driven Development limited to OO? Or is it possible/useful to use it in conjunction with a procedural language? I have to start a greater project in C the next few weeks and I'm thinking of ways how to the developing.
TDD is adesignparadigm and as such is not tied to any specific programming paradigm. Simply put, you write a test for your code before writing your of code. Seethisslide deck about TDD with C (the Bowling Game Kata).
can every if...then...else statement be converted into an equivalent statement using only ?:
The code: ``` if ( flag ) { exit(1); } else { return 0; } ``` cannot be converted into: ``` flag ? exit(1) : return 0; ``` A better example- this would be inside a loop: ``` if ( flag ) { continue; } else { break; } ``` cannot be converted to: ``` flag ? continue : break; ```
As the title described, how to use c library function fgets in assembly language? Indeed, I want to know how to get the file pointer to stdin. Thanks for your reply.
you can write a simple program using fgets and compile it with gcc -S to see how to do it
How to reverse bitwise AND (&) in C? For example I have an operation in C like this: ``` ((unsigned int)ptr & 0xff000000)) ``` The result is0xbf000000. What I need at this moment is how to reverse the above, i.e. determineptrby using the result from the operation and of course0xff000000. Is there any simple way to implement this in C?
Bitwise&can't be reversed: ``` 0 & 1 = 0 0 & 0 = 0 ```
As the title described, how to use c library function fgets in assembly language? Indeed, I want to know how to get the file pointer to stdin. Thanks for your reply.
you can write a simple program using fgets and compile it with gcc -S to see how to do it
How to reverse bitwise AND (&) in C? For example I have an operation in C like this: ``` ((unsigned int)ptr & 0xff000000)) ``` The result is0xbf000000. What I need at this moment is how to reverse the above, i.e. determineptrby using the result from the operation and of course0xff000000. Is there any simple way to implement this in C?
Bitwise&can't be reversed: ``` 0 & 1 = 0 0 & 0 = 0 ```
I have a static C library (saymylib.a) and I was wondering if it's possible to find out what functions are implemented inside that file. I don't have a corresponding header file. what I need is like the equivalent ofjavapfor Java.
On Windows you can usedumpbin. On Linux and friends you can usenm.
after calling JetSetColumns() and then JetUpdate() how do I retrieve the values for any freshly assigned autoincrement values?
when you call JetUpdate() the third arg is a 'bookmark'. Once JetUpdate() completes you can seek to the bookmark using JetGotoBookmark() -http://msdn.microsoft.com/en-us/library/ms683554(EXCHG.10).aspx
Is size_t only in C++ standard or C standard as well? I cannot find a C header in the "/usr/include" tree that defines size_t. If it is not in the C std, is GCC just doing some magic to make things work? Thanks, Chenz
size_tis defined in both<stddef.h>and<stdlib.h>
I have a static C library (saymylib.a) and I was wondering if it's possible to find out what functions are implemented inside that file. I don't have a corresponding header file. what I need is like the equivalent ofjavapfor Java.
On Windows you can usedumpbin. On Linux and friends you can usenm.
``` char a[]="HELLO"; char *p="HELLO"; ``` willa[2]andp[2]fetch the same character?
What they will fetch is a char-sized chunk of memory located 2 char-sized steps (2 bytes here) after the beginning of each, or after the address in memory to which each var points. This happens to be 'L' i the example, but this is not the same address in memory. So yes, in the example given, they will fetch the same character.
How can I use zlib library to decompress a PNG file? I need to read a PNG file using a C under gcc compiler.
Why not uselibpng? The PNG file format is fairly simple, but there are many different possible variations and encoding methods and it can be fairly tedious to ensure you cover all of the cases. Something like libpng handles all the conversion and stuff for you automatically.
I want to modify the registry of window OS using c. kindly guide me or give me some hints so that I can make it possible.
You need to use Win32 APIs such asRegOpenKeyExfor doing this.
I want to modify the registry of window OS using c. kindly guide me or give me some hints so that I can make it possible.
You need to use Win32 APIs such asRegOpenKeyExfor doing this.
I am using CreateProcess function for creating the process, is there any option to get the current state of the process (running or not). Kindly guide me how can I make it possible.
UseOpenProcessfunction with that dwProcessId if it returns NULL Process is not running otherwise it will return handle to that process
i want to use the dos commands in my ansi-c program how can i do so what type of lib or .h i will need for it if it is possible ?
Use thesystemfunction, fromstdlib.h. Here is Wikipedia documentation of this function
Is there any option to call msi file from CreateProcess in c language in window OS.
The WindowsShellExecute functionwill open a file of a registered type with the correct application, which I think is what you are asking about.
Does anyone have code snippet for Triple DES algorithm in C ? Thanks
OpenSSLis written in C and provides a 3DES algorithm. It may not be as simple as some of the other suggestions, but it may be worth checking out.
How can I encode a file using sha256 and c/c++ ??? Thanks!
SHA 256stands for Secure Hash Algorithm ! It will only produce the hash of a given file . You can't retrieve the original file from a given hash otherwise hash functions are useless. If you want to do encryption/decryptionAESwould be a better solution. Everything you need is inOpenSSL.
``` #include<conio.h> #include<stdio.h> #define abc 7 int main() { int abc=1; printf("%d",abc); getch(); return 0; } ``` why this program is giving compile time error
You're assigning7=1which is invalid. Since you've definedabcto be 7, the preprocessor translates the line: ``` int abc=1; ``` to: ``` int 7=1; ``` Which is a syntax error in C (mygccsayssyntax error before numeric constant).
What's the difference of pData1 and pData2, which are build as follows: pData1 = (int*) calloc (i,sizeof(int));int * pData2 = (int*) calloc (i,sizeof(int));
Without any more information, it would appear the only difference is thatpData2is local to the allocation since it is declared as anint *.pData1is not declared so it would have to have a larger (global?) scope and be defined elsewhere.
I need to create a simple application that doesn't work after 30 days. How can I do that ? Is there a way to use Trial program after 30 days ?
There is always a way to use a trial program after 30 days.
``` #include<conio.h> #include<stdio.h> #define abc 7 int main() { int abc=1; printf("%d",abc); getch(); return 0; } ``` why this program is giving compile time error
You're assigning7=1which is invalid. Since you've definedabcto be 7, the preprocessor translates the line: ``` int abc=1; ``` to: ``` int 7=1; ``` Which is a syntax error in C (mygccsayssyntax error before numeric constant).
What's the difference of pData1 and pData2, which are build as follows: pData1 = (int*) calloc (i,sizeof(int));int * pData2 = (int*) calloc (i,sizeof(int));
Without any more information, it would appear the only difference is thatpData2is local to the allocation since it is declared as anint *.pData1is not declared so it would have to have a larger (global?) scope and be defined elsewhere.
I need to create a simple application that doesn't work after 30 days. How can I do that ? Is there a way to use Trial program after 30 days ?
There is always a way to use a trial program after 30 days.
I'm reading a bit of C code in an OS kernel that says ``` x & ~(uint32_t)CST_IEc; ``` What does the~()mean? It's a tilde followed by parentheses!
~()is actually two things: (uint32_t)is a cast.~is a bitwise complement operator.
I have a time in the future when I want a notification to occur and need to know if::CeSetUserNotificationExexpects UTC or local time in thestStartTimefield of theCE_NOTIFICATION_TRIGGERstructure if thedwTypefield is set toCNT_TIME?
After actually testing::CeSetUserNotificationExwith both UTC and local time input, I'm in the position of answering my own question: ::CeSetUserNotificationExwants local time.
i have a task to transfer text files from one pc to another using FTP , i need some directions how can it be done in ansi c as its user requirement to do it in ansi c windows palte form so windows libs may also be used .............. looking for help!
what platform do you use? On windows, have a look at the winsock library.on Unix, examine sockets library Can you use an external library? thenthis heremight come in handy.
I'm writing a Linux kernel module, and I'd like to allocate an executable page. Plainkmalloc()returns a pointer within a non-executable page, and I get a kernel panic when executing code there. It has to work on Ubuntu Karmic x86, 2.6.31-20-generic-pae.
``` #include <linux/vmalloc.h> #include <asm/pgtype_types.h> ... char *p = __vmalloc(byte_size, GFP_KERNEL, PAGE_KERNEL_EXEC); ... if (p != NULL) vfree(p); ```
I'm just wondering exactly what factors affect how quickly createthread executes, and how long it has to live to make it "worth it". CONTEXT: Where in my game's loops should I spawn threads?
The main game loop is not the place to spawn worker threads. The main game loop should be as free of clutter as possible. Worker threads should be spawned during program startup and then used as need by the main game loop. Look into thread pooling techniques.
I'm reading a bit of C code in an OS kernel that says ``` x & ~(uint32_t)CST_IEc; ``` What does the~()mean? It's a tilde followed by parentheses!
~()is actually two things: (uint32_t)is a cast.~is a bitwise complement operator.
I have a time in the future when I want a notification to occur and need to know if::CeSetUserNotificationExexpects UTC or local time in thestStartTimefield of theCE_NOTIFICATION_TRIGGERstructure if thedwTypefield is set toCNT_TIME?
After actually testing::CeSetUserNotificationExwith both UTC and local time input, I'm in the position of answering my own question: ::CeSetUserNotificationExwants local time.
i have a task to transfer text files from one pc to another using FTP , i need some directions how can it be done in ansi c as its user requirement to do it in ansi c windows palte form so windows libs may also be used .............. looking for help!
what platform do you use? On windows, have a look at the winsock library.on Unix, examine sockets library Can you use an external library? thenthis heremight come in handy.
I'm writing a Linux kernel module, and I'd like to allocate an executable page. Plainkmalloc()returns a pointer within a non-executable page, and I get a kernel panic when executing code there. It has to work on Ubuntu Karmic x86, 2.6.31-20-generic-pae.
``` #include <linux/vmalloc.h> #include <asm/pgtype_types.h> ... char *p = __vmalloc(byte_size, GFP_KERNEL, PAGE_KERNEL_EXEC); ... if (p != NULL) vfree(p); ```
I know you can print withprintf()andputs(). I can also see thatprintf()allows you to interpolate variables and do formatting. Isputs()merely a primitive version ofprintf(). Should it be used for every possibleprintf()without string interpolation?
putsis simpler thanprintfbut be aware that the former automatically appends a newline. If that's not what you want, you canfputsyour string to stdout or useprintf.
I want to transfer files across the network using C or C++. What topics should I look up? How can I do this?
You should start by choosing a protocol.HTTPSandSFTPare both good choices, but there are obviously others. Once you have that straight, you can look up choices for client and server libraries.
I cannot understand why this throwsundefined reference to `floor'": ``` double curr_time = (double)time(NULL); return floor(curr_time); ``` Hasn't it been casted to double, which is whatfloorreceives?
You possibly have run in to the infamous-lmproblem: Compile as: ``` gcc yourfile.c -o out -lm ``` This is CFAQ 14.3item as well.
I have written the majority of my project in C++. However there are several "filters" or folders which need to be compiled as C and linked to the project. How can I configure this within VStudio? Thanks.
You can change the Language property by right-clicking on the individual files and settingConfiguration Properties > C/C++ > Advanced > CompileAs ToCompile As C (/TC). No such facility for the filter are present though.
What was the munch library (or program?) fromcfrontpackage? What is was used for?
Munch was used to scan the nm output and look for static constructors/destructors. See thecode(with comments) atSoftwarePreservation.com.
This one is quite obviously a function pointer : ``` typedef int (* foobar) (int a, unsigned char *b, unsigned int c); ``` But what does this one do? ``` typedef int (foobar *) (int a, unsigned char *b, unsigned int c); ```
It produces a compiler error, at least in GCC 4.3: ``` foo.cpp:1: error: expected `)' before '*' token ```
Are there any stdin input length limitations (in amount of input or input speed)?
No.stdinhas no limits on length or speed. If you want to get gigabytes of data viastdinyou can, and your computer will supply it as fast as it can.
How do I perform an unsigned right shift (>>> in Java) in C/C++?
In C, to get an unsigned shift, you just do a shift on an unsigned type. ``` unsigned int result = (unsigned int)valueToBeShifted >> shiftAmount; ``` Note that there is no guarantee that>>on a signed type gives you a signed shift in C -- this is implementation defined behavior. Most common implementations produce a signed shift if the type is signed, however.
Is there a canonical colorspace conversion library? I can't find any pre-existing solutions. Is CIELab conversion is too obscure?
It is not obscure, I have done it myself recently from RGB to CIELAB. Look at the source of OpenCV there is a lot of color convesrion functions. File is: ../src/cv/cvcolor.cpp Have a look at the function icvBGRx2Lab_32f_CnC3R for example. This is probably what are you looking for.
I have a c program which I can launch at command prompt. Is it possible for me the lunch this application in my another c program? If yes, how? All the google result shows me how to do that using pthread? Will that work? I suspect that I need a new process for my c program. Thank you.
It is system specific, but there is commonlyexecveor something like that and there is alwayssystem("/path/to/program");that you can use that is also system specific
I have a string"14 22 33 48". I need to insert each of the values in the string into the respective location in the array: ``` int matrix[5]; ``` so that ``` matrix[0] = 14; matrix[1] = 22; matrix[2] = 33; matrix[3] = 48; ``` How do I do this?
You can usesscanf: ``` sscanf(val, "%d %d %d %d", &matrix[0], &matrix[1], &matrix[2], &matrix[3] ); ```
Can we use thewmain()function with Unix compilers or it'll work only on/for Windows?
The only standard signatures formainare: ``` int main(void); int main(int argc, char *argv[]); ``` However, a freestanding implementation can provide extensions/allow other signatures. But those are not guranteed to be portable.wmainlooks like a Windows/VS thing. There's not much chance this will work on a *nix/GNU GCC.
Is there an option to enable the drop down menu that appears when coding in other languages such as html/vb/ect for C in Visual Studio?
You can press Ctrl+Space to produce this menu. For example, after typing "pri", press Ctrl+Space, if you're looking for printf().
Using my c++ program how can I find out what group the current user running my program belongs to? So my program need to figure out a couple of things : The current username of the userThe group the user belongs to How can do the above 2 using c++ on a RedHat / Linux machine?
Withgetuid(2)andgetgid(2). Seecredentials(7)for more information. Usegetpwuid(3)andgetgrgid(3)for the names.
I have written the majority of my project in C++. However there are several "filters" or folders which need to be compiled as C and linked to the project. How can I configure this within VStudio? Thanks.
You can change the Language property by right-clicking on the individual files and settingConfiguration Properties > C/C++ > Advanced > CompileAs ToCompile As C (/TC). No such facility for the filter are present though.
What was the munch library (or program?) fromcfrontpackage? What is was used for?
Munch was used to scan the nm output and look for static constructors/destructors. See thecode(with comments) atSoftwarePreservation.com.
If I want to make an OS X program as self-contained as possible to ease installation, what dynamic libraries can I expect everyone or most people to have? If I know that, I won't have to compile static libraries for everything.
All functions listed inSingle UNIX Specification V3are available. Obviously, all Cocoa frameworks are available as well. The exact contents depend on OS version.
I stumbled across this C code today. Can anyone tell me what the 'where' keyword means: ``` *y = sy + exit->y + (where * (entry->y + esy - exit->y)); ``` Edit: Ah.. my bad. It is just a variable name. VC++ highlighted it as though it was a keyword though.
It's just a variable name... (Thewhereis highlighted as blue here only because C# supportswhereas a keyword in LINQ.)
I was curious to know how I can round a number to the nearest whole number. For instance, if I had: ``` int a = 59 / 4; ``` which would be 14.75 if calculated in floating point; how can I store the result as 15 in "a"?
The standard idiom for integer rounding up is: ``` int a = (59 + (4 - 1)) / 4; ``` You add the divisor minus one to the dividend.
This is my warning. ``` implicit declaration of function 'exit' ``` How i can remove it. i am using linux & gcc compiler.
Add: ``` #include <stdlib.h> ``` to the top of your program.
I have a readline-based application with a prompt, however I'd like it to not show when the input is not interactive. ``` $ ./readline-app < command-list > result $ cat result prompt> Output line 1 prompt> Output line 2 prompt> $ ``` How can I make the prompt to not print in this case?
Useisatty(3)to determine whether stdin and/or stdout are connected to a tty.
If 2 or more threads are waiting on an event, doesSetEventunblock one or all of them(Or some of them) ?
It depends on if it is a manual or auto-reset event. If it is a manual reset, then multiple threads can be released until it is reset. If it is auto-reset, then only one will be signaled.
This is my warning. ``` implicit declaration of function 'exit' ``` How i can remove it. i am using linux & gcc compiler.
Add: ``` #include <stdlib.h> ``` to the top of your program.
I have a readline-based application with a prompt, however I'd like it to not show when the input is not interactive. ``` $ ./readline-app < command-list > result $ cat result prompt> Output line 1 prompt> Output line 2 prompt> $ ``` How can I make the prompt to not print in this case?
Useisatty(3)to determine whether stdin and/or stdout are connected to a tty.
If 2 or more threads are waiting on an event, doesSetEventunblock one or all of them(Or some of them) ?
It depends on if it is a manual or auto-reset event. If it is a manual reset, then multiple threads can be released until it is reset. If it is auto-reset, then only one will be signaled.
This is my warning. ``` implicit declaration of function 'exit' ``` How i can remove it. i am using linux & gcc compiler.
Add: ``` #include <stdlib.h> ``` to the top of your program.
I have a readline-based application with a prompt, however I'd like it to not show when the input is not interactive. ``` $ ./readline-app < command-list > result $ cat result prompt> Output line 1 prompt> Output line 2 prompt> $ ``` How can I make the prompt to not print in this case?
Useisatty(3)to determine whether stdin and/or stdout are connected to a tty.
If 2 or more threads are waiting on an event, doesSetEventunblock one or all of them(Or some of them) ?
It depends on if it is a manual or auto-reset event. If it is a manual reset, then multiple threads can be released until it is reset. If it is auto-reset, then only one will be signaled.
Does your editor/ide highlight thata++;in this C/C++ code as part of a comment? ``` int a=1; //some comment \ a++; printf("%d\n",a); ``` And what about this? ``` int a=1; //some comment ??/ a++; printf("%d\n",a); ```
VC6, Visual Studio 2003, 2005, 2008, 2010 all do.
There's a #pragma command to link in a library from the source file rather than from the project settings. I just can't seem to remember it. Can anyone here remind me? Thanks
``` #pragma comment(lib, "library") ``` http://msdn.microsoft.com/en-us/library/7f0aews7(VS.80).aspx
I am serializing some C structure to string and than deserializing it withstrtok(). But, unfortunately,strtok()don't detect empty fields (eg 1:2::4). Is there any alternative function?
On linux there'sstrsep. The strsep() function was introduced as a replacement for strtok(), since the latter cannot handle empty fields. However, strtok() conforms to C89/C99 and hence is more portable.
Suppose I do a ``` double d = 234.5; ``` I want to see the memory contents ofd[the whole 8 bytes] How do I do that?
``` unsigned char *p = (unsigned char *)&d; int i; for (i = 0; i < sizeof d; i++) printf("%02x ", p[i]); ```
If we have ``` char *val = someString; ``` and then say ``` if(val){ .... } ``` what is theifstatement actually checking here?
Yourifstatement is equivalent to: ``` if (val != NULL) { ... ``` The comp.lang.c FAQ containsthis question and answerwhich goes into a bit more detail why this is true.
How to design an algorithm to convert a UTF-8 string to a unicode string?
The commenters are right. Readhttp://en.wikipedia.org/wiki/Unicodeand rephrase question. UTF-8 is a ''represenation'' of unicode. You probably want to recode to some other interpretation, maybe Java Strings or Microsoft Visual C++ multibyte chracter strings...
Does your editor/ide highlight thata++;in this C/C++ code as part of a comment? ``` int a=1; //some comment \ a++; printf("%d\n",a); ``` And what about this? ``` int a=1; //some comment ??/ a++; printf("%d\n",a); ```
VC6, Visual Studio 2003, 2005, 2008, 2010 all do.
When I copy code from another file, the formatting is messed up, like this: ``` fun() { for(...) { for(...) { if(...) { } } } } ``` How can I autoformat this code in vim?
Try the following keystrokes: ``` gg=G ``` Explanation:gggoes to the top of the file,=is a command to fix the indentation andGtells it to perform the operation to the end of the file.
I am not able to understand the following statement from the file limits.h. What is the use of this statement and what does it accomplishes? ``` /* If we are not using GNU CC we have to define all the symbols ourself. Otherwise use gcc's definitions (see below). */ #if !defined __GNUC__ || __GNUC__ < 2 ```
It checks if your program is compiled by some other compiler than GCC, or some very old GCC version.
What happens inside memory if we try to free a pointer which is pointing to NULL? Is that ever valid? Why does it not show any warning/error messages?
From C99 section 7.20.3.2 : Thefreefunction Synopsis ``` 1 #include <stdlib.h> void free(void *ptr); ``` Description 2 The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation.If ptr is a null pointer, no action occurs.
I'm working on a client-server application written in C. I want to broadcast a message to all the machines available on the local network. How can I do that using the usual socket system calls in C?
Just send the message to the broadcast address of your subnet, which for 192.168.0.0/24 is 192.168.0.255, or just broadcast to 255.255.255.255.
Is there any way to get the source code by using object file in C? For example I have a source code simple.c cc simple.c Now I have a.out (object file). By this a.out whether can I get the source? code of simple.c
No. Turning a cow into hamburger is fairly easy. Turning hamburger into a living cow, somewhat more difficult.
If 2 file descriptors were dupped to the same file (i.e.506andSTDOUT), will invokingclose(506)cleanup the object associated by both and renderSTDOUTunusable? Or does kernel implement reference counting for its files?
The kernel implements reference counting, so the kernel object is not closed until all the file handles pointing to it are closed.
How do I convert a binary string like "010011101" to an int, and how do I convert an int, like 5, to a string "101" in C?
Thestrtolfunction in the standard library takes a "base" parameter, which in this case would be 2. ``` int fromBinary(const char *s) { return (int) strtol(s, NULL, 2); } ``` (first C code I've written in about 8 years :-)
How to get the absolute path for a given relative path programmatically in Linux? Incase of Windows we have the_fullpath()API. In other words, I mean what is analogous API to_fullpathof Windows in Linux?
As Paul mentioned, userealpath(). Please note though, that since many file systems in Linux supporthard links, any given directory can have a number of different absolute paths.
this is very basic...but please help me if anybody know about this... Can an array be called as a const pointer?
Do you mean "can array be used where a const pointer is expected"? In that case, yes: ``` void f(const int* p) { ... } int ar[10]; f(ar); // this works, array is essentially a pointer ```
How to install new units (i mean includes ex: stdio.h) for my C, and how to download more?
put the header file on the same folder as your main source file, ex: /folder /-->main.c-->function.h -->function.c then on main.c include it by writing : ``` #include "function.h" ``` the quotes are used to include a header file that's on the same folder as your c source file u can also write your own and include them like above.
I have been using ! (logical negation) in C and in other languages, I am curious does anyone know how to make your own ! function? or have a creative way of making one?
``` int my_negate(int x) { return x == 0 ? 1 : 0; } ```
I have to write some code to retrieve network packets from an Ethernet card in Windows using ANSI C. Can anybody tell me where to get started?
The easiest way to do this is to use the PCAP library, there's a version for Windowshereand a basic examplehere
In the execve() man page it is said that argv is a list of string arguments which is ended by a null pointer. But what is the NP for? I mean, the number of arguments is stored in argc, so what's the point of the null pointer?
execvedoes not takeargcas a parameter, it manually counts the arguments you give asargvto determineargcitself. This is for compatibility withexeclwhich takes a variable number of arguments instead of anargvarray.
I get a segmentation fault afterfreeing a certain pointer: ``` free(studentDB->name); ``` I can get its value without any errors or warnings: ``` printf("[DBG] studentDB->name: %s\n", studentDB->name); ``` However, as I said, the program crashes when I try to free it. What are the most common causes for afreecommand leading to a segmentation fault?
If you didn'tmalloc()it, you can'tfree()it. Where doesstudentDB->namecome from?