User Tools

Site Tools


View page as slide show

C versus C++

Adapted by Mithat Konar
from Differences Between C and C++
by Robert Niemann (Century College)

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.

Different header files

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

Input and output

  • Input/output is generally more complicated in C.

Console I/O

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

File I/O

  • 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() read a string from a file.
  • fputs() write a string to a file.
  • fgetc() read single character from a file.
  • fputc() write a single character to a file.

C does not have

  • classes
  • boolean type
  • reference variables
  • function overloading
  • // single line comments (in older versions of C).

Defining variables

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


  • Function prototypes are not required.
  • Best practice to use them anyway.
    int main()
        return 0;
    int foo()
        printf( "Hello world" );

Named constants and macros

  • const is available only in newer versions of C.
  • #define precompiler directive typically used instead.
    #define PI 3.1415
    #define TAX_RATE 0.065
  • #define can also be used to create macros.
    #define square(x) ((x) * (x))

Dynamic memory is different

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


  • No member functions.
  • No access specifiers.
  • You must use the struct keyword when defining struct variables.
    struct MyStruct
        int x;
    struct MyStruct aStructInstance;  // requires struct keyword

Some C Resources

C Examples

cplusplus/c_versus_cplusplus.txt · Last modified: 2019/05/06 16:40 by mithat