Sunday, June 2, 2013

ANSI C (1)

1. logically delete the command
# if 0

   statements

#endif

2. exit
exit(EXIT_SUCCESS);// stdlib.h

exit(EXIT_FAILURE);// stdlib.h

3.array variable as parameter
array variable, as function parameter, will pass the reference to the function, other kinds of variables will pass the values to the function
4.NUL  NULL
NUL=='\0' ;// the ASCII character 

NULL==0; // the pointer

5. read
#define MAX_INPUT 1000

char input[MAX_INPUT];

int ch;

while(gets(input)!=NULL)//read the line

{

}

while((ch=getchar())!=EOF&&ch!='\n')//read the character 

{

}

6. standard structure
#include <stdlib.h>

int main(void)

{

   return 0;

}

7.typedef  define
typedef char   *ptr_to_char;

define  p_ptr_to_char char*;

8 const pointer
int const *pci ; // you can modify pci, but not *pci

int *const pci;  // you can modify *pci, but not pci;

9 switch
switch(command)

{

  case 'A':

      add_entry();

      break;

  case 'B':

      delete_entry();

     // break;

  case 'C':

      edit_entry();

      break;

}

if command=='B', then delete_entry()  and  edit_entry() would be implemented, because there is no 'break' for case'B'. So pay attention to all the 'break'.
10 sizeof
it is an operator, not function
sizeof(int); // return the length by bytes

sizeof x;

11. pointer to string
//  string length

#include <stdlib.h>

size_t strlen(char *string)

{

   int length=0;

   while(*string++ !='\0')

          length+=1;

   return length;

}


// find the char in strings

#include <stdio.h>

#include<assert.h>

#define TRUE 1

#define FALSE 0

int find_char(char **strings, char value)

{

   assert(strings!=NULL)



   while(*strings !=NULL)

  {

       while(**strings!='\0') 

       {

            if(*(*strings)++==value)

                return TRUE;

       }

       strings++;

  }

   return FALSE;

}



No comments:

Post a Comment