Skip to main content

Posts

Variables and Constants in C

Variables In programming, a variable is a container (storage area) to hold data.To indicate the storage area, each variable should be given a unique name (identifier). Variable names are just the symbolic representation of a memory location. For example: int playerScore = 95; Here, playerScore is a variable of integer type. The variable is assigned value: 95. The value of a variable can be changed, hence the name 'variable'. In C programming, you have to declare a variable before you can use it.   Rules for naming a variable in C   A variable name can have letters (both uppercase and lowercase letters), digits and underscore only. The first letter of a variable should be either a letter or an underscore. However, it is discouraged to start variable name with an underscore. It is because variable name that starts with an underscore can conflict with system name and may cause error. There is no rule on how long a variable can be. However, only the first 31 characters of a

Keywords and Identifiers in C

C Keywords Keywords are predefined, reserved words used in programming that have special meanings to the compiler. Keywords are part of the syntax and they cannot be used as an identifier.    For example: int money;  Here, int is a keyword that indicates 'money' is a variable of type integer. As C is a case sensitive language, all keywords must be written in lowercase. Here is a list of all keywords allowed in ANSI C.There are 32 keywords . Keywords In C Along with these keywords, C supports other numerous keywords depending upon the compiler. C Identifiers Identifier refers to name given to entities such as variables, functions, structures etc. Identifier must be unique. They are created to give unique name to a entity to identify it during the execution of the program. For example:   int money; double accountBalance;  Here, money and accountBalance are identifiers.  Also remember, identifier names must be different from keywords. You cannot use 'int' as an ide

Bubble Sort and Selection Sort in C

Sorting is nothing but arranging the data in ascending or descending order. The term sorting came into picture, as humans realized the importance of searching quickly. sorting allows everyone to arrange data in an order, hence making it easier to search. Bubble Sort Algorithm Bubble Sort is a simple algorithm which is used to sort a given set of n elements. Bubble Sort compares all the element one by one and sort them based on their values. Watch the video for the demo  https://www.youtube.com/watch?v=yIQuKSwPlro If the given array has to be sorted in ascending order, then bubble sort will start by comparing the first element of the array with the second element, if the first element is greater than the second element, it will swap both the elements, and then move on to compare the second and the third element, and so on. If we have total n elements, then we need to repeat this process for n-1 times. But it is noted that when the array is sorted , comparing adjacent element does

Linear and Binary Search in C

Searching is one of the most common problems that arise in computing. Searching is the algorithmic process of finding a particular item in a collection of items. A search typically answers either True or False as to whether the item is present or not. On occasion it may be modified to return where the item is found. Search operations are usually carried out on a key field. Consider searching for a given value k in an array A of size n. There are 2 basic approaches: sequential search and binary search. Linear (Sequential) Search When data items are stored in a collection such as a list or array , we say that they have a linear or sequential relationship. Each data item is stored in a position relative to the others. In C array, these relative positions are the index values of the individual items. Since these index values are ordered, it is possible for us to visit them in sequence. This process gives rise to our first searching technique, the sequential search or linear sear

Command-line Arguments in C

The parameters passed to the program when the program is invoked are known as command line arguments. These parameters are the additional information like filename or other kind of input to the program. By passing command line arguments there is no need for the user to provide the input while executing the program. These command line arguments can be processed by using the arguments available in the main function. The main allows two parameters namely: argc and argv. The argc represents the argument counter which contains the number of arguments passed in the command line. The argv is a character pointer array which points to the arguments passed in the command line. To know the number of command line arguments, we can use the argc parameter of the main function and to access the individual arguments, we can use the argv array. The first element in the argv array is always the program name. So the first argument can be accessed by using argv[1] and so on. The prototype of main f