c89

Doesn't ANSI C allow printing each character at a same place after a fixed time period?

梦想与她 提交于 2019-12-11 00:30:30
问题 I am trying to generate random numbers to be printed on the console. I am programming in C on Linux. I wanted to print all the numbers at a single place after a time interval of a second for each number. I am using sleep() for stopping a 'time interval'. I tried \b , \r and all but none works. I just wanted this to run, for example : for (i = 0; i < 10; i++) { printf("%d", i); sleep(1); printf("\b"); } 回答1: stdout is probably buffered, so flush it. for(i=0;i<10;i++) { printf("%d",i); fflush

Can an ANSI C compiler remove a delay loop?

一世执手 提交于 2019-12-10 14:22:37
问题 Consider a while loop in ANSI C whose only purpose is to delay execution: unsigned long counter = DELAY_COUNT; while(counter--); I've seen this used a lot to enforce delays on embedded systems, where eg. there is no sleep function and timers or interrupts are limited. My reading of the ANSI C standard is that this can be completely removed by a conforming compiler. It has none of the side effects described in 5.1.2.3 : Accessing a volatile object, modifying an object, modifying a file, or

A legal array assignment. Is it possible?

馋奶兔 提交于 2019-12-10 04:09:58
问题 After reading the chapter about structures in the K&R book I decided to make some tests to understand them better, so I wrote this piece of code: #include <stdio.h> #include <string.h> struct test func(char *c); struct test { int i ; int j ; char x[20]; }; main(void) { char c[20]; struct {int i ; int j ; char x[20];} a = {5 , 7 , "someString"} , b; c = func("Another string").x; printf("%s\n" , c); } struct test func(char *c) { struct test temp; strcpy(temp.x , c); return temp; } My question

Typesafe varargs in C with gcc

99封情书 提交于 2019-12-09 08:19:04
问题 Many times I want a function to receive a variable number of arguments, terminated by NULL, for instance #define push(stack_t stack, ...) _push(__VARARG__, NULL); func _push(stack_t stack, char *s, ...) { va_list args; va_start(args, s); while (s = va_arg(args, char*)) push_single(stack, s); } Can I instruct gcc or clang to warn if foo receives non char* variables? Something similar to __attribute__(format) , but for multiple arguments of the same pointer type. 回答1: I know you're thinking of

Adding or assigning an integer literal to a size_t

落花浮王杯 提交于 2019-12-09 02:56:29
问题 In C I see a lot of code that adds or assigns an integer literal to a size_t variable. size_t foo = 1; foo += 1; What conversion takes place here, and can it ever happen that a size_t is "upgraded" to an int and then converted back to a size_t ? Would that still wraparound if I was at the max? size_t foo = SIZE_MAX; foo += 1; Is that defined behavior? It's an unsigned type size_t which is having a signed int added to it (that may be a larger type?) and the converted back to a size_t . Is

ANSI C: If a function pointer points to executable code does that mean less execution overhead than simply invoking the function? [duplicate]

隐身守侯 提交于 2019-12-07 21:52:16
问题 This question already has answers here : Does Function pointer make the program slow? (8 answers) Closed 2 years ago . We know that using function pointers in C can be quite helpful when used in the proper scenarios (calling a function at runtime vs compile time, making the code more readable, etc.), but there isn't much literature around simple function invocation vs using a function pointer. void foo(void) { printf("hello\n"); } int testFcn(void) { // simple invokation foo(); return 0; } //

Pass matrix as argument

最后都变了- 提交于 2019-12-07 05:06:19
问题 I want to pass two matrices as argument. These matrices have different size and i don't understand how i have to do this work: #include <stdio.h> #include <stdlib.h> void f(int m[3][], int n); int main() { int A[3][3]={{1,2,3},{4,5, 6},{7,8,9}}; int B[3][2]={{1,2},{3, 4}, {5, 6}}; f(A, 3); f(B, 2); return 0; } void f(int m[3][], int n) { int i,j; for(i=0;i<3;i++) { for(j=0;j<n;j++) printf("%5d", m[i][j]); } return; } How can I do this? 回答1: The only safe way that I know of to do this is to

C89: signed/unsigned mismatch

感情迁移 提交于 2019-12-07 04:49:49
问题 Are signed/unsigned mismatches necessarily bad? Here is my program: int main(int argc, char *argv[]) { unsigned int i; for (i = 1; i < argc; i++) { // signed/unsigned mismatch here } } argc is signed, i is not. Is this a problem? 回答1: "signed/unsigned mismatches" can be bad. In your question, you are asking about comparisons. When comparing two values of the same base type, but one signed and one unsigned, the signed value is converted to unsigned. So, int i = -1; unsigned int j = 10; if (i <

using ansi-c on windows platform can i get time of system upto milliseconds accuracy?

你说的曾经没有我的故事 提交于 2019-12-06 14:27:54
问题 i need to get the millisecond accuracy i take a look on this question but i am working on windows: it gives linking errors for POSIX functions. it will be very good if i can get UTC time since 1970 with milliseconds precision. 回答1: Not in ANSI C, but the Windows API provides a GetSystemTime function as illustrated here: http://msdn.microsoft.com/en-us/library/ms724950(v=VS.85).aspx 回答2: Sorry, but you can't do that using neither ANSI C nor the Windows API. You can get the system time with a

Safe Floating Point Division

扶醉桌前 提交于 2019-12-06 11:04:49
I have some places in my code where I want to assure that a division of 2 arbitrary floating point numbers (32 bit single precision) won't overflow. The target/compiler does not guarantee (explicitly enough) nice handling of -INF/INF and (does not fully guarantees IEEE 754 for the exceptional values - (possibly undefined) - and target might change). Also I cannot make save assumtions on the inputs for this few special places and I am bound to C90 standard libraries. I have read What Every Computer Scientist Should Know About Floating-Point Arithmetic but to be honest, I am a little bit lost.