Consider this simple program in Python:
n=int(input("enter your lower limit"))
m=int(input("enter your higher limit"))
list=[]
x=n
while x<=m:
if (x%2==0):
list.append(x)
x+=1
print("the even numbers in your range are:",end="")
print(len(list))
Here I can initially declare the elements of list to nothing and keep inserting a result in it, the range of which completely depends on user. And then I can check the length of that list therefore how many elements fulfilled my conditions between the user's range. So it gets easy in Python!
But in C, I must initially declare the number of elements of an array! I can declare it to a random large number and then keep inserting a result. And then find out the ultimate result (how many elements fulfilled my conditions between the user's range) by checking how many elements are there before the character \0
. But it still wastes a lot of memory and keeps garbage in the unused elements which may cause problem in bigger programs!
Well! I know a little about malloc()
. But I have to declare the size here too! Though I can free the memory later which reduces pressure in bigger programs but I really want to know if there exists any straightforward process in C like Python?