logo codesdope

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 that can print anything on the screen. Sounds good! Now, let's understand this code in detail so that we can print any message.

Let's start from printf("Hello World\n");

printf is a function which displays whatever is written inside the brackets ( ) following it on the screen.

Now, what is a function?

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 input from the user and gives back 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.

But why is \n there?

\n is newline character which is used for changing line.

Try this

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

As we have seen, '\n' printed a new line between 'Hello' and 'World'.

; - It 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.

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

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 main() function. It means that it represents what things are inside the 'main' or the body of the 'main'.

use of {} in c body

return 0 - As mentioned earlier, the function main returns an integer value (int main()), therefore here we are returning 0. return is a keyword which is used to return some value from a function. It 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 these all 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 definition 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.

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.

Comments are written between "/*" and "*/" 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 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
#include <stdio.h>
int main()
{
    printf("Hello World\n");
    return 0;
}
Output
Hello World

Comments can be multiline also as used in the example 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*/
#include <stdio.h>
int main()
{
    printf("Hello World\n");
    return 0;
}
Output
Hello World

I hope you are getting things. So, now go one step ahead.

Let's take 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.

&a; - address of the integer variable a. It means at the address of 'a'. As 'a' is in our program, it occupies some space in memory and also has an address ( You will see later ) .

scanf - It is a function that takes data from the user and assigns it to a variable.

Just like printf is used for displaying output, scanf is used to take input from the user.

scanf is like - take input from the keyboard, and if successful, the input value is stored at the address of 'a'. So in short, it means - take an integer input ( as '%d' is used for integer ) and store it at the address of 'a'.

Now, 'a' will be like

21
a

In 'printf' everything got printed as it is except '%d'. As stated, '%d' is used for integer. Now it will look for value for '%d', which we are giving by 'a' after a comma(,) ( 'a' is declared already ). So, in short, it means, print an integer and take its value from 'a'.

# 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'. Their values we are taking 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 '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.
So, overall it is that first we 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 input and make something appear on the screen. You will go through more in the next chapter but do solve questions based on these before going further to have a good control over this.

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.

The best way to predict the future is to create it.
-Peter Druker


Download Our App.
BlogsDope App
Get it on Google Play
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.