Sam Magura

Code, open-source, .etc

C programming example 2: Word reverser

Example 2: word_reverser

(View source)

This program prompts the user for a string, breaks the string into words,
reverses each word, and then prints the reversed words in their original order.
This example is intended to serve as an introduction to strings.

Includes examples of:

  • Strings as pointers and arrays
  • Using getline to read a line of text from the user
  • String functions: strchr, strcpy, strcat, strlen, strtok
  • Functions that write their return value to a char * taken as an argument
  • For loop

C programming example 1: Hello world

I have been learning the C programming language. I’d like to post the simple programs I’ve created, since I’ve found that there are few C programming resources available online — compared to number available for Python, Java, .etc.

These programs are targeted for the Linux user using the GNU C compiler (GCC).  I recommend downloading the official GNU C manual.  However, the source could still prove useful to Windows users; just don’t expect it to compile as is.

All of the code will be available in my GitHub repository dedicated to this project. The contents of the README files will be identical to what I will post here on the blog — except for introductions like this. The first example is actually two independent programs. The source can be found under the `hello_world` directory in the GitHub repository. All code is offered under the GNU General Public License; you are free to study, modify, and distribute any or all of it.

More examples will follow shortly! Thanks for reading.

 

Example 1: hello_world

(View source)

Two very simple programs. The programs should be compiled separately. To compile one, type:

$ gcc hello.c

This will create a new file called a.out. Run it by typing:

$ ./a.out

hello.c

The classic Hello World program. Uses the printf function.

guess.c

A slightly more complicated program. The program generates a random integer, and
prompts the user to guess the number. The user is notified whether their guess is
higher or lower than the actual number. This continues until the correct number
is guessed.

Includes examples of:

  • Functions, including return values
  • Using scanf to read integers from standard input
  • Random number generation, using time as seed
  • While loop, if statement

Intel Centrino Wireless-N 1030 on Linux

This wireless card came in my new Dell Inspiron 14z laptop, but I had to make a few changes for it to work correctly under Linux. I got this information from other posts scattered across the Internet, but I thought I would write them up here to save people some trouble. These changes were necessary under Linux 3.0.0-14-generic; they may not be required on newer versions. These fixes may be applicable to other wireless devices that use the iwlagn driver.

With power management and wireless-N enabled on my card, I get very poor connectivity. The device connects to the network, but rarely exceeds a download rate of over 1 kB/s.

First, power management needs to be disabled. You can check if power management is currently enabled by running iwconfig wlan0 from a terminal. This should output something like this:

wlan0     IEEE 802.11bg ESSID:"Femto"
Mode:Managed Frequency:2.437 GHz Access Point: 00:25:9C:45:94:56
Bit Rate=54 Mb/s Tx-Power=15 dBm
Retry long limit:7 RTS thr:off Fragment thr:off
Power Management:off
Link Quality=52/70 Signal level=-58 dBm
Rx invalid nwid:0 Rx invalid crypt:0 Rx invalid frag:0
Tx excessive retries:0 Invalid misc:887 Missed beacon:0


To turn power management off manually, do

sudo iwconfig wlan0 power off

and type in your password. You can permanently disable power management by editing the file

/usr/lib/pm-utils/power.d/wireless

On line 39, change “power on” to “power off”. You will need root access to edit this file. (Use sudo.) Credit to this poster for discovering this fix!

Next, wireless-N needs to be disabled. It seems as though Intel and/or the kernel developers are still working on wireless-N support for this device. If you run dmesg | grep iwlagn to search the kernel’s log for messages from the card’s driver, you may see the message iwlagn: Microcode SW error detected., followed by several lines of debug data.

Disabling wireless-N will prevent this error from occurring. This change is not required if you know you will only be using wireless access points that do not support wireless-N. (For example if your access point says wireless-G somewhere on it.)

Type in these commands at a terminal:

sudo rmmod iwlagn
sudo modprobe iwlagn 11n_disable=1

This unloads the iwlagn kernel module, then re-enables it without wireless-N. If the 11n_disable option is not recognized, try 11n_disable50 instead. To permanently disable wireless-N, create a file /etc/modprobe.d/iwlagn.conf containing a single line:

options iwlagn 11n_disable=1

You will need to restart your computer or perform the temporary fix (using the rmmod and modprobe commands) for this change to take effect.

I hope this helps! Please comment with any questions or corrections.

Post Navigation

Follow

Get every new post delivered to your Inbox.