Wednesday, July 05, 2006

Lets learn C

The C Journey

Lesson One : Introduction
C is everywhere, though .net, java, python is taking the world by storm. It is still one of the fundamental language to learn beginning programming. Though some may argue the case to be otherwise, but this is the way which worked best for me.

Lets get to the heart of the matter. Every C program begins with a main() function. main() is the entry point for a C program.

Let's write our first program, which is a hello world.

#include stdio.h
/* This program prints "Hello World" on the console. */
void main()
printf("Hello World\n");

The above program highlights many important features of C. Note the include file "stdio.h". The printf() function is located in this header file. You can treat stdio.h as a library containing many input/output functions. Later on we will see how to write our own such library.

Compile this program in your favorite editor and run it. You will be greeted with a "Hello World".

The "\n" at the end of "Hello World\n" string indicates a new line character. This instructs the compiler to postion the cursor on the next line.

And also note every C statement ends with a semicolon.
Also every block of statement is enclosed in a curly brace {...}.

Text written between /*.. */ are comments. This is for programmers reference. Comments are ignored by the compiler. Comments can also be represented by two forward slashes (//)
eg. // this line is a comment

The "void" indicates that the function doesn't return a value. This warrants an explanation as what a function is, which we will see in further section.

Lesson Two: Data Types
Objective: The objective of this section is to understand the following.
Memory Concepts
Data Types
Naming Conventions
scanf function
Arithmetic in C
Operator Precedence

Memory Concepts:
A computer has both short-term and long-term memory. A computer's long-term memory is called nonvolatile memory and is generally associated with mass storage devices, such as hard drives, large disk arrays, diskettes, and CD-ROMs. In later lessons, we will learn how to use nonvolatile memory for storing data.

This chapter concentrates on short-term, or volatile, memory. Volatile memory loses its data when power is removed from the computer. It's commonly referred to as RAM (random access memory).

RAM is made up of fixed-sized cells with each cell number referenced through an address. In later lessons, "Arrays," we will discuss memory cell reference through address pointers.

Generally Programmers reference memory cells through the use of variables. There are many types of variables, depending on the programming language, but all variables share similar characteristics, as described below

Name : The name of the variable used to reference data in program code

Type : The data type of the variable (number, character, and so on)

Value : The data value assigned to the memory location

Address: The address assigned to a variable, which points to a memory cell location

Data Types
We will look up at some of the data types such as strings, Boolean, arrays, objects, and data structures.

Let's concentrate on the following important data types
Floating-point numbers

Integers are whole numbers that represent positive and negative numbers, such as -3, -2, -1, 0, 1, 2, and 3, but not decimal or fractional numbers. Integer data types hold a maximum of four bytes of information and are declared with the int (short for integer) keyword as shown in the following line of code.

int x;

In C, you can declare more than one variable on the same line using a single int declaration statement, as demonstrated next. This preceding variable declaration declares three integer variables named x, y, and z.

int x, y, z;

Floating-Point Numbers
Floating-point numbers are all numbers, including signed and unsigned decimal and fractional numbers. Signed numbers include positive and negative numbers whereas unsigned numbers can only include positive values. Examples of floating-point numbers are shown in the following list.


Use the keyword float to declare floating-point numbers, as shown next. This code has three floating-point variable data types called operand1, operand2, and result.

float operand1;
float operand2;
float result;

Character data types are representations of integer values known as character codes. For example, the character code 90 represents the letter Z. Note that the letter Z is not the same as the character code 122, which represents letter z (lowercase letter z).

Characters represent more than the letters of the alphabet; they also represent numbers 0 through 9, special characters, such as the asterisk (*), and keyboard keys, such as the Del (delete) key and Esc (escape) key. In all, there are a total of 128 common character codes (0 through 127), which make up the most commonly used characters of a keyboard.

Character codes are most notably organized through the ASCII (American Standard Code for Information Interchange) character set. For a listing of common ASCII character codes, see Appendix E, "Common ASCII Character Codes."

DEFINITION ASCII or American Standard Code for Information Interchange is noted for its character set, which uses small integer values to represent character or keyboard values.

In C, character variables are created using the char (short for character) keyword as demonstrated next.

char firstInitial;
char middleInitial;
char lastInitial;

Character data assigned to character variables must be enclosed in single quotes ('), also known as tick marks or apostrophes. As you'll see in the next section, the equals sign (=) is used for assigning data to the character variable.

Initializing Variables and the Assignment Operator
When variables are first declared, the program assigns the variable name (address pointer) to an available memory location. It is never safe to assume that the newly assigned variable location is empty. It's possible that the memory location contains previously used data (or garbage). To prevent unwanted data from appearing in your newly created variables, initialize new variables, as shown here.

int x;
char firstInitial;

x = 0;
firstInitial = '\0';

The preceding code declares two variables: one integer and one character data type. After creating the two variables, I initialize them to a particular value. For the integer variable, I assign the value zero (0), and for the character data type, I assign the character set \0, which is known as the NULL character. I enclosed the NULL character in single quotes. Single quotes are required when assigning data to the character data type.

Printing Variable Contents
To print the contents of variables, use the printf function with new formatting options, as demonstrated in the following code block. First, I declare three variables (one integer, one float, and one character), and then I initialize each of them. After initializing the variables, I use the printf statement to output each variable's contents to the computer's screen.




//variable declarations
int x;
float y;
char c;

//variable initializations
x = -4443;
y = 554.21;
c = 'M';

//printing variable contents to standard output
printf("\nThe value of integer variable x is %d", x);
printf("\nThe value of float variable y is %f", y);
printf("\nThe value of character variable c is %c\n", c);


Conversion Specifiers
Because data is stored as unreadable data in the computer's memory, programmers in C must specifically tell input or output functions, such as printf, how to display the data as information. You can accomplish this seemingly difficult task using character sets known as conversion specifiers.

Conversion specifiers are made up of two characters: The first character is the percent sign (%), and the second is a special character, which tells the program how to convert the data. Table 2.2 describes the most common conversion specifiers for the data types discussed in this chapter.

Common Conversion Specifiers Used with Printf Conversion Specifier Description

Displays integer value

Displays floating-point numbers

Displays character

Displaying Integer Data Types with printf
Integer data types can easily be displayed using the %d conversion specifier with a printf statement as shown next.

printf("%d", 55);

The output of the preceding statement prints the following text:


The %d conversion specifier can also be used to output the contents of a variable declared as integer data type, as demonstrated next.

int operand1;
operand1 = 29;
printf("The value of operand1 is %d", operand1);

In the preceding statements, I declare a new integer variable called operand1. Next, I assign the number 29 to the newly created variable and display its contents using the printf function with the %d conversion specifier. Each variable displayed using a printf function must be outside the parentheses and separated with a comma (,).

Displaying Floating-Point Data Types with printf
To display floating-point numbers, use the %f conversion specifier demonstrated next.

printf("%f", 55.55);

Here's another example of the %f conversion specifier, which prints the contents of a floating-point variable:

float result;
result = 3.123456;
printf("The value of result is %f", result);

Although the %f conversion specifier displays floating-point numbers, it may not be enough to display the floating-point number with correct or wanted precision. The following printf statement demonstrates the precision problem.

printf("%f", 55.55);

This printf example outputs a floating-point number with a six-digit precision to the right of the decimal point, as shown next.


To create precision with floating-point numbers, adjust the conversion specifier using numbering schemes between the % sign and the f character conversion specifier.

printf("%.1f", 3.123456);
printf("\n%.2f", 3.123456);
printf("\n%.3f", 3.123456);
printf("\n%.4f", 3.123456);
printf("\n%.5f", 3.123456);
printf("\n%.6f", 3.123456);

The preceding code block produces the following output:


I included the escape sequence \n in each of the preceding printf statements (except the first line of code). Without the new line (\n) escape sequence, each statement's output would generate on the same line, making it difficult to read.

Displaying Character Data Types with printf
Characters are also easy to display using the %c conversion specifier, as shown next.

printf ("%c", 'M');

The output of this statement is simply the single letter M. Like the other conversion specifiers, you can output the contents of a character variable data type using the %c conversion specifier and a printf statement as demonstrated next.

char firstInitial;
firstInitial= 'S';
printf("The value of firstInitial is %c", firstInitial);

You can use multiple conversion specifiers in a single printf statement as shown next.

char firstName, middleName, lastName;
firstName= 'R';
middleName= 'R';
lastName= 'P';
printf("My Initials are %c.%c.%c.", firstName, middleName, lastName);

The output of the preceding program statements is as follows.

My Initials are R.R.P
Notice in the statement below that each variable displayed with the printf statement is outside the double quotes and is separated with a single comma.

printf("My Initials are %c.%.c.%c.", firstName, middleName, lastName);

Text inside printf's double quotes is reserved for displayable text, conversion specifiers, and escape sequences.

Monday, July 03, 2006

Build your own Forms Authentication

The authentication ticked is created with the FormsAuthenticationTicket class. Its constructor accepts the following parameters:

version : The version of the authentication ticket[this value will be one]
name : The user name associated with the authentication ticket
issueDate : The date that the authentication ticket was issued
expiration : The date that the authentication ticket should expire
isPersistent: A boolean value indicating whether to allow the ticket to persist
after the user closes the browser
userData : A string value of any data that you would like to store. For eg.,
say your email ID.

Add an form, with a button 'Create Ticket'. On click on this button invoke the 'Login' method.

void Login(Object s, EventArgs e)
if (txtUsername.Text == "xxx" && txtPassword.Text == "xxx")
FormsAuthenticationTicket objTicket;
HttpCookie objCookie;
objTicket = new FormsAuthenticationTicket(1, txtUsername.Text,
DateTime.Now, DateTime.Now.AddMinutes(60), true,
objCookie = new HttpCookie(".ASPXAUTH");
objCookie.Value = FormsAuthentication.Encrypt(objTicket);


In the default.aspx Page_Load event add the following code..

void Page_Load() {
FormsIdentity objTicket;

objTicket = (FormsIdentity)User.Identity;
lblName.Text = objTicket.Ticket.Name;
lblIssueDate.Text = objTicket.Ticket.IssueDate.ToString();
lblExpiration.Text = objTicket.Ticket.Expiration.ToString();
lblIsPersistent.Text = objTicket.Ticket.IsPersistent.ToString();
lblVersion.Text = objTicket.Ticket.Version.ToString();
lblUserData.Text = objTicket.Ticket.UserData;

Enjoy coding..