Posts

Showing posts with the label tutorial

How to set path in PYTHON

If you’ve installed Python in Windows using the default installation options, the path to the Python executable wasn’t added to the Windows  Path variable . The Path variable lists the directories that will be searched for executables when you type a command in the command prompt. By adding the path to the Python executable, you will be able to access  python.exe  by typing the  python keyword  (you won’t need to specify the full path to the program). C:\>python 'python' is not recognized as an internal or external command, operable program or batch file As you can see from the output above, the command was not found. To run python.exe, you need to specify the full path to the executable: C:\>C:\Python34\python --version Python 3.4.3. To add the path to the python.exe file to the Path variable, start the Run box and enter  sysdm.cpl This should open up the System Properties window. Go to the Advanced tab and click the Environment Var...

C startup part 28 command line arguments in C

It is possible to pass some values from the command line to your C programs when they are executed. These values are called  command line arguments  and many times they are important for your program especially when you want to control your program from outside instead of hard coding those values inside the code. The command line arguments are handled using main() function arguments where  argc  refers to the number of arguments passed, and  argv[]  is a pointer array which points to each argument passed to the program. Following is a simple example which checks if there is any argument supplied from the command line and take action accordingly − #include <stdio.h> int main ( int argc , char * argv [] ) { if ( argc == 2 ) { printf ( "The argument supplied is %s\n" , argv [ 1 ]); } else if ( argc > 2 ) { printf ( "Too many arguments supplied.\n" ); } else { printf ( "One argument e...

c startup part 27 memory management in C

This chapter explains dynamic memory management in C. The C programming language provides several functions for memory allocation and management. These functions can be found in the  <stdlib.h>  header file. Sr.No. Function & Description 1 void *calloc(int num, int size); This function allocates an array of  num  elements each of which size in bytes will be  size . 2 void free(void *address); This function releases a block of memory block specified by address. 3 void *malloc(int num); This function allocates an array of  num  bytes and leave them uninitialized. 4 void *realloc(void *address, int newsize); This function re-allocates memory extending it upto  newsize . Allocating Memory Dynamically While programming, if you are aware of the size of an array, then it is easy and you can define it as an array. For example, to store a name of any person, it can go up to a maximum of 100 characters, so you can defi...

C satrtup part 26 variable arguments

Sometimes, you may come across a situation, when you want to have a function, which can take variable number of arguments, i.e., parameters, instead of predefined number of parameters. The C programming language provides a solution for this situation and you are allowed to define a function which can accept variable number of parameters based on your requirement. The following example shows the definition of such a function. int func ( int , ... ) { . . . } int main () { func ( 1 , 2 , 3 ); func ( 1 , 2 , 3 , 4 ); } It should be noted that the function  func()  has its last argument as ellipses, i.e. three dotes ( ... ) and the one just before the ellipses is always an  int  which will represent the total number variable arguments passed. To use such functionality, you need to make use of  stdarg.h  header file which provides the functions and macros to implement the functionality of variable arguments and follow the ...

C startup part 25 recursion

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. void recursion () { recursion (); /* function calls itself */ } int main () { recursion (); } The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop. Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc. Number Factorial The following example calculates the factorial of a given number using a recursive function − Live Demo #include <stdio.h> unsigned long long int factorial ( unsigned int i ) { if ( i <= 1 ) { return 1 ; } return i * ...

C startup part 24 error handling

As such, C programming does not provide direct support for error handling but being a system programming language, it provides you access at lower level in the form of return values. Most of the C or even Unix function calls return -1 or NULL in case of any error and set an error code  errno . It is set as a global variable and indicates an error occurred during any function call. You can find various error codes defined in <error.h> header file. So a C programmer can check the returned values and can take appropriate action depending on the return value. It is a good practice, to set errno to 0 at the time of initializing a program. A value of 0 indicates that there is no error in the program. errno, perror(). and strerror() The C programming language provides  perror()  and  strerror()  functions which can be used to display the text message associated with  errno . The  perror()  function displays the string you pass to it, fo...