C Arithmetic Operators
Similar to other programming languages, C has five arithmetic operators:
- + (addition)
- - (subtraction)
- * (multiplication)
- / (division)
- % (modular)
If you don’t know the modular (%) operator, it’s the reminder of a division (/). For example, 15 % 6 is equal to 3.
These operators have the following precedence:
- Multiplication, division, and modular get processed first
- Addition and subtraction get processed last
Use parenthesis and square brackets to override the precedence.
Here is the example source code I wrote:
#include <stdio.h>
main() {
int x, y, z;
// Calculation through declaring variables
x = 9;
y = (x + 6) / 3;
z = x * y + 1 + 2 - 3 * 4;
x = (10 + 35) % 12;
printf("x = %d, y = %d, z = %dn", x, y, z);
// Calculate directly without declaring variable
printf("Answer is %dn", 3 * 17);
fflush(stdin);
getchar();
}
Below is the output:
x = 9, y = 5, z = 36 Answer is 51
C string
A string is an array of char data types and terminated by a null (0) character (also know as the first character in the ASCII character set). The null character is an invisible character in the end of a string. Therefore, always add a number to the length of the array when declaring the char array. For example, char name[21] where name originally contains 20 characters.
In order to assign value to the char array, use strcpy function in the standard C library (stdio.h). For example, strcpy(name, “Mark”). However, it gave me a warning when I tried to compile the source code using gcc. After I googled about the warning message “incompatible implicit declaration of built-in function ‘strcpy’”, I found out that I need to include string.h in addition to stdio.h. Perhaps it’s the difference between Dev-C++ compiler and gcc compiler.
To output to the screen using printf, use %s for string rather than %d for digit/integer. For instance, printf(“The contents of name are %sn”, name).
Here is source code for the basic string program:
#include <stdio.h>
#include <string.h>
main() {
char name[21];
strcpy(name, "Mark");
printf("The contents of name are %sn", name);
fflush(stdin);
getchar();
}
Data Types and Data Type Modifiers for C Programming Language
Below are the fundamental (or basic) data types of C:
- int – used for storing integers
- char – used for storing individual characters
- float – used for storing floating point of decimal numbers
- double – used for storing more decimal points than float
- void – special data type only for functions and pointers
When I was learning C++ two years ago, I couldn’t differentiate between float and double. After countless examples that I saw today, I finally understand the difference between these two. As I defined above, the float data type is used to storing short decimal points, like price which has two decimal points. The double data type is used to storing long decimal points, like pi or infinite decimal points.
The data type modifiers that I learned today are following:
- short, long – used only for int (integers), to change size of the data type for save memory space
- unsigned, signed – also used only for int. Unsigned modifies int to positive number range. Signed is the default modifier for all of the data types.
- There are more modifiers, but I only learned these two groups today.
Since these modifiers only work for integer data type, int can be removed when declaring the variable with the modifiers. For example:
int age;Â Â Â Â Â Â Â Â Â Â Â // No data type modifiers long age;Â Â Â Â Â Â Â Â Â Â // With modifiers unsigned age;Â // With modifiers
Linux Shell Programming Basics
I have been reading Wrox Beginning Linux Programming this week. The book mainly teaches how to programming in GNU/Linux using C. Since I’m learning the basics of C, I thought I could understand some of the materials in this book. In Chapter 2, it talks about shell programming and how does it relate to C programming in GNU/Linux. I wrote the following code which is based on the example code in Chapter 2:
#!/bin/sh # This file looks through all the files in the current # directory for the input string, and then prints the name of # those files to the standard output. echo "" echo Enter the lookup value: read input echo "" for file in * do if [ -f $file ] then if grep -q $input $file then echo $file fi fi done echo "" exit 0
And the screenshot below is the output:
Click the image to enlarge
Note that the read command is similar to the scanf function in C. They are used to let user input value. Also note that this script would take a moment to complete depending on the size of each file where the script file is in.
If you have this book, the above script is similar to the example provided in the Chapter 2 of the book, except I removed static input value (POSIX) and added an user input field using read command and several blank lines to make the output more cleaner.
Big Update
I upgraded this blog to WordPress 3.0 today and tried it. I love some of the new features for this release , especially the new theme customization options. Therefore I updated the structure and layout for the whole blog in order to use the full theme customization features in 3.0. Here are the updates I made:
- Switched to the new default theme
As I stated above, in order to use the full theme features, the theme needs to be compatible with 3.0. Some options didn’t appear when I was in the last theme after the upgrade. So I switched to the new default theme came with WP 3.0.
- Logo added (special)
The special means that this logo is only for the blog. The main difference between this one and beta one is that I added Ubuntu and NetBeans to each side of the logo in order to accommodate the header image size for the theme.
- Blog menu added
Menu creation and editing is a new feature in WP 3.0. I placed a menu below the logo. The items for this menu are relevant to the blog.
- Updated blog name and description
This domain name is based on my name. So I decided to rename this blog from robbychen.com Blog to Robby Chen Personal Blog, as well as its subtitle (description).
- Categories restructured
After upgraded to WordPress 3.0, I reordered the categories to include sub-categories once I noticed that the hierarchy feature for the categories was fixed.
I also discovered WordPress.tv when I tried to find the tutorials for WordPress 3.0. It’s the official WordPress video site for providing tips to use WordPress. I’m currently figuring out how to create multi-site setup with 3.0. Maybe I will use WordPress as a CMS for robbychen.com!
Planning to Create my Own WordPress Theme
After read about the malicious code in free WordPress themes, I have decided to learn and make my own WordPress theme after I complete the redesigning project. Since I created this blog about one year ago, I only used two free WordPress themes directly installed from WordPress.org. The first theme used CSS 3 styles. However, I remembered that sometimes the theme was corrupted for no reason and one time all of the post pages were changed to some kind of advertisement without my knowledge. I think that there were some malicious code in there although I didn’t look and modify the theme code. The current theme for this blog is called minimalism. I used this theme to increase site’s performance. The original layout for this theme was very ugly. The font size was small and the text color didn’t have enough contrast. With the help of FireBug, I changed a little bit of CSS and applied into the theme. The current modified theme still looks a bit ugly but I will make this theme as the theme template to create my own theme. I will also post the latest development on my projects here.
NetBeans 6.9 was Released Today
I’m exited to know that latest NetBeans was released today. I happened to download 6.9 during the class at Montgomery College today. I have no idea about today’s latest release until I downloaded and installed it to the Windows XP-based machine that I was working on and noticed that it is titled NetBeans IDE 6.9. I plan to upgrade NetBeans on both of my netbook and laptop to 6.9 tomorrow. Visit release information page to learn more about this latest release. Also check out Guided Video Tour of the NetBeans IDE 6.9 which I think it’s a new section for 6.9.
Linux movies website overview
I watched some psychology related films today because of my recent interest for multiple personality disorder (now called dissociative identity disorder). Therefore I don’t have much to write about GNU/Linux or web development. However, I’m going to talk a little bit about a website called linuxmovies.org. This website is about how the movie studios use GNU/Linux to create special effects for the movies. As those of you who followed my Twitter and/or identica feed, I already talked about the site. For this post, I will focus on the Software section. When I first arrived on this page, I only scrolled down to look for the free software. Perhaps it is my habit to believe that only free and open source software should be used on the GNU/Linux OS. Unsurprisingly, there are greater number of free software than commercial software. After looking through the list, I have chosen K-3D, including Blender, GIMP, and Audacity which I’ve already been using. After some usage of K-3D, I personally think that this program is easier to use than Blender once I know the basic usage of the program. Unfortunately, I’m not interested in art very much. Therefore I don’t fell that K-3D has any use for me, except when I want to improve creativity and get some inspirations for my upcoming projects.
C scanf Function
Continue from last C post, I will discuss scanf function today. The scanf function is similar to printf function with the opposite functionality. The printf function outputs the string on the screen whereas scanf function lets the user input values from the keyboard. The example of scanf function which I learned is scanf (“%d”, &number). As you can see, right now I only know how to use integer C variable type. I will learn to use other variable types as the tutorial progresses. Notice that there is an & operator in front of number variable. In my experience for basic C++, I remembered that it is either a reference or pointer. Here is my entire program code for the scanf exercise:
/* Generated by NetBeans */
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
/* End of generation */
int brothers, sisters, children;
printf("How many brothers and sisters do you have?n");
printf("Brothers: ");
scanf("%d", &brothers);
printf("Sisters: ");
scanf("%d", &sisters);
printf("How many children do you have? ");
scanf("%d", &children);
printf("You have %d brothers and %d sisters.nYou have %d children.n", brothers, sisters, children);
fflush(stdin);
/* Generated by NetBeans */
return (EXIT_SUCCESS);
}
As you can see in the code above, I use NetBeans both for my PHP development and C programming IDE. NetBeans automatically included the necessary C libraries and common statements for the project. The fflush(stdin) statement escapes the enter key according to the tutorial.

