Basic Programming Lab C Sample program in C

Basic Programming Lab C@ Sample program in C Data Types and format Specifiers Operators Conditional statements (if, if else, nested if, switch and conditional statements)

Sample program in C • All C programs will consist of at least one function, but it is usual to write a C program that comprises several functions. • The only function that has to be present is the function called main. • For more many programs the main function will act as a controlling function calling other functions. Because main function is the first function that is called when your program executes.

Compilation: Once the program has been typed it needs to be converted to machine language (0 s and 1 s) before the machine can execute it. To carry out this conversion we need another program called Compiler vendors provide an Integrated Development Environment (IDE) which consists of an Editor as well as the Compiler. For example, • Turbo C, Turbo C++ and • Microsoft C are some of the popular compilers that work under MS-DOS; • Visual C++ and Borland C++ are the compilers that work under Windows, • gcc compiler works under Linux.

Sample program in C (general form ) pre-processor directives global declarations main() { local variables to function main ; statements associated with function main ; } f 1() { local variables to function 1 ; statements associated with function 1 ; } f 2(){ local variables to function f 2 ; statements associated with function 2 ; }

Sample program in C (Example)

Sample program in C (pre-processor directives) #include<stdio. h> is a preprocessor directive, which contains standard Input and output functions such as printf and scanf etc. #include <math. h> this header file contains mathematical function like a = pow ( 3, 2 ) ;

Data Types: There are five basic data types associated with variables: • int - integer: a whole number. • float - floating point value: i. e. a number with a fractional part. • double - a double-precision floating point value. • char - a single character. • void - valueless special purpose type

Data Types: (Format specifiers) Format specifiers can be defined as the operators which are used in association with printf() function for printing the data and scanf() function for reading the data. Format specifier Description %d Integer Format Specifier %f Float Format Specifier Format specifiers start with a percentage % operator and followed by a special character for identifying the type of the data. %c Character Format Specifier %s String Format Specifier %ld Long Int Format Specifier

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

C-operators (Arithmetic Operators) Operator Description + 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.

C-operators (Relational Operators) Operator Description == 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 not. If the values are not equal, then the condition becomes true. > Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. < Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. >= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. <= Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true.

C-operators (Logical Operators) Operator Description Example && Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (A && B) is false. || Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. (A || B) is true. ! Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. !(A && B) is true.

C-operators (Bitwise Operators) p q p&q p|q p^q 0 0 0 1 0 1 1 1 0 1 0 0 1 1

C-operators (Bitwise Operators) Assume A = 60 and B = 13 in binary format, they will be as follows − A = 0011 1100 B = 0000 1101 --------A&B = 0000 1100 A|B = 0011 1101 A^B = 0011 0001 ~A = 1100 0011

C-operators (Bitwise Operators) Description Example & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) = 12, i. e. , 0000 1100 | Binary OR Operator copies a bit if it exists in either operand. (A | B) = 61, i. e. , 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) = 49, i. e. , 0011 0001 ~ Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. (~A ) = -61, i. e, . 1100 0011 in 2's complement form. << Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 = 240 i. e. , 1111 0000 >> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 = 15 i. e. , 0000 1111

C-operators (Assignment Operators) Description Example = Simple assignment operator. Assigns values from right side operands to left C = A + B will assign the value of A + B side operand to C += Add AND assignment operator. It adds the right operand to the left operand C += A is equivalent to C = C + A and assign the result to the left operand. -= Subtract AND assignment operator. It subtracts the right operand from the left operand assigns the result to the left operand. *= Multiply AND assignment operator. It multiplies the right operand with the C *= A is equivalent to C = C * A left operand assigns the result to the left operand. /= Divide AND assignment operator. It divides the left operand with the right operand assigns the result to the left operand. C /= A is equivalent to C = C / A %= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. C %= A is equivalent to C = C % A C -= A is equivalent to C = C - A <<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2 >>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2 &= Bitwise AND assignment operator. C &= 2 is same as C = C & 2 ^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2

C-operators (Misc Operators) Operator Description sizeof() & * ? : Example Returns the size of a variable. sizeof(a), where a is integer, will return 4. Returns the address of a variable. &a; returns the actual address of the variable. Pointer to a variable. *a; Conditional Expression. If Condition is true ? then value X : otherwise value Y

C - Decision Making

C - Decision Making Sr. No. 1 Statement & Description if statement An if statement consists of a boolean expression followed by one or more statements. 2 if. . . else statement An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. 3 nested if statements You can use one if or else if statement inside another if or else ifstatement(s). 4 switch statement A switch statement allows a variable to be tested for equality against a list of values. The ? : Operator 5

C - Decision Making (if statements) In decision control statements (if-else and nested if), group of statements are executed when condition is true. If condition is false, then else part statements are executed. Ø if statements Ø if else statements Ø nested if statements

C - Decision Making (if condition) Decision control statements if Syntax/Description Syntax: if (condition) { Statements; } Description: In these type of statements, if condition is true, then respective block of code is executed. Example

C - Decision Making (if. . else ) Decision control statements If. . else Syntax/Description Syntax: if (condition) { Statement 1; Statement 2; } else { Statement 3; Statement 4; } Description: In these type of statements, group of statements are executed when condition is true. If condition is false, then else part statements are executed. Example

C - Decision Making (if. . else ) Decision control statements Nested if Syntax/Description Syntax: if (condition 1){ Statement 1; } else_if(condition 2) { Statement 2; } else Statement 3; Description: If condition 1 is false, then condition 2 is checked and statements are executed if it is true. If condition 2 also gets failure, then else part is executed. Example

C - Decision Making (Switch) A switch statement is for multiple way selections used Allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case

C - Decision Making (Switch @ Syntax)

C - Decision Making (Switch @ Example) #include<stdio. h> int main( ) { int age; printf("n. Enter the number of the age: "); scanf("%d", &age); switch (age) { case 1: printf("You're one. "); break; case 2: printf("You're two. "); break; case 3: printf("You're three. "); case 4: printf("You're three or four. "); break; default: printf("You're not 1, 2, 3 or 4!"); } return 0; }

Assignment: • Write a program to request a whole number and print the number and its square? • Write a program any of your own program which illustrate the operators concepts? • Write a program checks whether the entered number is positive or negative? • Write a program to find the greatest of three numbers? . • Write a program to read marks and find the total marks then provide grade using switch concept? • Write a program to find roots of quadratic equation using if and switch?
- Slides: 27