Anúncio
Anúncio

Mais conteúdo relacionado

Anúncio
Anúncio

Programming in C

  1. Mrs. G. Nagalakshmi, M.Sc., M.Phil., Ms. N. Malathi, M.Sc.,
  2.  Array  Consecutive group of memory locations  Same name and type  To refer to an element, specify  Array name and position number  Format: arrayname[ position number ]  First element at position 0  n element array c: c[ 0 ], c[ 1 ]…c[ n - 1 ]  Array elements are like normal variables c[ 0 ] = 3; cout << c[ 0 ];  Performing operations in subscript. If x = 3, c[ 5 – 2 ] == c[ 3 ] == c[ x ]
  3. c[6] -45 6 0 72 1543 -89 0 62 -3 1 6453 78 Name of array (Note that all elements of this array have the same name, c) c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Position number of the element within array c
  4.  Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 };  If not enough initializers, rightmost elements become 0  If too many initializers, a syntax error is generated int n[ 5 ] = { 0 }  Sets all the elements to 0  If size omitted, the initializers determine it int n[] = { 1, 2, 3, 4, 5 };  5 initializers, therefore n is a 5 element array
  5.  A Structure is a collection of related data items, possibly of different types.  A structure type in C++ is called struct.  A struct is heterogeneous in that it can be composed of data of different types.  In contrast, array is homogeneous since it can contain only data of the same type. 5
  6.  Example: struct StudentInfo{ int Id; int age; char Gender; double CGA; };  Example: struct StudentGrade{ char Name[15]; char Course[9]; int Lab[5]; int Homework[3]; int Exam[2]; }; 6 The “StudentGrade” structure has 5 members of different array types. The “StudentInfo” structure has 4 members of different types.
  7. 7
  8.  A sequence of characters is often referred to as a character “string”.  A string is stored in an array of type char ending with the null character '0 '.
  9. char A_string[80]; cout << "Enter some words in a string:n"; //80 is the size of A_string cin.getline(A_string, 80); cout << A_string << “nEND OF OUTPUTn"; Output: Enter some words in a string: This is a test. This is a test. END OF OUTPUT
  10. // string prototype, already included in string.h //returns length of string(not counting'0‘) //you don't need to include it in your program int strlen(const char[]); int string_length = strlen("abcde"); //string_length is set to 5.
  11. int strcmp(char s1[], char s2[]); /*compares strings s1 and s2, returns < 0 if s1 < s2 = 0 if s1 == s2 (i.e. strcmp returns false) > 0 if s1 > s2 */ int strncmp(char s1[], char s2[], int limit); /* Same as strcmp except that at most limit characters are compared. */
  12.  fp  contains all information about file  Communication link between system and program  Mode can be  r open file for reading only  w open file for writing only  a open file for appending (adding) data FILE *fp; /*variable fp is pointer to type FILE*/ fp = fopen(“filename”, “mode”); /*opens file with name filename , assigns identifier to fp */
  13.  Writing mode  if file already exists then contents are deleted,  else new file with specified name created  Appending mode  if file already exists then file opened with contents safe  else new file created  Reading mode  if file already exists then opened with contents safe  else error occurs. FILE *p1, *p2; p1 = fopen(“data”,”r”); p2= fopen(“results”, w”);
  14.  r+ open to beginning for both reading/writing  w+ same as w except both for reading and writing  a+ same as ‘a’ except both for reading and writing
  15.  pointer can be reused after closing Syntax: fclose(file_pointer); Example: FILE *p1, *p2; p1 = fopen(“INPUT.txt”, “r”); p2 =fopen(“OUTPUT.txt”, “w”); …….. …….. fclose(p1); fclose(p2);
  16.  C provides several different functions for reading/writing  getc() – read a character  putc() – write a character  fprintf() – write set of data values  fscanf() – read set of data values  getw() – read integer  putw() – write integer
  17.  similar to scanf() and printf()  in addition provide file-pointer  given the following  file-pointer f1 (points to file opened in write mode)  file-pointer f2 (points to file opened in read mode)  integer variable i  float variable f  Example: fprintf(f1, “%d %fn”, i, f); fprintf(stdout, “%f n”, f); /*note: stdout refers to screen */ fscanf(f2, “%d %f”, &i, &f);  fscanf returns EOF when end-of-file reached
  18.  handle one integer at a time  syntax: putw(i,fp1);  i : an integer variable  fp1 : pointer to file ipened with mode w  syntax: i = getw(fp2);  i : an integer variable  fp2 : pointer to file opened with mode r  file pointer moves by one integer position, data stored in binary format native to local system  getw() returns end-of-file marker EOF when file end reached
  19.  Typical errors that occur  trying to read beyond end-of-file  trying to use a file that has not been opened  perform operation on file not permitted by ‘fopen’ mode  open file with invalid filename  write to write-protected file
  20.  how to jump to a given position (byte number) in a file without reading all the previous data?  fseek (file-pointer, offset, position);  position: 0 (beginning), 1 (current), 2 (end)  offset: number of locations to move from position Example: fseek(fp,-m, 1); /* move back by m bytes from current position */ fseek(fp,m,0); /* move to (m+1)th byte in file */ fseek(fp, -10, 2); /* what is this? */  ftell(fp) returns current byte position in file  rewind(fp) resets position to start of file
  21.  can give input to C program from command line E.g. > prog.c 10 name1 name2 ….  how to use these arguments? main ( int argc, char *argv[] )  argc – gives a count of number of arguments (including program name)  char *argv[] defines an array of pointers to character (or array of strings)  argv[0] – program name  argv[1] to argv[argc -1] give the other arguments as strings
Anúncio