question
stringlengths
25
894
answer
stringlengths
4
863
Is the following code legal according to C99? ``` ... for(....) { int x = 4; ... } ... ``` You can assume that before line 3 the variable x was never declared. C99 (PDF) Until now I have only found the following, but I dont think that this is enough: A block allows a set of declarations and statements to be grouped into one syntactic unit. The initializers of objects that have automatic storage duration, and the variable length array declarators of ordinary identifiers with block scope, are evaluated and the values are stored in the objects (including storing an indeterminate value in objects without an initializer) each time the declaration is reached in the order of execution, as if it were a statement, and within each declaration in the order that declarators appear. From page 145 of that PDF.
This is legal in both C99 and C89. Look at 6.8.2 , which defines compound statement
I'm looking to add SSL support to a client application written in C/C++ that I'm developing (it is multi-platform, designed to work on Linux and Windows). OpenSSL documentation is pretty poor, but I found a good working tutorialhere. To my knowledge, however, there is no way to bind the socket to a local IP address using the BIO handle. There is a rather old ticket on the OpenSSL bug tracker that addresses this, but I think that no solution has been found (one comment suggests usingBIO_get_accept_socket, but that will obviously not work for my client application). Any suggestions, solutions, or alternative libraries that offer this kind of functionality?
You can just create, bind and connect the socket yourself, then useSSL_set_fdto pass the socket to OpenSSL, followed bySSL_connectto set up the session.
I am expecting that both following vectors have the same representation in RAM: ``` char a_var[] = "XXX\x00"; char *p_var = "XXX"; ``` But strange, a call to a library function of typef(char argument[])crushs the running application if I call it usingf(p_var). But usingf(a_var)is Ok! Why?
The first creates an array of char containing the string. The contents of the array can be modified. The second creates a character pointer which points to a string literal. String literals cannot be modified.
say you have a source file namedsum.cthat looks like this: ``` #include "sum.h" int sum(int x, int y) { return x+y; } ``` What's the point of including method's header in it's own definition file? Aren't you supposed to include it only in source files that call thesumfunction?
This way you avoid possible problems if the definitions in the header and in the source files differ.
I'm working with sqlite3 for the first time, and cannot get it to properly check a file before it opens it. So far, sqlite always returns OK on any file. Also, the file name is a variable returned from the GTK file chooser. It returns an absolute path, I'm guessing this is not a problem. Thanks for any help. This is a snippet of the code: ``` int rc; char *filename; sqlite3 *db; filename = gtk_file_chooser_get_filename(etc.); if(SQLITE_OK == rc = sqlite3_open(filename,&db)) { etc. } ```
sqlite3_opendoesn't actually read the file until the first non-pragmastatement is prepared. sqlite3_open_v2provides other options.
I first encountered files ended with .tc and .th in a C library (http://www.vlfeat.org/api/files.html, only .tc files are listed there. To see .th file, one has to download its source codehttp://www.vlfeat.org/download/vlfeat-0.9.5-bin.tar.gz. They are under vl directory.). Just wonder what do they mean and relation with normal .h and .c files ? Thanks and regards!
They use those as a template and the files are not compiled directly, but by being #included in the corresponding .c or .h file after setting #defines that affect the final result. One example is what happens inmathop_sse2.c. They include the same mathop_sse2.tc twice, but the first time FLT is defined as VL_TYPE_DOUBLE and the second time is VL_TYPE_FLOAT. That way they avoid duplicating the exact same code for different types.
I am wondering how I can both import and export bitmaps to and from C. I'm basically lost on where to begin.
A bitmap in memory looks similar to this: ``` struct Image { int width; int height; char *data; // 1 byte per channel & only 1 channel == grayscale } struct Image theImage; theImage.width = 100; theImage.height = 100; theImage.data = malloc(sizeof(char) * theImage.width * theImage.height); ``` As to importing and exporting, there are some really simple file formats out there, take a look atBMP. For more complex formats you best use an already available library. Most frameworks already have load/save methods for the most common fileformats. You could take a look atSDLif you're looking for a lightweight library.
Every time I read about the "inline" declaration in C it is mentioned that it isonly a hintto the compiler (i.e. it does not have to obey it). Is there any benefit to adding it then, or should I just rely on the compiler knowing better than me?
There are two reasons to use theinlinekeyword. One is an optimization hint, and you can safely ignore it; your compiler is like to ignore it too. The other reason is to allow a function to exist in multiple translation units, and that usage is strictly necessary. If you put a function into a .h header file for example, you'd better declare it inline.
In Microsoft visual c++ compiler, you can specify linker options using ``` #pragma comment(lib, "MSVCRT") //links with the MVCRT library ``` see this page I find this feature very useful because linker errors are common and i want to just placeall the linker options in my source codeinstead of specifying them to the compiler. question:Is there a way to do this ingcc(ordev-cpporcodeblockside)? Thanks in advance.
GCC doesn't support this becauseto link correctly, the order in which you link your objects matters. See alsomy answerand others in the question"#pragma comment(lib, “xxx.lib”) equivalent under Linux?"
What are the best tools for profiling C/C++ applications on *nix? (I'm hoping to profile a server that is a mix of (blocking) file IO, epoll for network and fork()/execv() for some heavy lifting; but general help and more general tools are all also appreciated.) Can you get the big system picture of RAM, CPU, network and disk all in one overview, and drill into it? There's been a lot of talk on thekernel listsabout things likeperf timechart, but I haven't found anything turning up in Ubuntu yet.
I recommend taking stackshots, for whichpstackis useful. Here's some more information: Comments ongprof.How stackshots work.A blow-by-blow example.A very short explanation. If you want to spend money,Zoomlooks like a pretty good tool.
I would like someone to give a working example ofSetWindowPoson how to make a window "topmost" (be on top and stay there) using either C/C++/C#. Thanks in advance!
C/C++: ``` // This doesn't size or move the window, just makes it top-most. SetWindowPos( hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE ); ```
Is the there a way to forceanotherwindow to be on top?Notthe application's window, butanotherone, already running on the system. (Windows, C/C++/C#)
``` SetWindowPos(that_window_handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); ``` BringWindowToTopmoves the window to the top of the Z-order (for now) but doesnotmake it a topmost window.
So if I have an integer that is 32 bits. The first 28 bits (from left) are to store the size of a memory chunk, the next two are 0s and the last two are: to store the if it is the last node and thento store if it is used or not (respectively). What I am trying to do is to know how to turn the flag on and off on the isLast operation and the isUsed operation. (If we consider only the last two integers (again, we start left) then 01 would be not last and is used for example, one more example is 11 is last and is used, 00 is not last and not used.) I want to be able to turn the flags on and off in an easy way. I know I will need to use bit operations including & and | but I am not sure how. Please ask me questions if you need more description of the problem.
``` //turn on isUsed data |= 1; //turn off isUsed data &= ~1; //turn on notLast data &= ~2; //turn off notLast data |= 2; ```
As I wrote an app with the functionmain(int argc, char * argv[]) When i start up the application, I wanna send some parameters like (assume the app name isffdisk) ``` >ffdisk -f 111123123131321312312123 1 ``` You see the third param maybe an int64 param in my original intention...But in function main, argv[2] is string pointer, so how can convert this string into a int64 variable? Thanx...I am always stuck at messy problems....
You can use _atoi64, _strtoi64 functions in stdlib.h
I am having trouble to write the binary algorithm in C/C++.My question is like that: Apply binary algorithm to search for a number from 1 to 100 in a number guessing game.The user will respond with 'y' for a correct guess, 'h' if the guess is too high or 'l' if the guess is too low. I don't have any idea to apply it. Can someone just give me an example of the code.
Detailed instructionshereplus various implementations. ``` int low = 1; int high = 100; while (low <= high) { int mid = (low + high) / 2; char answer = evaluateGuess(mid); //return l, h or y; if ('y'==answer) { return mid; } if ('l' == answer) { low = mid + 1; } else { high = mid - 1; } } // If you get here the human player lied and the answer wasn't in [1..100] ```
In preprocessors, we can have switch between macros like, ``` #define BUFF(n) BUFF_##n ``` So, BUFF(1) would get replaced by BUFF_1, BUFF(2) would get replaced by BUFF_2 and song Can this be applicable to C variables? i.e., choosing between similar variables dynamically. I understand it is a weird situation and can be handled using arrays or any other constructs.. but the situation demands me such situation.. could u plz help with this.. thanks in advance
Yes, you can use that macro to applyBUFF_to just anything. The preprocessor will expand macros and then the compiler will try to compile the result. The latter might fail, since if you useBUFF(+)you getBUFF_+and that's not a valid variable name.
Considering the basic data types like char, int, float, double etc..in any standard language C/C++, Java etc Is there anything like.."operating on integers are faster than operating on characters".. by operating I mean assignment, arithmetic op/ comparison etc. Are data types slower than one another?
For almost anything you're doing this has almost no effect, butpurely for informational purposes, it is usually fastest to work with data types whose size is machine word size (i.e. 32 bits on x86 and 64-bits on amd64). Additionally, SSE/MMX instructions give you benefits as well if you can group these and work on them at the same time
If I call a function like myObj.setType("fluid"); many times in a program, how many copies of the literal "fluid" are saved in memory? Can the compiler recognize that this literal is already defined and just reference it again?
This has nothing to do with C++(the language). Instead, it is an "optimization" that a compiler can do. So, the answeryesandno, depending on the compiler/platform you are using. @David This is from thelatest draft of the language: § 2.14.6 (page 28)Whether all string literals are distinct (that is, are stored in non overlapping objects) isimplementationdefined. The effect of attempting to modify a string literal isundefined. The emphasis is mine. In other words, string literals in C++ areimmutablebecause modifying a string literal is undefined behavior. So, the compiler is free, to eliminate redundant copies. BTW, I am talking about C++ only ;)
I have a plist that is processed with a precompiled header file and in it there is the "variable" VERSION_STRING used a few times in such fields as CFBundleGetInfoString, ie the value for the key CFBundleGetInfoString is: MyProduct VERSION_STRING Copyright © 2009 MyCorp In MyHeader.h (which is the set as the Info.plist prefix header I would like to be able to build VERSION_STRING into the form MAJOR.MINOR.PATCH.BUILD where I have ``` #define MAJOR 1 #define MINOR 0 #define PATCH 0 #define BUILD 23 ``` For some reason I seem to be incapable of doing this. I might just be having one of those moments
Turns out there is actually an Apple Tech Note on this and a solution to the very problem I was having. So, for anyone that may come across this and is having the same problems I was check outTechnical Note TN2175: Preprocessing Info.plist files in Xcode Using the C Preprocessor
Does anybody know a library similar toboost::preprocessor(maybe not so advanced) that could be easily used/incorporated in plain C projects? Of course, the most (all ?) ofboost::preprocessoris usable when writing inCbut I would prefer a small library with only basic capabilities that doesn't depend on the monster like boost.
I suggest you use Boost.Preprocessor. Don't think i haven't read your question entirely. I suggest you use Boost.Preprocessor alone, not the whole boost package. In order to do that, you can use a small utility calledbcpwhich lets you extract the libraries that you need, in your case Boost.Preprocessor.
It looks like g_strncasecmp is deprecated, so I am looking for another function to do the same thing.
From the docs athttp://library.gnome.org/devel/glib/stable/glib-String-Utility-Functions.html#g-strncasecmp "There are therefore two replacement functions: g_ascii_strncasecmp(), which only works on ASCII and is not locale-sensitive, and g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8."
How does one deal with error return of a routine in C, when function calls go deep? Since C does not provide an exception throw mechanism, we have to check return values for each function. For example, the "a" routine may be called by "b", and "b" may called by many other routines, so if "a" returns an error, we then have to check it in "b" and all other routines calling "b". It can make the code complicated if "a" is a very basic routine. Is there any solution for such problem? Actually, here I want to get a quick return path if such kind error happens, so we only need to deal with this error in one place.
You can use setjmp() and longjmp() to simulate exceptions in C. http://en.wikipedia.org/wiki/Setjmp.h
I am more into C/C++. But many of my seniors here in college ask me to learn Java if I want to contribute to an open source project.. I'm in dilemma. what to do? Can't we do a design project in C/C++?
There are plenty of open source C and C++ projects - as well as loads in virtually any other language you can come up with. Of course it's never a bad idea to learn another language, but don't feel too constrained by "only" knowing C and C++. If you want to contribute toa specificopen source project which is written in Java, of course, that's a different matter... but if you're trying to find C and C++ open source projects, some of the major hosting sites support querying by project language, I believe. For example, you can look atGoogle Code C++ projectsandSourceForge projects tagged C++.
why can we do this in c? ``` int n; scanf("%d",&n); int a[n]; ``` I thought array is located memory during load time but seems like the above example works during runtime. Do I misunderstand any thing? can you guys help? Thanks,
I am no expert in C, but this could be avariable-length arrayas added byC99andsupported by GCC, for example. GCC allocates the memory for such array on stack, so that it gets automatically freed when you return from the function.
I creates program in c/c++ in turbo c . but i am facing problem is that i am not able to make it full screen in windows vista. please tell me how to do it?
Why are you still living in the Turbo era? Switch on to some better compiler. gcc and MSVC++ Professional Edition are better ones. On windows use IDEs like DevCpp or Code::Blocks, both support MinGw port of gcc.
Environment: I am using MS-VC++ 6.0, I include a group of header file with some data.The header files change often, so on every change I change the path setting and re-compilerA log file is generated based on the included header filesFor tracking of the header file from the log file, I wish to print the header file path inside the logQuestion-1:Is getting the header file path from inside the program possible?Question-2:I am using VC++, but if it is possible in gcc, then I can easily port, so, please let me know, if it is possible in any other environment like gcc
In VC++, compiling with the option/FCwill put the currently processed file'sentire pathin the__FILE__macro. That will account for your changes to the include path. Details here.
I have an array of floats where data are stored with varying decimal points so some are123.40000,123.45000,123.45600...now if i want to print these values in the string without the 0s in the end inprintf()so that they are123.4,123.45,123.456, without those 0s in the end. Is this possible? If so, how?
Use the %g formatter: ``` printf( "%g", 123.4000 ); ``` prints 123.4 Trailing zeros are removed, but unfortunately so is the trailing decimal point if the fractional part is zero. I don't know if there is actually any way of doing what you want directly using printf() - I think something like this is probably your best bet: ``` #include <stdio.h> #include <math.h> void print( FILE * f, double d ) { if ( d - floor(d) == 0.0 ) { fprintf( f, "%g.", d ); } else { fprintf( f, "%g", d ); } } int main() { print( stdout, 12.0 ); print( stdout, 12.300 ); } ```
As I wrote an app with the functionmain(int argc, char * argv[]) When i start up the application, I wanna send some parameters like (assume the app name isffdisk) ``` >ffdisk -f 111123123131321312312123 1 ``` You see the third param maybe an int64 param in my original intention...But in function main, argv[2] is string pointer, so how can convert this string into a int64 variable? Thanx...I am always stuck at messy problems....
You can use _atoi64, _strtoi64 functions in stdlib.h
I am having trouble to write the binary algorithm in C/C++.My question is like that: Apply binary algorithm to search for a number from 1 to 100 in a number guessing game.The user will respond with 'y' for a correct guess, 'h' if the guess is too high or 'l' if the guess is too low. I don't have any idea to apply it. Can someone just give me an example of the code.
Detailed instructionshereplus various implementations. ``` int low = 1; int high = 100; while (low <= high) { int mid = (low + high) / 2; char answer = evaluateGuess(mid); //return l, h or y; if ('y'==answer) { return mid; } if ('l' == answer) { low = mid + 1; } else { high = mid - 1; } } // If you get here the human player lied and the answer wasn't in [1..100] ```
In preprocessors, we can have switch between macros like, ``` #define BUFF(n) BUFF_##n ``` So, BUFF(1) would get replaced by BUFF_1, BUFF(2) would get replaced by BUFF_2 and song Can this be applicable to C variables? i.e., choosing between similar variables dynamically. I understand it is a weird situation and can be handled using arrays or any other constructs.. but the situation demands me such situation.. could u plz help with this.. thanks in advance
Yes, you can use that macro to applyBUFF_to just anything. The preprocessor will expand macros and then the compiler will try to compile the result. The latter might fail, since if you useBUFF(+)you getBUFF_+and that's not a valid variable name.
Considering the basic data types like char, int, float, double etc..in any standard language C/C++, Java etc Is there anything like.."operating on integers are faster than operating on characters".. by operating I mean assignment, arithmetic op/ comparison etc. Are data types slower than one another?
For almost anything you're doing this has almost no effect, butpurely for informational purposes, it is usually fastest to work with data types whose size is machine word size (i.e. 32 bits on x86 and 64-bits on amd64). Additionally, SSE/MMX instructions give you benefits as well if you can group these and work on them at the same time
If I call a function like myObj.setType("fluid"); many times in a program, how many copies of the literal "fluid" are saved in memory? Can the compiler recognize that this literal is already defined and just reference it again?
This has nothing to do with C++(the language). Instead, it is an "optimization" that a compiler can do. So, the answeryesandno, depending on the compiler/platform you are using. @David This is from thelatest draft of the language: § 2.14.6 (page 28)Whether all string literals are distinct (that is, are stored in non overlapping objects) isimplementationdefined. The effect of attempting to modify a string literal isundefined. The emphasis is mine. In other words, string literals in C++ areimmutablebecause modifying a string literal is undefined behavior. So, the compiler is free, to eliminate redundant copies. BTW, I am talking about C++ only ;)
I have a plist that is processed with a precompiled header file and in it there is the "variable" VERSION_STRING used a few times in such fields as CFBundleGetInfoString, ie the value for the key CFBundleGetInfoString is: MyProduct VERSION_STRING Copyright © 2009 MyCorp In MyHeader.h (which is the set as the Info.plist prefix header I would like to be able to build VERSION_STRING into the form MAJOR.MINOR.PATCH.BUILD where I have ``` #define MAJOR 1 #define MINOR 0 #define PATCH 0 #define BUILD 23 ``` For some reason I seem to be incapable of doing this. I might just be having one of those moments
Turns out there is actually an Apple Tech Note on this and a solution to the very problem I was having. So, for anyone that may come across this and is having the same problems I was check outTechnical Note TN2175: Preprocessing Info.plist files in Xcode Using the C Preprocessor
Does anybody know a library similar toboost::preprocessor(maybe not so advanced) that could be easily used/incorporated in plain C projects? Of course, the most (all ?) ofboost::preprocessoris usable when writing inCbut I would prefer a small library with only basic capabilities that doesn't depend on the monster like boost.
I suggest you use Boost.Preprocessor. Don't think i haven't read your question entirely. I suggest you use Boost.Preprocessor alone, not the whole boost package. In order to do that, you can use a small utility calledbcpwhich lets you extract the libraries that you need, in your case Boost.Preprocessor.
It looks like g_strncasecmp is deprecated, so I am looking for another function to do the same thing.
From the docs athttp://library.gnome.org/devel/glib/stable/glib-String-Utility-Functions.html#g-strncasecmp "There are therefore two replacement functions: g_ascii_strncasecmp(), which only works on ASCII and is not locale-sensitive, and g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8."
How does one deal with error return of a routine in C, when function calls go deep? Since C does not provide an exception throw mechanism, we have to check return values for each function. For example, the "a" routine may be called by "b", and "b" may called by many other routines, so if "a" returns an error, we then have to check it in "b" and all other routines calling "b". It can make the code complicated if "a" is a very basic routine. Is there any solution for such problem? Actually, here I want to get a quick return path if such kind error happens, so we only need to deal with this error in one place.
You can use setjmp() and longjmp() to simulate exceptions in C. http://en.wikipedia.org/wiki/Setjmp.h
I am more into C/C++. But many of my seniors here in college ask me to learn Java if I want to contribute to an open source project.. I'm in dilemma. what to do? Can't we do a design project in C/C++?
There are plenty of open source C and C++ projects - as well as loads in virtually any other language you can come up with. Of course it's never a bad idea to learn another language, but don't feel too constrained by "only" knowing C and C++. If you want to contribute toa specificopen source project which is written in Java, of course, that's a different matter... but if you're trying to find C and C++ open source projects, some of the major hosting sites support querying by project language, I believe. For example, you can look atGoogle Code C++ projectsandSourceForge projects tagged C++.
why can we do this in c? ``` int n; scanf("%d",&n); int a[n]; ``` I thought array is located memory during load time but seems like the above example works during runtime. Do I misunderstand any thing? can you guys help? Thanks,
I am no expert in C, but this could be avariable-length arrayas added byC99andsupported by GCC, for example. GCC allocates the memory for such array on stack, so that it gets automatically freed when you return from the function.
I creates program in c/c++ in turbo c . but i am facing problem is that i am not able to make it full screen in windows vista. please tell me how to do it?
Why are you still living in the Turbo era? Switch on to some better compiler. gcc and MSVC++ Professional Edition are better ones. On windows use IDEs like DevCpp or Code::Blocks, both support MinGw port of gcc.
Environment: I am using MS-VC++ 6.0, I include a group of header file with some data.The header files change often, so on every change I change the path setting and re-compilerA log file is generated based on the included header filesFor tracking of the header file from the log file, I wish to print the header file path inside the logQuestion-1:Is getting the header file path from inside the program possible?Question-2:I am using VC++, but if it is possible in gcc, then I can easily port, so, please let me know, if it is possible in any other environment like gcc
In VC++, compiling with the option/FCwill put the currently processed file'sentire pathin the__FILE__macro. That will account for your changes to the include path. Details here.
Does anybody know a library similar toboost::preprocessor(maybe not so advanced) that could be easily used/incorporated in plain C projects? Of course, the most (all ?) ofboost::preprocessoris usable when writing inCbut I would prefer a small library with only basic capabilities that doesn't depend on the monster like boost.
I suggest you use Boost.Preprocessor. Don't think i haven't read your question entirely. I suggest you use Boost.Preprocessor alone, not the whole boost package. In order to do that, you can use a small utility calledbcpwhich lets you extract the libraries that you need, in your case Boost.Preprocessor.
It looks like g_strncasecmp is deprecated, so I am looking for another function to do the same thing.
From the docs athttp://library.gnome.org/devel/glib/stable/glib-String-Utility-Functions.html#g-strncasecmp "There are therefore two replacement functions: g_ascii_strncasecmp(), which only works on ASCII and is not locale-sensitive, and g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8."
How does one deal with error return of a routine in C, when function calls go deep? Since C does not provide an exception throw mechanism, we have to check return values for each function. For example, the "a" routine may be called by "b", and "b" may called by many other routines, so if "a" returns an error, we then have to check it in "b" and all other routines calling "b". It can make the code complicated if "a" is a very basic routine. Is there any solution for such problem? Actually, here I want to get a quick return path if such kind error happens, so we only need to deal with this error in one place.
You can use setjmp() and longjmp() to simulate exceptions in C. http://en.wikipedia.org/wiki/Setjmp.h
I am more into C/C++. But many of my seniors here in college ask me to learn Java if I want to contribute to an open source project.. I'm in dilemma. what to do? Can't we do a design project in C/C++?
There are plenty of open source C and C++ projects - as well as loads in virtually any other language you can come up with. Of course it's never a bad idea to learn another language, but don't feel too constrained by "only" knowing C and C++. If you want to contribute toa specificopen source project which is written in Java, of course, that's a different matter... but if you're trying to find C and C++ open source projects, some of the major hosting sites support querying by project language, I believe. For example, you can look atGoogle Code C++ projectsandSourceForge projects tagged C++.
why can we do this in c? ``` int n; scanf("%d",&n); int a[n]; ``` I thought array is located memory during load time but seems like the above example works during runtime. Do I misunderstand any thing? can you guys help? Thanks,
I am no expert in C, but this could be avariable-length arrayas added byC99andsupported by GCC, for example. GCC allocates the memory for such array on stack, so that it gets automatically freed when you return from the function.
I creates program in c/c++ in turbo c . but i am facing problem is that i am not able to make it full screen in windows vista. please tell me how to do it?
Why are you still living in the Turbo era? Switch on to some better compiler. gcc and MSVC++ Professional Edition are better ones. On windows use IDEs like DevCpp or Code::Blocks, both support MinGw port of gcc.
Environment: I am using MS-VC++ 6.0, I include a group of header file with some data.The header files change often, so on every change I change the path setting and re-compilerA log file is generated based on the included header filesFor tracking of the header file from the log file, I wish to print the header file path inside the logQuestion-1:Is getting the header file path from inside the program possible?Question-2:I am using VC++, but if it is possible in gcc, then I can easily port, so, please let me know, if it is possible in any other environment like gcc
In VC++, compiling with the option/FCwill put the currently processed file'sentire pathin the__FILE__macro. That will account for your changes to the include path. Details here.
I have an array of floats where data are stored with varying decimal points so some are123.40000,123.45000,123.45600...now if i want to print these values in the string without the 0s in the end inprintf()so that they are123.4,123.45,123.456, without those 0s in the end. Is this possible? If so, how?
Use the %g formatter: ``` printf( "%g", 123.4000 ); ``` prints 123.4 Trailing zeros are removed, but unfortunately so is the trailing decimal point if the fractional part is zero. I don't know if there is actually any way of doing what you want directly using printf() - I think something like this is probably your best bet: ``` #include <stdio.h> #include <math.h> void print( FILE * f, double d ) { if ( d - floor(d) == 0.0 ) { fprintf( f, "%g.", d ); } else { fprintf( f, "%g", d ); } } int main() { print( stdout, 12.0 ); print( stdout, 12.300 ); } ```
I have the string:"foo$bar@baz" I'm looking to write a C program which will extra all three sub-strings ("foo","bar"and"baz") and put each into it's own string. P.S. Don't worry, this isnothomework.
What you are looking for isstrtok. It allows for you to set the delimiters as well.