Friday, August 12, 2011

Friday 8/12/11

//print2.c-more printf() properties
#include
int main(void)
{
unsigned int un = 300000000;
short end = 200;
long big = 65537;
long long verybig = 12345679008643;

printf("un = %u and not %d\n", un, un);
printf("end = %hd and %d\n", end, end);
printf("big = %ld and not %hd\n", big, big);
printf("verybig = %lld and not %ld\n", verybig, verybig);

return 0;
}
//using wrong specification can produce unexpected results
//the unsigned value 3000000000 and the signed value -129496296 have exactly the same internal representation in memory on our system


//displays code number for a character
#include
int main(void)
{
char ch;
printf("Please enter a cahracter.\n");
int i = 0;
while(i<5)
{
//you need to put a space in front of %c in order to keep the computer from interpreting the carriage return as input
scanf(" %c", &ch); //user inputs character
printf("The code for %c is %d. \n", ch, ch);
i = i + 1;
}
return 0;
}



//altnames.c portable names for integer types
#include
#include //supports portable types

int main(void)
{
int16_t me16;

me16 = 4593;
printf("First, assume int16_t is short: ");
printf("me16 = %hd\n", me16);
printf("Next, let's not make any assumptions.\n");
printf("Instead, use a \"macro\" from inttypes.h: ");
printf("me16 = %" PRId16 "\n", me16);

return 0;
}


//displays float value in two ways

#include
int main(void)
{
float aboat = 32000.0;
double abet = 2.14e9;
long double dip = 5.32e-5;

printf("%f can be written %e\n", aboat, aboat);
printf("%f can be written %e\n", abet, abet);
printf("%f can be written %e\n", dip, dip);

return 0;
}



//uses escape characters
#include
int main(void)
{
float salary;
printf("\aEnter your desired monthly salary:");
// \b is the backspace character
printf(" $______\b\b\b\b\b\b");
scanf(" %f", &salary);
printf("\n\t$%.2f a month is $%.2f a year.", salary, salary * 12.0);
printf("\rGee!\n");
return 0;
}



#include
int count = 0;
void test1(void){
printf("\ntest1 count = %d ", ++count);
}

void test2(void){
static int count;
printf("\ntest2 count = %d ", ++count);
}

int main(void)
{
int count = 0;
for( ; count < 5; count++)
{
test1();
test2();
}
}


#include
int i = 0;
int main()
{
int i;
//void f1(void);
i = 0;
printf("value of i in main %d\n", i);
f1();
printf("value of i after call %d\n", i);
}

int f1(void)
{
int i = 0;
i = 50;
printf("value of i in call %d\n", i);
}



#include
int g = 10;

int main()
{
f1();
printf(" after first call \n");
f1();
printf(" after second call \n");
f1();
printf(" after third call \n");
}

int f1(void)
{
static int k = 0;
int j = 10;
printf("value of k %d j %d", k, j);
k = k + 10;

return 0;
}



using System;
using System.Collections;
using System.Collections.Generic;

namespace program
{
static class CharStrExtMethods
{
public static int[] FindAll(this string matchStr, string searchedStr, int startPos)
{
int foundPos = -1; // -1 represents not found.
int count = 0;
List foundItems = new List();
Console.WriteLine("The search for item length is " + matchStr.Length);
do
{
foundPos = searchedStr.IndexOf(matchStr, startPos, StringComparison.Ordinal);
if(foundPos > - 1)
{
startPos = foundPos + 1;
count++;
foundItems.Add(foundPos);
Console.WriteLine("Found item at position: " + foundPos.ToString());
}
} while (foundPos > -1 && startPos < searchedStr.Length);

return ((int[])foundItems.ToArray());
}
}

class MainClass
{
static void Main(string[] args)
{
string data = "Red";
int[] allOccurrences = data.FindAll("BlueTeaRedredGreenRedYellow", 0);
}

}
}

No comments: