a forward slash and asterisk (/*) End with an asterisk and forward slash (*/) Ignored by the C compiler /* One line of comments */ /* Two lines of comments */
by preprocessor before program is compiled This line of code tells the preprocessor to include the contents of the stdio.h file in the program #include <stdio.h>
of a function definition, where the code of the function is written Has a return type: int Has a name: main (special function to start program) Has parameters: void (no parameters) int main(void)
a function Starts/ends with left/right curly brackets: { } Has some code: printf("Aloha!\n"); Returns a value: return 0; (matches the return type) { printf("Aloha!\n"); return 0; }
write program code and store file on disk Edit program with Emacs (or Pico) Command is: emacs programName.c Creates a C program text file, which ends in ".c" Only the percent sign (%) is shown for the UNIX prompt % emacs program.c
add other files and text replacement 3. Compile – creates object (machine) code, which is stored in a file ending in ".o" Command is: gcc –c programName.c % gcc –c program.c
links code and libraries to make an executable file, which is used to run the program Command is: gcc programName.o -o executableFileName % gcc program.o -o program
takes from disk and put in memory 6. Execute – CPU executes each instruction Command is: ./executableFileName The "dot slash" (./) is for the current directory (file) % ./program
a file to output the compile and link commands for us This is particularly useful if we have multiple C files The command "make" finds the text file "makefile" and executes the commands listed in it!
Must use a tab at the beginning of each command line Must put a blank line (newline character – '\n') after each group of statements, including the last line If you create it on your PC and ftp (file transfer protocol) it to UNIX, the newline characters will cause problems in UNIX, so need to create with a UNIX editor
on your PC and ftp (file transfer protocol) to UNIX: Warning: invalid white space character in directive UNIX newline is one character: 0x0A PC newline is two characters: 0x0D0A 0x0D = carriage return 0x0A = line feed
Should get the following output gcc -c program.c gcc program.o -o program Type “./program” to run executable The dot forward slash ("./“) is the current directory
in a program By convention, variables are usually declared at beginning of a function Before any executable statements Should initialize variables when declare them Uninitialized variables have unpredictable values
types in the C language char = 1 byte (-128 to 127) short = 2 bytes (-32,768 to 32,767) int = 4 bytes (-2,147,483,648 to +2,147,483,647) float = 4 bytes (± ~10-44 to ~1038 ) double = 8 bytes (± ~10-323 to ~10308 )
to indicate a sign, so all 8 or 16 bits can be used for the magnitude: unsigned char = 1 byte = 8 bits = 28 = 256 So the range in values is 0 to 255
bits = 216 = 65,536 So the range in values is 0 to 65,535 unsigned int = 4 bytes = 32 bits = 232 = 4,294,967,296 So the range in values is 0 to 4,294,967,295
keywords as variable names auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while
input characters Reads one character from the input stream, which can be from the keyboard, or from a file Returns the character See example program at: inputchar.c char charcter1 = ‘z’; character1 = getchar();
output Function prototype: int printf(char *format, arg1, arg2, …); Translates variables (arg1, arg2, …) to character output in a specified format Returns the number of characters printed
…) Formatted output is between the double parenthesis Usually ends with a newline ('\n') Each argument has a corresponding “conversion specifier”, which is preceded by a percent sign (%)
securely (without the program crashing or risking a buffer overflow), we need knowledge about arrays, pointers, and strings, which will be covered later in the semester For now, we have provided a makefile and C program files to use with programs inputting doubles
UH UNIX account: inputdouble.c – shows how to use function getdouble() getdouble.c – function definition getdouble() getdouble.h – function prototype getdouble() makefile-double - makefile code to combine 3 files double number = 0.0; number = getdouble();