80 likes | 129 Views
C vs C++. Networking CS 3470, Section 1 Sarah Diesburg. Hello World. C++ cout << “Hello World!” << 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.
E N D
C vs C++ Networking CS 3470, Section 1 Sarah Diesburg
Hello World • C++ cout << “Hello World!” << endl; • C printf(“Hello World!\n”);
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 • 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;
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;
Declaring Variables • C++ for(int i=0; i<condition; i++){ } • C int i; for(i=0; i<condition; i++) { }
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;
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; }