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 CTopicsCompilationUsing the gcc CompilerThe Anatomy of a C ProgramDifferences between JavaScript and C2Writing C ProgramsA 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 UMBCInvoking 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 CompilerAt 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.outIf 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.outAlthough 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 Programprogram header commentpreprocessor directives (if any)int main ( ){ statement(s) return 0 ;}10Comments in CAll comments must begin with the characters /* and end with the characters */11Preprocessor DirectivesLines 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 BodyA 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 VariablesWhen 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 variableSpace is set aside in memory to hold a value of the specified data typeThat space is associated with the variable nameThat space is associated with a unique addressVisualization 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 intFloating point (real numbers)float, doubleCharacterschar19Arithmetic Operators in C Name Operator ExampleAddition + num1 + num2Subtraction - initial - spentMultiplication * fathoms * 6Division / sum / countModulus % m % n20DivisionIf 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.86813What 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 StructuresAll 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 CWe write functions in C for the same reasons we do in JavaScriptTo be able to re-use code in other programsTo keep us from having to
View Full Document