Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
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 the most powerful and general-purpose programming language with ease

Arrow left icon
Product type Paperback
Published in Aug 2022
Publisher Packt
ISBN-13 9781801078450
Length 742 pages
Edition 2nd 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 (38) Chapters Close

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

Writing your first C program

We will begin with one of the simplest, most useful programs that can be created in C. This program was first used to introduce C by its creators, Brian W. Kernighan and Dennis M. Ritchie, in their now-classic work, The C Programming Language, which was published in 1978. The program prints a single line of output – the greeting Hello, world! – on the computer screen.

This simple program is important for several reasons. First, it gives us a flavor of what a C program is like, but more importantly, it proves that the necessary pieces of the development environment – the Operating System (OS), text editor, command-line interface, and compiler – are installed and working correctly on your computer system. Finally, it gives us a first taste of the basic programming development cycle. In the process of learning to program and, later, actually solving real problems with programming, you will repeat this cycle often. It is essential that you become both familiar and comfortable with this cycle.

This program is useful because it prints something out to the Terminal, also known as the console, telling us that it actually did something – it displays a message to us. We could write shorter programs in C, but they would not be of much use. Although we would be able to build and run them, we would have very little evidence that anything actually happened. So, here is your first C program. Throughout this book, and during the entirety of your programming experience, obtaining evidence of what actually happened is essential.

Since Kernighan and Ritchie introduced the Hello, world! program over 40 years ago, this simple program has been reused to introduce many programming languages and has been used in various settings. You can find variations of this program in Java, C++, Objective-C, Python, Ruby, and more. GitHub, an online source code repository, even introduces its website and its functions with a Hello World beginner's guide.

Hello, world!

Without further ado, here is the Hello, world! C program. It performs no calculations, nor does it accept any input. It only displays a short greeting and then ends, as follows:

#include <stdio.h>
int main()
{
    printf( "Hello, world!\n" );
    return 0;
}

Some minor details of this program have changed since it was first introduced. What is presented here will build and run with all C compilers that have been created in the last 30 years.

Before we get into the details of what each part of this program does, see whether you can identify which line of the program prints our greeting. You might find the punctuation peculiar; we will explain this in the next chapter. Additionally, notice how some punctuation marks come in pairs, while others do not. There are five paired and five unpaired punctuation marks in total. Can you identify them? (Note that we are not counting the punctuation, that is, the comma and exclamation point, in the Hello, world! message.)

There is another pairing in this simple program that is not so obvious at this time but one that we will explore further in the next chapter. As a hint, this pairing involves the int main() and return 0; lines.

Before we jump into creating, compiling, and running this program, we need to get an overview of the whole development process and the tools we'll be using.

Tip

If you are eager to begin creating your first program, you can jump ahead to the next section. If you do, please come back to the Understanding the program development cycle section to complete your understanding.

You have been reading a chapter from
Learn C Programming - Second Edition
Published in: Aug 2022
Publisher: Packt
ISBN-13: 9781801078450
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