DOC PREVIEW
UMBC CMSC 104 - CMSC 104 Introduction to C

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Introduction to CWriting C ProgramsUsing the C Compiler at UMBCInvoking the gcc CompilerThe Result : a.outProgram Development Using gccHello World in CScreenshot of Hello, World ProgramAnatomy of a C ProgramComments in CPreprocessor Directivesint main ( )The Function Bodyprintf (“Hello, World!n”) ;return 0 ;Declaring VariablesDeclaring VariablesMore About VariablesArithmetic Operators in CDivisionDivision (con’t)Getting Input from the UserControl StructuresFunctions in CFunction Example in C1Introduction to CTopicsCompilationUsing the gcc CompilerThe Anatomy of a C ProgramDifferences between JavaScript and C2Writing C ProgramsA programmer uses a text editor to create or modify files containing C code.A file containing source code is called a source file.After a C source file has been created, the programmer must invoke the C compiler before the program can be executed (run).The file must have an extension of .c, i.e filename.c3Using the C Compiler at UMBCInvoking the compiler is system dependent.At UMBC, we have two C compilers available, cc and gcc. For this class, we will use the gcc compiler as it is the compiler available on the Linux system.4Invoking the gcc CompilerAt the prompt, type gcc -ansi -Wall program.c where program.c is the C program source file.-ansi is a compiler option that tells the compiler to adhere to the ANSI C standard.-Wall is an option to turn on all compiler warnings (best for new programmers).5The Result : a.outIf there are no errors in pgm.c, this command produces an executable file, which is one that can be executed (run).The gcc compiler names the executable file a.out .To execute the program, at the prompt, type a.outAlthough we call this process “compiling a program,” what actually happens is more complicated.6Program Development Using gcc Source File pgm.cProgram Object Code File pgm.oExecutable File a.outPreprocessorModified Source Code in RAMCompilerLinkerOther Object Code Files (if any)Editor7Hello World in C/* Filename: hello.c * Author: Brian Kernighan & Dennis Ritchie * Date written: ?/?/1978 * Description: This program prints the greeting * “Hello, World!” */#include <stdio.h>int main(){ printf(“Hello, World!\n”); return 0;}8Screenshot of Hello, World Program9Anatomy of a C Programprogram header commentpreprocessor directives (if any)int main ( ){ statement(s) return 0 ;}10Comments in CAll comments must begin with the characters /* and end with the characters */11Preprocessor DirectivesLines that begin with a # in column 1 are called preprocessor directives (commands).Example: the #include <stdio.h> directive causes the preprocessor to include a copy of the standard input/output header file stdio.h at this point in the code.This header file was included because it contains information about the printf ( ) function that is used in this program.12int main ( )Every program must have a function called main. This is where program execution begins.main() is placed in the source code file as the first function for readability.The reserved word “int” indicates that main() returns an integer value.The parentheses following the reserved word “main” indicate that it is a function.13The Function BodyA left brace (curly bracket) -- { -- begins the body of every function. A corresponding right brace -- } -- ends the function body.The style is to place these braces on separate lines in column 1 and to indent the entire function body 3 to 4 spaces.14printf (“Hello, World!\n”) ;This line is a C statement.It is a call to the function printf ( ) with a single argument (parameter), namely the string “Hello, World!\n”.Even though a string may contain many characters, the string itself should be thought of as a single quantity. Notice that this line ends with a semicolon. All statements in C end with a semicolon.15return 0 ;Because function main() returns an integer value, there must be a statement that indicates what this value is.The statementreturn 0 ;indicates that main() returns a value of zero tothe operating system.A value of 0 indicates that the program successfully terminated execution.16Declaring VariablesWhen declaring a variable in C, you must also specify the type of the variable.Examples of variable declarations: int hw1Grade ; float finalAverage ;17Declaring Variables When we declare a variableSpace is set aside in memory to hold a value of the specified data typeThat space is associated with the variable nameThat space is associated with a unique addressVisualization of the declaration int hw1Grade ; type namehw1Grade garbage18More About VariablesC has three basic predefined data types:Integers (whole numbers)int, long int, short int, unsigned intFloating point (real numbers)float, doubleCharacterschar19Arithmetic Operators in C Name Operator ExampleAddition + num1 + num2Subtraction - initial - spentMultiplication * fathoms * 6Division / sum / countModulus % m % n20DivisionIf both operands of a division expression are integers, you will get an integer answer. The fractional portion is thrown away.Examples : 17 / 5 = 3 4 / 3 = 1 35 / 9 = 321Division (con’t)Division where at least one operand is a floating point number will produce a floating point answer.Examples : 17.0 / 5 = 3.4 4 / 3.2 = 1.25 35.2 / 9.1 = 3.86813What happens? The integer operand is temporarily converted to a floating point, then the division is performed.22Getting Input from the User#include <stdio.h>int main (){ int num1, num2; printf("Please enter the first value: "); scanf("%d", &num1); printf("Please enter the second value: "); scanf("%d", &num2); printf("The first number is: %d.\n", num1); printf("The second number is: %d.\n", num2); return 0 ;}23Control StructuresAll of the control structures we discussed in JavaScript are similar in C: if, if-else-if, while, do-while, for and switch.One difference is that you may only compare integers or characters in a switch statement.24Functions in CWe write functions in C for the same reasons we do in JavaScriptTo be able to re-use code in other programsTo keep us from having to


View Full Document

UMBC CMSC 104 - CMSC 104 Introduction to C

Download CMSC 104 Introduction to C
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CMSC 104 Introduction to C and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CMSC 104 Introduction to C 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?