question
stringlengths
24
425
answer
stringlengths
5
405
How does the commandls -lcount the number of hard links of an inode? Does it use the Linux API or is it code that requires deeper knowledge of the Linux kernel source code? I am not, yet, able to understand the source code ofls, because I just started learning C.
It callsstatto get information about the file (in astruct stat). Then it looks at thest_nlinkfield of thestruct stat.
lets say I have two int arrays, array1 and array2. Is this line: ``` array1[i++] = array2[j++]; ``` equal to this: ``` array1[i] = array2[j]; i++; j++; ``` ?
Yes they are equivalent,unlessyou've written#DEFINE i jor#DEFINE j i, in which case the behaviour of the first snippet isundefined.
There is an option '-fshort-double' in GCC, Clang does not understand it. Is there some analog of this option for Clang 3.7 ?
in looking at the web page:https://gcc.gnu.org/onlinedocs/gcc/Option-Summary.html#Option-Summary, there is no-fshort-doubleoption ingcc. therefore, the option is not valid to begin with. suggest removing that option when changing toclang
I am trying to obtain a number of bits from a byte in C. Currently I am doing so by shifting and then bit masking (0xFF), however the shifting operator is taking a long time. Is there another way to this using only bitwise operations? And if so how? Thanks!
If you want to move a particular (single) bit to the least-significant position, you can use a hack like this: ``` (x & mask) != 0 ``` wheremaskselects the single bit.
I am trying to obtain a number of bits from a byte in C. Currently I am doing so by shifting and then bit masking (0xFF), however the shifting operator is taking a long time. Is there another way to this using only bitwise operations? And if so how? Thanks!
If you want to move a particular (single) bit to the least-significant position, you can use a hack like this: ``` (x & mask) != 0 ``` wheremaskselects the single bit.
What is the asmjit equivalent ofmov eax,[ecx+0CC]? This is close:c.mov_ptr(x86::eax, x86::ptr(x86::ecx, 0x0CC)); but the arguments are wrong. Any ideas?
The answer is: a.mov(x86::eax, x86::ptr(x86::ecx, 0x0CC));
How do I return a double without gettingrounded: ``` int function() { double d = 5.894; printf("d=%f\n", d); return d; } int main() { double d = function(); printf("d=%f\n", d); return 0; } ``` This gives me ``` d=5.894000 d=5.000000 ``` What am I missing here?
Your function returns ٰintit must returndouble Change ``` int function() ``` to ``` double function() ```
How do I return a double without gettingrounded: ``` int function() { double d = 5.894; printf("d=%f\n", d); return d; } int main() { double d = function(); printf("d=%f\n", d); return 0; } ``` This gives me ``` d=5.894000 d=5.000000 ``` What am I missing here?
Your function returns ٰintit must returndouble Change ``` int function() ``` to ``` double function() ```
I need to get the path of %APPDATA% in my c program, to save a file in%APPDATA%\myprogram\How can I do that ?
You should be able to get that information throughgetenv, here is an example. ``` #include<stdio.h> #include<stdlib.h> int main () { char * app_data; app_data= getenv ("APPDATA"); if (app_data!=NULL) printf ("The appdata path is: %s",app_data); return 0; } ```
Is operator->allowed to use in C instead of.? Does its availability depend on compiler we are using? Is->operator available in the last C standard or does it come from the C++ standard? How those two differ?
In C,c->mis equivalent to(*c).m. The parentheses are necessary since.has a higher precedence than*. Any respectable compiler will generate the same code. In C++, unless->or*isoverloaded, the equivalence is as above.
Is operator->allowed to use in C instead of.? Does its availability depend on compiler we are using? Is->operator available in the last C standard or does it come from the C++ standard? How those two differ?
In C,c->mis equivalent to(*c).m. The parentheses are necessary since.has a higher precedence than*. Any respectable compiler will generate the same code. In C++, unless->or*isoverloaded, the equivalence is as above.
Is operator->allowed to use in C instead of.? Does its availability depend on compiler we are using? Is->operator available in the last C standard or does it come from the C++ standard? How those two differ?
In C,c->mis equivalent to(*c).m. The parentheses are necessary since.has a higher precedence than*. Any respectable compiler will generate the same code. In C++, unless->or*isoverloaded, the equivalence is as above.
I have a small Linux project written in C is it possible to analyze it with a SonarQube?
According tohttps://en.wikipedia.org/wiki/SonarQube, it support c/c++, and is cross-platform, so you can do it I think.
Is there a simple lexer/parser for C language or a subset of it which is based on Flex/Bison? I have found some open source parsers for C (TCC, LCC, ...) but none of them are based on bison.
There is aC11 grammarfor YACC (a predecessor of Bison), it should work with Bison (maybe some tweaks will be needed). GCC used to be based on Bison a long ago. GCC 3.4 source code contains afilewith C grammar.
I have a small Linux project written in C is it possible to analyze it with a SonarQube?
According tohttps://en.wikipedia.org/wiki/SonarQube, it support c/c++, and is cross-platform, so you can do it I think.
I am doing vectorization using AVX intrinsics, I want to fill constant floats like1.0into vector__m256. So that in one register I got a vector{1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}Does anyone knows how to do it? It is similar to this questionconstant float with SIMD But I am using AVX not SSE
The simplest method is to use_mm256_set1_ps: ``` __m256 v = _mm256_set1_ps(1.0f); ```
This question already has answers here:C comma operator(4 answers)Closed7 years ago. ``` int a=3,1; int b=(5,4); ``` I am a beginner in c and I noticed in a book this type of initialization . what does thisinitialisationmean?
int b = (5,4)will first evaluate 5 then 4. The last thing that is evaluated will be assigned to the variable. For example ``` int b = (5,4,3,2,1) ``` in this case the value of b will be 1.
This question already has answers here:C comma operator(4 answers)Closed7 years ago. ``` int a=3,1; int b=(5,4); ``` I am a beginner in c and I noticed in a book this type of initialization . what does thisinitialisationmean?
int b = (5,4)will first evaluate 5 then 4. The last thing that is evaluated will be assigned to the variable. For example ``` int b = (5,4,3,2,1) ``` in this case the value of b will be 1.
I was looking into a code snippet and saw below statement. How will below statement evaluated? ``` x= 5|(high == 1 ? y : high == 0 ? z:0); ```
The expression ``` x= 5|(high == 1 ? y : high == 0 ? z:0); ``` is evaluated as ``` x= 5|( high == 1 ? y : (high == 0 ? z:0) ); ``` It has similar effect as that of ``` if(high == 1) x = 5|y; else if(high == 0) x = 5|z; else x = 5|0; ```
Hi I want to declare a 12 bit variable in C or any "unconventional" size variable (a variable that is not in the order of 2^n). how would I do that. I looked everywhere and I couldn't find anything. If that is not possible how would you go about saving certain data in its own variable.
Use a bitfield: ``` struct { unsigned int twelve_bits: 12; } wrapper; ```
Say if you have the following IP address: ``` 192.168.0.255 ``` How can I store that human readable IP address into astruct in_addr? I'm brand new to network programming in C at this low level, so any help would be greatly appreciated.
You can do ``` struct in_addr p; inet_aton("192.168.0.255", &p.s_addr); ```
I Would like to know how I can do exponent function in this code part (that means how I can change ^ to something that works in c language)? ``` K = (S * (P/100/12)) / (1 - (1 + (P/100/12))^(-Y*12) ); ```
It's thepow()function frommath.h ``` K = (S * (P / 100 / 12)) / (1 - pow((1 + (P / 100 / 12)), (-Y * 12))); ```
I'm working on a C grammar analysis program. While running C preprocessor, there are some libraries which check the environment and use#errorto stop compiling. How can I disable them, since I only need the preprocess step to finish?
Why do you want to disable them? They are outputting an error and stopping compilation for a reason. The only way I know to disable them is to modify the source code and remove them.
I'm working on a C grammar analysis program. While running C preprocessor, there are some libraries which check the environment and use#errorto stop compiling. How can I disable them, since I only need the preprocess step to finish?
Why do you want to disable them? They are outputting an error and stopping compilation for a reason. The only way I know to disable them is to modify the source code and remove them.
I am using GDB(peda-gdb) to debug a program, can I write something in a heap address so when the program reach that address It can read it out? I know the address but I do not know how to write something in that address. Thanks.
You can write to the heap using C syntax, with commands that evaluate an expression likeprintorset var. For example, to write an integer value to a specific location: ``` (gdb) set var *(int *)0xYOURADDR = 23 ```
``` main() { int a,b=0,c=1,d=1; a=++b&&++c||++d; printf("%d %d %d",b,c,d); //1 2 1 b=0,c=1,d=1; a=b&&++c||++d; printf("%d %d %d",b,c,d); //0 1 2 } ``` Why second printf gives answer 0 1 2 instead of 0 2 1 ?
Why second printf gives answer 0 1 2 instead of 0 2 1 ? &&isshort-circuiting. In ``` a=b&&++c||++d; ``` ++cwill not be evaluated ifbis0which is the case here. Hencecis1instead of2.
The sizeof(char) in C gives 1 and sizeof("a") gives 2. Please help
Achari.e. a character has size 1. Thestring literal"a"isnota character. It is a "string" (and by string I meanchar[]). All "strings" in C are null-terminated, so your "string" is actually: ``` {'a','\0'} ``` And that's two characters. So size is 2.
I found that a single-linelanguage: c.travis.yml file basically does all I need, with the exception that I don't wantmake testto happen. Is there a way I could make Travis just run./configure && make?
Have you tried overriding the default test script as described in:https://docs.travis-ci.com/user/languages/c#Default-Test-Script? Something like: ``` script: ./configure && make ```
I have a memory address integer like this 0x80480ac and i want to assign it a char * like this: ``` char *address="\x08\x04\x80\xac"; ``` How can i do it?
I believe you are looking for: ``` char * address = (char *)0x080480ac; ```
I found that a single-linelanguage: c.travis.yml file basically does all I need, with the exception that I don't wantmake testto happen. Is there a way I could make Travis just run./configure && make?
Have you tried overriding the default test script as described in:https://docs.travis-ci.com/user/languages/c#Default-Test-Script? Something like: ``` script: ./configure && make ```
I have a memory address integer like this 0x80480ac and i want to assign it a char * like this: ``` char *address="\x08\x04\x80\xac"; ``` How can i do it?
I believe you are looking for: ``` char * address = (char *)0x080480ac; ```
Can someone explain mecmpfuncwhich is used in theqsortfunction? What areaandbin this function and what are they pointing to? ``` int cmpfunc(const void *a, const void *b) { return(*(int*)a - *(int*)b); } ```
aandbincmpfuncare pointers toconst voidtype.cmpfunccan accept pointer to elements of array of any data type.void *pointer can't be dereferenced, therefore a castint *is needed before dereferencing.
I want to display "string pointer affected" but I get an error. Here is my code: ``` #include<stdio.h> main() { char* *p; char * s="string pointer affected"; *p=s; printf("%s",*p); } ```
pdoesn't point to any known location, so writing to*pis a bad idea. You mean to say: ``` p = &s; ```
I want to display "string pointer affected" but I get an error. Here is my code: ``` #include<stdio.h> main() { char* *p; char * s="string pointer affected"; *p=s; printf("%s",*p); } ```
pdoesn't point to any known location, so writing to*pis a bad idea. You mean to say: ``` p = &s; ```
I've been trying to make a program to input two numbers from the user.The XCode application is showing error as Data argument not used by format string How am I supposed to get over this? please help thanks
just type%dinside the("Here is my number %d",Num) You can find more formats here:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
I want initialize a function pointer so that it is null. Which of these two ways is preferred? ``` void (*Pointer)(void) = NULL; ``` Or ``` void (*Pointer)(void) = (void (*)(void))0; ```
0is implicit convertible to any pointer type. Though how your compiler implementsNULLdepends. In your code you can simply write void (*Pointer)(void) = 0;but it won't be portable , so writevoid (*Pointer)(void) = NULL;
Within a function, we have the following: ``` __asm__("movl $0xe4ffffe4, -4(%ebp)"); ``` Does this mean that we move the contents of the memory address 0xe4ffffe4 over to the ebp register?
This: ``` movl $0xe4ffffe4, -4(%ebp) ``` Says "move the 4-byte value 0xe4ffffe4 into the slot 4 bytes before the address stored in register ebp."
I am working on small project using Arduino. I have this char array which used to store some values. the problem is How to set this char array to null after assign some values in Arduino? ``` char packetBuffer[200] ```
Usememsetfromstring.h: ``` memset(packetBuffer, 0, sizeof packetBuffer); ```
How do i make the printf("%d, veck[i]); print out all 100 numbers of the array instead of only 1-10? ``` int vek[100]; for(int a=0;a<10;a++){ for(int i=0;i<10;i++){ printf("%d ", vek[i]); //only shows numbers 1-10 } printf("\n"); } ```
You should change the index of vek. ``` printf("%d ", vek[10* a + i]); ```
How can I open a file in an ext2 file system. For example, lets say I want to open the file:/a/b/c.txt I'm looking at the functions here:http://www.nongnu.org/ext2-doc/ext2.html
The same as any other filesystem: usefopen("/path/to/the/file", "r")or similar. The documentation you found is only relevant to people implementing the filesystem.
In the following floor loop, how wouldsum += n --be evaluated? I'm very, very confused... ``` int sum; for(sum=0;n>0;sum+=n--); ```
Forsum += n--the following operations are performed addntosumdecrementn Withsum += --n nis decrementedthe new value ofnis added tosum n--is calledpostdecrement, and--nis calledpredecrement
The following code: ``` #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { pid_t mypid = getpid(); write(1, &mypid, sizeof(pid_t)); return 0; } ``` Prints gibberish instead of actual pid. Why?
write(..will not print formatted text, but rather binary output directly to a file descriptor. Just useprintforfprintf: ``` fprintf(stdout, "%d", (int) mypid); ```
How can I remove, in C, a part of a string? I am interested especially in a built-in function, or an efficient code to do it. I have this line of code: ``` char namefile[256] = "/Users/student/Desktop/Silviu/g7.08e11.01024" ``` and I want to keep only "g7.08e11.01024" so I can use it somewhere else.
Usestrrchr: ``` const char* shortName = strrchr(namefile, '/') + 1; ``` strrchrreturns the address of the last occurence of/in the string.
How can I remove, in C, a part of a string? I am interested especially in a built-in function, or an efficient code to do it. I have this line of code: ``` char namefile[256] = "/Users/student/Desktop/Silviu/g7.08e11.01024" ``` and I want to keep only "g7.08e11.01024" so I can use it somewhere else.
Usestrrchr: ``` const char* shortName = strrchr(namefile, '/') + 1; ``` strrchrreturns the address of the last occurence of/in the string.
here is my CMakeLists.txt: ``` cmake_minimum_required(VERSION 3.3) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") add_executable(ex ex.c) target_link_libraries(ex xdo) ``` the full code ishere of course, the libxdo.so and xdo.h is exist
the checked answer is wrong, the right solution is: ``` #define __USE_XOPEN #include "unistd.h" #include <xdo.h> ```
Header file math.h is for mathematical functions like cos,sin, tan.. But how to write the ln function and not log?
The C functionlogis the natural logarithm, which mathematicians usually write as "ln". The C functionlog10is the logarithm base 10, which is sometimes written "log".
here is my CMakeLists.txt: ``` cmake_minimum_required(VERSION 3.3) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") add_executable(ex ex.c) target_link_libraries(ex xdo) ``` the full code ishere of course, the libxdo.so and xdo.h is exist
the checked answer is wrong, the right solution is: ``` #define __USE_XOPEN #include "unistd.h" #include <xdo.h> ```
``` #include<string.h> #include<stdio.h> void main() { char *a="12345"; //Add number of that string } ``` How can i add number of that string example : ``` sum=1+2+3+4+5 sum=15 ``` How can i do that?
``` int sum = 0; char *a = "12345"; while (*a) { sum += *a - '0'; a++; } printf("sum=%d\n", sum); ```
Discovering gateway devices process using miniupnp is as follows: CallupnpDiscover(...)to find all available devices which support UPnP protocol.CallUPNP_GetValidIGD(...)to get first gateway from this list. Is there any solution to getallgateways from list, obtained in step 1?
Made aforkof miniupnpc. Added routineGetValidConnectedIGDs, which returns list of all gateway devices found.
``` #include<string.h> #include<stdio.h> void main() { char *a="12345"; //Add number of that string } ``` How can i add number of that string example : ``` sum=1+2+3+4+5 sum=15 ``` How can i do that?
``` int sum = 0; char *a = "12345"; while (*a) { sum += *a - '0'; a++; } printf("sum=%d\n", sum); ```
Discovering gateway devices process using miniupnp is as follows: CallupnpDiscover(...)to find all available devices which support UPnP protocol.CallUPNP_GetValidIGD(...)to get first gateway from this list. Is there any solution to getallgateways from list, obtained in step 1?
Made aforkof miniupnpc. Added routineGetValidConnectedIGDs, which returns list of all gateway devices found.
I have four parameters, each an integer and I want to find the smallest. What is the quickest and/or simplest way to do it? I can probably do a bubble sort but it seems overkill.
``` int min(int a, int b){ return a<b?a:b; } ``` call as: ``` min(min(a,b),min(c,d)) ```
I want to generate a random float value between 0 and 1 excluding 1, I.e.[0, 1). I've searched a lot but I couldn't find an answer of this. I've tried the following trick however it generates a negative values ``` (double)rand()/(double)RAND_MAX-1; ```
You need parentheses to force the right order: ``` (double)rand()/((double)RAND_MAX + 1); ``` Also note, that you need +1, not -1.
Can i create my own properties with: ``` XChangeProperty(display, w, property, type, format, mode, data, nelements) ``` or can i only use the properties shown in standards.freedesktop.org?
You can create any property you want. Just doXInternAtomwith your unique atom name, and you have apropertyof your own. Same fortype.
Can i create my own properties with: ``` XChangeProperty(display, w, property, type, format, mode, data, nelements) ``` or can i only use the properties shown in standards.freedesktop.org?
You can create any property you want. Just doXInternAtomwith your unique atom name, and you have apropertyof your own. Same fortype.
I would like to retrieve time passed since kernel's start (in kernel space). It should be the match the timeprintk();is using (For example:[ 5.832000] message). jiffiesprovide different time, so I am not sure it fits me. How can I achieve this?
What about usingget_monotonic_boottime?jiffiesare initialized to 5 minutes before boot to ensure that there is an overflow soon after booting and detect bugs.
How do I convert from string to unsigned int? I know thatstrtoul()converts from string tounsigned long int, but I want normalintand notlong int. I haven't been able to find a function that does that.
but I want normal [unsigned] int and not long [unsigned] int. You want to usestrtoul(), assign it to along unsigned int, test the result for being in the range of[0..UINT_MAX]and if this is the case assign thelong unsigned intto anunsigned int.
For example the text file contains a line :A B CSo the method will declare 3 variables:in A, B ,C;
No . This type of facility is available in scripting languages like python and ruby but not in c/c++. You can use hashing or a struct to store this type of information.
Closed. This question needsdetails or clarity. It is not currently accepting answers.Want to improve this question?Add details and clarify the problem byediting this post.Closed7 years ago.Improve this question How can i combine one increment and one decrement statements into one single statement in C language. ex: a++ and b-- into one single statement.
Writea++,b--; Two expressions separated by a comma are evaluated left to right.
Here's my problem.. I have two arrays: one has the total marks of students and another is a character string. So, I want the computer to assign a letter grade to the character string when the array with the total marks is in within a range.. Here's what a came up with but it won't compile and I don't have anything else in mind:
Try this: ``` sprintf(grade[i], "%s", "A+"); ```
I didnt get the logic behind the answer. I tried to print the value of a after definition and it shows a=72. Please help. ``` #include<stdio.h> main() { int a=400*400/400; if(a==400) printf("Do good"); else printf("Have good"); } ``` output : Have Good
I guess that on your platformintis 2 byte wide, 16 bit. Then ``` 400*400 = 28928 ``` Due to overflow ``` 28928/400 = 72 ```
What is the Win32 API for sending messages between computers? I mean, what used to be achieved by the "net send" command and is now using "msg". I imagine it is some API over NetBIOS?
Bothnet sendandmsguse theWTSSendMessage()function.
I have the following code, where we try to left shift certain bits of a value: ``` int main() { unsigned long mvb = 1; mvb << 8; printf("The shift value is %u\n", mvb); mvb << 56; printf("The shift value is %u\n", mvb); } ``` but the result for all those two operation are both1, what is the reason, and how use it correctly?
You need to assign it back to mvb after shifting like: ``` mvb = mvb << 8; ```
What are the differences, if any, between ``` *a = b; ``` and ``` a = &b; ``` Are these two different ways to make a var point to another var?
No, they do not do the same thing.*a = b;does not changeabut it changes whatapoints to.a = &b;does changeabut it does not change whatapointed to.
Starting from ``` char tval[20] = "temp:26.62"; ``` how can I add a space character untiltvalis filled? I need that in the end to obtain this: ``` char tval[20] = "temp:26.62 "; ```
Like this, probably ``` size_t prevlen = strlen(tval); memset(tval + prevlen, ' ', 19 - prevlen); *(tval + 19) = '\0'; ```
There are some#definestatements in my C language code, and now i find some bug. But the codeblocks IDE can not step into the #define block. so i want to get the Pre-compiled source code, which will expand the#definemacro.
If you use the-Eoption of gcc, you can expand the macros to a C file, and then compile the C file.
Withfopen, by setting it towwill automatically clear the file for me. However now I'm trying to do the same thing withopen, ``` int file = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); ``` This won't guarantee that the file is empty and write at the beginning(?). How can I achieve that?
AddO_TRUNC- Initially clear all data from the file.
First time publishing hereSimple situation: In PUTTY, i have to create a file named admin.pid where it stores a PID when the user starts the "app" i'm creating. How can i do this? Thanks
Get the PID usinggetpid().Open the file usingfopen().Write the PID to the file byfprintf().Close the file usingfclose().
There are some#definestatements in my C language code, and now i find some bug. But the codeblocks IDE can not step into the #define block. so i want to get the Pre-compiled source code, which will expand the#definemacro.
If you use the-Eoption of gcc, you can expand the macros to a C file, and then compile the C file.
Withfopen, by setting it towwill automatically clear the file for me. However now I'm trying to do the same thing withopen, ``` int file = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); ``` This won't guarantee that the file is empty and write at the beginning(?). How can I achieve that?
AddO_TRUNC- Initially clear all data from the file.
When you have the statement: *str++ = *end Does the*strget assigned the value of*endor does it get incremented and then get assigned the value of*end?
As apost-increment operator, it first assigns*endthen points to new/incremented address ofstr.
I'm learning C, and I am running into some problems which make me write (seemingly) redundant code. I am finding myself writing code like ``` printf("%c", someChar); printf(" "); ``` which (since it is in a loop) would output something like ``` a b c ``` Is there any way to combine these print statements?
just write :printf("%c ", someChar); note the space after%c
Which numbers can a double type contain? (in C language) I was trying to find the numbers that double can contain in c. I know that a float can contain numbers between -10^38
In C, adoubleisusuallyan IEEE double. I don't know if this is required by the standard, but these days it would be unusual for it to be something else. Here's some info on double precision formats, particularly IEEE:double-precision floating point formats
I have a simple C library that I compile to a.sofile on my linux machine. I'd like to do the same on my Mac, but after I compile and move the library to/usr/local/lib, but I'm not sure how to link it seeing asldconfigisn't a thing. How would I go about doing this?
While Linux has.sofiles, OSX has.dylibfiles. The process is similar, you just invoke compiler as in ``` clang -dynamiclib -o libname.dylib sources.c ```
I have the following source: ``` int main() { 000J; } ``` With gcc 4.8.4 it compiles without errors. I know there are suffixes like L or U, but I didn't find anything about J. So what does it do?
I get a warning: Imaginary constants are a GNU extension TheJsuffix is a GNU extension, which causes the literal to be of a_Complextype. More info here:https://gcc.gnu.org/onlinedocs/gcc/Complex.html
I have the following source: ``` int main() { 000J; } ``` With gcc 4.8.4 it compiles without errors. I know there are suffixes like L or U, but I didn't find anything about J. So what does it do?
I get a warning: Imaginary constants are a GNU extension TheJsuffix is a GNU extension, which causes the literal to be of a_Complextype. More info here:https://gcc.gnu.org/onlinedocs/gcc/Complex.html
I create 100000 of such structs, by malloc'ing pointers to them. ``` test_struct test { char value[100]; } ``` Does it mean that all char[100] variables (100 * 100000 ~ 10mb) will go to stack memory, exceeding it and causing my program to crash?
No. When you allocate a struct on the heap, all of its members go on the heap as well.
Thesizeof(data_type)operator returns thenumber of bytesand notoctets, so the size of a byte may not be 8 bits. How can one identify the size of a byte in bits on any platform?
I think you can dosizeof(type) * CHAR_BITto determine the number of bits. Include limits.h for the definition of CHAR_BIT.
If I dosizeof('r'), the character 'r' requires 4 bytes in memory. Alternatively, if I first declare acharvariable and initialize it like so: ``` char val = 'r'; printf("%d\n", sizeof(val)); ``` The output indicates that 'r' only requires 1 byte in memory. Why is this so?
This is because the constant'c'is interpreted as anint. If you run this: ``` printf("%d\n", sizeof( (char) 'c' ) ); ``` it will print1.
Thesizeof(data_type)operator returns thenumber of bytesand notoctets, so the size of a byte may not be 8 bits. How can one identify the size of a byte in bits on any platform?
I think you can dosizeof(type) * CHAR_BITto determine the number of bits. Include limits.h for the definition of CHAR_BIT.
If I dosizeof('r'), the character 'r' requires 4 bytes in memory. Alternatively, if I first declare acharvariable and initialize it like so: ``` char val = 'r'; printf("%d\n", sizeof(val)); ``` The output indicates that 'r' only requires 1 byte in memory. Why is this so?
This is because the constant'c'is interpreted as anint. If you run this: ``` printf("%d\n", sizeof( (char) 'c' ) ); ``` it will print1.
I want to create a function like this but with priority. For that, I need the pid, but I don't know how to get it. Any idea? Mine is to usesetpriority (0, pid, value), but I need the pid. ``` void cmd_exec(char * tr[]) { if (execvp(tr[0], tr)== -1) perror("Error"); } ```
To get the process-id of the calling process usegetpid(). Please find the Linux man-page here.current(Issue 7) POSIX documentation here.
I want to create a function like this but with priority. For that, I need the pid, but I don't know how to get it. Any idea? Mine is to usesetpriority (0, pid, value), but I need the pid. ``` void cmd_exec(char * tr[]) { if (execvp(tr[0], tr)== -1) perror("Error"); } ```
To get the process-id of the calling process usegetpid(). Please find the Linux man-page here.current(Issue 7) POSIX documentation here.
``` #include <stdio.h> void foo(); int main() { int b = 0; int a[6] = {1,2,3,4,5,6}; foo(); return 0; } void foo() { //write some code to print a and b } ``` My Question is: How to get the local variable from outside without pass any parameter ?
That's not possible. You need to pass a pointer to it to access it from a function or make it global.
I want to know that how to print0as000and1as001in c and so on. I tried usingprintf("%3d", var);but it doesn't seem to work can anyone please suggest any method
You can do this with ``` printf("%03d", var); ``` to provide a minimum field width of 3 padded with leading zeros. Don't forget the enclosing "quotes". If you had put them in your posted code: ``` printf("%3d", var); ``` it would have been padded with spaces.
``` printf("%d",pow(5,3)) ``` it's printing 0, and works fine when number is different from 5 why? Can anyone explain this ?
The return type of the power function is double. using the double conversion specifier to prints the output of power function. the conversion specifier for double is not "%d". It should be "%f".
I was debugging a C code and came across below expression. I want to know, how below expression will be evaluated in c? x += y*2 != z;
To figure out expressions like that start with theprecedence table: Multiplication has precedence of 3, so it will be evaluated first!=has precedence of 7, so it will be evaluated next+=has precedence of 14, so it will be evaluated last. Therefore,xwill be incremented by1ify*2is not equal toz.
This question already has answers here:What does this ">>=" operator mean in C?(5 answers)Closed7 years ago. I know>>is Binary Right Shift Operator but what does>>=mean, like if I want Log2 onxI needwhile(x >>= 1) result++;.
It's the bitwise shift right assignment operator;x >>= 1is equivalent tox = x >> 1. (It has the same precedence as assignment=).
This question already has answers here:What does this ">>=" operator mean in C?(5 answers)Closed7 years ago. I know>>is Binary Right Shift Operator but what does>>=mean, like if I want Log2 onxI needwhile(x >>= 1) result++;.
It's the bitwise shift right assignment operator;x >>= 1is equivalent tox = x >> 1. (It has the same precedence as assignment=).
How can I view C11 header files content of gcc compiler in CodeLite on Linux? if not possible, How/Where can I view them? Please ignore this location en.cppreference.com/w/c/header since it's just documentary with no actual header file content
All the system header files, including the standard C header files, are under/usr/include. You can open any of them with any editor, including the CodeLite editor.
I want to find straight lines marked in the image, the coordinates point A, point B is the beginning and end of the segment. Please help me! img:enter image description heresource:enter image description here
It is called the "Hough Transform" You could experiment using something like Mathlab, and then implement it using opencv houghtransform. https://github.com/Itseez/opencv/blob/master/samples/cpp/houghlines.cpp
Assume there is a structfakes: ``` struct fakes { .... }; ``` And there is a pointer: ``` struct fakes *fakes; ``` I'm wondering whethersizeof(struct fakes)andsizeof(*fakes)are the same.
I'm wondering whethersizeof(struct fakes)andsizeof(*fakes)are the same. Yes, they are guaranteed to be the same.
What is the difference between ``` float f = (float) 99.32 ; ``` and ``` float f = 99.32f ; ``` Both of them compiled and ran successfully.
``` float f = 99.32f ; ``` That is afloatliteral, which means a float variable being assigned with afloatvalue directly. ``` float f = (float) 99.32 ; ``` That is afloatvariable that is assigned adoublevalue that is cast tofloatbefore being assigned.
What is the difference between ``` float f = (float) 99.32 ; ``` and ``` float f = 99.32f ; ``` Both of them compiled and ran successfully.
``` float f = 99.32f ; ``` That is afloatliteral, which means a float variable being assigned with afloatvalue directly. ``` float f = (float) 99.32 ; ``` That is afloatvariable that is assigned adoublevalue that is cast tofloatbefore being assigned.
In one of my assignments I am seeing this line used: ``` int index = -1, k; ``` Im not sure what is happening when there are to entries for the one variable. What exactly is the variable "index" holding when it has two entries?
This is the definition of 2 variables, both of the same typeint:indexandk. Onlyindexis initialized with-1, andkis left un-initialized.
In one of my assignments I am seeing this line used: ``` int index = -1, k; ``` Im not sure what is happening when there are to entries for the one variable. What exactly is the variable "index" holding when it has two entries?
This is the definition of 2 variables, both of the same typeint:indexandk. Onlyindexis initialized with-1, andkis left un-initialized.
Code: ``` #include <stdio.h> int main() { long cn=1; char ch; while((ch=getchar())!=EOF) { printf("%ld\t%c\n",cn++,ch); } } ``` When I input word "secret" and hit enter it shows count up to 7 and not 6,why?
Because the "enter" character is read as well. This is in fact a "newline", ASCII code 10 (or hex 0A).
I am developing a code in c, which simply FTPs the files on FTP server by using curl utility, by following thisexample now i have one simple thing to do, specify curl to use particular interface (IP) of the server for upload, as other interface of the server is associated with other socket connection. is there any way to do it?
Seems like curl support--interfaceoption ``` curl --interface eth0 ```
I am developing a code in c, which simply FTPs the files on FTP server by using curl utility, by following thisexample now i have one simple thing to do, specify curl to use particular interface (IP) of the server for upload, as other interface of the server is associated with other socket connection. is there any way to do it?
Seems like curl support--interfaceoption ``` curl --interface eth0 ```
As per title, is there an equivalent ofpthread_barrier_ttype in windows? I' ve foundSYNCHRONIZATION_BARRIERbut seems available only since Windows 8. Is there something more portable? Thanks
Do it the old fashioned way before barriers were invented: with a counting semaphore