Click here to Skip to main content
15,889,335 members
Everything / Programming Languages / C

C

C

Great Reads

by Martin Mitáš
How to support scrolling within your controls.
by Espen Harlinn
Choosing the right synchronization mechanisms when working with threads, thread-pools, and I/O Completion ports to create high performance asynchronous servers in C++
by Andy Allinger
Add features to k-means for missing data, mixed data, and choosing the number of clusters
by Jeffrey Walton
Perform authenticated encryption with Crypto++.

Latest Articles

by Chris Boss
BASIC: A powerful language often underestimated and undervalued
by ColleagueRiley
A multi-platform single-header very simple-to-use framework library for creating GUI Libraries or simple GUI programs.
by FPGANinja
A walkthrough and source code for designing a stream interface in Vitis HLS
by Alexey Shtykov
The thing that could generate pseudo random numbers faster than standard library does

All Articles

Sort by Updated

C 

N 30 Apr 2024 by merano99
First of all, fgets() expects the maximum buffer size and not the length of an uninitialized string as already read in answer 1. When asked why the result is 4 and not 3, it should be noted that fgets() also reads the end-of-line character. When...
U 30 Apr 2024 by Dhanush G
#include #include int main() { char a[100]; printf("enter name:"); fgets(a,strlen(a),stdin); printf("%d",strlen(a)); } output: enter name:abc 4 What I have tried: shouldn't the lenght be 3 instead of 4?
U 30 Apr 2024 by Richard MacCutchan
char a[100]; printf("enter name:"); fgets(a,strlen(a),stdin); // at this point a contains any random set of characters You are calling strlen on an unitialised array, so the result will be anything from zero to the largest integer value. A...
N 30 Apr 2024 by Richard Deeming
Assuming you're trying to replicate the list shown on the Windows lock screen, it's not as simple as passing a filter to the list. By default, Windows will show all enabled local user accounts on the lock screen, assuming the computer is not...
N 30 Apr 2024 by Dave Kreskowiak
There is no procedure for filtering the users based on what you don't recognize. User accounts are user accounts. In a corporate domain environment, you can have other users you don't recognize, but you don't have any way of filtering them out...
N 30 Apr 2024 by Pete O'Hanlon
Well, whoever set you this piece of homework didn't give you a lot to go on. We aren't going to give you much to go on either because this is not a site where people will write code for you. If you bring code along that's not working, we're more...
U 29 Apr 2024 by samtoad
I'm again attempting to obtain a list of all users on a windows platform. I already obtained a general user list using the netuserenum function. I think you do the same with other functions, like NetQueryDisplayInformation. But my question...
U 29 Apr 2024 by k5054
Without some code examples, it's not clear where the issue is. In particular, how are you getting your data? For example char buff[10]; scanf("%s", buff); will overflow your input buffer. You can tell scanf how long your input buffer is...
N 29 Apr 2024 by Rick York
When I must use fixed-sized text buffers I make sure to use the size-limited library functions since they are much safer. Here is an example with your data : #define DATE_LENGTH 11 typedef char DateStr[ DATE_LENGTH + 1 ]; // plus one for the...
N 29 Apr 2024 by samtoad
In an application that I'm working on, I use dates, with size of the 'char' being declared as datestr[12], with the max length of 11 -> thru out the whole program... While testing all possible situations, I entered a date greater than size of 11...
U 27 Apr 2024 by Member 16250016
Hello, i try simple test function string in c, #include #include #include #include int main(int argc, char **argv) { const char *str ="G06111412/031/PL05"; unsigned short maxLen = strlen( str );...
N 26 Apr 2024 by OriginalGriff
Nothing there is saying that the substring is invalid: you get what I would expect. If I run your code at onlinegdb.com, I get a different result (if I ignore the copious compiler warnings): string "G06111412/031/PL05"- has lenght 18 char, cut...
N 25 Apr 2024 by merano99
The work description is either very incomplete and some things don't seem to fit together properly. The current description contains the following: Title:BBQ MANAGEMENT SYSTEM IN C My professor told me to put Menu: including drinks, foods and...
N 24 Apr 2024 by Pete O'Hanlon
Let's talk about what the question is really asking you for, and why your question is impossible to answer as it currently reads. To go back to basics, a flowchart is a series of steps showing that you can break a "big" problem into small,...
N 24 Apr 2024 by inday badiday
Title:BBQ MANAGEMENT SYSTEM IN C My professor told me to put Menu: including drinks, foods and etc. Sales Report Exit What I have tried: I am trying to solve the problem but I really can't
U 24 Apr 2024 by Member 16250016
Hello, i try simple test data type float in c, float can represent a range between 10^-37 to 10^ 37, but exemple follow show wrong value when runtime. #include #include #include #include int main(int...
N 23 Apr 2024 by CPallini
What Every Computer Scientist Should Know About Floating-Point Arithmetic[^]
N 23 Apr 2024 by OriginalGriff
To add to what k5054 has said, computers don't actually know about floating point numbers, they work in whole bytes and a float in C is stored in a relatively small amount of memory: 4 bytes (or 32 bits) only. This is broken into the mantissa...
N 23 Apr 2024 by k5054
A float has only about 7 decimal digits of precision. So for example given #include int main() { float f1 = 3445678336.0; float f2 = 3445678337.0; float result = f2 - f1; printf("%f\n", result); } The result is not...
N 23 Apr 2024 by k5054
Take a look at std::basic_istream::operator>> - cppreference.com[^] Look at the example code, which shows you how to read a value from a file into a variable. This assumes that your data (#1, #2, #3 etc) are all normally formatted...
N 23 Apr 2024 by CPallini
Try #include #include #include #include using namespace std; using Row = array; int main() { vector v; ifstream ifs("cmb.txt"); if (ifs) { string line; getline(ifs,...
U 23 Apr 2024 by juliet6
Hello, I am stuck trying to convert a delimited text file with five columns of data structured like this: col1 col2 col3 col4 col5 #1 #1 #1 #1 #1 #2 #2 #2 #2 #2 #3 #3 #3 #3 #3 ... I can print out each line of the...
16 Apr 2024 by Rick York
One error is apparent - this line in the main function : void mergesort(a,0,n-1); should not have the void statement in it. It should be like this : mergesort(a,0,n-1); Another one I noticed is also in main : printf( "%d", &a[i]...
16 Apr 2024 by OriginalGriff
Toi add to what Rick has said, you should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we...
16 Apr 2024 by Rajesh Majumder from Durham
What is the error of this mergesort C programming? What I have tried: #include void mergesort(); void merge(); main(){ int a[40],i,n; printf("Enter Array Size-"); scanf("%d",&n); printf("Enter Array Elements-"); for(i=0;i
9 Apr 2024 by merano99
Karsten has already addressed the problem of unsafe data types that are also accessed globally. Global variables and the limitation of a pointer field should be avoided. Especially if the code is to be used flexibly by other developers with as...
8 Apr 2024 by KarstenK
I dont like your code at all. The mismatch of types is problematic. Also the use of global data leeds to bugs when your code may evolve. tips: - store data in some byte array and give the save function also the size of data, so it can memcpy the...
8 Apr 2024 by steveb
int main() { int n = 10; float f = 10.5; save((void*) & n); save((void*)&f); int* pn; retrieve(0, (void**)&pn); printf("%d\n", *pn); float* pf; retrieve(1, (void**)&pf); printf("%g\n", *pf); return 0; }
8 Apr 2024 by ilostmyid2
In C, I need a function to return a void * via one of its arguments. Assume that there's a database which holds pointers to any type of data. Once the user stores the pointers and in other parts of the program the pointers are required to be...
1 Apr 2024 by KarstenK
Try some Learn C tutorial. Learn the basics before you start attempting to write some code. Tip: learn to use the debugger.
1 Apr 2024 by Pete O'Hanlon
We don't usually give people code if they haven't actually attempted anything yet. We will help you if you get stuck with your code, but we won't write it for you. What I will do, is offer you some guidance as to how to solve this. Write a...
1 Apr 2024 by Intellijoule
I want to implement a classical servo-like loop equation. For example, if A is gain, then: Af=A/(1-b*A), where Af is the resulting feedback gain, and B is beta, the sampled portion that must be updated to maintain a set point output of the loop's...
1 Apr 2024 by KarstenK
As somebody already wrote: best is to hold the complete map (your background) as drawable image the whole game, so your drawing code may look similar like:void drawScreen(int x, int y) { if( isSomeCarAt( x, y ) { drawCar( x, y ); } else {...
31 Mar 2024 by steveb
Generally in a computer graphics smooth rendering is achieved by using a back buffer to which an image is drawn with subsequent BLIT (Block Image Transfer) into the Video memory. Drawing into the video memory directly results in visual anomalies...
30 Mar 2024 by merano99
To restore the background position, the old background can be saved with its position before it is overwritten by cars or players. As soon as the field is free again, the saved position can be restored. An alternative would be to use a copy of...
30 Mar 2024 by k5054
If I were doing this, I might be tempted to track the grid, the player and the car(s) separately, something like: struct position { int row, col }; struct position player; struct vehicle { char direction; struct position pos; }; // and...
30 Mar 2024 by DosNecro
/* Rasal Amarasurya */ /* 21304178 */ #include #include #include /* For malloc, free */ #include "game.h" /* Include the game header */ void initializeGame(char **grid, int rows, int cols, FILE *file, int...
29 Mar 2024 by DosNecro
/* Rasal Amarasurya */ /* 21304178 */ #include #include #include /* For malloc, free */ #include "game.h" /* Include the game header */ void initializeGame(char **grid, int rows, int cols, FILE *file, int...
29 Mar 2024 by merano99
CPallini is right! The comment in the source code says that the initializeGame() function should determine the player position. int playerRow, playerCol; /* Removed the initialization here as it's set in initializeGame */ However, the prototype...
28 Mar 2024 by KarstenK
Verify that your new dll function has the same export macro signature as the other dll function. Mostly some DLL_EXPORT or similar macro stuff. Another common issue is that, your new dll isnt used by the calling program but a stale version in...
28 Mar 2024 by CPallini
You failed to properly initialize playerRow, playerRow variables. Put a breakpoint at movePlayer function in order to see that live.
27 Mar 2024 by Rick York
Firstly, there are no limits to DLL sizes or capacities. Secondly, it is important to note what is emitting that error message. It could be the compiler or the linker. If it is from the compiler then you have not defined a prototype for the...
27 Mar 2024 by samtoad
Currently, I'm trying to add another "C" routine into a DLL file. I've made the routine inside a DLL source file. I've declared the routine, and also indicated the intentions as it will be exported. The function name is also included in a DEF...
24 Mar 2024 by Sean Cundiff
Are you intending for only the parent child process to have CPU affinity? if(sched_setaffinity(0, sizeof(mask), &mask) == -1) { perror("sched_setaffinity()"); return EXIT_FAILURE; } If not you should probably change...
24 Mar 2024 by merano99
In fact, a lot can go wrong here at the same time, as Rick has already written. In addition to the problem of choosing the correct search range and step size, the function also has 4 jumps, as the formula already reveals. In addition to the risk...
23 Mar 2024 by PhaazeReborn
Greetings, I am self-studying OSTEP (Operating Systems Three Easy Pieces). It's an amazing book, and I'm learning a ton from it. Currently, I am working on the measurement homework at the end of Chapter 6 (Mechanism: Limited Direct Execution). I...
22 Mar 2024 by VaKa
as i dont know initial values for formula, s1, v1, k, s2 and looking at 1st example code, i suppose that cycle stops within 3 steps with incerment=1 (supposing k=0 at begininig), and resulting k is 3. at 2nd example of code, k increments with...
21 Mar 2024 by Саша Гайдамак
Help me write a program in C that will fit the values of k into the equation. formula = 5/1+k + 9/2+k + 13/3+k + 17/4+k -16 The value of k can be negative and an integer; in the case of the above selection method, the result should come out to...
21 Mar 2024 by Rick York
I don't think you are writing that correctly. Shouldn't it be 5/(1+k) + 9/(2+k) ... with parenthesis around each denominator term? This detail is very, very important. I would add parenthesis around each term summed so you don't have to rely...
20 Mar 2024 by CPallini
What Every Computer Scientist Should Know About Floating-Point Arithmetic[^].
19 Mar 2024 by merano99
There are several things to note here. It seems that only the variable k is changed with each loop. for (int i = -10000; i
19 Mar 2024 by Саша Гайдамак
please explain how it works? The program gives me the correct result only when I add integers, but in my task I would prefer to use the type double. working option: ` for (int i = -10000; i
19 Mar 2024 by Rick York
Step one is to make sure the variables formula, s1, s2, v1, and k are all of type double. You actually don't need all of them to be doubles but you want to make sure the result (formula) is a double and the division must be done with doubles. ...
19 Mar 2024 by steveb
Your function declared as taking 3 arguments. But you call it passing only 2 arguments. Hence you get an error during compilation. 3rd argument is not optional as declared.
19 Mar 2024 by samtoad
I have a question that may tax you, but it may also embarrass me at the same time, but here goes. Let's have some programming examples with which to work with first: Declaration of a "C" routine int RetIntDayNum(char * datestrg, int *...
19 Mar 2024 by CPallini
I could not believe that. So I tried the following piece of code #include int RetIntDayNum(char * datestrg, int * intdaynum, int * iostat); int main() { char foo[] = "foo"; int daynum = 42; int iostat = 10; ...
18 Mar 2024 by merano99
First of all, a distinction must be made between the declaration in the implementation and the call of a function. The following line is not a declaration but an assignment: iretv = RetIntDayNum(cdatestring, &idaynum, &iostat); VS2019 usually...
18 Mar 2024 by OriginalGriff
It depends on the compiler: most will give you an error "too few arguments to function" when you try to call it with two parameters. Other than that, it will probably blow up when you try to use iostat within the function as it's value will be...
11 Mar 2024 by Andre Oosthuizen
you can make use of the 'SystemTimeToTzSpecificLocalTime' function to achieve what you are trying - MS Learn | SystemTimeToTzSpecificLocalTime function (timezoneapi.h)[^] Your code will look like - #include #include void...
10 Mar 2024 by Pete O'Hanlon
I'm making an assumption here that you are talking about converting the two SYSTEMTIME structures in the DYNAMIC_TIME_ZONE_INFORMATION object. To accomplish this, I believe that you are going to start by converting the SYSTEMTIME structure to a...
10 Mar 2024 by samtoad
n using the GetDynamicTimeZoneInformation(&dtzi) function, and within the 'dtzi' structure, it tells you whether you're in the Daylight Savings time or not and when they will start, via these two dates. The return value of this function also...
8 Mar 2024 by Andre Oosthuizen
Quote: Found few projects that seem like they could do that, but am horrible at reading other people code If you cannot read other peoples code that contains a solution you are on a downward spiral as nobody is going to supply a 'copy and...
8 Mar 2024 by Member 12251575
Title says it all, I want to create taskbar application that would work in windows 10 and 11, preferably in C++, but I can also do C# and C if that's what it takes to make on. Visually it would be just bunch of text in taskbar that would update...
8 Mar 2024 by merano99
The program cannot function in the above form. It is immediately apparent that the offset would have to be a reference if the main program is to work sensibly as suggested. In addition, it seems urgently necessary to use a size_t as the data type...
7 Mar 2024 by k5054
You've tagged this with "embedded", so maybe you are running into malloc problems when using fgets. Maybe a better solution would be to use low level open() and read() Something like: #include #include #include ...
6 Mar 2024 by Richard MacCutchan
You have a potential hazard in your CustomGetLine function, in the following lines: if (dPtr[0] != cStartChar) { // Skip this line by reading the next one return CustomGetLine(dPtr, n, stream, cStartChar,...
6 Mar 2024 by _Asif_
have compiled your code on Windows and there are no memory allocation issues found. fgets do not allocate additional memory rather it simply copy content from the file stream into the source pointer. Since you already have allocated 5000 bytes of...
6 Mar 2024 by CPallini
I compiled your code, getting just the following warning: warning: suggest parentheses around assignment used as truth value [-Wparentheses] 50 | while (uPktlen = CustomGetLine(tempbuf, &PtrSize, pCurrFile, '$', u32Offset)) anyway...
6 Mar 2024 by Charan Kumar Oct2022
I have written a simple c program as below where we are trying to read line by line data by using fgets() and we skipping the lines which doesn't start with character '$' , the problem I'm facing is I'm getting error as fgets: Cannot allocate...
28 Feb 2024 by Chris Boss
BASIC: A powerful language often underestimated and undervalued
27 Feb 2024 by Rick York
This is how : Getting started with the SDL2 library for Game Development[^] Why should anyone write another tutorial for you when there plenty of them already? This was from the first page of links from a search at DDG.
27 Feb 2024 by OriginalGriff
We can't tell you. Why not? Because we don't - can't - know enough about your project, or your abilities, or the environment your engine will run under, or how it is supposed to communicate with external apps (which the job of an engine after...
27 Feb 2024 by P. Mehroof Abid
**This is not a bug, rather a question on the best way to write an engine using pure C and custom rasterizer** I'd like to know the most organized and best way to create a game engine using SDL2, a custom rasterizer and most importantly, C17. On...
25 Feb 2024 by ColleagueRiley
A multi-platform single-header very simple-to-use framework library for creating GUI Libraries or simple GUI programs.
21 Feb 2024 by Patrice T
Quote: How do I prevent roads from disappearing when the player moves through them in a grid-based game? - C programming Replace /* Clear the player's previous position */ grid[*prevPlayerRow][*prevPlayerCol] = ' '; with /* Clear...
21 Feb 2024 by merano99
Obviously, according to the source code in movePlayer(), the roads do not play a role in relation to the player, as the player should be able to move through them. It is therefore background. This means that the previous state should be saved and...
21 Feb 2024 by FPGANinja
A walkthrough and source code for designing a stream interface in Vitis HLS
20 Feb 2024 by OriginalGriff
Create a global variable called lastSurfaceType and initialise it to "road". When you move a player, set the square it is on to lastSurfaceType before you do the move, then set the variable to the type the player is about to move to. That way,...
20 Feb 2024 by DosNecro
I'm currently working on a grid-based game in C where I have roads represented by dots ('.') and a player represented by 'P'. The goal is for the player to reach the destination ('G'). However, I'm facing a challenge in keeping the roads visible...
19 Feb 2024 by Alexey Shtykov
The thing that could generate pseudo random numbers faster than standard library does
17 Feb 2024 by merano99
Quote: Logging every dice roll but the player names get corrupted After first analyzing the game, it does not seem to make sense to log individual dice rolls if the goal is to load or save the current game state. The log would perhaps make sense...
15 Feb 2024 by CPallini
You are lucky, the existing code already loads the 'locations' from a file ('locations.csv' in the 'data' folder). Have a look at the ReadLocations funciton and use it as reference for implementing your (say) ReadPlayers one. Then, with the...
15 Feb 2024 by OriginalGriff
We can't give you a specific answer to this: we have no idea what you tried, or any familiarity with the software - and I for one am not going to wade through the whole of a github project to work out how it works before trying to add features. ...
15 Feb 2024 by Richard MacCutchan
It very much depends on what the existing code does. In general terms you would just write data to a file to save, and read from the file to reload. But the exact implementation will very much depend on the structure of the existing code. See...
15 Feb 2024 by Member 16203198
I have this monopoly clone that I need to add the ability to save and load from a save but I have no idea where to even begin so any help is appreciated! GitHub - Mitul-Joby/Monopoly: An implementation of the board game monopoly in C, in your...
8 Feb 2024 by KarstenK
At first: Log all your close() calls. I guess there is one you do not want to be executed. But common speaken: You need to debug it. Because you wrote that it is some timing problem, you need to add timestamps in your debugs log. The disconnect...
8 Feb 2024 by mathiv327
Hi, Am trying relevent with socket programming but my thing is long duration connection which is deciated connection. but every after 1 min connection resetting connection due to recv failure with is error no : 22 error message : invalid...
6 Feb 2024 by _Asif_
This seems an interesting problem, and I thought why not ask ChatGPT and see how it responds to this problem. Surprisingly it has generated a code (first in Python, later in Java) that works (I just tested your two base codes by the way). Maybe...
5 Feb 2024 by OriginalGriff
I'd suggest that you move the data files to a "all users" data folder: Windows has a data folder which is available to all users: ProgramData. This is a hidden folder, normally in the root of the C: drive (but can be moved - open File Explorer...
5 Feb 2024 by KarstenK
To get the pathes windows has some special API named Known folder interfacesa or the more recente SHGetFolderPath. The parameters are useda as an identifier which path returns. But first you need some concept for your data. Best is when every...
5 Feb 2024 by samthelab
I've put together a simple multiuser program that runs great out of a PC-user A's directory. But the problems occur when user B' thru user D', on the same PC, runs the program and attempts access' to these date files. The results is usually quite...
4 Feb 2024 by k5054
There used to be a utility distributed with GCC called protoize that could assist with converting K&R function definitions to ANSI. However, it seems to have been discontinued around gcc 4, which goes back to circa 2005. There's some information...
4 Feb 2024 by OriginalGriff
Your best bet is to scrap your revised version, and revert to the original. Then if you must refactor the definitions write code to do that from the original, working version instead of trying to manually check each and every function. I'm not...
4 Feb 2024 by Member 14114220
Hi, I'm working since 6 years with a huge legacy software. This SW contains thousands of old style c functions with many parameters. Here an example with few parameters: old_function (x,y,z) int x; int y; int z; { // some code return (0);...
4 Feb 2024 by David Lafreniere
A core dump framework that stores crash information including call stacks on any embedded system.