Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

8
votes
2answers
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. ...
5
votes
2answers
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): ...
2
votes
1answer
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. ...
7
votes
2answers
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 ...
4
votes
4answers
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: ...
5
votes
1answer
3k views

Generic linked-list library in C

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. ...
5
votes
5answers
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. ...
7
votes
3answers
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. ...
14
votes
3answers
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 ...
8
votes
2answers
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 ...
7
votes
2answers
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. ...
10
votes
3answers
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 ...
3
votes
2answers
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 ...
9
votes
2answers
632 views

Insertion sort implementation adapted from the web

I've found this implementation on the web: ...
7
votes
4answers
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 ...
6
votes
1answer
575 views

UTF-8 character reader function

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)....
7
votes
2answers
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 ...
11
votes
4answers
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 ...
4
votes
2answers
3k views

Allocating memory for a matrix with a single malloc

For an interview, I was asked to write code allocate memory for a rows*cols matrix, using a single malloc(). I want your comments about this code that I wrote: <...
7
votes
1answer
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 ...
5
votes
2answers
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 ...
4
votes
1answer
2k views

Handling SIGINT signal for a server program

I have created a small server program that encrypts text using a named pipe: ...
16
votes
4answers
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'...
15
votes
2answers
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 ...
12
votes
5answers
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. ...
2
votes
2answers
831 views

abort() implementation

Here you go: #define abort(msg) (fprintf(stderr, msg) && *((char*)0))
12
votes
6answers
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 ...
7
votes
4answers
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 ...
3
votes
3answers
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. ...
139
votes
17answers
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 ...
12
votes
4answers
357 views

Draw random 1's and 0's

I was wondering if anyone would be so kind as to look at this code I've written for practice. Tell me what am I doing wrong, where can I optimize, anything. What this code does is (in terminal) draw ...
27
votes
5answers
20k views

Tiny Encryption Algorithm (TEA) for arbitrary sized data

The TEA is a very simple encryption algorithm requiring little time and space - perfect for embedded systems. There are extensions to it, and every version has its flaws (WEP was based on it), but ...