Return error code after first detected error

I have a function which does some initialization and calls other functions, each of which returns an error code. I want to be able to return from this function after the first detected error like this: int error_code = FirstFunction(); if (error_cod...
more »

2017-12-03 20:12 (6) Answers

Best way to read & store this input?

I have an input file called animals.dat which has data in the form like:- 1,Allegra,Pseudois nayaur,S,5 2,unknown,Ailurus fulgens,X,10 3,Athena,Moschus fuscus,X,2 The code I have been using to store and process my data is this. But, for some rea...
more »

2017-12-03 07:12 (2) Answers

How to search for a string in a file and compare?

That is my first post on stackoverflow. Btw I am really noob. I would like to search a string with a comand like that: ./a.out foobar. I have a file, called list.txt, in which I have a list of names, I want to search in that file to find foobar. Wh...
more »

2017-12-02 23:12 (0) Answers

Explain line of C code in qsort

I've been looking at different implementations of qsort, and there's a line in the source found here ( that I don't understand. It looks like a function pointer declaration. I'd appreciate ...
more »

2017-12-02 00:12 (2) Answers

Bitfield endianness in gcc

The endianness of bitfields is implementation defined. Is there a way to check, at compile time, whether via some macro or other compiler flag, what gcc's bitfield endianness actually is? In other words, given something like: struct X { uint32...
more »

2017-12-01 20:12 (1) Answers

Exit from while loop without break in C

I have the following code: void takeOrder(void) { int stop = 0; while(stop != 1) { printf("What is your order?\n"); printf("%c - fruitShake\n%c - milkShake\n", FRUIT_SHAKE_CHOICE, MILK_SHAKE_CHOICE); scanf("%c", &typeChoice); i...
more »

2017-12-01 16:12 (4) Answers

Splint: local variable used before definition

I know local variables can have "random" value when not set, but is it bad to set the first value of a local variable with a pointer? For example: void setValue(int* p_val) { *p_val = …; /* Assignment does not use *p_val */ } int main(void) {...
more »

2017-12-01 14:12 (1) Answers

Cannot write to screen memory in C

I am very new to C, it's my second high-level programming language after Java. I have gotten most of the basics down, but for whatever reason I am unable to write a single character to screen memory. This program is compiled using Turbo C for DOS on...
more »

2017-12-01 08:12 (3) Answers

Why 10/3 it's exact in C?

Take a look on this code. 10/3 return 3.3333332538604736328125000 and when I multiply by 3 in a calcutor i get 9.99, but if do the same by the code i get exactly 10.00. How it's posible ? #include <stdlib.h> #include <stdio.h> int main(...
more »

2017-11-30 21:11 (2) Answers

updating time out values in select() calls

I want to implement a timer using select() and I want it to reach the timeout code after intervals of every 3 seconds. if I remove "timeout.tv_sec+=8;" from my code it waits for 3 seconds and then starts printing "time out" continuously and with "ti...
more »

2017-11-30 12:11 (2) Answers

Separating hexadecimal escape sequences in strings

Can a string constant like "foo" "\x01" "bar" be written as a single string literal (while keeping the hexadecimal notation)? With "foo\x01bar" the escape sequence seems to be interpreted as \x01ba since I get the warning "hex escape sequence out of ...
more »

2017-11-30 11:11 (3) Answers

Inspecting caller frames with gdb

Suppose I have: #include <stdlib.h> int main() { int a = 2, b = 3; if (a!=b) abort(); } Compiled with: gcc -g c.c Running this, I'll get a coredump (due to the SIGABRT raised by abort()), which I can debug with: gdb a.o...
more »

2017-11-30 10:11 (2) Answers

Re-declaring variable inside for loop in C

I have compiled following program using gcc prog.c -Wall -Wextra -std=gnu11 -pedantic command on GCC compiler. I wondered, it is working fine without any warnings or errors. #include <stdio.h> int main(void) { for (int i = 0; i == 0; i...
more »

2017-11-30 06:11 (5) Answers

Enforcing ANSI C89 with clang

I'm trying to make the C compiler clang go into ANSI C89 mode but without success. Here is an example session: $ cat t.c #include <stdio.h> int main(void) { puts(__FUNCTION__); return 0; } $ gcc -pedantic -std=c89 -Wall t.c t.c: In f...
more »

2017-11-29 19:11 (1) Answers

Is it safe to cast size_t to unsigned long int?

I need a portable way to print the value of a variable n of type size_t. Since I use ANSI C89 I cannot use the z length modifier. My current approach is to cast the value to long unsigned int: printf("%lu\n", (long unsigned int) n); Provided that ...
more »

2017-11-29 16:11 (2) Answers