C++ Programming Lecture 1: Hello World Program and Using the Standard Library, Study notes of C programming

The 'Hello World' program in both C and C++ versions, explains how to compile it as C++, and discusses using the standard library functions in C++. It covers the use of header files, pre-processor commands, and the main function.

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

arjaa
arjaa 🇺🇸

4.2

(5)

229 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
G52CPP
C++ Programming
Lecture 1
Extra Material
Dr Jason Atkin
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download C++ Programming Lecture 1: Hello World Program and Using the Standard Library and more Study notes C programming in PDF only on Docsity!

G52CPP

C++ Programming

Lecture 1

Extra Material

Dr Jason Atkin

“Hello World”

A simple C++ (and C) program

Since you have had 2 semesters to

forget what you did in G51PRG

Compiling as C++

  • Name the files

.cpp

instead of

.c

  • Can still include header files• C preprocessor still exists
    • Can still use

#define

  • Templates often make macros unnecessary
    • Can still use

#include

  • But see next slide
    • Conditional compilation is still needed in

order to avoid multiple header file inclusion^ – i.e.

#ifndef

#define

#endif

Using the C library functions

-^

The

standard C library

is still available

-^

Header files have changed names^ – Add a

c

at the beginning and remove the

.h

  • e.g.

#include

-^

C++ header files MAY differ from the C versions^ – But provide the same functionality–

In C++ they may not actually be files

-^

Functions are in the

std

namespace

  • We will consider namespaces later -^

But, also available in global namespace^ – So you can use them as global functions

-^

Need to link to the

libstdc++

library in gcc

  • e.g.

gcc

test.cpp –lstdc++ -o test

  • or

g++

test.cpp –o test

int

main(int

argc,

char*

argv[])

#include

int main( int argc, char* argv[] ) {

printf("Hello world!\n");return 0;

•^

Define a function called

main()

, which returns a value of

type ‘

int

’ and has two parameters called

argc

and

argv

•^

Your program will start with a call to your ‘

main

’ function

argc

and

argv

specify the command line arguments

argc

is of type ‘

int

’ and is the count of arguments

char* and argv

int

main(

int argc, char* argv[]

argv

is of type ‘

char*[ ]

  • an array of ‘

char*

’s, or C-style strings

  • The elements of the array are the command line

arguments

-^

Remember:^ –

char*

is a

pointer

to a character

  • In this case, a pointer to an array of characters– With a value 0 at the end -^

This is the only type of string available in C, butC++ provides us with more possibilities