Calculating the Standard Deviation
C For Dummies Blog
by dgookin
3d ago
Difficulty: ★ ★ ☆ ☆ Even when statistics has barely brushed by you on the subway, you probably know the term standard deviation. It refers to how data is distributed in a group, their distance from the mean. You can use your C programming kung fu to code the standard deviation of a data set, which is the challenge for this month’s Exercise. Before marching naked into the unforgiving jungle of statistics, here are some terms to know: mean The sum of the values in a set divided by the number of values, symbol μ. The mean is what most people refer to when they say “average.” median The value in ..read more
Visit website
From Void to Integer
C For Dummies Blog
by dgookin
1w ago
You can’t just slap a data type upon a variable declared as void. No, a void type can be assigned a pointer. But even then, you can’t just typecast that pointer as an int and get away with it. From last week’s Lesson, I devised the code below to properly allocate void variable a as storage for an integer. But the code generates a few mismatch warnings and crashes when the program runs: 2024_04_20-Lesson-c.c #include <stdio.h> #include <stdlib.h> int main() { void *a; a = malloc(sizeof(int)); printf("Enter an integer: "); scanf("%d",(int)a); printf("You ente ..read more
Visit website
Out of the void
C For Dummies Blog
by dgookin
2w 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
3w 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
3w 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
3w 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
1M 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
1M 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
1M 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

Follow C For Dummies Blog on FeedSpot

Continue with Google
Continue with Apple
OR