lecture2.pdf - Computer Programming Using C COP 3275 –...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Computer Programming Using C COP 3275 – Spring 2018 Lecture 2: Hello World in C ! {"C"}" /*"Programming"*/" By Ahmed E. Khaled Recap to previous lecture ! •  Computer •  Machine Language •  Syntax and SemanNcs C •  C is a by-­‐product of the UNIX opera&ng system, which was developed at Bell Labs in the late 1960s. •  UNIX was wriYen in assembly language, low-­‐level programming language with a very strong correspondence between the language and the hardware architecture. •  Programs wriYen in assembly language are usually painful to develop and debug. •  But what is the main problem of developing in low-­‐level languages?! (requires knowledge of the hardware) •  Thompson decided that a higher level language was needed for the further development of UNIX. •  The most important feature of the high-­‐level programming languages is the portability (done by the compiler). •  Portable means a developed program can be adapted, so that an executable version can be created for totally different compuNng environments that are differ from the original one (where the program was originally designed). High-­‐level languages (HLL) •  HLL are plaaorm independent where you can write a program once and run it in different types of machines. •  A program wriYen in a high-­‐level language is called a source program, that must be translated into machine code for execuNon. •  The translaNon can be done through either interpreter or compiler to execute the program language or the scripNng language. •  Interpreter reads one statement from the source program at a Nme, translates it to the machine code, and then executes it right away (e.g., Python, Matlab, Perl). •  Compiler translates the enNre source program into a machine-­‐code (binary-­‐code), and the machine-­‐code file is then executed (e.g., C/C++). C takes a middle path between: •  low-­‐level assembly language Direct access to memory layout through pointer manipulaNon •  high-­‐level programming language like Java: Block structure Some encapsulaNon of code, via funcNons •  The standard for C language was completed in December 1989, while other language based on C started to evolve (e.g., C++). •  C++ added more features to support object-­‐oriented and modular programming (we may come across these terms from Nme to Nme). •  C++ facilitates large sohware development projects. •  C provides access to machine-­‐level concepts (e.g., memory addresses) that other programming languages try to hide. •  C provides a limited set of features to keep it as a small language, and relies heavily on “libraries” for more funcNons. •  C is very powerful, was originally designed for systems programming and now is used for applica&ons of all kinds (e.g., embedded systems, commercial data processing). •  C compilers are small and easily wri<en, which has helped make them widely available (portable). Recap important terms: •  •  •  •  •  •  •  •  Machine language Low-­‐level language High-­‐level language Interpreter Compiler Libraries Linker Portability C development environment includes System libraries and headers Applica&on Source Preprocessor and Compiler: converts your applicaNon to object code (machine code) for a specific plaaorm Linker: resolves external references, Nes object files and produces the executable program Stages of Compiling a C program •  Before a program can be executed, three steps are necessary: –  Preprocessing: •  •  •  •  Does some initial processing (e.g., stripping comments) Obeys commands that are known as directives. Finds/locates contents of libraries and headers. The output is translated into assembly instructions. –  Compiling: •  translates the preprocessing output into machine instructions (object code) to be run by the target processor. –  Linking: •  Combines and reorder the object code produced by the compiler with any additional code needed to yield a complete executable program. 11 Hello World, in C ! Phase 1: Sekng up your development environment (done once) •  One of the most popular UNIX-­‐based compilers is gcc (“GNU C Compiler”), which is available for a wide variety of plaaorms. •  In this course, we will use the gcc compiler through the different programming assignments. •  Mac: –  Step 1 install Xcode: go to the App Store -­‐> search for xcode -­‐>then Install –  Step 2 install Command line tools: open xcode -­‐> open the preferences -­‐> select Downloads –  Step 3 verify: open a terminal and type “gcc –v” •  Ubuntu (Linux): –  Step 1 install Build essenCal: open terminal and type “sudo apt-­‐get install gcc” then type “sudo apt-­‐get install build-­‐essenNal” –  Step 2 Verify: open a terminal and type “gcc –version” •  Windows: –  Step 1 Install MinGW (GCC) or Cygwin –  Step 2 verify: •  Open a terminal (CMD) and navigate to the installaNon path \MinGW\bin •  Type: “gcc –version” Two ways to compile and link a program: 1)  Use command line (terminal) Phase 2: Create/Edit your program •  Write the text of your program using any editor (e.g., Sublime, Notepad++, VIM, gedit, nano) •  Save your file with extension of “.c” (e.g., HelloWorld.c) •  Applica&on source is the .c files of your program. Phase 3: Compile & run your program 1)  Open a terminal (or cmd for windows). 2) Navigate to the folder (directory) of your “HelloWorld.c”: (e.g., cd Desktop/C_programming/) 3) Compile your applicaNon using gcc command as detailed in the next slide. 4) If the compiler gives errors, edit source file and re-­‐compile 5) Run your program, as detailed in the next slide. Tells the compiler to generate debugging informaNon (opConal) ApplicaNon Source gcc –Wall -­‐g –o hello HelloWorld.c Tells the compiler to generate ALL “warnings” (opConal) Name of the executable file The compiler will check for errors and then produces an executable program (named hello) in the same directory. To run the output, through the terminal type: ./hello By default, the compiler –with the following command-­‐ generates an executable named a.out gcc HelloWorld.c To run the output, type ./a.out The simplest form to generate the executable file named hello gcc -­‐o hello HelloWorld.c To run the output, type ./hello Two ways to compile and link a program: 2 ) U s e I D E ( I n t e g r a t e d d e v e l o p m e n t environment), that allows us to edit, link, execute and even debug without leaving the environment (Eclipse CDT, Clion). ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern