Link to Dr. T's page

C Programming Info and Notes for CSE1320
     Intermediate Programming with C

     "Dr. T's Helpful Stuff"

This document is divided into sections about getting started (including a short tutorial to get you started moving from Python to C), then a section about how to submit C programs in this class, and then a section with a variety of useful information about C programming and then about using omega, the Unix operating system mainframe, that we use in this class.

Beginner's guide to writing a C function
Dr. Brezeale's handy-dandy notes on moving from Python to C (see the Misc. section)


 
green yellow neon Reference material for Lab Assignments green yellow neon

How to submit your 1320 Lab - including script file info
     Updated DJan. 2012
     Lab Assignment Submission Information for Dr. Tiernan's section of CSE 1320

green yellow neon


Miscellaneous Coding Tidbits
i.e. REALLY USEFUL C BITS

Notes on errors reading chars with scanf and %c:

This is a classic C error in input.

      When C reads in data, it actually does not read each key directly. It reads the entered keystrokes that are saved in the input buffer. You've seen this when you were working on something, the screen seemed to stop but you kept typing, and when the screen unfroze it printed out all the things you had typed while it was frozen. Those keystrokes you typed went into an input buffer. C doesn't accept an input until you have pressed the enter key which also goes into the buffer.
      When C is trying to read numbers (%d, %f, etc) for input from the input buffer, it will ignore any whitespace characters it sees until it finds a letter or a number. A letter will make it act weird and a number will make it act OK. A whitespace char is anything that doesn't normally print a character to the screen when you type it like the space key, the enter key, the tab key, etc.
      However, when C is trying to read a char (%c) EVERY KEY THAT IS TYPED is a char. That means that any keystroke sitting in the input buffer is a char and C will read it when you use %c. AND, since C doesn't accept any input until an enter key is typed then the input buffer almost always has an enter key in it from the previous thing you read. Therefore, when you try to read a %c, it looks at the input buffer, sees a char (the enter key) and reads it. When you print it, it is blank.
So there are two simple fixes I usually recommend.

Fix 1:
      getchar(); //reads one char from input buffer
      printf("Enter whatever...");
      scanf("%c",&mychar);

or Fix 2:
      printf("Enter whatever...");
      scanf("%c %c",&mychar,&mychar ); //reads two chars and stores in same place so second overwrites the first


Notes on C strings:

Strings in C are just character arrays. To declare the array for the strings you declare it as type char. I recommend that you read the name into a temp array (char dest_input[100]), check the length with the strlen command, and then copy arrays that are 40 chars or less to the 2-D string array (char travelname[TRIPMAX][NAMEMAX]) with the command strcpy. The strlen and strcpy commands are in string.h.
The following pieces of code would be used in your program:

      #include (angle bracket) string.h (angle bracket)      //to get the string commands. Replace (angle bracket) with the symbol - HTML doesn't like to print it.
      // declarations of constants TRIPMAX (6) and NAMEMAX (40)
      //... other stuff here

      char travelname[TRIPMAX][NAMEMAX];
      char dest_in[100];      //temp array to hold name when reading
      int tripnum;            // variable used as counter for current trip
      //... other stuff here

      scanf("%s",dest_in);      //yes, just the name of the array
      if (strlen(dest_in) < NAMEMAX)
            strcpy(travelname[tripnum],dest_in);      //yes, just like this

Both strlen and strcpy need the location of the array as input. Therefore if we pass in the name of the array then we are passing in the address, i.e. the location, of the array. That's why we don't use the [ ] and why we don't need the & operator.


Notes on fractional parts being exact when working with HH.MM (a float value representing hours and minutes):

When using HH.MM and trying to extract the MM component as an integer you may have tried to calculate minutes with:
      int hrs = hhmm; // assuming hhmm is the float with the value HH.MM
      int min = (hhmm - hrs) * 100;
