Archive

Archive for the ‘c programming language’ Category

[ C lang] reading a text file line by line

February 22, 2020 Leave a comment

Problem
I had a text file that I was reading line by line in C. Every line was tokenized. The file looked like this:

1978	Aachen Cathedral	DE	C	EUR	0	3
1978	City of Quito	EC	C	LAC	70	2
1978	Galápagos Islands	EC	N	LAC	14066514	1
...

I converted every year to an integer. From the 2nd line I got 1978 (as int), but in the first line the string to int conversion failed.

Solution
It turned out that the token “1978” in the first line was 7 characters long, not 4. At that point I opened the file in a hex editor and there were 3 special bytes at the beginning: EF BB BF. This is a byte order mark (BOM).

bom

As a quick fix, I removed those 3 bytes in the hex editor.

I tried it under Python too and it caused a problem there too:

>>> f = open("input.txt")
>>> s = f.readline()
>>> s
'\ufeff1978\tAachen Cathedral\tDE\tC\tEUR\t0\t3\n'
>>> s.split()
['\ufeff1978', 'Aachen', 'Cathedral', 'DE', 'C', 'EUR', '0', '3']
>>> s.split()[0]
'\ufeff1978'
>>> int(s.split()[0])
Traceback (most recent call last):
  File "", line 1, in
ValueError: invalid literal for int() with base 10: '\ufeff1978'
>>>

If you know how to ignore those bytes during file read, please leave a comment. I’m interested in both C and Python solutions.

C compiler for Windows

January 15, 2020 Leave a comment

Problem
You need a C compiler under Windows.

Solution
At https://nuwen.net/mingw.html you can find a MinGW distribution, which is x64-native and contains GCC and Boost. Download the installer, install it, and add its “bin” folder to your PATH.

Then you have a C compiler under Windows.

Using sqrt in C lang.

January 3, 2020 Leave a comment

Problem
The following code compiles with “gcc in.c“:

#include 
#include 

int main()
{
    double result = sqrt(9.0);
    printf("%lf\n", result);

    return 0;
}

The following code drops an error if you try to compile with “gcc in2.c“:

#include 
#include 

int main()
{
    double value = 9.0;
    double result = sqrt(value);
    printf("%lf\n", result);

    return 0;
}

The error message says: “undefined reference to `sqrt'”.

Solution and Explanation
In the second case, compile it with “gcc -lm in2.c“, which links the math library. But why does the first example work? In that case the compiler figures out (knows) the value of the square root operation and replaces the function call with a constant 3.0 value. The optimizer is that clever :) In the second case however it tries to call the sqrt() function but it’s not found. If you link the math library, it’ll find it.

IDE for C under Windows

April 13, 2014 Leave a comment


Problem
A friend of mine asked me to help him in C programming. He has Windows. What IDE to use?

Solution
Code::Blocks is an excellent choice. It has an installer that also contains a C compiler. Awesome. Just install it and you are ready to develop. It has all the nice features that you expect from an IDE.

Code::Blocks is actually cross platform, thus it exists under Linux too! It’s also good for C++. It’s open source.

Categories: c programming language, linux, windows Tags: ,

The “Clockwise/Spiral Rule” of C

February 3, 2014 Leave a comment

There is a technique known as the “Clockwise/Spiral Rule” which enables any C programmer to parse in their head any C declaration.

Here it is: http://c-faq.com/decl/spiral.anderson.html.

C gibberish ↔ English

September 3, 2013 Leave a comment

See http://www.cdecl.org/.

Examples:

int *p
    declare p as pointer to int
int *p[]
    declare p as array of pointer to int
int (*p)[]
    declare p as pointer to array of int

Discussion @reddit.

Categories: c programming language Tags:

Dark corners of C

February 27, 2013 Leave a comment

A Quiz About Integers in C

The C language’s rules for integer operations have some quirks that can make even small programs behave in confusing ways. This post is a review of these rules in the form of a quiz containing 20 questions. I recommend going through the questions in order. If you are a beginning C programmer, you should consult a C book as you go through these questions since there are a lot of little things (such as what “1U” means) that I have not bothered to explain. If you are a serious C programmer, I expect you’ll do well — this quiz is not intended to be extremely difficult. You should assume C99. Also assume that x86 or x86-64 is the target. In other words, please answer each question in the context of a C compiler whose implementation-defined characteristics include two’s complement signed integers, 8-bit chars, 16-bit shorts, and 32-bit ints. The long type is 32 bits on x86, but 64 bits on x86-64 (this is LP64, for those who care about such things). Summary: Assume implementation-defined behaviors that Clang / GCC / Intel CC would make when targeting LP64. Make no assumptions about undefined behaviors.

Categories: c programming language Tags: ,