CS253: Software Development with C++

Spring 2018

Cpp Is C

See this page as a slide show

Cpp Is C

CS253 Cpp Is C

C++ is C

When C++ was being developed, C compatibility was very important. It still is. There are zillions of C programs out there, and the creator of C++ wanted it to be very easy to take a C program and turn it into a C++ program.

Therefore, C++ is just about a superset of C. This means that almost all C programs are C++ programs.

Valid C & C++

This is a valid C program, and a valid C++ program:

#include <stdio.h>
int main() {
    printf("Hello, world!\n");
    return 0;
}
Hello, world!

printf is as much a part of C++ as int and while.

97.1680846116% of C programs are valid C++.

C++, not C

This is a valid C program, but it is not a valid C++ program, because C++ took over class as a keyword:

int main() {
    int class=0;
    return class;
}
c.cc: In function 'int main()':
c.cc:2:5: error: expected primary-expression before 'int'
c.cc:3:12: error: expected primary-expression before 'class'
c.cc:3:12: error: expected ';' before 'class'
c.cc:3:12: error: expected primary-expression before 'class'

This is a rare exception.

Better C++

Sure, it’s better C++ style to do the first program like this:

#include <iostream>
using namespace std;
int main() {
    cout << "Hello, world!\n";
    return 0;
}
Hello, world!

However, that doesn’t change the fact that the first program is C++.

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2016-12-26T17:58

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building