Home > Notes > C Last Updated: 11/17/06
Welcome! This is C / C++ notes Page!

    Regular Test Programs:
  • inoutput : input/output program
    Assignments:
  • hello.c : prints out 'hello world'
  • lower.c : converts all letters to lowercase
  • higher.c : converts all letters to uppercase
  • tabs.c : converts all tabs to spaces
  • tabulate.c : scan/count/sort/print characters in a file; uses insertion sort
  • encode.c : encodes a txt file into compressed .huf file
  • decode.c : decodes previous file back into original text

 


++print commands:

printf (" COMMANDS ")

COMMANDS it can contain:
\t = tab  (puts a tab in between whatever)
\n = new line  (cursor goes to next line)

eg.
printf("Hello world!\n");
printf("What's up!");

OUTPUT: Hello world!
        What's up!

eg.
printf("Hello world!");
printf("What's up!");

OUTPUT: Hello world!What's up!


printing int, float, ...
%d   = decimal integer (minimum as possible)
%6d  = decimal integer, at least 6 characters wide
%f   = floating point (minimum as possible)
%6f  = floating point, at least 6 chracters wide
%.2f = floating point, 2 characters after decimal point
%6.2f= floating point, at least 6 wide and 2 after decimal point

%s   = string
%c   = character
%%   = % symbol
%x   = hexadecimal
%o   = octal

eg.
int x = 20;
int y = 60;
printf("%d%d", x, y);  /* note that you must have variables that match each 
                           sequence of integers, floating points, etc. */
OUTPUT: 2060

eg.
float x = 5.0
float y = 6.3
printf("%3.0f %3.1f", x, y);

OUTPUT: 5 6.3


++arrays:

~initializing int array:
  int ndigit[10];

++pointers & arrays:
~reference notes:
int a[100], *p;  /* array a has int values, p is a pointer to an int */
p = a;           /* pointer p now points to the first value of array a */
a = p;           /* THIS IS NOT ALLOWED!! */
*a = 11;         /* assign 11 to the first element of a; (same as: a[0] = 11;) */
*p = 11;         /* same */
*(a+i) = 17;     /* assign 17 to i-th element of a; (same as: a[i] = 17) */
*(p+i) = 17;     /* same */
p = a+i;         /* now p points to the i-th element */
p[2] = 23;       /* assigns 23 to the (i+2)-th element of a */
*(p+2) = 23;     /* same */

~more reference notes:
double x, y;
double *p, *q;   /* p and q are pointers to a double */
p = &x;          /* p points to x */
q = p;           /* q points to whatever p is pointing to; (same as: q = &x) */
*p = 17;         /* sets x = 17; since p points to x and p points to 17, x = 17 */
y = *p;          /* y = 17; y = p is pointing to address of x and x = 17 */
p = NULL;        /* sets p = 0 */

~example of pointer in use:
void swap (double *x, double *y) {
  double temp;
  temp = *x;
  *x = *y;
  *y = temp;
}

USAGE: swap(&a, &b);  /* program swaps variable a with b */

~equilivants of pointers and arrays:
&a[0]	&p[0]	a	 p        (they all point to the first element of a)
a[0]	p[0]	*a	 *p       (they all represent the first element of a, a[0])
&a[i]	&p[i]	a+i	 p+i      (they all point to the ith element of array a)
a[i]	p[i]	*(a+i)	 *(p+i)   (they are all the ith element of array a)
void f(int a[]); void f(int *a);  (they specify that the parameter is a pointer to an int)
f(a)	f(p)	f(&a[0]) f(&p[0]) (see the first line in this group)

++strings:
-strings in C/C++ is an array of char tyype, unlike Java. for example a string constant could be: 
"I am a string" and this will be stored in memory as an array. Note that the length of the string 
is 13, but it requires an array of 14 characters in memory to include space for the zero(terminating char) 
at the end.

~defining a string:
char s[100];  /* providing space for a string */
--or--
char *s;
s = (char *) malloc(100);  // providing space for a string of 99 length + 1 space for a terminating 
                           // 0 at the end of a string
~string functions in library "string.h":
let: char *s, *t;  /* character pointers s and t */ 
     int n;

-strcpy(s,t)         copy string t to sttring s
-strncpy(s,t,n)      copy first n characcters of t to s
                     (does not insert \0 at end, but strncat does)
-strcpy(s,t+i)       copy the part of t  from the ith char to the end, to s
-strncpy(s,t+i,n)    copy at most n charracters from t starting at position i, to s
-strcat(s,t)         concatenate t to ennd of s
-strncat(s,t,n)      concatenate at mostt n characters of t to end of s
-strncat(s,t+i,n)    concatenate at mostt n characters from t starting at position i to s
-strcat(strcat(strcat(s,t),u),v)   concaatenate strings t, u, and v to s
-if(strchr(s, ';'))...             find  if there is a ; inside of string s
-if(p=strchr(s, ';')) *p=0;        findss the first ; inside of string s and cuts the rest    
-while(p=strchr(s, '.')) *p=' ';   replaaces all . inside of string s

~string operations:
-strlen(s)           returns the length  of the string s
-strcmp(s,t)         compares s with t;  returns negative if s is less than t, returns positive if s 
                     is greater than t, returns 0 if s==t
-strncmp(s,t,n)      compares at most n  chars of s to t; returns<0 if s 

Home | Profile | Notes | Space | Links