Your first C and C++ programs

Slides:



Advertisements
Similar presentations
Linking & Loading CS-502 Operating Systems
Advertisements

Functions and InterfacesCS-2303, C-Term Functions and Interfaces in C CS-2303, System Programming Concepts (Slides include materials from The C Programming.
Introduction to C Programming CE Lecture 1 Introduction to C.
1 CSSE 332 Course Introduction. Roll Call and Introductions Name (nickname) Name (nickname) Hometown Hometown Local Residence Local Residence Major Major.
More Miscellaneous Topics CS-2301 B-term More Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The.
CS201 - Information. CS201 - Laboratories All labs will be done using Linux on the PC’s or esus. All labs MUST use Makefiles. First lab is due NEXT WEEK.
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
Computer Science 210 Computer Organization Introduction to C.
Programming With C.
1 計算機程式設計 Introduction to Computer Programming Lecture01: Introduction and Hello World 9/10/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction.
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.
Compilation & Linking Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens The Preprocessor When a C compiler is invoked, the.
N from what language did C++ originate? n what’s input, output device? n what’s main memory, memory location, memory address? n what’s a program, data?
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.
Department of Electronic & Electrical Engineering Introduction to C - The Development cycle. Why C? The development cycle. Using Visual Studio ? A simple.
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
An overview of C Language. Overview of C C language is a general purpose and structured programming language developed by 'Dennis Ritchie' at AT &T's.
L071 Introduction to C Topics Compilation Using the gcc Compiler The Anatomy of a C Program Reading Sections
Introduction to GCC Department of Radio Physics and Electronics ILug-Cal Introduction to GCC Department of Radio Physics and Electronics, Calcutta University.
Introduction to C Topics Compilation Using the gcc Compiler
UMBC CMSC 104 – Section 01, Fall 2016
Computer Science 210 Computer Organization
Computer Terms Review from what language did C++ originate?
C Programming.
CSE 220 – C Programming C Fundamentals.
C Programming Hardik H. Maheta.
C Language By Sra Sontisirikit
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.
Linking & Loading.
Day 01 Introduction to Linux and C
Introduction to C Topics Compilation Using the gcc Compiler
CS-3013 Operating Systems C-term 2008
Introduction to C Topics Compilation Using the gcc Compiler
The Preprocessor Based on Chapter 1 in C++ for Java Programmers by Weiss When a C compiler is invoked, the first thing that happens is that the code is.
Computer Science 210 Computer Organization
Programming Assignment #4 Binary Trees in C++
C Programming.
مباني كامپيوتر و برنامه سازي
Makefiles and Notes on Programming Assignment PA2
Govt. Polytechnic,Dhangar
Introduction to C Topics Compilation Using the gcc Compiler
Creating your first C program
“Under the Hood” of Polymorphism
Classes, Constructors, etc., in C++
Containers and the Standard Template Library (STL)
Miscellaneous C++ Topics
Programming Assignment #1 12-Month Calendar—
Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Symbolic Constants in C
Recursion and Implementation of Functions
Programming Assignment #6
Scope Rules and Storage Types
Programming Fundamentals Lecture #3 Overview of Computer Programming
Programming Assignment #5
Linking & Loading CS-502 Operating Systems
2008/09/29: Lecture 7 CMSC 104, Section 0101 John Y. Park
Differences between Java and C
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Functions in C and C++ CS-2303 System Programming Concepts Hugh C. Lauer (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
Introduction to C Topics Compilation Using the gcc Compiler
A Deeper Look at Classes
Computer Terms Review from what language did C++ originate?
Linking & Loading CS-502 Operating Systems
Introduction to C Topics Compilation Using the gcc Compiler
Chapter 2 part #1 C++ Program Structure
Introduction to Classes and Objects
Presentation transcript:

Your first C and C++ programs Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5th and 6th editions, by Deitel and Deitel) CS-2303, A-Term 2012 Your first C and C++ programs

Your first C and C++ programs Your first C program /* * helloWorld.c - "hello, world" program */ #include <stdio.h> int main (void) { printf(″Hello, World!\n″); return 0; } CS-2303, A-Term 2012 Your first C and C++ programs

Your first C program (continued) /* * helloWorld.c - "hello, world" program */ #include <stdio.h> int main (void) { printf(″Hello, World!\n″); return 0; } A comment:– Surrounded by /* and */ or “//” to end of line Same as in Java CS-2303, A-Term 2012 Your first C and C++ programs

Your first C program (continued) /* * helloWorld.c - "hello, world" program */ #include <stdio.h> int main (void) { printf(″Hello, World!\n″); return 0; } A function definition Note:– by convention, operating system invokes function named “main” to start a program. “main” must return zero if successful, otherwise some non-zero value so OS can recognize an error CS-2303, A-Term 2012 Your first C and C++ programs

Your first C program (continued) /* * helloWorld.c - "hello, world" program */ #include <stdio.h> int main (void) { printf(″Hello, World!\n″); return 0; } A function definition Note:– by convention, operating system invokes function named “main” to start a program. “main” must return zero if successful, otherwise some non-zero value so OS can recognize an error (arguments to main to be discussed later in course) CS-2303, A-Term 2012 Your first C and C++ programs

Your first C program (continued) /* * helloWorld.c - "hello, world" program */ #include <stdio.h> int main (void) { printf(″Hello, World!\n″); return 0; } printf — A call to the C library function to format and print a string of text See K & R §1.1, §1.2, §1.5.3, and §7.2 Question:– where is printf declared? CS-2303, A-Term 2012 Your first C and C++ programs

Your first C program (continued) /* * helloWorld.c - "hello, world" program */ #include <stdio.h> int main (void) { printf(″Hello, World!\n″); return 0; }  Answer:– In this file! Major difference from Java An “include file” in C or C++ (a.k.a. a “header file”) is a text file declaring a bunch of stuff that is provided elsewhere! E.g., in libraries OR in other C programs (i.e., modules) that you create Copied bodily into your program as first step of compilation! CS-2303, A-Term 2012 Your first C and C++ programs

Your first C and C++ programs Questions? CS-2303, A-Term 2012 Your first C and C++ programs

Your first C and C++ programs Your first C++ program Note file extension An “include” file (as in C) // helloworld.cpp #include <iostream> int main(void) { std::cout << ″Hello World!″ << std::endl; return 0; } A (static) object of the standard class ostream which is a subclass of iostream CS-2303, A-Term 2012 Your first C and C++ programs

Your first C++ program (continued) // helloworld.cpp #include <iostream> int main(void) { std::cout << ″Hello World!″ << std::endl; return 0; } Another left shift operator, applied to the constant endl (defined in iostream) The left shift operator applied to an ostream object and a string constant CS-2303, A-Term 2012 Your first C and C++ programs

Your first C++ program (continued) // helloworld.cpp #include <iostream> int main(void) { std::cout << ″Hello World!″ << std::endl; return 0; } The scope resolution operator Provides access to names declared in a class CS-2303, A-Term 2012 Your first C and C++ programs

Your first C and C++ programs Questions? CS-2303, A-Term 2012 Your first C and C++ programs

What happens to your code … … after it has been compiled but before it runs? CS-2303, A-Term 2012 Your first C and C++ programs

Code generator & optimizer Compilation (part 1) All part of gcc HelloWorld.c Preprocessor  Modifies the source code Creates internal data structure representing the program  Language translator Code generator & optimizer  Generates machine code HelloWorld.o However, program is not yet ready to execute! CS-2303, A-Term 2012 Your first C and C++ programs

Compilation (continued) printf.c printf.o Code gen & opt. Preprocessor Translator HelloWorld.c HelloWorld.o Code gen & opt. Preprocessor Translator gcc gcc ar (i.e., archive) Linker (part of gcc) a.out (or file name of your command or program) Library of .o files for common routines Loader (part of OS) Note: this topic is developed in greater detail in CS-2011 Memory CS-2303, A-Term 2012 Your first C and C++ programs

Your first C and C++ programs Questions? CS-2303, A-Term 2012 Your first C and C++ programs

Laboratory Assignment #1 Wednesday, August 29 — Edit, compile, and run both “Hello, World!” programs I.e., in C and C++ On CCC Linux Use gcc for compiler Also, write a short program to do a simple numerical calculation Based on your knowledge of Java If time, work on Programming Assignment #1 Due Friday, August 31 CS-2303, A-Term 2012 Your first C and C++ programs

Extra Laboratory Credit IF you already know how to do the laboratory AND and IF you have already submitted the required solution to Turnin you may earn extra laboratory credit by helping the TAs help others Speak to the TAs to take advantage of this CS-2303, A-Term 2012 Your first C and C++ programs

Your first C and C++ programs Questions? CS-2303, A-Term 2012 Your first C and C++ programs