Out of the void
C For Dummies Blog
by dgookin
2d ago
The four standard data types in C programming are char, int, float, and double. A fifth type is void, which is used to declare storage of an unknown type. My goal is to see how badly I can abuse this data type. The void data type used primarily with pointers. For example, in the qsort() function, the compar() function is prototyped like this: int (*compar)(const void *, const void *)); The const void * argument represents a pointer of an unknown type. When you code your own compar() function, you substitute const void * with a pointer based on the data type of whatever data the qsort() functi ..read more
Visit website
C Blog 11th Anniversary
C For Dummies Blog
by dgookin
1w ago
Today marks this blog’s 11th anniversary. The first post was made 11 years ago on April 13th, 2013. This date coincided with the release of my book, Beginning Programming with C For Dummies. The original C For Dummies website (launched in 1997) lacked a blog, which meant I had to write new pages to add articles updating information from the book or offering challenges. This approach proved to be too cumbersome, so I added the blog. I wrote and and posted for a few months before the book came out to ensure that new users would see material and not view a disappointing “coming soon” page. Sinc ..read more
Visit website
Using the epoll() Function to Scan for Standard Input
C For Dummies Blog
by dgookin
1w ago
As with the select() function covered in last week’s Lesson, you can use other networking functions to scan for pending standard input while a program otherwise spins busy. The epoll() family of functions allow for such monitoring. The epoll() functions are preferred by network programmers over the older select() function. They rely more on functions than macros to get the job done, though the steps involved are similar: Create a set of file descriptors to monitor, monitor the descriptors, act when one of the descriptors resports as busy. Here is the updated code from last week’s Lesson: 2024 ..read more
Visit website
Calculating the Absolute Value – Solution
C For Dummies Blog
by dgookin
2w ago
This month’s Exercise may not have been challenging. Still, remember that blog is about learning the C language. Sometimes performing what could be a simple task may end up being complex than you anticipated. Sadly, this challenge probably wasn’t that complex. Still, it opens an insight into a programming quirk you may not have expected. Before revealing the quirk, here is my solution: 2024_04-Exercise.c #include <stdio.h> int main() { int a; printf("Enter an integer: "); scanf("%d",&a); if( a<0 ) a = -a; printf("The absolute value is %d\n",a ..read more
Visit website
Reading the Keyboard Queue ala Networking
C For Dummies Blog
by dgookin
2w ago
A network program monitors one or more file descriptors (such as sockets) for activity. It reacts when information is ready to process. This technique can also be applied to keyboard input, though not as elegantly as the kbhit() function shown in last week’s Lesson. Networking programming is a complex booger of a topic, primarily because the code deals with multiple input and output sources as once. Each connection is monitored for activity in an endless loop. When activity is detected on one of the sources, the program acts. One way to monitor the several network connections is to use the se ..read more
Visit website
Calculating the Absolute Value
C For Dummies Blog
by dgookin
2w ago
Difficulty: ★ ☆ ☆ ☆ I started my technology writing career at a computer book publishing house, CompuSoft. It’s no longer around, but I do recall ghost writing books such as the BASIC Handbook, which was an encyclopedia of the BASIC programming language. The first command listed in this book was ABS. The ABS command returns the absolute value of an integer, which is its positive value: ABS(5) is 5. ABS(-200) is 200. I’ve seen absolute values used when dealing with exponentiation as some calculations result in both positive and negative values. Whatever. Still, it’s easier for me to explain h ..read more
Visit website
Looking for a Keyboard Hit
C For Dummies Blog
by dgookin
3w ago
The C language is famously platform independent. This feature may seem unimportant these days, but back in the early computer era having a language you code code on multiple systems was key to the C language’s success. This benefit may be one reason why C lacks a specific function to check on the keyboard status to determine whether a key has been pressed. Such a function is hardware-dependent. Even so, C does have a function that lets you plumb the depths of a specific piece of hardware. It’s the mysterious and often feared function ioctl(). Rather than struggle to pronounce it, consider it ..read more
Visit website
What’s Next, Keyboard?
C For Dummies Blog
by dgookin
1M 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
1M 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
1M 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

Follow C For Dummies Blog on FeedSpot

Continue with Google
Continue with Apple
OR