Sunday, December 4, 2011

Look at a String literals



#include <stdio.h>
#include <string.h>
char *ap1="char *ap1";
static char *ap2="static char *ap2";
int ii;
main()
{
int i;
char a[] ="char a[]";
char a1[] ="char a1[]";
static char a2[32] ="static char a2[10] ";
const char a3[32] ="const char a3[10] ";
char *ap ="char *ap";
static char *aptr ="static *aptr";

printf("strlen a[] %d \n",strlen(a));
printf("sizeof a[] %d \n",sizeof(a));
printf("a[sizeof] %d \n",a[strlen(a)]);

char help[20]="a b c d e ";

printf("local:\n&i %p %d \n",&i,i);
printf("a1 %p %s \n",a1,a1);
printf("a2 %p %s \n",a2,a2);
printf("a3 %p %s \n",a3,a3);
printf("*ap %p %s \n",ap,ap);
printf("*aptr %p %s \n",aptr,aptr);
printf("global:\n*ap1 %p %s \n",ap1,ap1);
printf("*ap2 %p %s \n",ap2,ap2);
printf("&ii %p %s \n",&ii,ii);

}

[output]: ./a.out

strlen a[] 8
sizeof a[] 9
a[sizeof] 0
local:
&i 0xbff103f8 -1074723816 < stack
a1 0xbff103e5 char a1[]
a2 0x8049940 static char a2[10]
a3 0xbff103c5 const char a3[10]
*ap 0x80486d8 char *ap
*aptr 0x80486cb static *aptr
global:
*ap1 0x80486b0 char *ap1
*ap2 0x80486ba static char *ap2
&ii 0x8049968 (null)

NULL in stdio is ((void*)0)

Tuesday, November 29, 2011

Quick Makefile


CC=gcc
S = file1.c file2.c

O = $(S:%.c=%.o)


exe: ${O}
     $(CC) ${LIBS} -o $<



# pull in dependency info for *existing* .o files
-include $(OBJS:.o=.d)

%.o: %.c 

 $(CC) ${INCLUDES} $(CFLAGS) -MMD -o $@ -c $<
 gcc -MM $(CFLAGS) $*.c > $*.d


.PHONY clean
clean:
     \rm -rf *.o exe


Note: Green highlighted stuff optional or use -MMD, are used to create dependencies

Another Example:
--------------------
ERR = $(error found an error!)
.PHONY: err

err: ; $(ERR)

Thursday, October 27, 2011

Vi Tips - QuickFix


See
:help :compiler
:help :make

(the latter without a preceding exclamation mark) which will
automagically display the first error (if any) as soon as the make job
terminates. (Not sure if you'll get to the first _warning_ or _error_),
and a few other useful commands for use after that:

:cfir[st]
:cla[st]
:cn[ext]
:cp[revious]
:cnf[ile]
:cpf[ile]

for navigating the error list, and

:cope[n]
:ccl[ose]

to see it in its own window (the "quickfix" window), where hitting Enter
on an error moves to that line in the source. Of course, these commands
can be mapped for ease of use; for instance I have the following in my
vimrc:

:map :cnext
:map :cprev

which I use not so much for compiling but for the ":helpgrep" and
":vimgrep" commands (whose results also come in a quickfix window).

See also ":help quickfix.txt"



====================Vimdiff ==================
# vimdiff
Most of what you asked for is folding: vim user manual chapter on folding. Outside of diffs I sometime use:
   * zo -> open fold.
   * zc -> close fold.
But you wll probably be better served by:
   * zr -> reducing folding level.
   * zm -> one more folding level, please.
or even:

   * zR -> Reduce completely the folding, I said!.
   * zM -> fold Most!.
   
]c - Jump to the next change.
[c - Jump to the previous change.
:diffupdate :diffu -> recalculate the diff,

Sunday, August 21, 2011

Good Technical Books

1. Computer Architecture and Organization by John P. Hayes
Publisher: McGraw-Hill Companies; 3rd edition (December 1, 1997)
ISBN-10: 0070273553
ISBN-13: 978-0070273559http://www.blogger.com/img/blank.gif

2. Solid State Pulse Circuits by Bell David A
ISBN: 8120307445,
ISBN-13: 9788120307445
Publisher: Prentice Hall PTR

3. THREADTIME: Multithreaded programming guide by Scott J. Norton, Mark D. Dispasquale
Publisher: Prentice Hall PTR (November 1, 1996)
Language: Englishhttp://www.blogger.com/img/blank.gif
ISBN-10: 0131900676
ISBN-13: 978-0131900677
Link1: Google.books

4.The Magic Garden Explained: the Internals of Unix System V Release 4: an Open Systems Design By Berny Goodheart James Cox.
Publisher: Prentice Hall
Author: Berny Goodheart James Cox
ISBN: 0130981389
EAN: 9780130981387

Thursday, July 7, 2011

Step to extract a specific file from tar ball

1) zcat compressed.tar.Z | tar xvf - file1 file2
2) cat compressed.tar | tar xvf - file1 file2
3) tar -xf filename.tar file1 file2

Wednesday, May 18, 2011

JDBC Applications with MySQL


$cat Connect.java
import java.sql.*;

public class Connect
{
public static void main (String[] args)
{
Connection conn = null;
//String query = "Select * FROM mysql.user";
String query = "Show databases";
String dbtime;
try
{
String userName = "root";
String password = "mysql";
String url = "jdbc:mysql://10.255.6.58/test";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
conn = DriverManager.getConnection (url, userName, password);

System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
System.err.println ("Message :"+e.getMessage());
System.err.println ("Error message: " + e);

e.printStackTrace();
}
finally
{
if (conn != null)
{
try
{
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query);

while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
} //end while

conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
}


Compile Connect.java to produce a class file Connect.class that contains executable Java code:

% javac Connect.java

Then invoke the class file as follows and it should connect to and disconnect from your MySQL server:

% java -classpath .:/path/mysql-connectorJ-5.jar Connect
Database connection established
mysql
test
userdb
Database connection terminated


http://www.kitebird.com/articles/jdbc.html
http://download.oracle.com/javase/tutorial/jdbc/basics/connecting.html
http://www.java-samples.com/showtutorial.php?tutorialid=9