Sunday 14 July 2019

Your first C program


Your first C program

You will learn to write a “Hello, World!” program in this section.

Why “Hello, World!” program?

“Hello, World!” is a simple program that displays “Hello, World!” on the screen. Since, it’s a very simple program, it is used to illustrate the basic syntax of any programming language.
This program is often used to introduce programming language to a beginner. So, let’s get started.
  1. #include <stdio.h>
  2. int main()
  3. {
  4. printf("Hello, World!\n");
  5. return 0;
  6. }

How “Hello, World!” program works?

Include stdio.h header file in your program.
C programming is small and cannot do much by itself. You need to use libraries that are necessary to run the program. The stdio.h is a header file and C compiler knows the location of that file. To use the file, you need to include it in your program using #includepreprocessor.
Why do you need stdio.h file in this program?
In this program, we have used printf() function which displays the text inside the quotation mark. Since printf() is defined in stdio.h, you need to include stdio.h.
The main() function
In C programming, the code execution begins from the start of main() function (doesn’t matter if main() isn’t located at the beginning).
The code inside the curly braces { } is the body of main() function. The main() function is mandatory in every C program.
int main() {
}
This program doesn’t do anything but, it’s a valid C program.
The printf() function
The printf() is a library function that sends formatted output to the screen (displays the string inside the quotation mark). Notice the semicolon at the end of the statement.
In our program, it displays Hello, World! on the screen.
Remember, you need to include stdio.h file in your program for this to work.
The return statement
The return statement return 0; inside the main() function ends the program. This statement isn’t mandatory. However, it’s considered good programming practice to use it

What will you gain if you learn C?

What will you gain if you learn C?

If you don’t know C, you don’t know what you are doing as a programmer. Sure, your application works fine and all. But, if you can’t say why while (*s++ = *p++); copies a string, you’re programming on a superstition. ( Joel Spolsky’s words, not mine ).
  1. You will understand how a computer works.

    If you know C, you will not only know how your program works but, you will be able to create a mental model on how a computer works (including memory management and allocation). You will learn to appreciate the freedom that C provides unlike Python and Java.

    Understanding C allows you to write programs that you never thought were possible before (or at the very least, you will have a broader understanding of computer architecture and programming as a whole).
  2. C is the lingua franca of programming.

    Almost all high-level programming languages like Java, Python, JavaScript etc. can interface with C programming. Also, it’s a good language to express common ideas in programming. Doesn’t matter if the person you are talking with doesn’t know C, you can still convey your idea in a way they can understand.
  3. Opportunity to work on open source projects that impact millions of people.

    At first, you may overlook the fact that C is an important language. If you need to develop a mobile app, you need Java (for Android), Swift and Objective C (for iOS). And there are dozens of languages like C#, PHP, ASP.net, Ruby, Python for building web application. Then, where is C programming?

    Python is used for making wide range for applications. And, C is used for making Python. If you want to contribute to Python, you need to know C programming to work on Python interpreter that impacts millions of Python programmers. This is just one example. A large number of softwares that you use today is powered by C.

    Some of the larger open source projects where C programming is used are Linux Kernel, Python Interpreter, SQLite Database.

    Another language that’s commonly used for large open source project is C++. If you know C and C++, you can contribute to large open source projects that impacts hundreds of millions of people.
  4. You will write better programs.

    To be honest, this statement may not be true all the time. However, knowing how computer works and manage memory gives you insight on how to write efficient code in other programming languages.
  5. You will find it much easier to learn other programming languages.

    A lot of popular programming languages are based on C (and C++, considered superset of C programming with OOP features). If you know C, you will get a head start learning C++.

    Languages like C# and Java are related to C and C++. Also, the syntax of JavaScript and PHP is similar to C.

    If you know C and C++ programming, you will not have any problem switching to another language.

What is C Programming? - The Basics

What is C Programming? - The Basics

Before getting started with C programming, lets get familiarized with the language first.
C is a general-purpose programming language used for wide range of applications from Operating systems like Windows and iOS to software that is used for creating 3D movies.
C programming is highly efficient. That’s the main reason why it’s very popular despite being more than 40 years old.
Standard C programs are portable. The source code written in one system works in another operating system without any change.
As mentioned, it’s a good language to start learning programming. If you know C programming, you will not just understand how your program works, but will also be able to create a mental picture on how a computer works.
C is a powerful general-purpose programming language. It is fast, portable and available in all platforms.
If you are new to programming, C is a good choice to start your programming journey.
This is a comprehensive guide on how to get started in C programming language, why you should learn it and how you can learn it.