Beginner's guide to writing a C function
Dr. Brezeale's handy-dandy notes on moving from Python to C (see the Misc. section)
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
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
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.
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
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
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
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.
"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.