Skip to main content

Posts

Showing posts with the label C_Programming

C Programming #10 (Pointers)

Call by value To understand this you have to be familiar with functions. Whenever we call a function we passed something to it in general we pass values of the variable to call the function Eg:- sum=calsum(a,b,c); f=factor(a); Call by reference  To get this topic one concept you have to keep in mind that if we do anything in computer it get stored somewhere in the memory so instead of passing value of the variable if we are able to pass location of number ie:address of number then it is call by reference. If you are still not able to understand it then dont worry it's too tough for beginners but in time you will get the things Pointer Notation Consider:- i=3 Here i is the location name 3 is the value of the location 65524 is the location number To know the address ie location number you can use the program:- #include<stdio.h> int main() { int i=3; printf("Address of i=%u\n",&i); printf("Value of i=...

C Programming #9 (Functions)

Function is a self contained block of statement that perform a coherent task of some kind. In simple word to write a C Program in a professional manner and in a clean way it is must to use  functions. Function is used in the C program where you want to do a specific kind of work always. Example: #include<stdio.h> void message(); int main() { message(); printf("This is 1\n"); return 0; } void message() { printf("This is 2\n"); } Output: This is 2 This is 1 Now you must be wondering that This is 1 Should come first but I have made use of function and designed how it should appear. In this program I have used Message() three times void message() This is for function prototype declaration It is necessary to mention prototype of every function that we intend to define in a program message() This is the function call means it call it's own function and main() is temporarily suspended till the control is in function message af...

C Programming #8 (Case Control Instruction)

In Programming we come across some situations where we have to choose from number of alternatives and choice should be specific to condition so the control statement that allow us to make a decision from number of choice is called as switch or we can say switch-case as they are used together. The switch statement looks like : switch (integer expression) { case constant 1: do this; case constant 2: do this; case constant 3: do this; default: do this; } In these case do this is the command statement that is to be followed when case is satisfied Example: #include<stdio.h> int main() { int i=2; switch(i) { case 1: printf("I am in case 1\n"); case 2: printf("I am in case 2\n"); case 3: printf("I am in case 3\n"); default: printf("I am in default"); } return 0; } Output of the program will be : I am in case 2 I am in case 3 I am in default But this is not the output we needed this program should give us...

C Programming #7 (More Complex Repetition)

The for loop: Most of the time only few programmers use while loop as they are too busy using the for loop. The for allows us to specify three thing about a loop in a single line: Setting a loop counter to an initial value. Testing the loop counter to determine whether it's value has reached the number of repetition desired. Increasing the value of loop counter each time the body of the loop has been executed. General for of for statement: for(initialize counter;test counter;increment counter) { do this; and this; and this; } Example program: /*Calculation of simple interest for 3 sets of p, n and r*/ #include<stdio.h> int main() { int p,n,count; float r,si; for(count=1;count<=3;count=count+1) { printf("Enter values of p,n and r"); scanf("%d%d%f,&p,&n,&r"); si=p*n*r/100; printf("Simple Interest=Rs.%f\n",si"); } return 0; } Explanation: When the for statement is exe...

C Programming #6 (Loop Control Instruction)

In C Programming if we want to repeat some statements here we make the use of loops. There are three methods by which we can repeat part of program. Using for statement Using a while statement Using a do-while statement The while loop: It will repeat the program until the condition in while loop become false. Example: /*Calculation of simple interest for 3 sets of p, n and r*/ #include<stdio.h> int main() { int p,n,count; float r,si; count=1; while(count<=3) { printf("\nEnter values of p,n and r"); scanf("%d%d%f",&p,&n,&r); si=p*n*r/100; printf("Simple interest = Rs. %\nf",si); count=count+1; } return 0; } Here this program while loop has the condition to repeat the program 3 times. As in while loop at the end count is increased by 1 every time it execute and when it reaches to 3 control comes out of program and the program terminates. Tips and Traps As I mentioned in m...

C Programming #5 (Complex decision making)

