Skip to main content

Posts

Showing posts from 2008

The While Loop

The While Loop While programming we often need to do something a fixed number of times. In such cases the While loop is used. Lets study it with the help of an example below; /* Calculation of simple interest for 3 sets of p, n and r */ 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. %f", si ) ; count = count + 1 ; } } The program above executes all statements after while three times.The logic of calculating simple interest is written in braces after the while word and is called as the body of the while loop.The brackets after the while contains the condition.All the statements within the loop are executed till the condition is true. Another program which uses while loop,to print the average of the numbers input . The Program

loops2

The versatility of the computer lies in its ability to perform a set of instructions repeatedly. This involves repeating some portion of the program either a specified number of times or until a particular condition is being satisfied. This repetitive operation is done through a loop control instruction. There are three methods by way of which we can repeat a part of a program; (1) Using a for statement (2) Using a while statement (3) Using a do-while statement

Loops

Till now we have studied the use of Decision or Sequential Control statements. When executed thesestatements always perform the same series of actions, in the same way, exactly once.But in order to perform programs that can do an action over and over,we need LOOPS. Thus in the next post we are going to discuss about the Loops.

Forms of IF statement

Forms of IF Statement: The if statement can take any of the given forms: (1) if ( condition ) do this ; (2) if ( condition ) { do this ; and this ; } (3) if ( condition ) do this ; else do this ; (4) if ( condition ) { do this ; and this ; } else { do this ; and this ; } (5) if ( condition ) do this ; else { if ( condition ) do this ; else { do this ; and this ; } } (6) if ( condition ) { if ( condition ) do this ; else { do this ; and this ; } } else do this ;

The if-else Statement

The if-else Statement As described in earlier post,the if statement will execute a single statement, or a group of statements, when the expression following if evaluates to true.But it does nothing when the expression evaluates to false. In case, we want to execute one group of statements if the expression evaluates to true and another group of statements if the expression evaluates to false,we need to use the If-else statement. The syntax of If-else statement is as follows; if(condition) { statement; } else { statement2; } If the condition 1 is evaluated true,then statement1 will execute else statement2 will be executed. A simple example is; if(a =2500 ) { hra = 1200; da = basic * 0.5 ;pt=150; } else { hra = basic*0.1 ; da = basic * 25.01 / 100 ; } gs = basic + hra + da ; net=gs-pt; printf ( "Net salary = Rs. %f", net ) ; }

The IF statement

The IF statement The C language uses the keyword if to implement the decision control instruction. The general form of IF statement is as follows; if ( condition ) execute this statement ; The condition following the keyword if is always enclosed within a pair of parentheses. If the condition is true then the given statement is executed.If the condition is not true then the statement is not executed; instead the program skips past it. To express conditions in C we use conditional operators. Expression Meaning x == y x is equal to y x != y x is not equal to y x <> x is less than y x > y x is greater than y x <= y x is less than or equal to y x >= y x is greater than or equal to y Lets consider a sample program; /* The IF statement */ main( ) { int num ; printf ( "Enter a number less than 50 " ) ; scanf ( "%d", &num ) ; if ( num <= 50 ) printf ( "You are fail" ) ; } On execution,if you enter a number less than equal to 50 you will get t

Decision Control Structure

The Decision Control Structure Till now we have used sequence control structure in the programs,in which the various steps are executed sequentially i.e.in the same order in which they appear in the program. In C programing the instructions are executed sequentially,by default. At times,we need a set of instructions to be executed in one situation and an another set of instructions to be executed in another situation. In such cases we have to use decision control instructions.This can be acheived in C using; (a)The if statement (b)The if-else statement (c)The conditional operators

Hierarchy of Operations

