c

How to convert integer to hexadecimal in C?

How to convert integer to hexadecimal in C? ...

can prefix and post be used at the same time on a variable in c

#include <stdio.h> int main() { char *q; char *p = "sweta"; q = ++p++; printf("%s", q); } what is the output is this program valid as it gives an error of l value required. ...

insert a bash like widget into gtk

Hi, When you write a program in c which has a lot of printf for a lot purpose: debuggin, information, etc. I would like to do something like that in gtk, having some widget that would display all the printf messages I usually would do in a normal c program Thanks, ---UPDATE--- I'm using vte as ptomato advised. Although some problems ...

ANSI C unions - are they really useful?

From a response to some question yesterday, I learned that it is nonportable and unsafe to write into one union member and read the value from another member of a different type, assuming underlying alignment of the members. So after some research I found a written source that repeats this claim and specifies a popular example - using un...

Why does this simple C command line tool always exit without working?

I've just typed in an example from K&R... #include <stdio.h> int main (int argc, const char * argv[]) { int c, nl; nl = 0; while ((c = getchar()) != EOF) { if (c == '\n') { ++nl; } } printf("new lines=> %d\n", nl); return 0; } However, when I build & run, I get... minim...

Data Validation in C

I was trying out a simple program in c for validating user data. The program is supposed to identify whether a user entered character is a number, alphabet or a special character. Somehow , the code identifies every kind of input character as a number. I have appended the code below, i'd be grateful if someone could kindly point out wh...

Why spinlocks are used in interrupt handlers

Hi, I would like to know why spin locks are used instead of semaphores inside an interrupt handler. Thanks & Regards, Mousey ...

finding errors in a given c code

I am interested to know on what things I need to concentrate on debugging c code without a debugger. What are the things to look for? Generally I look for the following: Check whether correct value and type is being passed to a function. Look for unallocated and uninitialized variables Check for function syntax and function is used i...

Dereferencing pointer does break strict anti-aliasing rules using Berkeley sockets

I've got code that looks something like this, where addr is a sockaddr*: struct sockaddr_in *sin = (struct sockaddr_in *) addr; const char *IP=inet_ntoa(sin -> sin_addr); I believe this is very typical code for using Berkeley sockets. However, when I compile this, I'm getting the following warning: dereferencing pointer 'sin' does br...

How to enable C autocomplete in Code::Blocks?

How to enable C Std lib functions autocomplete in Code::Blocks? ...

Bit twiddling: which bit is set?

I have a 64-bit unsigned integer with exactly 1 bit set. I'd like to assign a value to each of the possible 64 values (in this case, the odd primes, so 0x1 corresponds to 3, 0x2 corresponds to 5, ..., 0x8000000000000000 corresponds to 313). It seems like the best way would be to convert 1 -> 0, 2 -> 1, 4 -> 2, 8 -> 3, ..., 2^63 -> 63 a...

Why won't this C program pick up escaped backslashes?

I'm doing K&R's Exercise 1-10 Write a program to copy its input to its output, replacing each tab by \t, each backspace by \b and each backslash by \\. This makes tabs and backspaces visible in an unambiguous way. I came up with this... #include <stdio.h> int main () { int c; printf("\n"); // For readability while (...

What is size of the time variable in C programming?

What is the size of the time variable that are used to show datetime in bits ? Actually in time.h file the time variable that are used, are they store into int variable or what ? ...

Problem with character input by getchar() or getche().

When I compile the C codes (from Teach yourself C, 2nd edition-Herbert Schildt) written below in Borland C++ v5.02, I get a warning message as below: "c61.c(7,6): Conversion may lose significant digits" What's wrong? #include "stdio.h" main() { char ch; ch = getchar(); printf(" You typed: %c", ch); return 0; } Same problem with...

Trying to tokenize a string separated by commas

I'm novice so be gentle. trying to read a file of strings as such: "1,Duck_Soup,1933,Comedy,5,12" and tokenize it to different vars for each of the tokens between the commas. That's my code and I keep getting "segmentation fault" no matter what I try. Please help me fix the code, thank you. For starters I want to make it print the tok...

Size of two structs are the same although one of them has a struct instance in them

typedef struct BaseMessage { int u32DeviceID : 32; int u32CoreID : 32; unsigned int u16Class : 16; unsigned int u8OpCode : 8; unsigned int u16CRC : 16; } BaseMessage; typedef struct MessageWithParameters { BaseMessage base; int u8Param1 : 8; int u8Param2 : 8; } MessageWithParameters; typedef ...

What is the right way to typedef a type and the same type's pointer?

What is the right way to typedef a type and the same type's pointer? Here is what I mean. Should I do this: typedef unsigned int delay; typedef unsigned int * delayp; Or should I do this: typedef unsigned int delay; typedef delay * delayp; Or perhaps I should not typedef pointer at all and just use delay * instead of delayp everywh...

Fast way of converting float of range -1 to 1 to short?

I need to repeatedly convert 1024+ consecutive 4 byte floats (range -1 to 1) to 2 byte shorts (range -32768 to 32767) and write to disk. Currently I do this with a loop: short v = 0; for (unsigned int sample = 0; sample < length; sample++) { v = (short)(inbuffer[sample * 2] * 32767.0f); fwrite(&v, 2, 1, file); } And this wor...

What is better way to loop over two ranges - multiply them together and do it one loop, or loop over each range separately?

I can't decide how I should loop over ranges. This way: for (int i = 0; i < max_i; i++) { for (int j = 0; j < max_j; j++) { // first way - two loops } } Or this way: for (int k = 0; k < max_i*max_j; k++) { // second way - one loop } Thanks, Boda Cydo. ...

How to use XOR to Encrypt a music file

I want to encrypt and decrypt 10000 bytes of a music file. I used the code : if(file!=NULL && g!= NULL) { while(!feof(file)) { count++; char aByte; fread(&aByte, sizeof(char), 1, file); if (count <= 10000) { if (count % 2 == 0) { if ((aByte ^ 'A')...