Close
Close

Basics of C


Now let's start from where we left off, 'The Hello World' code.

#include <stdio.h>
int main()
{
    printf("Hello World\n");
    return 0;
}
Output
Hello World

On compiling the above code, "Hello World" gets printed on the screen.

So, we have just written a code which can print anything on the screen. Sounds good! Now, let's understand this code in detail so that we can print any other message.

Let's start from printf("Hello World\n");printf is a function which displays whatever is written inside the brackets ( ) on the screen.

We have used a word function, so let's discuss what it is.

A function is like a machine which takes something from us (not necessarily) and then performs an operation on it. In simple words, a function takes an input from the user and gives back an output.

functions in c

In the above example, printf is taking "Hello World\n" as input and in return, displaying "Hello World" on the screen.

So, we gave "Hello World\n" to the printf function and it printed (displayed) "Hello World" on the screen without '\n'. So, let's discuss what it is.

\n is a special character called newline character which is used for changing lines or printing new lines. So, '\n' after the 'Hello World' changed the line or printed a new line. Let's run the following code for an example.

#include <stdio.h>
int main()
{
    printf("Hello\n World\n");
    return 0;
}
Output
Hello
 World

As you can see, '\n' printed a new line between 'Hello' and 'World'. So, '\n' is used to print a new line in our program.

Try writing the same code without '\n' at the last, you will see that the cursor won't change line and will start printing everything on the same line.

So we are clear with the printf("Hello World\n") part, let's proceed further and discuss more about the code we have written.

; → Semicolon tells the compiler about the end of a statement. We need to put ';' at the end of every statement like function calls, variable declarations, etc.

So, by putting ';' at the end of printf("Hello World\n"), we have ended that statement there. Anything written after that will be a new statement. For example, printf("Hello World\n"); return0; is totally valid and C will treat both these as different statements even they are on a same line.

#include <stdio.h>
int main()
{
    printf("Hello World\n"); return 0;
}
Output
Hello World

We have understood everything about the printf("Hello World\n"); statement, let's learn about int main().

int main()main is also a function. It is compiled first when a C code is executed that is, the execution of any C program starts from the main() function. int before main means that the function will return (or give us back) an integer.

Since the execution of every C program starts from this main function, all C programs must have this main() function.

{ } → The curly braces ({ }) just after the main() function encloses the body of the main() function. It means that it represents what things are inside the 'main' or the body of the 'main'.

use of {} in c body

Till now, we know that to print 'Hello World' on the screen, we used printf and to execute this statement, we put this statement inside the main function (inside { } after main) because C will execute the main function and thus, the statements inside it will be executed. Let's move ahead and understand the rest of the code.

return 0 → As mentioned earlier, the main function returns an integer value (int main()), therefore we are returning 0 here. return is a keyword which is used to return some value from a function. Returning 0 indicates that our program has been run successfully and we terminate our main function with this return statement.

You will go through a whole chapter on functions, so just keep this structure in your mind to write programs and you will understand all these in a much better way in the Function chapter.


#include <stdio.h>
stdio.h stands for standard input output. It is a library in C which contains definitions of functions such as 'printf'. Thus, it will tell the compiler how 'printf' should work. #include is a pre-processor which is used to link the program with stdio.h (pre-processors will be taught in later chapters).

functions in c

So, the conclusion is - '#include' will make 'stdio.h' library available for use and then our computer will know what is 'printf'. 'stdio.h' contains the definitions of functions like 'printf'. The program will start its execution from the 'main' function and the execution of 'main' will make 'printf' to give messages on the screen. 'return 0' will return 0 and indicate that our program has been run successfully.

execution of hello world in C

So, the format to write codes in C is like:

#include <stdio.h>
int main()
{
    STATEMENT;
    STATEMENT;
    STATEMENT;
    ...
    return 0;
}

Let's try out some examples of printf.

#include <stdio.h>
int main()
{
    printf("  *  \n *** \n*****\n");
    return 0;
}
Output
  *  
 *** 
*****

Commenting


Comments are statements written inside code which are just ignored by the compiler while compiling our code. Comments are written to make our code more readable for humans.

Comments are written between "/* ... */" or after //.

Why Comments


