int main (void) { printf(″Hello, World!\n″); return 0; } CS-2303, A-Term 2012 Your first C and C++ programs"> int main (void) { printf(″Hello, World!\n″); return 0; } CS-2303, A-Term 2012 Your first C and C++ programs">
Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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
4
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
5
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
6
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
7
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
8
Your first C and C++ programs
Questions? CS-2303, A-Term 2012 Your first C and C++ programs
9
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
10
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
11
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
12
Your first C and C++ programs
Questions? CS-2303, A-Term 2012 Your first C and C++ programs
13
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
14
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
15
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
16
Your first C and C++ programs
Questions? CS-2303, A-Term 2012 Your first C and C++ programs
17
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
18
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
19
Your first C and C++ programs
Questions? CS-2303, A-Term 2012 Your first C and C++ programs
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.