Display binary representation

Monday, August 3rd, 2009

#include <stdio.h>
#include <conio.h>

int main(void)
{
  char ch;
  int i;

  printf("Enter a character: ");
  ch = getche();
  printf("\n");

  /* display binary representation */
  for(i = 128; i > 0; i = i / 2){
    if(i & ch) 
        printf("1 ");
    else 
        printf("0 ");
  }
  return 0;
}

           
       

Output Hex

Monday, July 20th, 2009

  
#include <stdio.h>

int main(void)
{
  printf("\xA0 \xA1 \xA2 \xA3");

  return 0;
}

           
       

Disallowing NULLs

Wednesday, July 15th, 2009

/*
mysql> DROP TABLE Employee;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE Employee (
    ->     Name VARCHAR(50) NOT NULL,
    ->     Phone VARCHAR(15) NOT NULL
    -> );
Query OK, 0 rows affected (0.08 sec)

mysql> Describe Employee;
+——-+————-+——+—–+———+——-+
| Field | Type        | Null | Key | Default | Extra |
+——-+————-+——+—–+———+——-+
| Name  | varchar(50) |      |     |         |       |
| Phone | varchar(15) |      |     |         |       |
+——-+————-+——+—–+———+——-+
2 rows in set (0.01 sec)

mysql> INSERT INTO Employee (Name, Phone)
    ->             VALUES (’Joe Yin’, ’666 2323′);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Employee (Name) VALUES (’John Doe’);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO Employee (Name, Phone) VALUES (’John Doe’, NULL);
ERROR 1048 (23000): Column ’Phone’ cannot be null
mysql> Select * from Employee;
+———-+———-+
| Name     | Phone    |
+———-+———-+
| Joe Yin  | 666 2323 |
| John Doe |          |
+———-+———-+
2 rows in set (0.00 sec)

*/  
DROP TABLE Employee;

CREATE TABLE Employee (
    Name VARCHAR(50) NOT NULL, 
    Phone VARCHAR(15) NOT NULL
);

Describe Employee;

INSERT INTO Employee (Name, Phone)
            VALUES (‘Joe Yin‘, ’666 2323‘);
INSERT INTO Employee (Name) VALUES (‘John Doe‘);
INSERT INTO Employee (Name, Phone) VALUES (‘John Doe‘, NULL);

Select * from Employee;

           
       

Use for loop statement to set value to an integer variable

Saturday, July 11th, 2009

 
SQL>
SQL> set echo on
SQL>
SQL> DECLARE
  2      v_MyChar VARCHAR2(20) := ‘test‘;
  3      v_NUMBER NUMBER;
  4      V_Date DATE := SYSDATE;
  5      v_counter INTEGER;
  6  BEGIN
  7      DBMS_OUTPUT.PUT_LINE(‘This is a Test‘);
  8      DBMS_OUTPUT.PUT_LINE(‘Of Syntax Error Debugging‘);
  9      For v_COUNTER IN 1..5 LOOP
 10          DBMS_OUTPUT.PUT_LINE(‘You are in loop:‘ || v_counter);
 11      END LOOP;
 12  END;
 13  /
This is a Test
Of Syntax Error Debugging
You are in loop:1
You are in loop:2
You are in loop:3
You are in loop:4
You are in loop:5

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL>
SQL> –

 

Displaying String: out quotation marks in a printf

Friday, July 10th, 2009

/* Displaying String: out quotation marks in a printf */

#include <stdio.h>

void main()
{
  printf("\n\"It is a wise father that knows his own child.\"  Shakespeare");
}