Introduction to C Topics Compilation Using the gcc Compiler

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Creating your first C program. Writing C Programs  The programmer uses a text editor to create or modify files containing C code. 
Introduction to C Programming Overview of C Hello World program Unix environment C programming basics.
Introduction to C Programming
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C/C++ Programming This lecture has major focus on programming, compilation.
Computer Science 210 Computer Organization Introduction to C.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Creating your first C++ program
Programming With C.
CMSC 104, Version 9/011 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program 104 C Programming Standards and Indentation.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C 2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park 1.
CMSC 104, Lecture 111 Introduction to C Topics l Compilation l Using the gcc Compiler l The Anatomy of a C Program l 104 C Programming Standards and Indentation.
CMSC 1041 Introduction to C Creating your first C program.
Anatomy of the simplest C Program Sen Zhang SUNY Oneonta Oneonta, NEW YORK © 2004
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
Introduction to C Programming
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Introduction to C Topics Compilation Using the gcc Compiler
UMBC CMSC 104 – Section 01, Fall 2016
CSCE 206 Structured Programming in C
User-Written Functions
Computer Science 210 Computer Organization
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Functions, Part 2 of 2 Topics Functions That Return a Value
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 - Introduction to C Programming
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Introduction to C Programming
Computer Science 210 Computer Organization
CMSC 104, Section 4 Richard Chang
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
Chapter 2 - Introduction to C Programming
Introduction to C Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
Govt. Polytechnic,Dhangar
Variables in C Topics Naming Variables Declaring Variables
Creating your first C program
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park
Introduction to C Topics Compilation Using the gcc Compiler
Programs written in C and C++ can run on many different computers
Chapter 2 - Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
Variables in C Topics Naming Variables Declaring Variables
Introduction to C Topics Compilation Using the gcc Compiler
Introduction to C Programming
Variables in C Topics Naming Variables Declaring Variables
Functions, Part 2 of 42 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Chapter 2 part #1 C++ Program Structure
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Differences between JavaScript and C

Writing 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.c

Using 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.

Invoking 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).

The 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.

Program Development Using gcc Editor Source File pgm.c Preprocessor Modified Source Code in RAM Compiler Program Object Code File pgm.o Other Object Code Files (if any) Linker Executable File a.out

Hello 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; }

Screenshot of Hello, World Program

Anatomy of a C Program program header comment preprocessor directives (if any) int main ( ) { statement(s) return 0 ; }

Comments in C All comments must begin with the characters /* and end with the characters */

Preprocessor 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.

int 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.

The 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.

printf (“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.

return 0 ; Because function main() returns an integer value, there must be a statement that indicates what this value is. The statement return 0 ; indicates that main() returns a value of zero to the operating system. A value of 0 indicates that the program successfully terminated execution.

Declaring Variables When declaring a variable in C, you must also specify the type of the variable. Examples of variable declarations: int hw1Grade ; float finalAverage ;

Declaring 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 name hw1Grade garbage

More About Variables C has three basic predefined data types: Integers (whole numbers) int, long int, short int, unsigned int Floating point (real numbers) float, double Characters char

Arithmetic Operators in C Name Operator Example Addition + num1 + num2 Subtraction - initial - spent Multiplication * fathoms * 6 Division / sum / count Modulus % m % n

Division 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 = 3

Division (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.

Getting 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 ; }

Control 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.

Functions 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 repeat the same code over and over Each programming language you learn has a different way of defining and calling functions.

Function Example in C #include <stdio.h> float AverageTwo (int num1, int num2) ; int main ( ) { float ave ; int value1 = 5, value2 = 8 ; ave = AverageTwo (value1, value2) ; printf (“The average of %d and %d is %f\n”, value1, value2, ave) ; return 0 ; } float AverageTwo (int num1, int num2) float average ; average = (num1 + num2) / 2.0 ; return average ;