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

Running your first C program

Your hello1.c program has been successfully compiled, and you now have an a.out file in the same directory. It's time to run it! Let's get started:

  1. In a Terminal, command-line, or console window (depending on your OS), navigate to the directory that holds a.out.
  2. At the command prompt, usually indicated by a > character in the first column, enter ./a.out.
  3. You should see Hello, world!.
  4. If you see that, we can now verify the output of your program.
  5. Note that the command prompt, $, is not on the same line as Hello, world!. This means you correctly entered \n in the output stream. If not, you need to re-edit hello1.c and make sure \n occurs immediately preceding the second ", recompile it, and rerun a.out.
  6. If Hello, world! is on a line by itself with a command prompt before and after it – woo-hoo! You did it!

If everything works as it should, you should see the following output in your Terminal:

Figure 1.1 – A successful compilation and execution of hello1.c

Figure 1.1 – A successful compilation and execution of hello1.c

In the screenshot of our Terminal session, > is the prompt the Terminal gives to indicate that it is ready to take input from us. After the first prompt, we have entered cc hello1.c followed by <return>. The fact that the very next thing we see is another prompt means that the compilation was successful, and we can now run the compiled program. We then enter a.out followed by <return> to execute hello1.c. We should see our desired message, Hello, world!, followed by a prompt awaiting Figure 1.1 – further input.

If the compiler spews out any error messages, read what the compiler is telling you and try to understand what error it is telling you to fix. Always focus on the very first error message, to begin with; later error messages are usually the result of the very first error. Then, go back to the editing phase and examine where your entered program is different from what has been shown here. The two must match exactly. Then, come back to this phase; hopefully, your program will compile successfully (that is, there will be no error messages).

It's always important to remember to do a little dance, and make a little joy, get down tonight! when you've successfully completed something. Programming can be very frustrating, so remembering to celebrate even your small successes will make your life a little bit more joyful through all the frustration. Too many programmers forget this incremental and regular step of celebrating with joy!

As we progress through this book, we'll add more compiler options to the cc command to make our life easier.

Tip

If you skipped over the Understanding the program development cycle section of this chapter, now would be a good time to read it before moving on.

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 €18.99/month. Cancel anytime