Questions tagged [c]

C is a general-purpose computer programming language used for operating systems, games, and other high performance work and is clearly distinct from C++. It was developed in 1972 by Dennis Ritchie for use with the Unix operating system.

2,632 questions
1k views

Using bucket sort to rank students based on their marks

Problem: n students have marks and names. Rank students based on their marks and print their names. ...
3k views

Given two Binary trees, find whether the second is a sub tree of the first

Given two binary trees, you have to find whether the second is a sub tree of the first. First attempt (brute force): ...
330 views

Second largest element of a large array

Given an array which contains integers (may contain thousands of elements), you have to write a program to find the second largest element of the group. ...
898 views

Merge sort 2 sorted linked list

My coding skill is pretty rusty and moreover, I have never paid much attention to writing elegant code. I want to start on this. Problem is to merge 2 sorted linked list. Algorithmically this is ...
455 views

Euclidian distance - optimization and casting

I'm trying to optimize a simple Euclidian distance function in C. It's for a DLL, and calculates distances from one point to many. I have: Version 1: ...
3k views

I'm pretty new to C so I decided to implement SRFI-1 (linked-list library) to help me learn. What I have so far is working, but only handles integers. ...
2k views

Finding the maximum row of a 4x4 matrix

I have written a recursive function that I believe will return the 'maximum' row of the 4x4 matrix it is fed. By 'maximum' I mean that the maximum row of the matrix. ...
365 views

Determining if one string occurs at the end of another

This is exercise 5-4 from K&R. I spent hours tweaking it but now it seems to work. I'm new to pointers and I'd welcome any comments about how to do it better. ...
755 views

Brainfuck to C converter

This program converts Brainfuck source code to C and compiles it with gcc. It runs very well (that was my first time I played Lost Kingdom), however, the code is quite long because some parts are ...
1k views

C-style array class

I often use C API's in C++ that force me to use C-style arrays. I got sick of constantly using a dynamic vector with &vec[0], so I wrote this C-style array ...
565 views

Find number of divisors for integer and sum of integers

This function computes and returns the number of divisors an integer has. But it is very slow and I believe it can be optimized for speed. ...
32k views

Caesar Cipher program in C

I'm a beginner-intermediate C++ programmer, and I never used or understood C input & validation. I just always used C++ streams. Anyway, I just want code critique, as I have never used the C ...
284 views

Best syntax for executing an action between each loop iteration.

The question of the cleanest way to write a loop that executes some action between each iteration has always interested me. In a sense, what is the best way in c/c++ to implement this fictional ...
632 views

Insertion sort implementation adapted from the web

I've found this implementation on the web: ...
3k views

'do { statement; } while(0)' against 'statement' when writing C macro? [closed]

Which one of the following is preferrable and why when writing C macro? Type 1 #define I2C_START() I2C_WAIT_IDLE(); SSP1CON2bits.SEN = 1 Type 2 ...
575 views

You may see full code here (note that the link points to the specific commit). Language is "clean C" (that is, a subset of C89, C99 and C++98 — it is intended to compile under all of these standards)....
748 views

Terminating a C loop when maximum hardware limit reached

In response to the SO question C: f>0 vs Perl: \$f>0?, the use of overflow to terminate the loop is referred to as "poor programming practice". Paraphrased: Your c code is actually exiting because ...
2k views

Finding the minimum in a sorted, rotated array

Given an array that is first sorted non-decreasing and then rotated right by an unspecified number of times, find the index of its minimal element efficiently. If multiple such minimal elements ...
3k views

348 views

Bilinear scaling using SSE2 on Core 2 CPUs

I am looking for some help with improving this bilinear scaling SSE2 code on Core 2 CPUs. On my Atom N270 and on an i7, this code is about 2x faster than the MMX code. But under Core 2 CPUs, it is ...
4k views

Function pointers and switch statements

I feel like I can make my switch statements more elegant with a function pointer, i.e. I'd like to set the digestLength and a pointer to a function in the same ...
2k views

Handling SIGINT signal for a server program

I have created a small server program that encrypts text using a named pipe: ...
13k views

Simple C Implementation for Unix “tail” Command

The following is like the Unix "tail" program. It was assigned as an exercise in Chapter 5 of Kernighan & Ritchie's The C Programming Language. Because I've only read through most of Chapter 5, I'...
1k views

Eratosthenes sieve and MPI

I wrote this sieve of Eratosthenes with MPI, but I'm not sure if it's good enough. Should I use MPI_Scatter and MPI_Gather ...
2k views

Binary tree encoding

I implemented a solution to this coding challenge on the Code Golf. I have decent experience with C/C++, but it's been a while since I've used them extensively. ...
831 views

abort() implementation

Here you go: #define abort(msg) (fprintf(stderr, msg) && *((char*)0))
27k views

C function to find and delete a node from a singly linked list

I'm looking for the most commonly used style for writing the delete_item() function of a singly linked list, that find a matching item and deletes it. Is what I ...
294 views

Struct to web style string

I've had to make several functions to turn some structures into strings. I am a still green when it comes C so I am unsure if I am doing this a very awkward way. The system I am coding for does not ...
645 views

How does this Binary Search Tree look?

I wrote a BST in C a while back and may use it at some point. ...
26k views

Searching an element in a sorted array

I was applying for a position, and they asked me to complete a coding problem for them. I did so and submitted it, but I later found out I was rejected from the position. Anyways, I have an eclectic ...