Stack Exchange Network

Stack Exchange network consists of 174 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]

This challenge is related to the C language. Note that challenges that require the answers to be in a specific language are generally discouraged.

7
votes
1answer
296 views

Fully Modular C: Grading

You are a Computer Science professor teaching the C programming language. One principle you seek to impart to the students is modularity. Unfortunately, past classes have tended not to get the message,...
41
votes
12answers
4k views

Read out the C variable declaration

Background Variable declaration statement in C consists of three parts: the name of the variable, its base type, and the type modifier(s). There are three kinds of type modifiers: Pointer ...
-3
votes
2answers
193 views

Zero sum in python 2.7 [closed]

Write a function in Python 2.7 that takes a list of integers as an argument. It should return True or False depending on whether there are any 2 integers in the argument list that sum to zero. [4,-4,9,...
11
votes
0answers
234 views

quine program in C using only macros [closed]

Asked on Stack Overflow, but they suggested to move it here A while ago I was reading about quines. It made me wonder if it possible to write a macro in C which will print itself, without writing any ...
10
votes
6answers
767 views

Golf my “pre-golfed” C

Background For my code-golf submissions in C, I need a processing tool. Like in many other languages, whitespace is mostly irrelevant in C source (but not always!) -- still makes the code much more ...
23
votes
20answers
2k views

Jimmy these arrays down

My coworker, Jimmy is kinda new to C/C++. He's also kind of a slow learner. Now, to be fair, his code always compiles, but he has some really sloppy habits. For example, everybody knows that you can ...
6
votes
0answers
179 views

Make an integer adder with the C preprocessor [duplicate]

The C preprocessor is not turing-complete, but you can do some pretty cool stuff with it. The challenge is to make an integer adder with it. You can use any representation you like: base 2, base 10, ...
35
votes
25answers
4k views

Expand a C array

In the C programming language, arrays are defined like this: int foo[] = {4, 8, 15, 16, 23, 42}; //Foo implicitly has a size of 6 The size of the array is ...
1
vote
1answer
571 views

Escape control characters in a C string

C/C++ use double quotes "" for making string literals. If I want the double-quote itself, or certain other characters, to appear in the string, I have to escape ...
12
votes
5answers
2k views

Risk, the Warlight Way

Introduction In this game, players use their armies to fight other players' armies, capture territories, and become the last man standing. Each turn, players receive a base number of armies to use at ...
6
votes
2answers
482 views

Implement multiplication by a constant with addition and bit shifts

While looking at the ARM instruction set, you notice that the ADD instruction has the so-called "Flexible second operand", which can be abused for quick ...
2
votes
2answers
382 views

Code Golf: Leibniz Series

This is the same question as Solve the CodeSprint4 Leibniz code golf challenge in Python in 66 characters. The variation is that I'm a C programmer. My code for the above question is: ...
5
votes
2answers
662 views

C golfing tips - returning 0 without return statement

I'm looking for some solution in returning values in main function, I need to shorten my program more, return 0 takes too much characters of my code, I need to ...
9
votes
6answers
2k views

All primes from 0 to 1000

Is it possible to make this C code smaller? It prints out all primes from 0 to 1000. C, 89 chars ...
27
votes
6answers
2k views

Write a C / C++ Polyglot

This challenge's concept is pretty simple. All you have to do is write a program that will compile as both valid C and valid C++! Well, there are some catches. The program must behave differently when ...
1
vote
0answers
369 views

Implement multiplication without using any loops, branches or function calls [closed]

I came up with the following in GNU C r=0;m(a,b){x:goto*(int*[]){&&x,&&y}[r+=a&1?0:b,a>>=1,b<<=1,a<1];y:;} One could argue ...
3
votes
3answers
351 views

Preprocessing primality

The challenge: Given an integer N, 1 < N < 2³¹, write code that uses the C preprocessor to determine whether N is prime, and produces a "Hello world" program if N is prime, or an error ...
12
votes
14answers
9k views

Compare two integers in C or C++ without comparison operators

Produce the shortest program which takes two signed integers as input (through stdin or as arguments) and displays 3 different outputs depending upon whether the first number is (1) greater than, (2) ...
7
votes
2answers
984 views

Smallest Lua table copy

So I really like to mess with tables and shallow and deep copies and I can do it in 239 chars! I wonder if there's a shorter way to do it... Rules: It must be done in less than 239 chars. You must ...
23
votes
4answers
2k views

Add two numbers safely, in C

Everyone knows that C is a lovely, safe, high level programming language. However you, as a coder are set the following task. Write a program to add two numbers. Input: Two space separated ...
11
votes
14answers
2k views

Golfed+Fast sorting in C

[Latest update: benchmark program and preliminary resuls available, see below] So I want to test the speed/complexity tradeoff with a classic application: sorting. Write an ANSI C function that ...
18
votes
1answer
2k views

Create a C preprocessor

The goal is to create a preprocessor for the C language, as small as possible in terms of source code size in bytes, in your preferred language. Its input will be a C source file, and its output will ...
12
votes
7answers
1k views

C code indenter

Situation: You are a high school teacher, teaching your computing class how to write C programs. However, since it is just the beginning of the term, you haven't taught them about the importance of ...
6
votes
9answers
2k views

Shortest C \ C++ function to generate -1, 0 ,1 [closed]

Let a function receive two integers i and j between 0 and ...
0
votes
0answers
133 views

reversion of string in c [duplicate]

In python reversing of string is as easy as: string[::-1] But not very same in c. This is the code I am using to reverse integers in C: ...
-3
votes
3answers
669 views

C without the braces [closed]

My challenge is language-specific, but needs a lot of creativity: write a valid C program with only one pair of braces {} and none of the keywords ...
5
votes
6answers
1k views

Make FizzBuzz using only preprocessor directives

I have a source file here: #include <FizzBuzz.h> fizzbuzz And I want to make "fizzbuzz" turn into a FizzBuzz program ...
3
votes
1answer
796 views

Flip the commented-out code [closed]

This puzzle works for any programming language that has the same // single-line and /* ... */ multi-line comments that C++, C# ...
5
votes
1answer
14k views

Smallest C code to Crash an Operating System? [duplicate]

A shortest C program, that can crash the Operating system, either on a Windows or a Linux PC... Rules: The code is to be developed in C language, but it may use other api, specific to linux or ...
12
votes
1answer
712 views

Implement an IEEE 754 64-bit binary floating point number through integer manipulation

(I've tagged the question "C" for the time being, but if you're aware of another language that supports unions, you can also use that.) Your task is to build the four standard mathematical operators <...
0
votes
2answers
316 views

Shortest conditional to run a specific statement: C code golf

Currently I have the code (expanded for your benefit): <number> != <number> ? printf("|\\"); : <number>; The idea here, is to do this condition ...
7
votes
21answers
5k views

print 1 to 100 without using recursion and conditions [closed]

can we print 1 to 100 without using any if conditions and loops in c&c++? Conditon: main point is you must not use recursion...and doesnt hardcode code in it for e.g ...
17
votes
7answers
2k views

C: replace AES FIPS-197 SubBytes table by constant-time code

In FIPS-197 (the Advanced Encryption Standard, known as AES), it is made heavy use of SubBytes, which could be implemented as ...
23
votes
3answers
8k views

Create a C program that takes the longest period of time to compile in gcc

Create a short C program that takes an absurdly long time to compile with gcc. Entries will be scored by timing the compilation then subtracting the reference program's compile time. Rules Any C ...
5
votes
6answers
2k views

RPN calculator without pointers

Me and some other students were at a bar discussing programming languages, in particular the C programming language. At some point a first year student said he didn't need no damn pointers. Another ...
4
votes
4answers
3k views

The shortest code to tell if a number is even or odd

One of my colleagues proposed us a challenge: to write the shortest C/C++ program to determine if a number is even or odd. These are the requirements: Get a number from keyboard, at run-time (not ...
2
votes
3answers
775 views

m3ph1st0s's programming puzzle 4 (C/C++): “3-way swap” [closed]

My last puzzle has generated some confusion and controversy so I decided to give up, for now, on those "replace one character"-type puzzles. Hope this 4th puzzle will redeem myself after all the ...
11
votes
5answers
2k views

m3ph1st0s's programming puzzle 3 (C): “Easy bug” [closed]

This is the 3rd of my series of C/C++ puzzles; in case you missed the first 2 they are here: (1) m3ph1st0s's programming puzzle 1 (C++) (2) m3ph1st0s's programming puzzle 2 (C++): "Call ...
90
votes
98answers
17k views

When does (x == x+2)? [closed]

The challenge: Define x in such a way that the expression (x == x+2) would evaluate to true. I tagged the question with C, but ...
-2
votes
12answers
834 views

Code-golf: square of the number of ones

I'm solving problem: in file "a.in" given the number N - length of the number consists of ones. Need to gets square of it, and put this in file "a.out". This is my shortest solution(150 bytes): ...
6
votes
4answers
791 views

Convert __DATE__-style string to sortable string

Goal is to write the shortest possible C89 and C99-compliant single-module C program which will compute and print out a single-line string whose sort order will correspond with the date given by the ...
7
votes
12answers
1k views

“First time” test

What is the shortest C expression that is defined to evaluated to false the first time and true for every time after that: Reference implementation: ...
24
votes
6answers
3k views

Write the prettiest bytebeat composition

Bytebeat is a style of music one can compose by writing a simple C program that's output is piped to aplay or /dev/dsp. ...
4
votes
2answers
747 views

HangOver ACM problem!

there is only one rule: get accept for this problem http://acm.ut.ac.ir/sc/problemset/view/1032! to submit your code, you need to create an account(if you don't want to do so, just post your code ...
10
votes
2answers
656 views

Which Numbers Would Crash this Function?

Which values of x and y will cause a crash with some C compilers? int f(int x, int y) { return (y==0) ? 0 : (x/y); }
7
votes
7answers
2k views

Shortest code that return SIGSEGV among the given languages [duplicate]

I come across a question in a coding competion "write a code that returns SIGSEGV(Segmentation fault ) " . Points were given on the basis of length of code. The prgramming languages available ...
2
votes
2answers
802 views

Fastest C/C++ comparison function for an opaque field containing doubles

Description: Write the fastest comparison function for an opaque field containing a double. Here is my example: ...
4
votes
5answers
3k views

C: Output “tomorrow's date”

I'm a code golf novice, but I'm eager to give it a go. My challenge is: Write the shortest program in C which takes in a date D as input and outputs the date ...
125
votes
48answers
20k views

Tips for golfing in C

What general tips do you have for golfing in C? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to C (e.g. "remove comments" is not an ...
-1
votes
1answer
4k views

Poisson random number generator [closed]

What is the shortest function (implemented in C) that will return a random draw from a Poisson distribution (given the mean or λ parameter)? ...