Exploring the Nuances: C vs. C++

C versus C++ - part 1

Estimated read time: 1:20

    Summary

    In this engaging video, the primary differences between the C and C++ programming languages are discussed from the perspective of a C++ developer trying to code in C. C++ is a superset of C, allowing C code development within a C++ environment, but it's recommended to use a dedicated C compiler for genuine C compatibility. The creator showcases how to start projects in C within the Code::Blocks environment, explaining file naming conventions, including header files, and the distinctions in input and output operations between the two languages. Additionally, the video covers formatted printing using printf and provides practical examples of both console and file input/output, illustrating key operations in C coding.

      Highlights

      • C++ includes all of C but use a C compiler for pure C compatibility! ๐ŸŒ
      • Header files in C end with .hโ€”donโ€™t forget! ๐Ÿ–น
      • Coding in C involves printf and scanf for input and output; get familiar with these functions! ๐Ÿ“ˆ
      • Format specifiers in printf are keyโ€”practice using %d, %s, %f, and %c. ๐Ÿ“Š
      • File operations in C: you'll work with fopen, fclose, and moreโ€”remember your FILE pointers! ๐Ÿ“œ

      Key Takeaways

      • C++ is a superset of C; you can technically write C in C++ environments, but it's better to use a C compiler. ๐Ÿ› ๏ธ
      • Header files in C have a .h extension, differing from C++'s lack of extensions. ๐Ÿ“‚
      • Standard I/O functions in C, like printf and scanf, offer a different approach than C++ streams like cout and cin. ๐Ÿ”„
      • Understanding formats in printf (e.g., %d for integers) is crucial for formatted output in C. ๐Ÿ“œ
      • File I/O in C requires the use of FILE pointers and functions like fopen, fclose, and fscanf/fprintf for file operations. ๐Ÿ“

      Overview

      Starting a journey into the differences between C and C++ can be both enlightening and challenging. This video eases in a C++ programmer into the realm of C, pointing out how C++ encompasses all of C's functionality, yet highlights the importance of using a dedicated C compiler for accurate C coding. The journey begins in Code::Blocks, where setting up a project in C demonstrates the step-by-step procedure crucial for newcomers to dive into C development with confidence.

        The narrative delves into C's handling of header files and standard I/O. Unlike C++, C header files bear the .h extension, indicating their distinct role in coding. Emphasis is placed on input and output in C primarily utilizing printf and scanf instead of C++'s stream-based system, employing format specifiers critical for manipulating output efficiently. Through practical setup and code demonstrations, developers can see firsthand how these functions back up C programming.

          Further along, the video transitions to handling file I/O operations in C, lending insight into the FILE pointer system. Developers are shown how to open, manage, and close files using C-specific functions like fopen and fclose. Understanding this aspect highlights C's unique approach and operational methodology, shedding light on why grasping these concepts is crucial for C programming success.

            Chapters

            • 00:00 - 00:30: Introduction to C vs C++ This chapter introduces the primary differences between C and C++, targeting C++ programmers interested in C. It highlights that C++ is a superset of C, suggesting that C code can be developed within a C++ environment.
            • 00:30 - 02:00: Setting Up a C Project in Code Blocks The chapter emphasizes the importance of using a dedicated C compiler to ensure that the developed code is completely C compatible. It introduces the process of starting a development project in Code Blocks for C programming.
            • 02:00 - 04:00: Differences between Header Files in C and C++ The chapter discusses differences between header files in the C and C++ programming languages. During the discussion, the speaker humorously notes the presence of a cat attempting to distract from the recording process. The tutorial proceeds with setting up a new console application project using a software called Code Blocks, emphasizing the selection of the C language for the project setup.
            • 04:00 - 09:00: Formatted Console Input and Output in C The chapter introduces the topic of formatted console input and output in the C programming language. It discusses the use of different compilers, noting that while there are various options available, the focus will be on using the GCC compiler. The GCC compiler is highlighted for its capability to support both C and C++ languages.
            • 09:00 - 13:00: Example of Formatted Output with Variables The chapter discusses the new GCC compiler family, highlighting it as a well-regarded C compiler. It then focuses on a C project, noting the difference in file naming conventions, where '.C' is used instead of '.CPP', which is standard for C files.
            • 13:00 - 19:00: Using scanf for Input The chapter 'Using scanf for Input' begins by discussing the differences in C source code files compared to other languages, specifically C++. It highlights that header files in C are different from those in C++. The chapter seems to introduce the reader to the essential header files commonly used in C programming, which are crucial for managing input and output operations.
            • 19:00 - 29:00: File Input and Output in C The chapter titled 'File Input and Output in C' discusses the essential concept of handling input and output in C programming through the console. It highlights the requirement of including the 'stdio.h' header file for this purpose. Moreover, the transcript notes a distinction between C and C++ in terms of standard header files, as C mandates a '.h' extension, contrary to C++. Although the transcript is incomplete, it suggests a focus on template codeblock additions and general practices for managing input/output effectively in C.
            • 29:00 - 30:30: Reading String Buffers from Files The chapter discusses the concept of reading string buffers from files, specifically focusing on the use of system header files versus local source header files. It touches on the tendency of programmers to habitually include standard library header files even when they are not directly needed for a specific program. The standard library, while not used in the examples given, offers a plethora of utility functions that are commonly employed in programming.

            C versus C++ - part 1 Transcription

            • 00:00 - 00:30 in this module we're going to be learning the primary differences between the language C and the language C++ this assumes that you're coming to this from the perspective of a C++ programmer who is interested in developing in the language C C++ is a superset of the language C which means in theory you can develop C code within a C++ environment
            • 00:30 - 01:00 but you're strongly strongly urged to use a dedicated C compiler when you're developing your code just to make sure that whatever code that you do develop is in fact 100% C compatible so what I'm gonna do is start out by opening up a code blocks project for development as a C program
            • 01:00 - 01:30 and in the background there you could very well be hearing the world's best cat doing her best to make sure that this video doesn't get done I will attempt to do my best to actually finish it alright so here within code blocks we are going to start a new project and this is going to be a console application but when we get to the language we want to use I want to make sure I select the language see and then
            • 01:30 - 02:00 let's just call this C versus CPP lecture next and here if you had a variety of compilers that you wanted to use you could select which a specific compiler you wanted we're gonna go with the default the new GCC compiler that offers in addition to a C++ compiler a
            • 02:00 - 02:30 very good C compiler so the new GCC compiler family is a family of compilers and that's gonna work for us just fine and now what I have is a C project go over and look in sources we can see first off that our file name is not dot CPP main dot CPP but rather just main dot C dot C is the standard extension
            • 02:30 - 03:00 for C source code files so let me double click this and open it up here and what we can see here our headers are different and our input and output are different alright so let's start with the first of these yep header files in C are different from what they are in C++ the two most common header files are
            • 03:00 - 03:30 added by have been added by codeblocks fors in its template if you want to do input an output to the console you will need to include stdio.h note that unlike C++ standard libraries and headers that have no extension all the standard header files in C have a dot H extension we're again using angled
            • 03:30 - 04:00 brackets to indicate that we are going to be using the system header files rather than header files that should be looked up in my local source tree standard library contains a bunch of standard utility functions though none of those are going to be used in this program it's quite often that you end up using those so a lot of programmers just habitually add the standard library even though they might not necessarily be
            • 04:00 - 04:30 required we also have a couple of the libraries that you want to be well aware of string dot H here's where all your functions for manipulating strings and doing other string operations and C type dot H for testing character classes and a number of math functions are available in math dot H okay so let's move on to
            • 04:30 - 05:00 the next thing that we pointed out input and output are different and initially the way C handles console input and output seems more complicated and cumbersome than C++ but once you get even a little bit used to it turns out that in many cases it's actually preferable to using C in and C out so the primary way that you achieve console input and output in C++ pardon me in C
            • 05:00 - 05:30 is to use a variety of consuela io functions if you want to write a formatted string to the console you use print F which is what we have going on here to get or read a formatted string from the keyboard you would use scanf and then four characters and strings we've got sets of functions for those put char writes a single character get charged gets a single character put
            • 05:30 - 06:00 us you can write an entire string to the console and get us to read an entire string from the keyboard I'm going to show you examples of these two in action starting with just a simple uploading hello world notice for our new line this is using the escape sequence there's no equivalent of a MDL in C so if I now build and run this I will see hello
            • 06:00 - 06:30 world right what if I want to output the value of some variables for example maybe I've got a certain number of spoons I've got five spoons and I want to print out something along the lines of I have however many spoons
            • 06:30 - 07:00 well the way this is achieved is by using so-called format strings what I'm going to insert here is a place holder as % d for a value that I want to print out % the % inside this formatted string indicates to the printf function that what follows is a value format and D
            • 07:00 - 07:30 says that what we're going to print out is a decimal integer so that's the first argument to printf then the next argument is the value I want to print which can be a variable or anything any expression that resolves to something that can be printed out as a decimal integer and now if I run that you'll see
            • 07:30 - 08:00 I have 5 spoons I guess I should probably use something resembling proper grammar use a period there so let's take a closer look at some of the available formatting strings I'm gonna grab an example for this so you don't have to bear with my typing so
            • 08:00 - 08:30 here in this example I've got a variety of values I have an integer set to the value 5 I've got a C string which is a null terminated string set to the value ABC I've got a single character set to value s and I've got this float which is a really lame approximation of Pi here I have a print statement that prints out
            • 08:30 - 09:00 all of these to print out this integer in decimal format I'm specifying percent D so that's our format string this indicates I want to print out a value the percent sign indicates I want to print out a value the D indicates I want this bit of data to be printed out as an integer next I've got % s that is the
            • 09:00 - 09:30 format placeholder for Strings and it indicates that I want this to be printed out as a string F is used for floating points and doubles and C is used for characters so print those out and we get v ABC 3.14 and enough zeros to make out
            • 09:30 - 10:00 six trailing decimal places and then our strength our character s so if you want to add some additional texture on this some values you can do something like that so overall a rather powerful and sometimes a better way of printing out
            • 10:00 - 10:30 stuff than using C out what about getting stuff from the user scanf scanf know let me grab an example of that too alright so notice your amusing stdio again scanf and printf are both defined in that header Here I am prompting the
            • 10:30 - 11:00 user to enter a number this is a statement where I get the number and store it in that variable and let's break this down looks a little cryptic but essentially you really only need to provide two things one a formatter that says to scanf what kind of value it should be getting in this case we're
            • 11:00 - 11:30 going to be getting an integer value and the second thing is where do we want to put it so this is a pointer so this needs to be a pointer argument that means if I want to put it into num I have to provide the address of this variable as an argument so what this says is whatever the user enters interpret that as an integer and write
            • 11:30 - 12:00 it into the address of num then here's simply I echo back whatever the user entered so 5 to 42 then I get back 42 so the typical place where you get tripped up is here it's very easy to forget that this needs to be a pointer so I'm not going to show you examples of
            • 12:00 - 12:30 put chard get sharp what I'd like to move on to is file i/o so similarly file i/o is different than it is in C++ the key to working with files in C is the file type the type the data type capital F capital i capital L capital E and in
            • 12:30 - 13:00 order to work with files you need to create a pointer to a datatype file so whenever you're working with files you'll probably have something that looks like this to create a pointer to a file F open will open a file F close will close it if you want to check while you're working with a file typically while you're reading from a file whether
            • 13:00 - 13:30 you've reached the end of a file there's a function called F yo F notice all these file oriented functions begin with F to read from a file you use F scanf whereas to read from the console it's just plain old scanf put an F in front of it it becomes the function used for reading from a file similarly we've got F printf for writing to a file rather than just plain printf and if you want
            • 13:30 - 14:00 to go a print out into our strings we've got F get s and put us to read and write strings and if you want to go character by character you've got F yet C and F put C so let's look at a quick example of using F print death
            • 14:00 - 14:30 so again I'm only including stdio.h that is where our file processing stuff is as well here I'm creating my file pointer which is what I'm going to use to manipulate my file and this is just a temporary variable X the F open function
            • 14:30 - 15:00 I'm using to open a file called output txt in W mode in W mode and this works the same way that the open modes in C++ work W is for writing so this is this is telling us telling the F open function to open up a file output txt for writing - if the file does not exist create it if it does exist open it and wipe its
            • 15:00 - 15:30 contents this then returns a pointer to the thing that is used to manipulate our file which we are going to access through this pointer variable right here to check to make sure that our file actually opened we can use this idiom in other words this is going to return a null pointer if this operation fails so if we're not able to open up with dot
            • 15:30 - 16:00 txt for writing this is going to return a null pointer and not null will be true which means we're going to return one we could also put in there some kind of statement indicating the nature of the air although we aren't in this case then what am I doing here I am counting from one up to ten incrementing by one and then writing to our file so the F printf
            • 16:00 - 16:30 function passing it the file that we wish to print to and then the same syntax that we use with printf in other words I want to print out a decimal integer followed by a new line and the decimal integer I wish to print out is this value X so in effect what this is going to do is print out the values 1 up to 10 to our file it's good
            • 16:30 - 17:00 practice that always close your files when you're done using them and that should bring our program to an end let's run this and we weren't expecting to see anything on the command line on the console but let's see whether we have our file narratives output dot txt and
            • 17:00 - 17:30 if I open that up what I expect to see is a series of integers from 1 to 10 which is what I do have reading from a file is similar except that there's a additional minor complexity
            • 17:30 - 18:00 and that is that guy okay so in this particular program what I'm attempting to do is to open a file called input dot txt again using F open as my file pointer passing it passing the F open function the are string to indicate this is going to be open for reading and here
            • 18:00 - 18:30 I'm doing the check again to see if if I'll open for reading if not we're gonna return one and quit our program otherwise we're gonna do some stuff and then close it alright so let's look at what we're doing here boy that looks like that looks like a lot but in this case what I'm doing is remember get s gets strings so this is going to read
            • 18:30 - 19:00 the contents of my file one string at a time when it reads things it's going to put it here and again this is a pointer and don't forget that arrays function has pointers this is the upper limit of the number of characters to read while I'm getting s and that is where things should be read from and this uses a idiom that we've seen before in C++ so
            • 19:00 - 19:30 as long as this does not fail or in this case this operation does not return no we're going to print out whatever we got into our buffer onto the command line so the slightly cumbersome thing is this and this is typical technique use when you're reading strings in C because you
            • 19:30 - 20:00 don't have string objects in C you have to use so-called C strings and we'll talk more about that later but this is a buffer that I'm using to read things into this is gonna be able to store up to 990 characters so if any luck any line that were reading from our file will be less than a thousand characters long so this
            • 20:00 - 20:30 will get that data a line at a time and that'll print it out