C is a procedural programming language. It was initially developed by Dennis Ritchie in the year 1972. It was mainly developed as a system programming language to write an operating system. The main features of C language include low-level access to memory, a simple set of keywords, and a clean style; these features make C language suitable for system programmings like an operating system or compiler development.

Many later languages have borrowed syntax/features directly or indirectly from C language. Like syntax of Java, PHP, JavaScript, and many other languages are mainly based on C language. C++ is nearly a superset of C language (There are few programs that may compile in C, but not in C++). 

Why to Learn C Programming?

C programming language is a MUST for students and working professionals to become a great Software Engineer especially when they are working in Software Development Domain. I will list down some of the key advantages of learning C Programming:

  • Easy to learn
  • Structured language
  • It produces efficient programs
  • It can handle low-level activities
  • It can be compiled on a variety of computer platforms

We have already brought you the application of C where you can check it out! 

Introduction to C

Every full C program begins inside a function called “main”. A function is simply a collection of commands that do “something”. The main function is always called when the program first executes. From the main, we can call other functions, whether they are written by us or by others, or use built-in language features. To access the standard functions that come with your compiler, you need to include a header with the #include directive. What this does is effectively take everything in the header and paste it into your program. Let’s look at a working program: 

#include <stdio.h>
int main()
{
    printf( "I am alive!  Beware.\n" );
    getchar();
    return 0;
}

Let’s look at the elements of the program. The #include is a “preprocessor” directive that tells the compiler to put code from the header called stdio.h into our program before actually creating the executable.

By including header files, you can gain access to many different functions–both the printf and getchar functions are included in stdio.h.

The next important line is int main(). This line tells the compiler that there is a function named main and that the function returns an integer, hence int. The “curly braces” ({ and }) signal the beginning and end of functions and other code blocks. If you have programmed in Pascal, you will know them as BEGIN and END. Even if you haven’t programmed in Pascal, this is a good way to think about their meaning.

The printf function is the standard C way of displaying output on the screen. The quotes tell the compiler that you want to output the literal string as-is (almost). The ‘\n’ sequence is actually treated as a single character that stands for a newline (we’ll talk about this later in more detail); for the time being, just remember that there are a few sequences that, when they appear in a string literal, are actually not displayed literally by printf and that ‘\n’ is one of them. The actual effect of ‘\n’ is to move the cursor on your screen to the next line. Notice the semicolon: it tells the compiler that you’re at the end of a command, such as a function call. You will see that the semicolon is used to end many lines in C.

The next command is getchar(). This is another function call: it reads in a single character and waits for the user to hit enter before reading the character.

This line is included because many compiler environments will open a new console window, run the program, and then close the window before you can see the output. This command keeps that window from closing because the program is not done yet because it waits for you to hit enter. Including that line gives you time to see the program run.

Finally, at the end of the program, we return a value from main to the operating system by using the return statement.

This return value is important as it can be used to tell the operating system whether our program succeeded or not. A return value of 0 means success.

Explaining your Code

Comments are critical for all but the most trivial programs and this tutorial will often use them to explain sections of code.

When you tell the compiler a section of text is a comment, it will ignore it when running the code, allowing you to use any text you want to describe the real code. To create a comment in C, you surround the text with /* and then */ to block off everything between as a comment.

Certain compiler environments or text editors will change the color of a commented area to make it easier to spot, but some will not. Be certain not to accidentally comment out code (that is, to tell the compiler part of your code is a comment) you need for the program.

When you are learning to program, it is also useful to comment out sections of code in order to see how the output results.

Using Variables

So far you should be able to write a simple program to display information typed in by you, the programmer, and to describe your program with comments. That’s great, but what about interacting with your user? Fortunately, it is also possible for your program to accept input.

But first, before you try to receive input, you must have a place to store that input. In programming, input and data are stored in variables. There are several different types of variables; when you tell the compiler you are declaring a variable, you must include the data type along with the name of the variable. Several basic types include char, int, and float. Each type can store different types of data. 

Before you can use a variable, you must tell the compiler about it by declaring it and telling the compiler about what its “type” is. To declare a variable you use the syntax <variable type> <name of variable>;. (The brackets here indicate that you replace the expression with text described within the brackets.) For instance, a basic variable declaration might look like this: 

int myVariable;

Note once again the use of a semicolon at the end of the line. Even though we’re not calling a function, we need a semicolon at the end of the “expression”. This code would create a variable called myVariable; now we are free to use myVariable later in the program.

It is permissible to declare multiple variables of the same type on the same line; separate each one with a comma. If you attempt to use an undefined variable, your program will not run, and you will receive an error message informing you that you have made a mistake.

Here are some variable declaration examples: 

int x;
int a, b, c, d;
char letter;
float the_float;

While you can have multiple variables of the same type, you cannot have multiple variables with the same name. Moreover, you cannot have variables and functions with the same name.

A final restriction on variables is that variable declarations must come before other types of statements in the given “code block” (a code block is just a segment of code surrounded by { and }). So in C you must declare all of your variables before you do anything else:

#include <stdio.h>
int main() 
{
    int x;
    printf( "Declare x first" );
 
    return 0;
}

The int getchar(void) function reads the next available character from the screen and returns it as an integer. This function reads only a single character at a time. You can use this method in the loop in case you want to read more than one character from the screen.

The int putchar(int c) function puts the passed character on the screen and returns the same character. This function puts only a single character at a time. You can use this method in the loop in case you want to display more than one character on the screen. Check the following example!

#include <stdio.h>
 
int main()
{
    int this_is_a_number;
 
    printf( "Please enter a number: " );
    scanf( "%d", &this_is_a_number );
    printf( "You entered %d", this_is_a_number );
    getchar();
    return 0;
}

When the above code is compiled and executed, it waits for you to input some text. When you enter a text and press enter, then the program proceeds and reads only a single character and displays it as follows.

$./a.out
Enter a value : this is test
You entered: t

Before ending up this cute introduction, I can highly recommend checking out 5 C Programming Tips You Must Learn to Get Started

Content Protection by DMCA.com