C versus C++

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

Different header files

  • <stdio.h> input/output
  • <stdlib.h> standard utility functions
  • <string.h> string operations
  • <ctype.h> character class tests
  • <math.h> math functions

Input and output

  • C's console and file I/O initially seems more complicated than C++.
  • But it's sometimes preferable even in C++.

Console I/O

  • printf() write a formatted string to the console
  • scanf() read a formatted string from the keyboard
  • putchar() write a single character to the console
  • getchar() read a single character from the keyboard
  • puts() write a string to the console
  • gets() read a string from the keyboard

File I/O

  • Works with pointers to type FILE:
    FILE *fp;  /* create a pointer to FILE */
  • fopen(), fclose() open/close a text file.
  • feof() detect end-of-file marker in a file.
  • fscanf() read formatted string from a file.
  • fprintf() write formatted string to a file.
  • fgets(), fputs() read/write a string.
  • fgetc(), fputc() read/write a single character.

C is not object-oriented

  • You can't create classes or objects in C.
  • You can't use any of C++'s predefined classes and objects.
  • So, no
    • cout
    • cin
    • string
    • class
    • etc.

C does not have

  • boolean type
    • use int instead
  • reference variables and function parameters
    • use pointers instead
  • function overloading
    • use clever naming instead
  • // single line comments (in older versions of C)
    • use /* multi-line syntax */ instead

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 */
 
  ...
  • Use null-terminated character arrays.
  • Use functions in string.h to operate on strings.

Defining variables

int  main( )
{
    int a,b,c;
    float x,y,z;
 
    ...
}
  • In older versions of C, you must define all variables at the beginning of a function.
  • Common practice today anyway.

Prototypes

/* int foo(); <--optional */
 
int main()
{
    foo();
    return 0;
}
 
int foo()
{
    printf( "Hello world" );
}
  • Function prototypes are not required.
  • Best practice to use them anyway.

Named constants and macros

  • const modifier is available only in newer versions of C.
  • #define preprocessor directive typically used instead.
    #define PI 3.1415
    #define TAX_RATE 0.065
  • #define also often used to create function-like macros.
    #define square(x) ((x) * (x))

Casting

int myInt = 42;
double myDouble;
 
myDouble = (double)myInt/3;  /* cast myInt to a double */
  • Place the type you want a value cast to inside parenthesis before the value.
  • No static_cast<>, etc.

Dynamic memory

  • Use malloc() and free().
  • Allocating memory:
    int *x = (int *) malloc(sizeof(int));          /* a single int */
    int *xArry = (int *) malloc(sizeof(int) * 10); /* array of ints */
  • Deallocating memory:
    free(x);
    free(xArry);

Structs

struct MyStruct
{
    double x;
    double y;
};
 
struct MyStruct aStructInstance;  /* requires struct keyword */
  • Only member variables are allowed.
    • No member functions.
  • Everything is public.
    • No access specifiers.
  • You must use the struct keyword when defining struct variables.

Classes

  • Nope. Nada. Epic fail. No classes or objects in C at all.

Some C Resources