fuddlepixelFuddlepixel coz tech is fun();

Beginning C programming

Let us get started with programming in C, starting from very basic, let us print some sentence on the screen, from the classic Hello World !

By Shubham Ramdeo
04 July 2015 · 5 mins read
# c  newbie 

Beginning C programming

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

The 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.

  • Recipe Name
  • Ingredients
  • Steps of making
  • Tips

Similarly, In computer programming,

  • Program Name
  • Data we will use
  • Instructions
  • Flow control

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

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.

  • The semicolon (;) is added to show that this line of instruction has been completed.

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 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 :)


Share this :



Update me weekly ✉

Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.

I'm in ♥


Or follow us
   


About author

Shubham Ramdeo

Shubham Ramdeo

Shubham Ramdeo is the founder of Fuddlepixel. He is a geek, a writer, involved with many technical and humanity projects.

Meet me!



Recent





Related

c 

newbie 

Top