I have not written a post since last Tuesday because I’m busy with other things which are not related to the IT. During that time, I’ve been thinking about change the posting schedule for my blog to twice a day instead of once a day in order to changing my procrastination habit. I will try to write a post in the morning and one in the afternoon beginning this Thursday. This post is talking about how to use comment in C. Let’s begin!
Most of you reading this post probably already experienced with comments in other programming languages. If you didn’t, comments are some human-readable characters that placed in the source code to make it more understandable to other humans. Comments can be any languages as long as it’s the same language as the target audience. They are ignored by the compilers / interpreters so comments can be a reference or documentation to the program.
Comments in C begin with /* and end with */ for multi-line comments, and begin with // for single-line comments. For example:
Note that the single-line comment (//) is not available in the old C compilers. This type of comment is integrated from C++, therefore only the new C compilers that come after the existence of C++ supports the single-line comment.
The following source code shows the example of comments usage:
/* * Comment.c * * A program used to demonstrate the use of comments * by showing how to calculate the volume of a sphere * * by Robby Chen, 2010. * */ #include <stdio.h> main() { double PI = 3.1415926536;Â Â Â Â Â Â // We need a lot of precision int radius; double volume; printf("Please enter the radius: "); scanf("%d", &radius); /* The volume of a sphere is: four thirds times PI times the radius cubed */ volume = 4 / 3 * PI * radius * radius * radius; printf("The volume of the sphere would be %fn", volume); fflush(stdin); getchar(); }The above source code originally was to calculate the area of a circle. I modified it to calculate the volume of a sphere in order to avoid plagiarism.