The preference in which arithematic operations are performed in an arithematic expression is called as Hierarchy of operations. Its very important in C Programming to predefine the Hierarchy of operations to the C compiler in order to solve the given arithematic expression correctly. e.g.: If a programmer wishes to perform ,z=a+b/c;it may be interpretted as z=(a+b)/c or z=a+(b/c). and if a=3,b=6,c=2 then using the same expression we will obtain two different answwers as z=4.5 or z=6. To avoid this condition we must be aware of hierarchy of operations used in C programming. In arithematic expressions scanning is always done from left to right. The priority of operations is as shown below, Priority Operators First Paranthesis os brackets() Second Multiplication & Divison Third Addition & Substraction Fourth Assignment i.e.= If we consider the logical operators used in C language,the operator precedence will be like; Operators Type ! Logical

Increment-Decrement Operators

PRE/POST INCREMENT/DECREMENT OPERATORS(Unary operators) In programming we often require to increment value of certain variable in step of one or may require to decrement a value. This is usually written as i=i+1 for incrementing the value of variable i or i=i-1 to decrement the value of i. To accomplish this C language provides unary operators called as increment/decrement operators.Using these operators the above variable can be written as i++ or ++i. If ++ is after the variable then it is called as post increment and if it is before the variable then it is called as predecrement. In case of post increment(i++) the old value of the variable is used first and then the increment in the value of variable is done. In preincrement the value of variable is incremented first and then this value is used in the expression. same is in the case of decrement,i.e.pre decremant and post decrement, denoted as --i and i--.

Using symbolic Constants(#define)

Using Symbolic constants(#define) While developing a program in C language,we often use certain unique constants in a program.These const. may appear repeatedly in a C program,at number of places. Foe example,if we want to calculate the area of n number of circles,we require a constant 3.14 which is the value of'pi'. Now we may like to change the value of pi from 3.14 to 3.14159.To achieve this we will have to search through-out the program and explicitly change the value of pi whereever it has been used.If any value is left unchanged the program will produce wrong results.When we use variable ,value may change accidently,which results to wrong output. To avoid these type of mistakes w use symbolic constants in our C program. the syntax of a symbolic constant is; #define symbolic name value e.g. #define pi 3.14159 #define total 100

Inpu-output functions(Formatted)

Formatted Functions The formatted functions used in C programming are printf and scanf functions. Printf prints the formatted output on screen as specified in format specifier.This function can also be used for displaying all the datatype variables. e.g. printf(char,var1,var2,....); This function is used to enter the data. The scanf function accepts the formatted input from user as specified in format specifier & in variable address given as parameter. e.g. scanf("%d",&char); Example: main() { int x; char A; printf("\nx=%d",x); printf("\nx=%c",ch); printf("\nPress any key to continue"); getch(); } In the program above \n is the new line character.

Inpu-Output functions(Unformatted)

The C environment assumes that the standard input device(keyboard) and the standard output device(Monitor) are always connected to the C environment. Here's a list of some unformatted functions used in C along with their uses. Input / Output Functions Unformatted functions getch() Waits for user to press any key, returns the key pressed that can be assigned to variable getche() like getch() but echoes character pressed to screen getchar() Accepts one character from user, terminates only when user presses enter. Even if user typed more than one character return first one character only. putchar() can pass character type parameter, a single character that you want to appear on screen. gets() By passing character array as parameter can. accept string input including spaces. puts() By passing character array as parameter can display the content of string

C - The Programmer's Language

C - The Programmer's Language C language is widely famous and founds its applications everywhere because of its versatility and flexibility.In fact C language is a Programmers’s language. Not all computer programming languages are for programmers. For instance the COBOL and BASIC are not the languages for programmers.C was created,influenced, and tested by working programmers. That’s the reason C gives the programmer what the programmer wants.C is the language with few restrictions, few complaints, block structures, stand alone functions, and a compact set of keywords.Using C Language , you can nearly achieve the efficiency of assembly code combined with the structure of ALGOL.This has made C language highly popular between professional programmers. C was initially used for systems programming. Here are few example of system programming; 1) Operating systems 2) Interpreters 3) Editors 4) Compilers 5) File utilities 6) Performance enhancers 7) Real-time executives C founds wide

Format Specifiers

Format Specifiers: The format specifiers are used in C programming to specify the format of the datatypes. The various format specifiers used in C programming are as follows; %d : Used for getting integer. %c : Used for getiing character output. %f : For float value. %l : For long integer. %s : For a string.

Basic Variables

As a programmer, you will frequently want your program to "remember" a value. For example, if your program requests a value from the user, or if it calculates a value, you will want to remember it somewhere so you can use it later. This post will show you the different types of variables ( Reminder: )that are available in C. The four basic variables are int(integers,float (decimals),double (long numbers)and char (characters). The following sample program will show you how they could be used; #include stdio.h int main(void) { int integer; float decimal; double longnumber; char character; integer = 9; decimal = 5.6; longnumber = 56489; character = 'G'; return 0; } Now lets learn to display the contents of the different variable types. Heres a sample program; #include stdio.h int main(void) { int integer = 9; float decimal = 5.6; double longnumber = 564897; char character = 'G'; printf("The contents of the variable int

Datatypes

As our English Language is constructed of alphabets,words,sentences and paragraphs. Similarly in C language a program is constructed using constants,variables and keywords.A character is the most basic element.Any alphabet,number or special symbol is a character.These characters when combined properly form constants,variables and keywords. A 'constant' is that which never changes its value;while an 'variable' may change its value. Constants in C programming are divided as 1)Primary Constants 2)Secondary Constants. 1)Primary Constants: Integer, Real(Floating) and Character are the sub types of Primary constants. Rules for writing Integer Constants: a)It must have atleast one digit. b)It should not contain a decimal point,as its a integer. c)If theres no sign then a positive sign is assumed. d)It should not contain any comma or blank space. e)The range of integer constant is -32768 to 32767. Ex. 454 -4256 Rules for writing Real Constants: The real constants are also calle

A simple C program

Now lets start with a simplest C program in this post. Read the program carefully and try to understand the meaning of each statement. Sample Program: /* Lets begin programming */ main() { printf("Hi people .This is our first C program \n"); } Explaination of the above program: Every C program must start with the main()function. The ';' is used as a statement terminator in C. { } marks the beginning and the end of a function. /* */ encloses comments if any. printf() is a C function for displaying variable or constant. \n is the escape sequence for new line. I run this program on C blocks software and here is the Output: Sounds easy,right? Still any question post it to the comment box.I'll be happy to resolve them.

Features of C

Features of C a) Middle Level Language : C is a middle level language as it combines elements of high-level language with the functionals of assembly language.C allows manipulation of bits, bytes and addresses - the basic elements with which the computer functions. Also, C code is very portable, that is software written on one type of computer can be adapted to work on another type. Although C has five basic built-in data types, it is not strongly typed language as compared to high level languages, C permits almost all data type conversions. It allows direct manipulation of bits, bytes, words, and pointers. b) Block Structured Language : C is referred as a structured language because it is similar in many ways to other structured languages like ALGOL, Pascal and the likes. C allows compartmentalisation of code and data. This is a distinguishing feature of any structured language. It refers to the ability of a language to section off and hide all information and instructions necessar

Beginning with C programming.

To start with C programming,firstly you need a 'Compiler'. A Compiler is a program which converts the written program into the machine language. To type a program we need a Text Editor(e.g. Notepad). But almost every compiler comes along with an in-built text editor.AT beginner level you could use a Turbo C or Turbo C++ compiler. If you have Windows 7 64bit OS , take a look at this post , about Installing turbo C on Win 7 64 bit . You can use the search bar given on this page to find a suitable compiler for you. The compiler(TC.EXE) is usually situated in C:\TC\BIN directory of your computer. After starting the compiler select New from the File menu . Then type the desired program. The next step is to save your written program,press F2 key to save your program. Give a proper name to the program. Now,compile and execute the program by pressing Ctrl+F9 . To view the output of your program press Alt+F5 .

History of C.

What is C Programming? Before beginning to start actual programming part,let's first find out what exactly C programming is & take a look where it came from? The C programming language was developed in 1972 at AT & T's Bell Lab. in USA by Dennis Ritchie. C is a development of earlier developed languages BPCL and B. Enough with the introduction part,lets now move to the next post and start programming.