As mentioned earlier, it makes our code more readable. Assume that you have written a software and after releasing it, you hired few good programmers for its maintenance. Without comments, it would be a very difficult job for them to understand your code. And most of the time it happens that the person who has written a code is not the one who is going to modify it. So, make a habit of writing good comments.

Comments are written after //. These are single line comments. It means that if you change the line then the new line will not be the part of the comment.

//This is single line comment
//This is also a comment
//and will ignored by the complier
#include <stdio.h>
int main()
{
    printf("Hello World\n");
    return 0;
}
Output
Hello World

Comments can be multiline also as used in the example given below. But we can't put one comment inside another eg.- /* This is a /*comment*/ */ is invalid.

/*Hello World code*/

/* Comments will not be compiled and will be ignored */

/* It is a
multiline
comment*/

//Single line comment
#include <stdio.h>
int main()
{
    //printf function
    printf("Hello World\n");
    return 0;
}
Output
Hello World

I hope you are getting things because we are going to move one step further.

Taking Input from User


# include <stdio.h>
int main()
{
    int a;
    printf("Enter an integer" );
    scanf("%d" , &a);
    printf("Entered value is : %d\n" , a );
    return 0;
}
Output
Enter a integer21
Entered value is : 21

Let's try to understand the above code.

int a; → It is a statement which declares that the variable 'a' is an integer. And by this declaration, 'a' enters into the world of our program. Since 'a' is in our program, our computer will give some space in its memory for 'a'.

allocation to space to a variable in C

The next statement is printf which we already know that it will print "Enter an integer" on the screen.

After this, we have scanf. It is another function like printf. As printf is used to display something on the screen, scanf is used to take values from the keyboard. Its definition is also stored in the 'stdio.h' library.

Inside the scanf, we have %d and &a. %d is used for integers in C and here, it is telling that the scanf will take an integer value from the keyboard.

&a → It means at the address of 'a'. As 'a' is in our program, it must be present somewhere in the memory of our computer and thus, it must occupy some space in the memory and this memory also has an address (you will learn about it later) and &a is pointing to that address.

address of variable in C

As stated above, scanf function takes data from the user and assigns it to a variable and %d written inside the scanf is for taking the integer inputs, scanf("%d", &a) will take the integer input from the keyboard, and if successful, store the value of the input at the address of 'a'. In short, it means that the value entered by the keyboard will be stored in the variable 'a'.

Now, 'a' will be like

21
a

or 'a' will store a value of 21 because 21 has been entered by the keyboard.

In printf("Entered value is : %d\n" , a ) everything got printed as it is except %d. As stated, '%d' is used for integers. Thus, the complier will look for value for '%d', which we are giving by 'a' after a comma(,). So, the value of the variable 'a' will be printed in place of '%d' in the printf.

# include <stdio.h>
int main()
{
    int x;
    int y;
    printf("Enter two integers\n" ) ;
    scanf("%d %d" ,&x,&y);
    int z = x+y;
    printf("Entered value is : %d and %d and their sum is %d\n" ,x,y,z );
    return 0;
}
Output
Enter two integers
21 2
Entered value is : 21 and 2 and their sum is 23

We have declared two integers 'x' and 'y'. We are taking their values from the user by 'scanf'.

scanf("%d %d" ,&x,&y); → There are two '%d' inside 'scanf' to take two integers and store them at the address of 'x' and 'y' respectively.

int z = x+y; → We have declared a new integer variable 'z' and it is equal to the sum of the values of 'x' and 'y'. i.e. x+y.

printf("Entered value is : %d and %d and their sum is %d\n" ,x,y,z );Three '%d' are used and their values will be printed from 'x', 'y' and 'z' respectively.

Overall, we first declared two integers and took their values from the user. Then we declared one more variable 'z' and made it equal to 'x+y' and at last, we printed everything that we wished.

The overall conclusion is that we now know how to take input and make something appear on the screen. You will learn more in the next chapter but do solve questions based on these before going further to have a good control over this chapter.

In future, when writing long codes, make sure that you run your code from time to time while writing instead of completing your whole code and running at last. This will make debuging your code easier if you have made some errors.

To learn from simple videos, you can always look at our C++ video course on CodesDope Pro. It has over 750 practice questions and over 200 solved examples.
The best way to predict the future is to create it.
- Peter Druker


Ask Yours
Post Yours
Doubt? Ask question