Decision making is very important in C program so to get most out of it similar to boolean maths in C we use logical operators Using if else can be tough while making program with some specific condition So here you can use operators like: if((percentage>=50)&&(percentage<60)) printf("Second division"); Here && in if statement is working as AND operator in this both the condition is to be true so that command in if statement to be execute. So if || is used it is similar to OR operator in this type of statement any one condition is to be true for command to be executed. and ! is NOT operator we will see this one in detail in my further blogs. The conditional operator: The for of conditional operator is like  expression 1? expression 2 : expression 3 It mean if the expression 1 is true then exp 2 will be executed if not then exp 3 will be executed. Example: int x,y; scanf("%d",&x); /*Here I have used scanf to take input f...

C Programming #4 (Decision Control Instruction)

Decision! While writing program when its is executed it execute in the sequence which it is written but in serious programming sequence is according to some condition so here is the need to make decision .Which is implemented in C using : The if statement The if-else statement The conditional operator The if statement: The general form of if statement looks like this: if(this condition is true) execute this statement; Example: #include<stdio.h> int main() { int num; printf("Enter a number less than 10"); scanf("%d",&num); if(num<10) printf("What an obedient servant you are!\n"); return 0; } The if-else statement: In this form when the if statement turn to be false then the else statement is executed Example: /*Calculation of gross salary*/ #include<stdio.h> int main() { float bs,gs,da,hra; printf("Enter basic salary"); scanf("%f",&bs); if(bs<1500) ...

C Programming for Beginner #3 (Instructions)

Types of Instruction Type Declaration: It is used to declare type of variables used in C Program. Arithmetic: It is used to perform Arithmetic operation on constant or variable. Control: It is used to control the sequence of execution. With the help of these instruction we make our C Program. Type Declaration:- Example:  int a float b char code Explanation : int a means we are a can store an integer values in a like 1,2,3,4 etc. float b means we can store a decimal values in b like 1.5 ,2.6 etc. char is used to declare character in C Program. Arithmetic : c=a+b (Addition) c=a/b (Division) c=a*b (Multiplication) c=a%b (Modulus) these gives us the remainder after division. Control : This instruction comes in picture when we deal with the loops,case control sequence of program or decision making. Example Program: include<stdio.h> int main() { int a=5,b=5; c=a+b; d=a-b; e=a/b; f=a*b; g=a%b; printf...

C Programming for Beginners #2 (First Program hello world)

This is the First C Program from which every programmer have started. #include<stdio.h> int main() { printf("Hello world\n"); return 0; } Explanation of the Program include is to add the specific library to compile the program. In this case stdio.h is the library in which commands are predefined it is called as preprocessor directive. printf is used when we have to display something on screen. In printf \n is used to print on the new line. After every end of command ; is used. To compile the C program there are many compilers available but if you ask me I compile and run my programs on ubuntu so in ubuntu the program is typed in text editor and saved it as .c file and in terminal it is compiled by two ways using gcc name.c and in place of name you have to enter the name of file you have saved. It will give the compiled program as a.out and you can run it by ./a.out command Note:- To give your program the name you want inste...

C Programming for Beginners #1 (Getting started)

C Programming is easy to learn there is a close analogy between learning English language and learning C language. We will start from the basic concept of C like which alphabets,digits,special symbols we can use. Alphabets - A,B.......Y,Z Digits - 0,1,2,3,4,5,6,7,8,9 Special symbols - ~ ' ! @ # % ^ & * ( ) _ - + = | \ { } [ ] : ; " ` < > , . ? / $ In C Programming we typically do lots of calculation the result of these calculation is stored in the computer memory it's similar to human memory so all the result in C is in memory location For now just just keep in mind that in C there are two types of constant:- Primary constant Secondary constant Primary constant:- Integer constant Real constant Character constant Secondary constant:- Array Pointer Structure Union Enum.Etc I'll cover all this topic and using this we are going to learn C programming. C keywords :- C keyword are the word whose meaning are already d...