C Program Output #include <stdio.h> main () { printf ( Hi everyone\n ); Hi everyone
#include <stdio.h> main () { printf ( Hi everyone\n ); #include and main are Keywords (or Reserved Words) Reserved Words have special meaning in Programming Languages Keywords can t be used for any other purpose.
#include <stdio.h> main () { printf ( Hi everyone\n ); Contains Utilities for Input and Output Include information from Standard Library Use utility from Standard Library Not part of C Language but library exists with all C installations
#include <stdio.h> main () { printf ( Hi everyone\n ); Each of H, i, e, etc. is a character String Sequence of characters in... Causes Hi everyone to be printed on output device (monitor) \n means new line ; terminates a statement
C Program #include <stdio.h> main () { printf ( hello\n ); printf ( all\n ); hello all Output
C Program Output #include <stdio.h> main () { printf ( hello everyone\n ); printf ( Best wishes\n ); Identical Hello everyone Best wishes #include <stdio.h> main () { printf ( hello everyone\nbest wishes\n );
C Program #include <stdio.h> main () { printf ( hello ); printf ( everyone\n ); or #include <stdio.h> main () { printf ( hello everyone\n ); Output Hello everyone What happens when you omit the trailing \n?
General Structure of C program /* Include files for input/output functions*/ #include <stdio.h> /* function definitions*/ function definitions /* Main function- each declarations and statements in a function are separated by semi colon */ main() { /* variables; arrays; records; function declarations etc */ declarations /* Each statement is separated by semi colon*/ statements
Comments are enclosed within /* --------- */ Contents within /* and */ are ignored by compiler Used for informal description and documentation for readability explain design decisions Comments do NOT increase size of executable program One line comment can precede // main () { float inc, tax;... tax = (inc - 30) * 0.1; /* according to income tax table */ // Computing Tax Comment
#include statements used to include the header file for input/output stdio.h, the standard mathematics library math.h etc. These files are enclosed within < > #define helps in defining constant symbol. compiler replaces all occurrences of M by 5. Different from variable. # M 5; main () { int x; x= 10+M; printf(x);
Variables and Constants are basic data objects a constant value does not change during program execution variables may change values any number of times Variables are associated with a Type int (integer) float, double (has fractional part) char (character) Type is declared first before variable is used main () { int a, b, c; float d, e;...
Standard: int, float, char, double User defined datatypes: arrays, structures, pointers, enumerated datatype etc.
Form of Declaration: type list of variables; /* each separated by, and finally terminated by ; */ Examples: int x, y, z; float p, q[3][4]; array char name[20]; char ch = A ; /* character is enclosed within */
#include <stdio.h> #define i 6 main() { /* integer declaration */ int x, y; /* Assignment statements */ x=7; y= i + x; /* output statement */ printf("%d\n", y); Format descriptor
An expression is a combination of variables, constants and operators written according to the syntax of C language. Every expression evaluates to a value of a certain type that can be assigned to a variable. Precedence in Arithmetic Operators An arithmetic expression without parenthesis will be evaluated from left to right using the rules of precedence of operators. Each operator in C has a precedence associated with it. There are two distinct priority levels of arithmetic operators in C. High priority * / % Low priority + -
When Parenthesis are used, the expressions within parenthesis assume highest priority. Parenthesized sub expression are evaluated from left to right If parenthesis are nested, the evaluation begins with the innermost sub expression. The operators of higher precedence are evaluated first. The operators of same precedence are evaluated from right to left or from left to right depending on the level. This is known as associativity property of an operator. The associability rule is applied when two or more operators of the same precedence level appear in the sub expression.
x + y * z / 2 + p x + (y * z) / 2 + p x + ((y * z) / 2) + p (x + ((y * z) / 2)) + p ((x + ((y * z) / 2)) + p) x + y - z / 2 * p (x + y) - z / 2 * p (x + y) (z / 2) * p (x + y) ((z / 2) * p) ((x + y) ((z / 2) * p))
i = i + 2 i += 2 i = i +1 i++ or ++i i = i +1 ; x = x + i x +=(++i); x = x + i ; i = i +1 x += (i++); i = i -1 ; x = x + i x +=(--i); x = x + i ; i = i -1 x += (i--); i = i -2 ; x = x + i i -= 2; x += i; x = x + i ; i = i -2 x += i; i -=2 x=x-i; i=i+1 x -= i++
Logical Operators && AND OR! NOT Relational Operators == equality!= Not equal to < less than <= less than equal to > greater than >= greater than equal to
exp? exp1 : exp2 An expression exp is evaluated and if the value is nonzero (or true - represented by 1) then expression exp1 is the final value otherwise exp2 is the final value of entire expression.
Assignment statement x = expression; Compound statement {s1; s2;. ; Collection of statements, each separated by semi colon and enclosed in brackets Multiple lines comments are enclosed within /* comments */ Single line comment can be preceded by //
if (cond) statement; if (cond) s1 else s2; Here cond is a boolean condition which can have non zero value representing true and 0 representing false. Statement may be simple or compound.
for (i = m1; i <= m2; i+=m3) Here { body ; m1 : initial value; m2 : maximum value of I m3 : increment (positive or negative) body sequence of statements.
While statement while (cond) { body ; Do-while statement do {body while cond; In while loop, cond is first checked and if it is true then body is executed. Loop is repeated till condition fails. Do-while loop, first body is executed and then cond is checked. If it is true then loop repeats else exit the loop.
switch (exp) { case v 1 :s 1 ; break; case v 2 :s 2 ; break; case v n :s n ; break; default : s optional If the value of exp is v j then s j is executed and switch statement is exited using break statement. Execution always starts from 1 to last.
Input statement scanf(&x, &y); Input the values of x and y &x means address of x Output statement: Assume x=2; y=5; printf ( %d\n%d\n, x, y); Output 2 5 Format descriptor printf ( x = %d\ny = %d\n, x, y); Output x= 2 y= 5
/* reads single character and stores in character variable x */ x = getchar(); /* prints single character stored in x*/ putchar(x); /* the following functions are in standard file named stdio.h */ scanf(control, v1, v2,..); printf(control, e1,e2,...); Control in input/output control = "seq of format descriptor
Description Meaning %d a decimal integer %o a octal integer %x a hexadecimal integer %c a single character %s a character string %f a decimal number (float or double) \n skip to new line
printf("%4d%7.2f\n%c\n", x, y, z) printf( %c %d %f, ch, i, x); scanf("%4d%8.2f \n", &x, &y) scanf( %c %d %f, &ch, &i, &x); Here & represents memory addresses
Implicit type conversion C permits mixing of constants and variables of different types in an expression. C automatically converts any intermediate values to the proper type so that the expression can be evaluated without loosing any significance. This automatic type conversion is know as implicit type conversion During evaluation it adheres to very strict rules and type conversion. If the operands are of different types the lower type is automatically converted to the higher type before the operation proceeds. The result is of higher type.
1. If one operand is long double, the other will be converted to long double and result will be long double. 2. If one operand is double, the other will be converted to double and result will be double. 3. If one operand is float, the other will be converted to float and result will be float. 4. If one of the operand is unsigned long int, the other will be converted into unsigned long int and result will be unsigned long int. 5. If one of the operand is long int, the other will be converted to long int and the result will be long int. 6. If one operand is unsigned int the other will be converted to unsigned int and the result will be unsigned int.
Many times there may arise a situation where we want to force a type conversion in a way that is different from automatic conversion. Consider for example the calculation of number of female and male students in a class female_students Ratio = ------------------- male_students Since if female_students and male_students are declared as integers, the decimal part will be rounded off and its ratio will represent a wrong figure.
This problem can be solved by converting locally one of the variables to the floating point as shown below. Ratio = (float) female_students / male_students The operator float converts the female_students to floating point for the purpose of evaluation of the expression. Then using the rule of automatic conversion, the division is performed by floating point mode, thus retaining the fractional part of the result. The process of such a local conversion is known as explicit conversion or casting a value. The general form is (type_name) expression
Formula: x1 = [(-b) + (b 2 4ac)]/2a x2 = [(-b) - (b 2 4ac)]/2a Algorithm: input a, b, c; d = b*b 4*a*c; if (d 0) then { e = sqrt(d); r1 = (-b + e) / (2*a); r2 = (-b - e) / (2*a); i1=0; i2 =0 else { e = sqrt(-d); r1 = (-b) / (2*a); r2 = r1; i1= e / (2*a); i2 = -e/(2*a); output r1,r2,i1,i2
C Program (Roots for quadratic equations) #include<stdio.h> #include<math.h> main () { float a, b, c, d, e, r1, r2, i1, i2; printf("input the values of a, b and c\n"); scanf("%f%f%f", &a, &b, &c); d = b*b-4*a*c; printf("d=%f\n", d); if (d >= 0) { e = sqrt(d); printf("e=%f\n", e); r1 = (-b + e) / (2*a); r2 = (-b - e) / (2*a); i1=0; i2 =0; else { e = sqrt(-d); printf("e=%f\n", e); r1 = (-b)/ (2*a); r2 = r1; i1 = e / (2*a); i2 = -e/(2*a); printf("r1= %f\nr2= %f\ni1= %f\ni2= %f\n", r1,r2,i1,i2);
Formula: fact = n * n-1 * * 2 * 1 Algorithm: input n; i = 0; initialization fact = 1; looping while (i < n) { i = i + 1; fact = i * fact; output fact Execution Let n = 4; i = 0; fact = 1; while loop 0 < 4; i = i +1=1; fact = 1 * 1; 1 < 4; i = i +1=2; fact = 1 * 2; 2 < 4; i = i +1=3; fact = 2 * 3; 3 < 4; i = i +1=4; fact = 6 * 4 4 < 4 is false so exit the while loop; output fact = 24 Prof Saroj Kaushik, IIT Delhi 38
C Program (factorial using while) #include<stdio.h> main () { int n, i, fact; i = 0; fact = 1; printf("input the value of n\n"); scanf("%d", &n); while (i < n) { i = i+1; fact = i * fact; ; printf("\nthe Output of fact program is as follows:\n"); printf("n = %d\nfact = %d\n", n, fact);
C Program (factorial using do-while) #include<stdio.h> main () { int n, i, fact; i = 1; fact = 1; printf("input the value of n\n"); scanf("%d", &n); do { fact = fact * i; i = i+1; while (i <= n); printf("\nthe Output of fact program is as follows:\n"); printf("n = %d\nfact = %d\n", n, fact);
Algorithm: input num; rev = 0; while (num > 0) { rev = rev*10 + num mod 10; num = num div 10; output rev Execution num = 245; rev = 0; while loop rev = 0 * 10 + 5 = 5; num = 24; rev = 5 * 10 + 4 = 54; num = 2; rev = 54 * 10 + 2 = 542; num = 0; exit while output rev as 542 Prof Saroj Kaushik, IIT Delhi 41
C Program (reversal of integer number) #include<stdio.h> main () { int num, rev, q; printf("input the value of num\n"); scanf("%d", &num); printf("\nnum = %d", num); rev = 0; while (num > 0) { rev = rev * 10 + num % 10; num = num / 10; ; printf("\nrev= %d\n", rev); Remainder operator Integer division
Algorithm: Here number is read in the loop. At the end of loop, read elements are not available. max = 0; i=0; while (i < 100) { i = i+1; input num; if (num > max) then max = num; output max Prof Saroj Kaushik, IIT Delhi 43
C program (Max with fixed 100 numbers) #include<stdio.h> main() { int max, num, i=0; max= 0; while (i < 100) { i = i+1; printf("\ninput the number \n"); scanf("%d",&num ); if (num > max) max = num; ; printf("\nmax = %d",max);
C program (Max of list with variable numbers) #include<stdio.h> main() { int max, num, i=0; max= 0; printf("\ninput number of elements\n"); scanf("%d",&n ); while (i < n) { i = i+1; printf("\ninput the number \n"); scanf("%d",&num ); if (num > max) max = num; ; printf("\nmax = %d",max);
C program (Max of a list of numbers not known in advance. So terminate the list by -1000) #include<stdio.h> main() { int max, num; max= 0; printf("\ninput the number & -1000 for ending\n"); scanf("%d",&num ); while (num!= -1000) { if (num > max) max = num; scanf("%d",&num ); ; printf("\nmax = %d",max);
Algorithm: input num(i), i = 1,100; max = 0; i = 0; while (i < 100) { i = i+1; if (num(i) > max) then max = num(i); output max, num(i), i = 1,100 Execution Let numbers are: 4, 2,7,1 max = 0; i = 0; while loop i = 1; 4 > max so max = 4; i = 2; 2 < max so no change i = 3; 7 > max so max = 7; i = 4; 1 < max so no change exit of while output max as 7 and numbers as 4, 2, 7, 1 Prof Saroj Kaushik, IIT Delhi 47
Series:f=0!+1!+2!+ +n! (n 0) Algorithm: Here we are given the value of n. Series sum is calculated by finding sum after adding previous sum with a term at each step. input n; f=0;term=1;i=0; while (i < n) { f=f+term; i=i+1; term = term * i; output f
C program (Value of the series containing factorial of number starting from 0 to n) #include<stdio.h> main () { int n, i =0, f = 0, term = 1; printf("input the value of n\n"); scanf("%d", &n); while (i < n) { f = f + term; i = i+1; term = term * i; ; printf("\nthe value of the series is= %d\n", f);