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>
- 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;
}
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;
}
Output:
Hello World!
Print
Within the print function, you can generally print strings which remains within "".
For example
printf("Hello World!");
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;
example: int var;
or
<datatype> vairable name= value;
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);
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;
}
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);
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
*/
or,
//text
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){
/**/
}
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;
}
Output:
What is the first value?10
What is the second value?20
The summation of 10 & 20 is: 30
_In Python: _
We don't need to worry that much .
Print
We can print using print()
For example:
print("Any String here")
or,
F string:
print(f"{variable name} Normal String")
For exmaple:
var_0=14
print(f"The value of the variable is: {var_0}")
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)
Output:
12
34
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))
<class 'int'>
<class 'int'>
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)
Output: 46
Let's change the var_1 value with a string
var_1="Hello"
var_2=34
print(var_1+var_2)
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);
}
}
Output:
Hello34
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.
var_1="Hello"
var_2=str(34)
print(var_1+var_2)
Output:
Hello34
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)
Output:
What is your first value?13
13
Is this 13 an integer?
Let's check it:
var_1=input("What is your first value?")
print(type(var_1))
Output:
What is your first value?13
<class 'str'>
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))
Output:
What is your first value?12
<class 'int'>
Method/Function
here, you can define a function in this format:
def <method name>(parameter names):
#Function body
return <something>
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)
Output:
What is your first value?14
What is your second value?56
The summation of 14 & 56 is 70
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))
Output:
What is your first value?14
What is your second value?56
The summation of 14 & 56 is 70
Top comments (0)