Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learn C Programming

You're reading from   Learn C Programming A beginner's guide to learning C programming the easy and disciplined way

Arrow left icon
Product type Paperback
Published in Jun 2020
Publisher Packt
ISBN-13 9781789349917
Length 646 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Jeff Szuhay Jeff Szuhay
Author Profile Icon Jeff Szuhay
Jeff Szuhay
Arrow right icon
View More author details
Toc

Table of Contents (33) Chapters Close

Preface 1. Section 1: C Fundamentals
2. Running Hello, World! FREE CHAPTER 3. Understanding Program Structure 4. Working with Basic Data Types 5. Using Variables and Assignment 6. Exploring Operators and Expressions 7. Exploring Conditional Program Flow 8. Exploring Loops and Iteration 9. Creating and Using Enumerations 10. Section 2: Complex Data Types
11. Creating and Using Structures 12. Creating Custom Data Types with typedef 13. Working with Arrays 14. Working with Multi-Dimensional Arrays 15. Using Pointers 16. Understanding Arrays and Pointers 17. Working with Strings 18. Creating and Using More Complex Structures 19. Section 3: Memory Manipulation
20. Understanding Memory Allocation and Lifetime 21. Using Dynamic Memory Allocation 22. Section 4: Input and Output
23. Exploring Formatted Output 24. Getting Input from the Command Line 25. Exploring Formatted Input 26. Working with Files 27. Using File Input and File Output 28. Section 5: Building Blocks for Larger Programs
29. Working with Multi-File Programs 30. Understanding Scope 31. Other Books You May Enjoy Appendix

Adding comments to the Hello, world! program

Now that we have explored the various ways we can comment on code and commenting styles, let's copy hello1.c to hello2.c and add appropriate comments.

You can either copy hello1.c to hello2.c with your command interpreter or, in your editor, open hello1.c and immediately save it as hello2.c. Regardless of how you do this, you should have both hello1.c and hello2.c in your Chapter1_HelloWorld directory.

In your editor, modify hello2.c so that it looks as follows:

/*
* hello2.c
* My first C program with comments.
* by <your name>
* created yyyy/mm/dd
*/

#include <stdio.h>

int main()
{
printf( "Hello, world!\n" );
return 0;
}

/* eof */

Note how the * at the beginning of each line providing a comment makes it clear that there are several lines of comments in a group; the group begins with /* and eventually ends with */. Compile, run, and verify this program. Be certain you haven't introduced an accidental character here or there, which is always possible and should always be verified.

This is now a complete program. We know from the evidence from hello1.c that the program is correct– it displays our intended message in the way we desire. The first six lines of comments provide minimal information about the program's author and the date it was written. This program's heading information may be simple or it may be more comprehensive. For now, we will keep such heading information simple.

The program itself is so simple that anyone who understands C would know that a simple message is printed. No further commenting is needed here.

Finally, we mark the end of the file with a comment; the only benefit to such a marking is when there are multiple editor windows open and/or programs get very long. This simple demarcation lets humans know we're at the EOF. This final EOF indicator is entirely optional and becomes more of a stylistic preference than a practice with rigorous rationale.

I have found that in every programming language I have used, my commenting style has adapted to the clarity or obtuseness of the given language. When I programmed in assembler language at university or later in an early version of Fortran 4, I commented on almost every line. But for C++ or Objective-C, I found I comment only sparsely or in globs– large sections of comments that explain a concept or programming solution.

Furthermore, even within a given language, when the problem being solved is unusual or I am using a novel approach to its solution, more comments are in order.

In the remainder of this book, depending on the code sample, we'll explore various useful commenting practices that are effective, even when the code is subject to change.

You have been reading a chapter from
Learn C Programming
Published in: Jun 2020
Publisher: Packt
ISBN-13: 9781789349917
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image