<< < 165 166 167 168 169 170 171 172 173 174 > >>   Sort: Date

Why does this code crash?
Why does this code: char *p = "hello, world!"; p[0] = 'H'; crash? String constants are in fact constant. The compiler may place them in nonwritable storage, and it is therefore not safe to modify them. When you need writable strings, you must allocate writable memory for them, either by declaring an...
2015-05-27, 1182👍, 0💬

Why is my simple program, which hardly does more than print
Why is my simple program, which hardly does more than print ``Hello, world!'' in a window, compiling to such a huge executable (several hundred K)? Should I #include fewer header files? What you're seeing is the current (poor) state of the ``art'' in library design. As run-time libraries accrete mor...
2015-07-06, 1181👍, 0💬

How can I access an I O board directly?
How can I access an I O board directly? At one level, at least, it's quite simple: you have a device register which is actually wired up so that the bits written to it get coverted to actual voltage levels in the real world that you can do interesting things with. In general, there are two ways to g...
2015-04-20, 1181👍, 0💬

I am splitting up a program into multiple source files for the first time ...
I'm splitting up a program into multiple source files for the first time, and I'm wondering what to put in .c files and what to put in .h files. (What does ``.h'' mean, anyway?) As a general rule, you should put these things in header (.h) files: macro definitions (preprocessor #defines) structure, ...
2016-02-18, 1180👍, 0💬

What are the complete rules for header file searching?
What are the complete rules for header file searching? The exact behavior is implementation-defined (which means that it is supposed to be documented; Typically, headers named with &lt;> syntax are searched for in one or more standard places. Header files named with "" syntax are first searched ...
2016-02-12, 1180👍, 0💬

I keep getting errors due to library functions being undefined, but I am including all the right header files.
I keep getting errors due to library functions being undefined, but I am including all the right header files. In the general case of calling code in an external library, using #include to pull in the right header file(s) is only half of the story; you also have to tell the linker to search the exte...
2015-07-14, 1180👍, 0💬

How can I do serial comm port I O?
How can I do serial (``comm'') port I/O? It's system-dependent. Under Unix, you typically open, read, and write a device file in /dev, and use the facilities of the terminal driver to adjust its characteristics.Under MS-DOS, you can use the predefined stream stdaux, or a special file like COM1, or s...
2015-04-22, 1180👍, 0💬

Why cant I open a file by its explicit path?
Why can't I open a file by its explicit path? The call fopen("c:\newdir\file.dat", "r") is failing. The file you actually requested--with the characters \n and \f in its name--probably doesn't exist, and isn't what you thought you were trying to open. In character constants and string literals, the ...
2015-04-08, 1180👍, 0💬

I have got this tricky preprocessing I want to do and I cant figure out a way to do it.
I have got this tricky preprocessing I want to do and I cant figure out a way to do it. C's preprocessor is not intended as a general-purpose tool. (Note also that it is not guaranteed to be available as a separate program.) Rather than forcing it to do something inappropriate, you might want to wri...
2016-01-19, 1179👍, 0💬

How can I generate random numbers with a normal or Gaussian distribution?
How can I generate random numbers with a normal or Gaussian distribution? There are a number of ways of doing this. 1. Exploit the Central Limit Theorem (``law of large numbers'') and add up several uniformly-distributed random numbers: #include &lt;stdlib.h> #include &lt;math.h> #define NSU...
2015-07-22, 1179👍, 0💬

What are the differences between C and CPP?
What are the differences between C and CPP? Q: Is C++ a superset of C? What are the differences between C and C++? Can I use a C++ compiler to compile C code? A: C++ was derived from C, and is largely based on it, but there are some legal C constructs which are not legal C++. Conversely, ANSI C inhe...
2015-01-10, 1179👍, 0💬

My compiler is complaining that printf is undefined ...
My compiler is complaining that printf is undefined! How can this be? It's the world's most popular C function... Allegedly, there are C compilers for Microsoft Windows which do n ot support printf, on the argument that printf is for printing to old-fashioned terminals, while under Windows the right...
2015-07-03, 1178👍, 0💬

I have a program that seems to run correctly
I have a program that seems to run correctly, but it crashes as it's exiting, after the last statement in main(). What could be causing this? A:There are at least three things to look for: 1. If a semicolon in a previous declaration is missing, main might be inadvertently declared as returning a str...
2015-05-29, 1178👍, 0💬

How can I return a sequence of random numbers which dont repeat at all?
How can I return a sequence of random numbers which dont repeat at all? What you're looking for is often called a ``random permutation'' or ``shuffle.'' One way is to initialize an array with the values to be shuffled, then randomly interchange each of the cells with another one later in the array: ...
2015-07-24, 1176👍, 0💬

How can I make it pause before closing the program output window?
I'm compiling some test programs on a windows-based system, and the windows containing my program's output are closing so quickly after my program calls exit that I can't see the output. How can I make it pause before closing? After wondering why the author of your compiler's run-time system didn't ...
2015-04-24, 1176👍, 0💬

I heard that you have to include stdio.h before calling printf. Why?
I heard that you have to include stdio.h before calling printf. Why? So that a proper prototype for printf will be in scope. A compiler may use a different calling sequence for functions which accept variable-length argument lists. (It might do so if calls using variable-length argument lists were l...
2015-06-17, 1175👍, 0💬

Why doesn't sizeof tell me the size of the block of memory pointed to by a pointer?
Why doesn't sizeof tell me the size of the block of memory pointed to by a pointer? sizeof tells you the size of the pointer. There is no portable way to find out the size of a malloc'ed block. (Remember, too, that sizeof operates at compile time
2016-03-21, 1174👍, 0💬

I am checking a string to see if it matches a particular value...
I'm checking a string to see if it matches a particular value. Why isn't this code working? char *string; ... if(string == "value") { /* string matches "value" */ ... } Strings in C are represented as arrays of characters, and C never manipulates (assigns, compares, etc.) arrays as a whole. The == o...
2016-03-11, 1174👍, 0💬

I heard that you have to include ......
I heard that you have to #include before calling printf. Why? So that a proper prototype for printf will be in scope. A compiler may use a different calling sequence for functions which accept variable-length argument lists. (It might do so if calls using variable-length argument lists were less eff...
2016-01-08, 1172👍, 0💬

Why doesnt this code work?
Why doesn't this code: double d; scanf("%f", &amp;d); work? Unlike printf, scanf uses %lf for values of type double, and %f for float.%f tells scanf to expect a pointer-to-float, not the pointer-to-double you gave it. Either use %lf, or declare the receiving variable as a float.
2015-10-30, 1172👍, 0💬

How can I send mail from within a C program?
How can I send mail from within a C program? Under Unix, open a pipe to the mail program, or perhaps /usr/lib/sendmail.
2015-04-17, 1172👍, 0💬

Does C have anything like the `substr extract substrin routine present in other languages?
Does C have anything like the `substr extract substrin routine present in other languages? Not as such. To extract a substring of length LEN starting at index POS in a source string, use something like char dest[LEN+1]; strncpy(dest, &source[POS], LEN); dest[LEN] = '\0'; /* ensure \0 termination...
2016-03-07, 1171👍, 0💬

How can I check whether a file exists? I want to warn the user if a requested input file is missing.
How can I check whether a file exists? I want to warn the user if a requested input file is missing. It's surprisingly difficult to make this determination reliably and portably. Any test you make can be invalidated if the file is created or deleted (i.e. by some other process) between the time you ...
2015-04-15, 1171👍, 0💬

Why do some people write if(0 == x) instead of if(x == 0)?
Why do some people write if(0 == x) instead of if(x == 0)? It's a trick to guard against the common error of writing if(x = 0) If you're in the habit of writing the constant before the ==, the compiler will complain if you accidentally type if(0 = x) Evidently it can be easier for some people to rem...
2015-05-15, 1170👍, 0💬

<< < 165 166 167 168 169 170 171 172 173 174 > >>   Sort: Date