Cnotes-Feb26-Spr04 - Introduction to C COP 3330 Dr. David...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to C COP 3330 © Dr. David A. Workman School of Computer Science University of Central Florida January 2004 Revised: Feb. 26, 2004
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Revised: Feb. 26, 2004 (c) Dr. David A. Workman 2 References C: A Reference Manual Prentice-Hall, 1995, ISBN = 0-13-326224-3
Background image of page 2
Revised: Feb. 26, 2004 (c) Dr. David A. Workman 3 Getting Started The C Environment C Source Files C Object Files (.o) gcc Compiler Your C Working Directory Header-files .h Source-files .c .h files contain the file interfaces . c files contain the implementation details The program entry point is the main() function that must be defined in a .c file. > gcc -c filename.c One or more compilation-only steps. Each successful compilation produces a .o file. Only .cpp files are compiled. .h files will be compiled when a #include statement is processed. > gcc -c filename.c This illustrates how to redirect stderr to a file to save compilation messages. > gcc [ libraries ] mainfile .o file1 .o file2 .o -o executable-name Compile the main file, link .o files, and produce an executable.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Revised: Feb. 26, 2004 (c) Dr. David A. Workman 4 Make Files # makefile for foo_to_v5d.c conversion program (this is a comment) PROGRAM = Mail1 CFLAGS = -c CC = gcc LIBS = -lsocket -lnsl OBJECTS = $(PROGRAM).o file1.o file2.o … filen.o $(PROGRAM): $(OBJECTS) $(CC) $(OBJECTS) $(LIBS) -o $@ $(PROGRAM). o: $(PROGRAM). c $(CC) $(CFLAGS) $(PROGRAM).cpp file1 .o: file1 .c $(CC) $(CFLAGS) file1 .c -o file1 .o ... filen .o: filen .cpp $(CC) $(CFLAGS) filen .c -o filen .o # introduces comments identifier = declarations Identifier : introduces a rule File1 : File2 command See Notes To "run" a makefile : > make or > make -f makefile-name identifier = declarations
Background image of page 4
(c) Dr. David A. Workman 5 Example #include <stdio.h> #include <stdlib.h> int main() { /* This program is designed to: 1) Prompt the user for a file name. 2) Open the file for output. 3) Prompt the user for a message string. 4) Write the message string to the output file. 5) Close the output file and quit. */ char buff[2048]; //buffer for reading in user responses to prompts. FILE *fout; // file variable for the output file char *foutname; // name of the output file printf(“Enter the name of your output file: “); fgets(buff,sizeof(buff),stdin); //reads characters from user. Terminated by null byte. foutname = malloc(strlen(buff)+1); //reserve one byte for string terminator strcpy(foutname, buff); //copy buff into memory pointed to by foutname (null byte copied too) fopen(fout, foutname, “w”); //open output file for “write only” and update fout printf(“Enter your output message: “); fgets(buff,sizeof(buff),stdin); //reads characters from user. Terminated by null byte. fprintf(fout,”%s\n”,buff);
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/04/2011 for the course COP 3330 taught by Professor Staff during the Summer '08 term at University of Central Florida.

Page1 / 19

Cnotes-Feb26-Spr04 - Introduction to C COP 3330 Dr. David...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online