Easy Tutorial: Computer Programming for DUMMIES -- C++ vs C programming (strings)


Image source

I usually hate these rage faces, but that picture was pretty funny if you are a programming nerd! This is part 15 of my programming tutorials. Here are the rest if you need to catch up:

Part 1: Hello World!

Part 2: Variables

Part 3: Functions

Part 4: if(), else, else if()

Part 5: Loops

Part 6: Arrays

Part 7: Basic Input/Output

Part 9: Sorting Arrays

Part 10: Random Numbers

Part 11: Colored Text in your Terminal

Part 12: Recursion

Part 13: Binary Search

Part 14: 2D Arrays

I want to go in a different direction with this tutorial. Maybe this will become sort of a sub-series to my tutorial series if it is gets good reception. This tutorial will compare the two languages C and C++. C is a very old language. It may be a little bit dated, but it still has very useful applications. C++ was derived from C, so they are very similar. In my opinion, C++ is a bit easier to use. People will warn you to be careful with C, because it will let you figuratively shoot yourself in the foot! I don't think that it is too dangerous as long as you know what you are doing (I won't lie, people have messed up their systems with it, but they were probably doing something risky or stupid -- so don't worry!). Anyways, we are about to look at....

String Processing!

First, let's look at how to declare a string in C vs C++.
Note: for string functions in C, include string.h -- to use string as a data type in C++, include string:
C

#include<string.h>

C++

#include<string>

In C, a string is a character array:

char s[7];
strcpy(s, "string");
  • Don't forget to allocate memory for the null character too! ('\0')
  • char s[7] = {'s','t','r','i','n','g','\0'}; <-- equivalent
  • strcpy() is a C function that stores the second argument in the first argument

In C++, a string is still a character array, but we can handle it a little more easily:

string s = "string";

Comparisons

in C, here is how two strings are compared:

if( (strcmp(s, "string")) == 0)
        printf("Equivalent.\n");
  • if s is equivalent to "string", "Equivalent." is printed
  • strcmp() is a C function that
    • returns 0 if the strings are equivalent
    • returns a value less than 0 if the first argument is smaller
    • returns a value greater than 0 if the first argument is larger

in C++, it is yet again easier:

if(s == "string")
        printf("Equivalent.\n");

Concatenation

... Or putting two strings together.

In C:

strcat(s, " and more string");
  • strcat() is a C function that adds the second argument on to the end of the first
  • suppose s still contains "string" (except there was more space allocated to the array)
  • after the strcat() call, s now contains "string and more string"

In C++

s = s + " and more string";
  • this has the same effect as the C example above
  • again, this is easier!

Printing a String

In C:

printf("%s", s);
  • for printf(), remember to include stdio.h
  • %s is the format specifier for a string
  • prints the contents of s

In C++:

cout << s;
  • for cout, include iostream
  • you might also want to insert this line of code with the includes: "using namespace std;"
  • also prints the contents of s

Other String functions in C:


Image source

Now here are two programs I wrote that do the exact same thing. One is in C, and the other is in C++.

C

#include<stdio.h>
#include <string.h>

int main()
{
    char s[50];
    char s1[6] = {'H','e','l','l','o','\0'};
    char s2[8];
    strcpy(s2, " world!");
    strcat(s1, s2);
    strcat(s, s1);
    printf("%s\n\n", s);
    
    if( !(strcmp(s1, s2)) )
        printf("s1 is equivalent to s2\n\n");
    else
        printf("s1 is NOT equivalent to s2\n\n");

    return 0;
}

C++

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string s;
    string s1 = "Hello";
    string s2 = " world!";
    s = s1 + s2;
    cout << s << endl << endl;

    if(s1 == s2)
        cout << "s1 is equivalent to s2\n\n";
    else
        cout << "s1 is NOT equivalent to s2\n\n";

    return 0;
}

Output for C:

[cmw4026@omega test]$ gcc string.c
[cmw4026@omega test]$ ./a.out
Hello world!

s1 is NOT equivalent to s2

[cmw4026@omega test]$

Output for C++:

[cmw4026@omega test]$ g++ string.cpp
[cmw4026@omega test]$ ./a.out
Hello world!

s1 is NOT equivalent to s2

[cmw4026@omega test]$

Now that we are done, I bet you are thinking, "Why the heck does anyone use C instead of C++!? It is so much harder!!" C is a bit more difficult sometimes, but not always! Basic i/o and string processing are just a few things that are more difficult in C. C is still a valuable language to know because it will let you do things that other languages will not!

I hope this was helpful! Leave any suggestions in the comments!

Sort:  

Freaking awesome man, I love love love this post!

Amazing article, it was good to see the suitle differences between the two.
I've worked with strings in C++ before but not with C.

Thanks! You can actually use any of the C functions in C++ because it is directly derived from C.

Its kind of like the difference between a Toyota Camry and a Ford Mustang. Both cars will get you to where you need to go, but sometimes you want or need the power and control that the manual transmission, 5.0 liter V8 Coyote engine of the Mustang. At other times, you want the ease of use / simplicity / finesse of a Camry.

In my line of work (game development) we switch between the two all the time, depending on what we need for that particular point in code, since C/C++ usually come as a pair with compilers.