What’s Next, Keyboard?
C For Dummies Blog
by dgookin
5d ago
Suppose you must write code that remains busy while checking to see whether a key has been pressed. The program repeats a loop, performing various tasks, but eager for that key press. When a key is pressed, the code fetches the key. Two things stand in your way to make this happen. First, standard input is streaming. True, input can be buffered or unbuffered, but it all comes in the stream not directly from the keyboard. Second, C library functions that read standard input are blocking calls. They wait for input, pausing the entire program. The process I’m referencing is called polling. Activ ..read more
Visit website
Squaring a Value
C For Dummies Blog
by dgookin
1w ago
The C language lacks an operator that squares a value or, more generally, raises a value to a specific power. To do so, use the pow() function. Yet I’m finding the need for a square() function as I explore some interesting and obscure mathematical thingies. In my digital travels, I’ve seen a few “square” or “power” operators. Perhaps the most common is the caret, ^, as in 2^5 which raises two to the fifth power. Remember that in C, the ^ is the bitwise exclusive OR operator. Another exponent operator I’ve seen is ** as in 2**5, which raises two to the fifth. In C, ** is used to identify a poi ..read more
Visit website
The alloca() Function
C For Dummies Blog
by dgookin
2w ago
Any memory allocated in a function is retained when the function leaves, unless you first free it. This rule applies to all memory allocation functions — except one. Of course, memory allocated in the main() function is freed when it returns (the program exits). This is the reason why I’m often lazy and forget to free memory at the end of the main() function, a sin for which I repent. But an exception to the memory allocation/freeing rule is the alloca() function. Like an auto variable, the alloca() function’s scope is local to the function it’s used in: Memory is allocated, but released when ..read more
Visit website
Calculating the Subfactorial – Solution
C For Dummies Blog
by dgookin
2w ago
Do you feel adequately deranged working this month’s Exercise? I’m more of a math fanboy than an expert, yet I enjoyed the process of coding a subfactorial based in the equation presented. My solution does require a recursive function to determine a factorial; factorials are needed when calculating a subfactorial. But my derange() function need not be recursive. It’s sequential, which is obvious when looking at the formula shown in Figure 1. Figure 1. The equation for calculating a subfactorial. Recursive functions work well for continued fractions, but the equation in Figure 1 is finite. A ..read more
Visit website
Calculating the Subfactorial
C For Dummies Blog
by dgookin
3w ago
Difficulty: ★ ★ ★ ★ Calculating a factorial is a common programming exercise, often coupled with introducing the concept of recursion. Mathematically, the factorial has a twin: the subfactorial. Its calculation can be a fun programming challenge, which is the subject of this month’s Exercise. As a review, a factorial is a value written as n!. The value represents integer n multiplied sequentially. For example, 5! is 1×2×3×4×5, which equals 120. This value is the number of permutations possible with five items. Figure 1 illustrates 3! or three factorial. It shows the number of ways to arrange ..read more
Visit website
Fun with switch case, Part I
C For Dummies Blog
by dgookin
1M ago
The switch-case construction, or switch statement, provides your code with a decision tree that both easy to read and to debug. This construction is a bit daunting for the beginner, but becomes more familiar as you use it. It’s not without its quirks. As a review, the switch statement is followed by an expression. switch(a) The expression is often a single variable, a above, though it can be any valid C language expression. The result of this expression is compared with a block of case statements: case const: Each case keyword is followed by a constant, which is compared with the switch state ..read more
Visit website
Creating Your Own Environment Variables
C For Dummies Blog
by dgookin
1M ago
Programs use environment variables, thanks to the getenv() function shown in last week’s Lesson. They can also create their own environment variables, reset variable values, and remove variables. To add your own environment variable, use the setenv() function, prototyped in the stdlib.h header file. Here is the man page format: int setenv(const char *name, const char *value, int overwrite); Strings name and value are strings representing the environment variable name and value to add. The overwrite option is zero or non-zero. If overwrite is zero and name already exists, it’s not overwritten ..read more
Visit website
Peering into the Environment
C For Dummies Blog
by dgookin
1M ago
The operating system’s environment provides temporary storage for variables, settings, and options. These values are easily accessible from any C program. One of the first posts I made on this blog was about accessing the environment. Time to revisit. You can view the environment from a terminal window. In Linux, type the env command to see a huge dump of environment variables. For Windows/DOS, type the set command. The output shows a list of environment variables. It’s often long and detailed. The format for each variable is the same. For example: HOME=/home/dang The HOME variable in Linux i ..read more
Visit website
Positive Negative Positive Negative – Solution
C For Dummies Blog
by dgookin
1M ago
This month’s Exercise presents what I often refer to as a code “toggle.” Many paths lead to a solution. Because of my interest in coding various math equations, my goal is to craft a solution distilled into a single expression. Turns out that all of my solutions easily fit into a single expression, even the one that didn’t work. For my first solution, I use the modulus operator in a ternary expression: 2024_02-Exercise-a.c #include <stdio.h> int main() { int a; for( a=0; a<14; a++ ) printf("%d\n",a%2?-1:1); return(0); } Variable a loops from 0 through 14. The ..read more
Visit website
A Nifty Little Test
C For Dummies Blog
by dgookin
1M ago
When I teach C programming, I’m careful to admonish beginners about the difference between the = (assignment) and == (is equal to) operators. Yet there are times when these two operators collaborate. The single equal sign is the assignment operator, used in expressions to set a value: a = 32; All too often, this operator is mistakenly used as a comparison: if(a=32) Most compilers flag this operation with a warning. Yep, you probably meant to write: if(a==32) Still, the improper format does result in a comparison. The expression a=32 evaluates to 32, or whatever the assignment. In the world of ..read more
Visit website

Follow C For Dummies Blog on FeedSpot

Continue with Google
Continue with Apple
OR