1 / 8

C vs C++

C vs C++. Networking CS 3470, Section 1 Sarah Diesburg. Hello World. C++ cout &lt;&lt; “Hello World!” &lt;&lt; endl; C printf(“Hello World!<br>”);. Dynamic Array/Memory Allocation. C++ int *x_array = new int[10]; delete[] x_array; C int *x_array = malloc(sizeof(int) *10); free(x_array);. Structs.

jimv
Download Presentation

C vs C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C vs C++ Networking CS 3470, Section 1 Sarah Diesburg

  2. Hello World • C++ cout << “Hello World!” << endl; • C printf(“Hello World!\n”);

  3. Dynamic Array/Memory Allocation • C++ int *x_array = new int[10]; delete[] x_array; • C int *x_array = malloc(sizeof(int) *10); free(x_array);

  4. Structs • After defining a struct, how do you declare a new instance? struct a_struct { int x; }; • C++ a_struct struct_instance; • C struct a_struct struct_instance;

  5. Typedefs • To get around typing so much, use a typedef typedef struct a_struct { int x; }a_struct_type; • Now declare it a_struct_type struct_instance;

  6. Declaring Variables • C++ for(int i=0; i<condition; i++){ } • C int i; for(i=0; i<condition; i++) { }

  7. Boolean • No boolean type in C • You can simulate through enum typedef enum {FALSE, TRUE} bool; • Why is false before true? typedef enum {FALSE=0, TRUE=1} bool;

  8. Boolean Example #include <stdio.h> #include <stdlib.h> typedefenum {FALSE, TRUE} bool; main() { bool flag1=TRUE; bool flag2=FALSE; if(flag1 > flag2) printf("The truth wins again!\n"); return 0; }

More Related