Table of Contents

View page as slide show

C versus C++

Mithat Konar
Derived from Differences Between C and C++ by Robert Niemann
2020-05-03

Different header files

Input and output

Console I/O

File I/O

C is not object-oriented

C does not have

Strings

#include <string.h>       /* needed to use strlen() function */
 
int main(void) {
  char s1[81] = {'H', 'e', 'l', 'l', 'o', '\0'};
  char s2[81] = "Hello World";
  char s3[] = "O hai.";
 
  int len1 = strlen(s1),  /*  5 */
      len2 = strlen(s2),  /* 11 */
      len3 = strlen(s3);  /*  6 */
 
  ...

Defining variables

int  main( )
{
    int a,b,c;
    float x,y,z;
 
    ...
}

Prototypes

/* int foo(); <--optional */
 
int main()
{
    foo();
    return 0;
}
 
int foo()
{
    printf( "Hello world" );
}

Named constants and macros

Casting

int myInt = 42;
double myDouble;
 
myDouble = (double)myInt/3;  /* cast myInt to a double */

Dynamic memory

Structs

struct MyStruct
{
    double x;
    double y;
};
 
struct MyStruct aStructInstance;  /* requires struct keyword */

Classes

Some C Resources