but gotten the wrong value, for example if hhmm was 3.05 you might have gotten min = 4.
Basically, the problem is that floats are stored in binary so they aren't exact as decimal values. For example, .05 might be stored as .049999999999999999 . Therefore, sometimes the value of a fraction doesn't end up as expected. So, to work around this a simple strategy is that instead of:
      minutes = ((time - hours) * 100;
use this:
      minutes = time * 100 - hours * 100;
and it will typically solve the problem.

Links to information about using 'getline' instead of 'gets':
    
GNU C Manual - Line-Oriented Input
     GNU C Programming Tutorial - getline
To get input from data files:
     Include stdio.h as a header file, declare a FILE * variable, use fopen to connect the FILE * variable to your physical file. See references further below for C file I/O examples.
To deal with reading strings correctly from files:
     When reading a string after reading a number from a file, you may need to read a junk character out between reading the number and the string. Use the getc(file_pointer) command to read one character from a file. If one doesn't work try using two getc commands.
To compile multiple physical files into a single program:
     To compile two files (Ex: L41.c and L42.c) together into a single program, use the gcc command with both file names following. Example:
     myomega>gcc L41.c L42.c
To get time and date information for your program using C functions:
     Information and an example using localtime(), time(), and the struct tm are available at www.cplusplus.com/ref/ctime/localtime.html Click on the various links to see the struct definition and other examples

green neon
Coding and Other Tips for Lab Assignments in CSE1320

Links to information about C file I/O:
Reference from Boston University CS class
C File I/O
Use section "2. C FIle I/O". Ignore section "1. Redirection".

Reference from University of Essex
File I/O, fopen and fclose
Clearly written with examples

green yellow neon CSE1320 Powerpoint presentations about C green yellow neon

Beginner's guide to writing a C function
This has hints about control structures, variable types, arrays, structs, pointers, linked lists, recursion and DEBUGGING!
Sticky Bits in C online presentation
Sticky Bits in C to download

Reference card for C commands to keep handy while you start writing functions

Intro to omega
Covers how to connect to omega, a few Unix commands, a short list of editors and compilers and an example of using the GDB debugger which is available for gcc.
A Powerpoint presentation by Do Kim (CSE senior) edited by Dr. T.
Posted 31 August 2004

Reference card for UNIX commands to use on omega

green neon

Misc. Handy C++ stuff if we work on C++ labs:
A) Dr. T continues to incorrectly say and write gpp for the compiler name - it is g++ .
B) In your file containing main make sure you #include the iostream file and that you have the line
      using namespace std;
after the #includes to make C++ work well.
C) For purposes of Lab #4 you can continue to use C strings and string functions. Just make sure you include the correct library files for those functions.
D) To use an input file with C++ takes the following steps:
      1) at the top use #include < fstream.h > or just fstream without .h
      2) declare an input file stream object, connect it to a physical file, and open the file by a declaration like the following:
            ifstream infile("lab4data.dat");
      where ifstream is the input file stream class type, infile is the object, i.e. variable, name, and "lab4data.dat" is the name of a file that you want to use for the input file. Notice that this declaration does the equivalent of both declaring a FILE * variable and doing an fopen command in C. Now you should be able to use the infile object anywhere that you would use the cin object. You may choose your own object variable name. You should use the same input file as for Lab #3.
E) To check for the end of a file in C++ you test infile.eof(). This returns true if the file is at the end. Note that infile is the file stream object referred to in part D) above.

green yellow neon
Reference links on Unix, Unix editors, and other info

These links go to various resources for Unix, the vi editor, and the emacs editor. There is also a link to the main OIT web page for questions about UTA's systems.

Picture of a frog

   "how-to" Unix reference manual from UTA OIT
   - be sure to scroll down to the bottom of the OIT page if no Unix guide
   info is visible to the right of the menu bar

   vi Unix editor reference from UTA OIT
   - be sure to scroll down to the bottom of the OIT page if no vi editor
   info is visible to the right of the menu bar

   vi Unix editor reference: University of Washington

   vi Unix editor tutorial: University of Hawaii

   emacs Unix editor reference manuals: GNU organization

   pico Unix editor reference from the University of Michigan

   pico Unix editor reference from the University of Chicago

   OIT - Office of Information Technology website which has other helpful links


Copyright Tyger Design, Inc. Web page created by Tyger Design.