Your first C and C++ programs Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd 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, 5 th and 6 th editions, by Deitel and Deitel) 1
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 2
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 3
Your first C program (continued) /* * helloworld.c - "hello, world" program */ #include <stdio.h> int main (void) { printf( Hello, World!\n ); return 0; } main must return zero if successful, otherwise some non-zero value so OS can recognize an error A function definition Note: by convention, operating system invokes function named main to start a program. 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; } 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) A function definition Note: by convention, operating system invokes function named main to start a program. 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; } Question: where is printf declared? 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 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> Answer: In this file! int main (void) { printf( Hello, World!\n ); return 0; } 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! o 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 7
Questions? 8
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 9
Your first C++ program (continued) // helloworld.cpp #include <iostream> int main(void) { std::cout << Hello World! << std::endl; return 0; } The left shift operator applied to an ostream object and a string constant Another left shift operator, applied to the constant endl (defined in 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; } The scope resolution operator Provides access to names declared in a class CS-2303, A-Term 2012 Your first C and C++ programs 11
Questions? 12
What happens to your code after it has been compiled but before it runs? 13
Compilation (part 1) 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! 14
Compilation (continued) gcc HelloWorld.c Preprocessor Translator Code gen & opt. HelloWorld.o gcc printf.c Preprocessor Translator Code gen & opt. printf.o ar (i.e., archive) Linker (part of gcc) Library of.o files for common routines a.out (or file name of your command or program) Note: this topic is developed in greater detail in CS-2011 Loader (part of OS) Memory 15
Questions? 16
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 17
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 18
Questions? 19