Posts

C - Pointers

Image
A pointer is a special type of variable which is used to store an address of an existing variable. Now let's see how to use a pointer. In the following example we have used a pointer to indirectly access a variable. A pointer can be called in two different ways. 1. Call by value 2. Call by reference Call by value In call by value, the copy of values of the variables are passed to the functions. In the following example copies of variables a and b are assigned to the variables x and y in the function swap. The values does not change here.Because they are just variables not pointers.We can't swap the values of variables as above. Call by Reference In call by reference, the memory addresses are assigned to the pointers.Therefore it will permanently change the value after a function call. In here values are changed because there are no variables but pointers.We can change the values of pointers simply as above. In here I have given you a brie...

C - Functions

Image
C language is a function oriented programming language. When you develop a program ,you can create your own function or you can use existing functions. The main advantage of using a function is re usability. How to use existing functions main() is also a inbuilt function. There are some math library functions are included in C. Let's see how can we use such a function from a example. Output In the above code i have called the math.h library function inorder to call a inbuild math library function. It is the only alternation i have done to this code rather than the examples i have done before. Also by giving as '%.2f ' I have given a limit to display the float value. If i haven't given a limit,the float value will display fully as you can see below. And we should call the function to find the output of the input variable 'v '. Function calling is done at the last printf statement as sqrt(v). The syntax of declaring a function. ...

C - Arrays

Image
An array is a data structure which can be used to store set of values with similar data types. In C , arrays can be devided into two types. Single Dimensional Arrays Multi Dimensional Arrays Single Dimensional Arrays The declaration form of single dimensional array is, data_type array_name[size]; Ex: int number[5]; float number[5]; In a array index is starting from 0. Therefore the last index is always equals to (size - 1) In the above examples the size is defined to 5 therefore the last index is 4. We can assign values to relative indexes as below. number[0]=10; number[1]=4; number[2]=5; number[3]=47; number[4]=43; Also we can ask users to input numbers for the array and we can display the entered numbers. Let's see how that can happen from a example. Output of the above code.   Multi Dimensional Arrays In multi dimensional arrays, we can find different number of rows and columns. In below I have  explained how to declare a 3 ...

C- Control Structures

A control structure defines the execution order of the given program statements. There are three types of control structures. Sequence (Linear) Selection Repetition (Iteration) Selection Structure In C language we can use the If and Switch statements to represent the selection structure. If statement also come in two ways. 1. if(condition){      statement;    }    else{     statement;    } 2. if(condition){      statement;    }    else if{     statement;    }    else{     statement;    } This second way is called as nested if statement. and also there can be only a if condition in a program. ex:  1. #include <stdio.h> #include <stdlib.h> int main() {     int no1,no2;     printf("enter two numbers");     scanf("%d %d",&no1,...

C- Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical functions. C language is rich in built-in operators and provides the following types of operators −       Arithmetic Operators       Relational Operators       Logical Operators       Bitwise Operators       Assignment Operators       Misc Operators Arithmetic Operators + Adds two operands. − Subtracts second operand from the first. * Multiplies both operands. / Divides numerator by de-numerator. % Modulus Operator and remainder of after an integer division. ++ Increment operator increases the integer value by one. -- Decrement operator decreases the integer value by one. Rational Operators == Checks if the values of two operands are equal or not. If yes, then the condition becomes true. != Checks if the values of two operands are equal or n...

C ASCII Code

Image
#include <stdio.h> int main() { char chr; printf("Enter a character: "); scanf("%c",&chr);     // When %c text format is used, character is displayed in case of character types printf("You entered %c.\n",chr);  // When %d text format is used, integer is displayed in case of character types printf("ASCII value of %c is %d.", chr, chr);  return 0; } By adding // we can denote a comment in C. In the above example i have described the code inside of it by using comments. The output of the above code

C - Input/Output

Image
#include <stdio.h> int main() {     int testInteger;     printf("Enter an integer: ");     scanf("%d",&testInteger);      printf("Number = %d",testInteger);     return 0; } Above program shows how we can input value to a program and how to print that value entered. %d is used to indicate integer input or output. Same as that we use, %f - float %c or %s - char %f - double