Monday, July 21, 2003

Programming in C: A Tutorial
Brian W. Kernighan
Bell Laboratories, Murray Hill, N. J.
http://www.lysator.liu.se/c/bwk-tutor.html

//A Simple C Program
main( ) {
printf("hello, world");
}

//A Working C Program; Variables; Types and Type Declarations
main( ) {
int a, b, c, sum;
a = 1; b = 2; c = 3;
sum = a + b + c;
printf("sum is %d", sum);
}

//Simple I/O -- getchar, putchar, printf
main( ) {
char c;
c = getchar( );
putchar(c);
}

//If; relational operators; compound statements
c = getchar( );
if( c == '?' )
printf("why did you type a question mark?\n");

//While Statement; Assignment within an Expression; Null Statement
main( ) {
char c;
while( (c=getchar( )) != '\0' )
putchar(c);
}

//Arithmetic
main( ) {
char c;
while( (c=getchar( )) != '\0' )
if( 'A'<=c && c<='Z' )
putchar(c+'a'-'A');
else
putchar(c);
}

//Else Clause; Conditional Expressions
main( ) {
int let, dig, other, c;
let = dig = other = 0;
while( (c=getchar( )) != '\0' )
if( ('A'<=c && c<='Z') || ('a'<=c && c<='z') )
++let;
else if( '0'<=c && c<='9' ) ++dig;
else ++other;
printf("%d letters, %d digits, %d others\n", let, dig, other);
}

//Increment and Decrement Operators
main( ) {
int c,n;
n = 0;
while( (c=getchar( )) != '\0' )
if( c == '\n' )
++n;
printf("%d lines\n", n);
}

//Arrays
main( ) {
int n, c;
char line[100];
n = 0;
while( (c=getchar( )) != '\n' ) {
if( n < 100 )
line[n] = c;
n++;
}
printf("length = %d\n", n);
}

main( ) {
int n, c; char line[100];
n = 0;
while( (c=getchar( )) != '\0' )
if( c == '\n' ) {
printf("%d0, n);
n = 0;
}
else {
if( n < 100 ) line[n] = c;
n++;
}
}

//Character Arrays; Strings
main( ) {
int n;
char line[100];
n = 0;
while( (line[n++]=getchar( )) != '\n' );
line[n] = '\0';
printf("%d:\t%s", n, line);
}

Sunday, July 20, 2003

http://members.tripod.com/~johnt/c.html
Learn C by example in just 5 hours.C tutorial on-line.

(example-1)
main()
{
puts("hello world guess who is writing a c program");
return(0);
}

example-2
main()
{
printf(hello);
}

example-3

main() {
int count;
puts("Please enter a number: ");
scanf("%d", &count);
printf("The number is %d",count);
}

example-4
main()
{
puts("hello there");
puts("what is your name?")
pause()
puts("It is nice to meet you")
}
pause();
{
int move_on;
printf("press entere to continue");
move_on=getchar();
return(0);
}

example-5
main()
{
float cost,tax,luxury,total;
luxury=0.0;
printf("Enter the cost of the item: ");
scanf("%f", &cost);
tax=cost*0.06;
if(cost>40000.0)
luxury=cost*0.005;
total=cost+tax+luxury;
printf("the total cost is %0.2f",total);
}

example-6
if(cost >40000)
{
luxury=cost*0.005;
printf("The luxury tax is %.2f",luxury);
}
else
{
puts("There is no luxury tax for the items");
luxury=0.0;
}

example-7
main()
{
int row,column;
puts("\t\tMY Handy multipication table");
for(row=1;tow<=10;row++)
{
for(column=1;column<=10;column++)
printf("%6d", row*column);
putchar('\n');
}
}

example-9

main()
{
int temp;
float celsius;
char repeat;
char flag;
do
{
flag='n";
do
{
if(flag=='n')
printf("Input a valid temperature :");
else
printf("input a valid temperature,stupid:");
scanf("%d",&temp);
flag='y';
}
while (temp<0||temp >100);
celsius=(5.0/9.0)*(temp-32);
printf("%d degrees F is %6.2f degrees celsius\n",temp,celsius);
printf("Do you have another temperature?");
repeat=getchar();
putchar('\n');
}
while (repeat=='y' || repeat=='Y");
}

example 10
main()
{
int temps[31];
int index,total;
float average,celsius;
total=0.0;
for(index=0;index<31;index++)
{
printf("enter temperature #%d:",index);
scanf("%d",&temps[index]);
}
for(index=0;index<31;index++)
total+=temps[index];
average=total/31.0
printf("average is:%f\n\n", average);
puts9"fahrenheit\tcelsius\n");
for(index=0;index<31;index++)
{
celsius=(5.0/9.0)*(temps[index]-32);
printf("%d\t\t%6.2f\n",temps[index],celsius);
}
}

example-10

#define count 31
main()
{
int temps[count];
int index;
float celsius;
for(index=0; index< count;index++)
{
celsius=(5.0/9.0)*(heat[index]-32);
printf("%d\t\t%6.2f\n",heat[index],celsius);
}
}

example-13


#include "stdio.h"
main()
{
FILE*fp;
int letter;
if((fp=fopen("MYFILE","r"))==NULL)
{
puts("Cannot oepn the file");
exit();
}
while((letter=fgetc(fp)) !=eof)
printf("%c",letter);
fclose(fp);
}