×
 Introduction
Introduction to Newbie Programmer Series Introduction to Computer The Computer Programming Language Setting up the Computer for Programming
 Lets Get Started !
Beginning C Programming Variables a Handbag ! Basic Variable types in C Basic Formatted Output Basic Formatted Input
 Loops
Looping Repeating Concept Different Kinds of Loops Advanced concepts and trick with Loops
 Operators
Operators, An Introduction Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Miscellaneous Operators and Operators Precedence
 Controlling the Flow
Flow Charts Conditional Statements Using Goto and Label Break and Continue the loops Switch the Cases Some Practical Applications of Control Flow
 The program structure
The program Structure Functions More Concepts of Functions Scope of the Variables across functions How the C language itself Works ? Scope of the Variables across files Static Variables Auto and Register Variables #define PreProcessor Some more PreProcessors
 Arrays and Structures
Introduction to Arrays Arrays, some more Concepts Arrays, Cool Examples Structures Introduction Structure, Some more Concepts Structure, Some Examples
 File Management
Basics of File management in C Steps Involved with File Handling in C
 Miscellaneous
Inside Logic Gates – The Electronic Logic Binary Number System Mathematical Reasoning
Newbie Programmer
full book coming soon
by Shubham Ramdeo

We have set our environments, But what to write ? Now let's begin real programming !


 

beginning programmingThe real programming stuffs starts here! In the Last post, We have set up our computer for programming and we did made a small program called "Hello World" in C but I didn't explain that. In this part, we will learn structure of a program and create some more. If you are new to this series, please checkout the previous parts from (here) so that you can easily understand what is going on.


Computer programming, in the words of the great Richard Stalleman, is like a Recipe.

Similarly, In computer programming,

Now we come back to our hello world program and learn how it works.

It was like :

#include <stdio.h>
main()
{
     printf("Hello, World !");
     getchar();
}

Now we will break it and discuss in detail line by line.

#include <stdio.h>

With this line of code, we are telling the C program that we are going to use the computer's Input and Output Devices. Like here, we will be using Keyboard and Display. To use them, we have to give some certain commands. These commands are saved in a list named "stdio.h" meaning Standard Input Output. This is a kind of library which contain commands of specific purpose. Based on our use and purpose, we select certain library.

You can ignore it for now and just write it the way it is!

main()

With this, we tell the C program that this is the "main part" of our program. This main part runs first than any other thing in our program.

{

This symbol here shows that "main part starts here". Inside this, we will give instructions for the "main" part of our software.

printf ("Hello, World !");

We are using here a command called "printf", which is saved in the "stdio.h" library. With this command, we are telling the computer to : Print "Hello, World !" on the screen.

getchar();

We are giving here a another instruction, this means get a character. Our program will wait till we press Enter.

}

With this we are ending the instructions given to the "main" part.

How the program will work ?

First thing, to run this and other codes below, please read the previous part (here).

Let's read the code once again,

#include <stdio.h>
main()
{
    printf("Hello, World !");
    getchar();
}

First of all the program will search for the main(). When it has been found, It will read all the instruction line by line given between { and }.

Here it reads first line printf("Hello,World!); but computer don't know what is printf ? So it searches for library for reference. We have already written #include<stdio.h> So the computer reads this library and learns what is printf. After learning, it applies to our program to print "Hello, World !" on the Screen.

Then the computer reads the second line. Again it uses stdio.h library for reference and learns to wait till we enter any key from the keyboard.

Then in the next instruction, Computer finds } and knows that the main part of the program has been end.

Now Computer have also learned our program, that is we want to use the computer to display a message "Hello, World !" on the screen. So it makes an executable file of our program in which our program is translated into the computer's own language. From now, we will run this executable file only. And so the Hello World has been Completed. The output will be :

Hello, World !

More Examples

We can make similar programs like this.

The following program Prints my name.

#include<stdio.h>
main()
{
    printf("Hello Shubhu <3");
    getchar();
}

make your own and comment below.

Now let us learn one more quick think. Check this out :

#include<stdio.h>
main()
{
    printf("Hello Shubhu");
    printf("Whats up ?");
    getchar();
}

The program runs smoothly but there is something wrong in the output we get.

Hello ShubhuWhats up ?

I want that these both lines comes one by one as different lines. So we will tell the computer for this. We will use /n to tell the computer to print the next line in a new line. Whenever we type, After writing a line, we press ENTER and the new line begin. This is very similar to /n

So the correct code is:

#include<stdio.h>
main()
{
    printf("Hello Shubhu \n");
    printf("Whats up ? \n");
    getchar();
}

 And the output is also correct

Hello Shubhu
Whats up ?

Lets do one more.

Check this out.

#include<stdio.h>
main()
{
    printf("Hello ! ,press enter to continue... \n");
    getchar();
    printf("So you like programming !, press enter to contine... \n");
    getchar();
    printf("Awesome \n");
    getchar();
}

OUTPUT:

(//the program starts)
Hello ! ,press enter to continue...
(//I pressed enter)
So you like programming !, press enter to continue...
(//I pressed enter)
Awesome
(//I pressed enter and the program ends.)

Use the previous parts to learn how to run these codes. Also send me your examples by commenting below. Its simple Do this :

#include <stdio.h>
main()
{
    printf(" --whatever you want to say-- \n ");
    getchar(); //if you want Enter to be pressed other wise no need
}

This parts ends here. I hope everything works correctly. If you have any doubt, just comment below. We will learn about variables and will apply maths in the next part. Stay Connected :)

 

Please share this as much as you can so that we can get connected with more and more people who want to learn programming.

© Shubham Ramdeo

Site Map
with
© Shubham Ramdeo