DEV Community

0 to Advance of Java, Python & C, C++ : Part 3

In C we have :

  • Header file to add: Firstly, you need to add a header file with a #include preprocessor . This header file includes all of the library needed for you. This basically ends with a .h or .H extension. generally, we use STDIO.H or stdio.h which means standard input & output. for example:
#include <stdfio.h>
Enter fullscreen mode Exit fullscreen mode
  • Main method: You need to declare a main method . Generally you set void as parameter for your main method for example,
<return type> main(void){
         <method body>
         return 0;
}
Enter fullscreen mode Exit fullscreen mode

Basically these 2 needs to be there to run a basic code. Although you need to know about printf() function, variables etc. to print your first code.
Here is a code:

#include <stdio.h> //header file
//Main method
int main(void){

    //Print function
    printf("Hello World!");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output:
Hello World!

Print
Within the print function, you can generally print strings which remains within "".
For example

printf("Hello World!");
Enter fullscreen mode Exit fullscreen mode

This will show Hello World! as output.
But what about printing any number?

For that, we need to declare an integer variable. we generally declare it by this:

<datatype> vairable name;
Enter fullscreen mode Exit fullscreen mode

example: int var;
or

<datatype> vairable name= value;

Enter fullscreen mode Exit fullscreen mode

example: int var=5;

Now, you can put this in your code and print it using this:

    int var=5;
    printf("The value of var is: %d",var);
Enter fullscreen mode Exit fullscreen mode

So, what is this %d etc? %d is a format specifier. As var variable is an integer and %d represents integer . so, in the string "The value of var is: %d" ; in the position of %d , we will have a value of var. Why?
because we told it using a comma and the variable name

%d",var
So, the whole code will be:

#include <stdio.h> //header file
//Main method
int main(void){

    int var=5;
    printf("The value of var is: %d",var);
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Output: The value of var is: 5

Input
To take an input, you need to use scanf().Also, you need to use the format specifier and "&".
for example,

float var,
scanf("%f",&var);
Enter fullscreen mode Exit fullscreen mode

Here we have taken a float input and stored it to "var" variable.
So, what is this "&" is for? Basically it means pointer. So, you take the input and save it to a location of a memory . "&var" indicates the pointer.

Comment
To use a comment, we need to use

/*
text
*/
Enter fullscreen mode Exit fullscreen mode

or,

//text
Enter fullscreen mode Exit fullscreen mode

Function
You need to have at least 1 main function to execute anything. Also, you need to add

  • Header file

  • Function prototype

  • Method return type, method name, parameter list etc.


/*Include header file*/
/*Include function prototypes*/
return-type function-name(parameter list){
    /**/
}
Enter fullscreen mode Exit fullscreen mode

so, for example:

#include <stdio.h> /*Include header file*/

/*Include function prototypes*/
int result,a,b;


//Addition function
int Addition(int a, int b){
    result=a+b;
    printf("The summation of %d & %d is: %d",a,b,result);

}


//Main method
int main(void) {
    printf("What is the first value?");
    scanf("%d",&a);
    printf("What is the second value?");
    scanf("%d",&b);
    Addition(a,b);//Calling Addition function
    return 0;

}
Enter fullscreen mode Exit fullscreen mode

Output:

What is the first value?10
What is the second value?20
The summation of 10 & 20 is: 30
Enter fullscreen mode Exit fullscreen mode

_In Python: _

We don't need to worry that much .
Print

We can print using print()
For example:

print("Any String here")
Enter fullscreen mode Exit fullscreen mode

or,
F string:

print(f"{variable name} Normal String")
Enter fullscreen mode Exit fullscreen mode

For exmaple:

var_0=14
print(f"The value of the variable is: {var_0}")
Enter fullscreen mode Exit fullscreen mode

We can keep any string here and print them.

Let's print some integers using variables. This is going to be interesting.

var_1=12
var_2=34
print(var_1)
print(var_2)
Enter fullscreen mode Exit fullscreen mode

Output:

12
34
Enter fullscreen mode Exit fullscreen mode

Now, the question is that, is 12 & 34 integers?
Let's check the type:

var_1=12
var_2=34
print(type(var_1))
print(type(var_2))
Enter fullscreen mode Exit fullscreen mode
<class 'int'>
<class 'int'>
Enter fullscreen mode Exit fullscreen mode

So, the compiler guess it right .
Did you notice that , you did not even tell them that 12, 34 is integer? Python by itself predicted that.
Now, lets add them

var_1=12
var_2=34
print(var_1+var_2)
Enter fullscreen mode Exit fullscreen mode

Output: 46
Let's change the var_1 value with a string

var_1="Hello"
var_2=34
print(var_1+var_2)

Enter fullscreen mode Exit fullscreen mode

Now, this is not going to work. Python is not going to assume var_2 as string and add them together.

But in Java, if you add String & integer, java would add them together making them string

public class test{
    //main method
    public static void main(String[] args) {
        int var_2=34;
        System.out.println("Hello"+var_2);


    }
}

Enter fullscreen mode Exit fullscreen mode

Output:

Hello34

Enter fullscreen mode Exit fullscreen mode

So, this is a thing you need to know. To make them work in Python, you need to convert the var_2 value to a string.


Enter fullscreen mode Exit fullscreen mode
var_1="Hello"
var_2=str(34)
print(var_1+var_2)

Enter fullscreen mode Exit fullscreen mode

Output:

Hello34
Enter fullscreen mode Exit fullscreen mode

Now, it works fine . Fine right!
Notice that , you can add or divide or do anything within the print() . Generally, you can not do these in C. You need to use format specifier like %d etc. to express variables . But can not add within them.

Input

To take an input, you need to use input() . Also, while taking the input value, you can print some string too

For example:

var_1=input("What is your first value?")
print(var_1)
Enter fullscreen mode Exit fullscreen mode

Output:

What is your first value?13
13
Enter fullscreen mode Exit fullscreen mode

Is this 13 an integer?
Let's check it:

var_1=input("What is your first value?")
print(type(var_1))
Enter fullscreen mode Exit fullscreen mode

Output:

What is your first value?13
<class 'str'>
Enter fullscreen mode Exit fullscreen mode

Why is this? Because we took input using input("") .So, we need to convert it to integer and then work with it.

var_1=int(input("What is your first value?"))
print(type(var_1))
Enter fullscreen mode Exit fullscreen mode

Output:

What is your first value?12
<class 'int'>
Enter fullscreen mode Exit fullscreen mode

Method/Function

here, you can define a function in this format:

def <method name>(parameter names):
    #Function body
    return <something>

Enter fullscreen mode Exit fullscreen mode

Let's check an example:

var_1 = int(input("What is your first value?"))
var_2 = int(input("What is your second value?"))


# function method_1
def method_1(a, b):
    print(f"The summation of {a} & {b} is {a + b}")


# Calling method_1
method_1(var_1, var_2)
Enter fullscreen mode Exit fullscreen mode

Output:

What is your first value?14
What is your second value?56
The summation of 14 & 56 is 70
Enter fullscreen mode Exit fullscreen mode

We, can also use return here to solve this problem

var_1 = int(input("What is your first value?"))
var_2 = int(input("What is your second value?"))


# function method_1
def method_1(a, b):
    return (f"The summation of {a} & {b} is {a + b}")


# Calling method_1
print(method_1(var_1, var_2))
Enter fullscreen mode Exit fullscreen mode

Output:

What is your first value?14
What is your second value?56
The summation of 14 & 56 is 70
Enter fullscreen mode Exit fullscreen mode

Discussion (0)