Tuesday, December 23, 2008

c programming articles

c++ articles

mysql

MySQL is a small, fast and highly configurable DBMS. It supports a number of different table fileformats, depending on the requirements of the user.

These are the main MySQL clients and processes (mysqld):
  • mysqld - MySQL server daemon
  • safe_mysqld - Server process monitor
  • mysqlaccess - Tool for creating MySQL users
  • mysqladmin - Utility for administering MySQL
  • mysqldump - Tool for dumping the contents of a MySQL database. Useful for backing up a database from within the console.
  • mysql - Command line interface to MySQL
  • mysqlshow - List all MySQL database
  • Mysql Administrator - This is a GUI tool which makes administering mysql database a painless task. Read more about it here.
Field Types in SQL
INTEGER - A whole number
VARCHAR(10) - Up to 10 characters.
CHAR(10) - Fixed number of characters
DATE - A date
DATETIME - Date and time
FLOAT - Floating point numbers
Field Types specific to MySQL
TEXT - Allows up to 65535 characters
DECIMAL(10,2) - Up to 10 digits before the point, 2 after.
Create a database
$ mysqladmin --user=ravi --password=xxx create database addressdb
Using the database
$ mysql --user=ravi --password=xxx
mysql> USE addressdb
Create a table
mysql> CREATE TABLE p_addr (i INTEGER PRIMARY KEY,address TEXT,email VARCHAR(30),pincode DECIMAL(10),phone DECIMAL(15),website TEXT);
Add a column called "name" to the table
mysql> ALTER TABLE p_addr ADD name VARCHAR(30);
Inserting values into table
mysql> INSERT INTO p_addr VALUES (1,"My, present, address","ravi@localhost",681024,2122536, "http://linuxhelp.blogspot.com","Ravi");
List the contents of the table
mysql> SELECT * FROM p_addr;
Delete a row from the table
mysql> DELETE FROM p_addr WHERE i=1;
Rename a column in the table from "address" to "home_address"
mysql> ALTER TABLE p_addr CHANGE address home_address INTEGER;
Note: You cannot use this method to rename a column which is a primary key.

Change an existing record in the table
mysql> UPDATE p_addr SET name="Sumitra" WHERE i=2;
Delete the table from the database
mysql> DROP TABLE p_addr;
List the databases
$ mysqlshow --user=ravi --password=xxx
+-----------+
| Databases |
+-----------+
| addressdb |
| myblog |
| mysql |
| test |
+-----------+
List the tables in the database "addressdb"
$ mysqlshow --user=ravi --password=xxx addressdb

Database: addressdb
+---------+
| Tables |
+---------+
| p_addr |
| mytble |
| phonebk |
+---------+
These are only a subset of the commands in mysql. But this will be enough for creating and maintaining a simple database.

find command usage

find command

Using "find" command in *NIX systems

1) To find a file say "hello.c" in /usr/xyz directory, do
#find /usr/xyz -name "hello.c"

If you dont know the exact location, you can search in "/" dir, and if you dont know the exact file name, you can use "*" like
#find / -name "he*.c" or
#find / -name "he*.c*"

2) To find a string in some specific files, lets say you want to find "some function" in all C++ files (having extension ".cpp") in, lets say "/usr/xyz/prj1" directory,

#find /usr/xyz/prj1 -name "*.cpp" | xargs grep "some function"

3) You want to do something with the result of find command, eg you want to see listing in time order (ls -ltr) of the output of find command
#find . -name "a*.pdf" -exec ls -ltr {} +
Here we will be seeing all the pdf files that start with "a" in reverse time wise list (ls -ltr)

or lets say you want to delete all the files with .tmp extension in /mytmp dir, you can do so by
#find /mytmp -name "*.tmp" -exec rm {} +

gcc - How to find what preprocessor does to a C program

There are gcc options that does only preprocessing or preprocessing and compiling or all preprocessing, compiling and linking to make an executable. If we need to see what does the compiler does during preprocessing we can ask it to do only preprocessing and see the result to find out what it does in preprocessing. This can be useful in many situations where we need to identify preprocessor errors, in academics etc.
Lets understand this with an example:
I am using Fedora8 with gcc - gcc (GCC) 4.1.2 20070925 (Red Hat 4.1.2-33)

Example contains a C file named "first.c" and a header file named "first.h" as following:

first.c
-------
#include "first.h"
#define MAX 10
int main()
{
int i;
int j;

printf("hello world \n");
i=MAX;
j=MIN;
printf("i - %d and MAX is %d\n",i,MAX);
printf("j - %d and MIN is %d\n",j,MIN);
return 0;
}
----

first.h
----
#ifndef __FIRST
#define __FIRST

#define MIN 5
#include
#endif
----

if you compile the program and run it you will get
#gcc first.c
#./a.out
hello world
i - 10 and MAX is 10
j - 5 and MIN is 5

What we need to find out here is what preprocessor does to this program, as we all know, preprocessor does include the header file first.h in the program, which internally includes stdio.h (in my case it is /usr/include/stdio.h file), replaces MACROS (MIN and MAX here) with their values. In my case the gcc gives "-E" option for preprocessing only, find out your option by seeing manual page. So I run

#gcc -E first.c > first_only_preprocessor.out

and when you see the output file "first_only_preprocessor.out", you will find out what preprocessor does to our example C file.

Lets see, for example, what it does to our printfs, where we are printing MAX and MIN, I searched MAX and MIN in the output as follows:

# grep 'MIN\|MAX' first_only_preprocessor.out
OR
# gcc -E first.c | grep 'MIN\|MAX'

output is
printf("i - %d and MAX is %d\n",i,10);
printf("j - %d and MIN is %d\n",j,5);

As you see, preprocessor correctly replaces MAX with 10 and MIN with 5 :)

Think on the ideas where you can use this ...