User Tools

Site Tools


cplusplus:c_versus_cplusplus

This is an old revision of the document!


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

  • <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

  • Use null-terminated character arrays.
  • Use functions in string.h to manipulate strings.
#include <string.h>
 
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

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

Prototypes

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

Named constants and macros

  • const 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

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

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

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

Classes

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

Some C Resources

C Examples

cplusplus/c_versus_cplusplus.1588554643.txt.gz · Last modified: 2020/05/04 01:10 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki