Вы находитесь на странице: 1из 5

C (programming language) In computing, C (/'si?

/, as in the letter C) is a general-purpose programming la nguage initially developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs.[5][6] Like most imperative languages in the ALGOL tradition, C has facilit ies for structured programming and allows lexical variable scope and recursion, while a static type system prevents many unintended operations. Its design provi des constructs that map efficiently to typical machine instructions, and therefo re it has found lasting use in applications that had formerly been coded in asse mbly language, most notably system software like the Unix computer operating sys tem.[7] C is one of the most widely used programming languages of all time,[8][9] and C compilers are available for the majority of available computer architectures and operating systems. Many later languages have borrowed directly or indirectly from C, including D, G o, Rust, Java, JavaScript, Limbo, LPC, C#, Objective-C, Perl, PHP, Python, Veril og (hardware description language),[4] and Unix's C shell. These languages have drawn many of their control structures and other basic features from C. Most of them (with Python being the most dramatic exception) are also very syntactically similar to C in general, and they tend to combine the recognizable expression a nd statement syntax of C with underlying type systems, data models, and semantic s that can be radically different. C++ and Objective-C started as compilers that generated C code; C++ is currently nearly a superset of C,[10] while ObjectiveC is a strict superset of C.[11] Before there was an official standard for C, many users and implementors relied on an informal specification contained in a book by Dennis Ritchie and Brian Ker nighan; that version is generally referred to as "K&R" C. In 1989 the American N ational Standards Institute published a standard for C (generally called "ANSI C " or "C89"). The next year, the same specification was approved by the Internati onal Organization for Standardization as an international standard (generally ca lled "C90"). ISO later released an extension to the internationalization support of the standard in 1995, and a revised standard (known as "C99") in 1999. The c urrent version of the standard (now known as "C11") was approved in December 201 1. DESIGN C is an imperative (procedural) language. It was designed to be compiled using a relatively straightforward compiler, to provide low-level access to memory, to provide language constructs that map efficiently to machine instructions, and to require minimal run-time support. C was therefore useful for many applications that had formerly been coded in assembly language, such as in system programming . Despite its low-level capabilities, the language was designed to encourage cross -platform programming. A standards-compliant and portably written C program can be compiled for a very wide variety of computer platforms and operating systems with few changes to its source code. The language has become available on a very wide range of platforms, from embedded microcontrollers to supercomputers. C programming while and do...while Loop C programming loops Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied, i.e., loops are used in performing repetitive work in programming. Suppose you want to execute some code/s 10 times. You can perform it by writing that code/s only one time and repeat the execution 10 times using loop.

There are 3 types of loops in C programming: for loop while loop do...while loop Syntax of while loop while (test expression) { statement/s to be executed. } The while loop checks whether the test expression is true or not. If it is true , code/s inside the body of while loop is executed,that is, code/s inside the br aces { } are executed. Then again the test expression is checked whether test ex pression is true or not. This process continues until the test expression become s false. example: I read input from the keyboard using scanf() in a while loop and print back on t he screen using printf(): #include <stdio.h> #include <conio.h> int main(void) { char ch; printf("enter your name"); while(ch!='/n') { scanf("%c",&ch); printf("%c",ch); } getch(); return 0; } Flowchart of while loop in C programming Example of while loop Write a C program to find the factorial of a number, where the number is entered by user. (Hints: factorial of n = 1*2*3*...*n /*C program to demonstrate the working of while loop*/ #include <stdio.h> int main(){ int number,factorial; printf("Enter a number.\n"); scanf("%d",&number); factorial=1; while (number>0){ /* while loop continues util test condition number>0 is true */ factorial=factorial*number; --number; } printf("Factorial=%d",factorial); return 0;

} Output Enter a number. 5 Factorial=120 do...while loop In C, do...while loop is very similar se two loops is that, in while loops, n do...while loop code is executed at he code are executed at least once in to while loop. Only difference between the test expression is checked at first but, i first then the condition is checked. So, t do...while loops.

Syntax of do...while loops do { some code/s; } while (test expression); At first codes inside body of do is executed. Then, the test expression is check ed. If it is true, code/s inside body of do are executed again and the process c ontinues until test expression becomes false(zero). Notice, there is semicolon in the end of while (); in do...while loop. Flowchart of working of do...while loop in C programming. Example of do...while loop Write a C program to add all the numbers entered by a user until user enters 0. /*C program to demonstrate the working of do...while statement*/ #include <stdio.h> int main(){ int sum=0,num; do /* Codes inside the body of do...while loops are at least exec uted once. */ { printf("Enter a number\n"); scanf("%d",&num); sum+=num; } while(num!=0); printf("sum=%d",sum); return 0; } Output Enter a number 3 Enter a number -2 Enter a number 0 sum=1 In this C program, user is asked a number and it is added with sum. Then, only t he test condition in the do...while loop is checked. If the test condition is tr ue,i.e, num is not equal to 0, the body of do...while loop is again executed unt il num equals to zero.

C while Program Program Program

Loops Examples to reverse a integer to Calculate the power of a number to Check Armstrong number

C Programming break and continue Statement There are two statement built in C, break; and continue; to interrupt the normal flow of control of a program. Loops performs a set of operation repeately until certain condition becomes false but, it is sometimes desirable to skip some sta tements inside loop and terminate the loop immediately without checking the test expression. In such cases, break and continue statements are used. break Statement In C programming, break is used in terminating the loop immediately after it is encountered. The break statement is used with conditional if statement. Syntax of break statement break; The break statement can be used in terminating all three loops for, while and do ...while loops. Flowchart of break statement in C programming. The figure below explains the working of break statement in all three type of lo ops. working of break statement in C programming in for, while and do...while loops Example of break statement Write a C program to find average of maximum of n positive numbers entered by us er. But, if the input is negative, display the average(excluding the average of negative input) and end the program. /* C program to demonstrate the working of break statement by terminating a loop , if user inputs negative number*/ # include <stdio.h> int main(){ float num,average,sum; int i,n; printf("Maximum no. of inputs\n"); scanf("%d",&n); for(i=1;i<=n;++i){ printf("Enter n%d: ",i); scanf("%f",&num); if(num<0.0) break; //for loop breaks if num<0.0 sum=sum+num; } average=sum/(i-1); printf("Average=%.2f",average); return 0; } Output Maximum no. of inputs 4 Enter n1: 1.5 Enter n2: 12.5 Enter n3: 7.2

Enter n4: -1 Average=7.07 In this program, when the user inputs number less than zero, the loop is termina ted using break statement with executing the statement below it i.e., without ex ecuting sum=sum+num. In C, break statements are also used in switch...case statement. You will study it in C switch...case statement chapter. continue Statement It is sometimes desirable to skip some statements inside the loop. In such cases , continue statements are used. Syntax of continue Statement continue; Just like break, continue is also used with conditional if statement. Flowchart of continue statement in C programming For better understanding of how continue statements works in C programming. Anal yze the figure below which bypasses some code/s inside loops using continue stat ement. Working of continue statement in C programming language Example of continue statement Write a C program to find the product of 4 integers entered by a user. If user e nters 0 skip it. //program to demonstrate the working of continue statement in C programming # include <stdio.h> int main(){ int i,num,product; for(i=1,product=1;i<=4;++i){ printf("Enter num%d:",i); scanf("%d",&num); if(num==0) continue; / *In this program, when num equals to zero, it skips the statement product*=num and continue the loop. */ product*=num; } printf("product=%d",product); return 0; } Output Enter num1:3 Enter num2:0 Enter num3:-5 Enter num4:2 product=-30

Вам также может понравиться