c

Why is C so good at crapping out on undefined variables but when a var lacks initialization, a check of if it is untrue goes fine?

e.g. I'm usually compulsive obsessive and like to do static int i = 0; if (!i) i = var; but static int i; if (!i) i = var; would also work. Why? Why can't it segfault so we can all be happy that undefined variables are evil and be concise about it? Not even the compilers complain:( This 'philosophy' of indecisiveness in C has...

Why are static variables auto-initialized to zero?

Possible Duplicate: Why global and static variables are initialized to their default values? What is the technical reason this happens? And is it supported by the standard across all platforms? Is it possible that certain implementations may return undefined variables if static variables aren't explicitly initialized? ...

Can't pinpoint Segmentation fault

Possible Duplicate: Seg Fault in Knowledge Tree I can't pinpoint my seg fault in my information tree. It is supposed to read from file or get user input if the file does not exist. It's supposed to guess the animal based on yes or no questions. I have limited experience with c so any help would be greatly appreciated. .c fil...

Find first unset bit in buffer (optimization)

What's the fastest/cleanest way to find the bit offset of the first unset bit in an array of arbitrary length? Assume the prototype for your function is something like this size_t first_unset_bit(char unsigned const *buf, size_t bit_count, size_t start_bit); and that it may be called several times in quick succession on the same buffer....

Maximum value of unsigned char

#include <stdio.h> int main() { unsigned char i=0x80; printf("%d",i<<1); return 0; } Why does this program print 256? As I understand this, since 0x80= 0b10000000, and unsigned char has 8 bits, the '1' should overflow after left shift and the output should be 0, not 256. ...

using fscanf to read in a integer from a file

Hello, gcc 4.4.4 c89 I have the following file that contains name, age, and gender. I am trying to read in the age. "Bloggs, Joe" 34 M I can open the file successfully: fp = fopen("input.txt", "r"); if(fp == NULL) { fprintf(stderr, "Failed to open file [ %s ]\n", strerror(errno)); return 1; } And I try and read in the ag...

CustomAction succeeds on development computer, fails on deployment computer

I'm creating a WiX installer to install a program which connects to a database. To help with this, I've created a C dll which checks to see if a certain instance of SQL exists on a server: extern "C" UINT __stdcall DBConTest(MSIHANDLE hInstaller) { FILE *fp; fp = fopen("dbcontestdll.txt", "w"); _ConnectionPtr pCon; int iErrCode; HRE...

What happens to fields not named by a designated initializer?

In C99 (and not in C++), it's possible to initialize structs using this syntax: struct info { char name[8+1]; int sz; int typ; }; struct info arr[] = { [0] = { .sz = 20, .name = "abc" }, [9] = { .sz = -1, .name = "" } }; What happens to the unspecified fields? ...

Pass settimeofday() epoch time?

Hi, Is it possible to pass settimeofday() my time_t/epoch time value, in C? Could someone give me an example of how I could do it ... my C skills are a little rusty :S Would it be: time_t time = somevalue; settimeofday(somevalue, NULL); I don't have admin access where I'm working and so can't test it out. Thanks in advance! ...

Amazing Micro Applications and Open Source Projects

As time goes on people are finding smaller, more efficient ways to do things. I'm really impressed at some of the amazing things people have made that are so incredibly simple. Our quest for "a better wheel" has lead to things like a 3Kb javascript framework, a 1kB ORM, a tweet sized framework & DI container, an entire forum in 1kb, a 9...

gcc Linkage option -L: Alternative ways how to specify the path to the dynamic library

If I compile my source code with "-L." the dynamic library libmd5.so can be found. gcc main.c -g -O2 -Wall -o main -L. -lmd5 -lcr But if I leave the "-L."-option away, the linker does not find the dynamic library. How can I change that without having to invoke "-L."? (additional info libmd5.so and libmd5.so.1.0.1 are located in /hom...

Call .NET managed code from native C

I need to be able to call .NET 3.5 managed code in a DLL from native C. I was not provided with .h or .lib files. I've tried making a C++ DLL to expose a C-compatible interface but I don't seem to be able to include the DLLs I need to use in my project. Is there any way to call .NET managed code from native C? ...

Please explain an apparent conflict between precedence of && and || and the actual result of an expression.

main() { int i=-3, j=2, k=0, m; m = ++i || ++j && ++k; printf("%d %d %d %d", i, j, k, m); } Whats the output? The output shows j==2 and k==0, implying that ++i was evaluated (and caused the || operator to short-circuit its right hand side) before the expression ++j && ++k which appears to contradict the f...

Parse Tokens from a String - C Programming - StrTok Issue

My application produces strings like the one below. I need to parse values between the separator into individual values. 2342|2sd45|dswer|2342||5523|||3654|Pswt I am using strtok to do this in a loop. For the fifth token, I am getting 5523. However, I need to account for the empty value between the two separators || as well. 5523 ...

Compiling with OpenSSL ssl_conn errors.

I am trying to compile a small .c file which uses OpenSSL includes, at first I had problems compiling but I solved it installing libssl-dev and that solved the include errors. But now when I try to compile I get: ‘ssl_conn’ has no member named ‘encrypted’ ‘ssl_conn’ has no member named ‘write_seq’ ‘ssl_conn’ has no member named ‘read_s...

Making C functions available to php

I have a C dll containing functions and enumerations I want to make accessible from php. How can I do it? ...

Useful GCC flags for C

Beyond setting -Wall, and setting -std=XXX, what other really useful, but less known compiler flags are there for use in C? I'm particularly interested in any additional warnings, and/or and turning warnings into errors in some cases to absolutely minimize any accidental type mismatches. ...

Can I add a systemcall from a module?

Can I add a systemcall from a module? ...

What are the benefits (and drawbacks) of a weakly typed language?

I'm a big fan of PHP and it's obviously a very weakly-typed language. I realize some of the benefits include the general independence of changing variable types on the fly and such. What I'm wondering about are the drawbacks. What can you get out of a strongly-typed language like C that you otherwise can't get from a weakly-typed one li...

How do you get the HTTP Path Info in C?

I have written a C CGI executable, and I want it to be able to retrieve the PATH_INFO from Apache. For example, if I have the compiled C file as /var/www/html/file, and I request http://localhost/file/pathinfo, How do I get the pathinfo portion? If you have any idea, please help. Thanks in advance! ...