Archives

C – Functions – Strings

 

String Length

FAQ

  • Find the given string length without using strlng()
  • Find the given sting length using while/for loop

 

Code : Using for loop

 

Code : Using while loop and pointer

 

 


String Concatenate (Joint)

FAQ

  • Joint two different/same string without using strcat() function?
  • Joint two strings using pointers
  • concatenate two string into one string using pointer in while loop

Code : Using pointer in while loop.

 

Code : Using pointer in for loop.

 



 

 


 

 

 

C – Macros

What is Macro?

Macros are the names of text/ literal values/ string (constant values) or code fragment, which will expand when pre-processor processes the macro.

Pre-processor processes the macros at compile time; hence that macros replace with the corresponding code fragments.


Exercise

How you define Macro Years in sec?

 


How you define Marco using find small number?

Answer : #define MIN(x,y) ((x<y)?x:y)

Example

 


Find out MIN & MAX using MACRO in C?

Answer : #define MIN(X,Y) (X<Y ? X:Y)  or #define MAX(X,Y) (X>Y ? X:Y)


Write a macro to find the smallest number among 3 given numbers?

Answer

 


#define MAX_NUM 15   Referring to the sample above, what is MAX_NUM?

(A) MAX_NUM is a precompiler constant.

(B) MAX_NUM is a preprocessor macro.

(C) MAX_NUM is an integer variable.

C – Operators

Operators

  • arithmetic : +, -, *, /, %
  • assignment : =
  • augmented assignment : +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
  • bitwise logic : ~, &, |, ^
  • bitwise shifts : <<, >>
  • boolean logic : !, &&, ||
  • conditional evaluation : ? :
  • equality testing : ==, !=
  • calling functions : ( )
  • increment and decrement : ++, —
  • member selection : ., ->
  • object size : sizeof
  • order relations : <, <=, >, >=
  • reference and dereference : &, *, [ ]
  • sequencing : ,
  • subexpression grouping : ( )
  • type conversion : (typename)

Address of Operator (Address of variable

  • & // Address of Operator – It cannot be used on constants.It cannot be used on variable which are declared using register storage class.

main() -function

  • main() – main is a function. Every function has a pair of parentheses ()
  • Can a program be compiled without main() function?
  • Yes, it can be but cannot be executed, as the execution requires main() function definition.

function

  • () - function -Every function has a pair of parentheses ()

semicolon

  • ; // Any C statement always ends with a ;

Comma Operator

  • , Comma Operator – can be used to separate two or more expressions
  • Eg: printf(hi) , printf(Hello);

scope or block

  • A local block is any portion of a C program that is enclosed by the left brace { and the right brace } .
  • anything between the two braces is contained in a local block.
  • {} // scope or block
    • Increment Operator
      • ++x (pre increments)
      • x++ (post increments)
      • Eg:
        • S++; // ++, as it is single machine instruction (INC) internally.(recommended)
        • S = S+1; // ++, as it wil take two machine cycle internally.(not recommended)
    • Decrements Operator
      • --x (pre Decrements)
      • x-- (post Decrements)

Boolean Operator

  • == Equal
  • != Not equal
  • > Greater than
  • >= Greater than or equal
  • < Less than
  • <= Less than or equal
  • && Logical AND
  • || Logical OR
  • ! Logical NOT

 

Operators

  1. Arithmetic Operators
    • Unary Operators
      • Eg: +5 , -8 .
    • Binary Operators
      • Eg
        • X = 5+6; (Addition + Operator)
        • X = 20-10; (Subraction - Operator
        • X = 5*3; (Multiplication * Operator)
        • X = 5/3; (Division / Operator)
        • X = 5%3; (Modular % Operator) -remainder
  2. Relational Operators
    • A>B; (Greater than)
    • A<B; (lesser than)
    • A>=B; (Greater than equal to)
    • A<=B; (Lesser than equal to)
    • A==B; (equal to)
    • A!=B; (not equal to)
  3. Logical Operator
    • A && B; (AND)
    • A || B; (OR)
    • !A=A; (NOT)
  4. Increment and Decrements Operator
    • Increment Operator
      • ++x; (pre increments)
      • x++; (post increments)
        • Eg:
        • S++; // ++, as it is single machine instruction (INC) internally.(recommended)
        • S = S+1; // ++, as it wil take two machine cycle internally.(not recommended)
    • Decrements Operator
      • --x; (pre Decrements)
      • x--; (post Decrements)
  5. Short hand assignment Operators
  6. conditional operators (Ternary Operator)

syntax : expression 1 ? expression 2 : expression 3;

  • Rule
    • if expression 1 is true then expression 2 is executed
    • else expression 1 is false then expression 3 is executed
  • Advantage : Using ?: reduce the number of line codes and improve the performance of application.
  • Example Invalid:In this below example this is an error in this line i>45? return(*P): return (*q); We cannot use return keyword in the terenary operators.

     
  • Example Valid : In this below example a is lesser than b . so the b value
  • More examples :
    • Find largest number among 3 numbers using ternary operator

Associativity

Associativity is only needed when the operators in an expression have the same precedence. Usually + and – have the same precedence.

Consider the expression 7 – 4 + 2. The result could be either (7 – 4) + 2 = 5 or 7 – (4 + 2) = 1. The former result corresponds to the case when + and – are left-associative, the latter to when + and – are right-associative.

Usually the addition, subtraction, multiplication, and division operators are left-associative, while the exponentiation, assignment and conditional operators are right-associative. To prevent cases where operands would be associated with two operators, or no operator at all, operators with the same precedence must have the same associativity.

 

 


 

C – Decision Making Branching Looping

Decision making, Branching, Looping statements

Control Statements (Two types)

  • C program is executing sequentially line by line from the first statement to last statement.
  • But we can change this sequentially execution in c, by using control statements
  • Types
    • Unconditional statements (goto statement)
      • It will change the execution order without any checking process
      • Syntax : goto label_name_position; label_name_position : ArunEworld;
      • Rules
        • label should be a valid identifier.
        • label should be end with colon :
        • Does not require to declare a label .
    • Conditional statements (Two types)
      • It will change the execution order with checking process.
      • types
        • Decision making statements(five types)
        • Looping statements (Three types)
      • Note : return statement or return keyword cannot be used with conditional operators(terynary).

Note : C programming language assumes True and False

  • True = non-zero and non-null. // eg: 1, Arun, True
  • False = zero or null. //eg : 0, nil, false

Decision Making statements (five types)

  • It will change the execution order with checking process.
  • types
    • Simple if Statements
    • if- elsestatements
    • else ...if ladder statements
    • nested if statements
    • switch statements

Simple if Statements

  • It will change the execution order from one place to another place (or) skip the execution of group of statements (or) Execute the group of statements with checking one condition.
  • Syntax : if(Boolean_expression) goto label;(or) if(Boolean_expression) statement; (or)
  • Note : statement(s) will execute if the Boolean expression is true.
  • Rules
    • Test conditions should be in brackets ()
    • Test condition should be Relational /Logical expression
    • Should use open { and } close braces, if use group of statements
    • examples
      • if (a<b) big =1;
      • if (a=1) goto ArunEworld;
  • Example Programs
    • Evaluate_mark_of_students_using_if_statement.c
    • Evaluvate_sale_tax_using_if_statement.c

if- else statements

  • Execute the group of statement if test condition is true, else test condition is false it will execute the another group of statements
  • Syntax : if(Boolean_expression) statement_1; else statement_2; (or)

     
  • Rules
    • Test conditions should be in brackets ()
    • Test condition should be Relational /Logical expression
    • Should use open { and } close braces, if use group of statements
    • examples
      • if (a<b) big =b; else big =a;
      • if (a=1) goto ArunEworld; else goto Arun;
      • if (a<b) { s=a+b; d =a-b; } else m = a+b;
  • Example Programs
    • check_given_marks_fail_or_pass_using_if_else_statement.c
    • Biggest_number_using_if_statement.c

else ...if ladder statements

  • else..if is can use to checking more than one conditions, Execute the group of statement if test condition_1 is true, else..if test condition_2 is true it will execute the another group of statements, else above two test condition is false execute another group of statements
  • Syntax :

(or)

  • Rules
    • Test conditions should be in brackets ()
    • Test condition should be Relational /Logical expression
    • Should use open { and } close braces, if use group of statements
    • examples

       

      • if (a>b) goto ArunEworld; elseif(b>a) goto Arun; else goto Eworld;
      • if (a<b) { s=a+b;d =a-b; } eslesif(b>a){ d =a-b; b= a-b;} else {m = a+b; a=a+b;}
  • Example Programs
    • Display_Day_using_if_elseif_statement.c

nested if statements

  • Syntax :
  • Examples : Biggest_of_three_numbers_using_nested-if.c

switch statements

  • group of if..else statements collection is called switch statement.
  • Syntax :
  • Rule
    • Expression must have an integral or enumerated type.
    • Constant-expression must be the same data type as the variable in the switch.
  • Example: Display_Day_using_switch_statement.c

nested switch

  • Syntax :

     

Looping control statements (Three types)

While Loop

  • Syntax : while(condition) //The condition may be any expression, and true is any nonzero
  • value. The loop iterates while the condition is true.

 

Do..while Loop

  • Syntax :

     
  • Rules:
    • condition is true while statement break the do loops

for Loop

  • Syntax :

     
  • Rules:
    • init – The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables.You are not required to put a statement here, as long as a semicolon appears.
    • condition – Next, the condition is evaluated. If it is true, the body of the loop is executed.If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the ‘for’ loop.
    • increment – After the body of the ‘for’ loop executes, the flow of control jumps back up to the increment statement.This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.

nested for loops

  • Syntax :

     

nested while loops

  • Syntax :

     

Infinite Loop

  • A loop running continuously for indefinite number of times is called infinite loop.

Infinite For Loop:


  •  

Infinite While Loop:


  •  

Infinite Do-While Loop:


break

  • Break can appear only with in the looping control and switch statement.
  • The purpose of the break is to bring the control out from the used blocks.
  • Syntax : break;

continue

 

  • Syntax : continue;

Goto Statement

Excersize

After the execution of above statement, the control is transferred to statement number
A. 8
B. 11
C. 16
D. 20

  • Answer: Option B (Explanation: It is a computed GO TO statement. Since J COKE = 3 + 1 = 4, the control is transferred to statement 11).

 


 

 

 


 

C – Bit-wise Operator

Bit wise Operators (Can only used on integrals, no floats and doubles)

Bit-Wise Logic Operator

  • & AND
  • | OR
  • ^ XOR
  • ~ ONE’S COMPLEMENT (Unary Operator)

& AND

| OR

^ XOR

~ ONE’S COMPLEMENT (Unary Operator)

 

Bit-wise Shift

  • << LEFT SHIFT
  • >> RIGHT SHIFT
  • Example : A = 0101 1000 >>1 Output : 000010000 .
  • Reference Videos:
    1. https://www.youtube.com/watch?v=d0AwjSpNXR
  1. Special Operators

 



 

Exercise

1) Which is not a bit-wise operator?

  1. &
  2. |
  3. <<
  4. &&

Ans : 4


2) Predict the output of following program.

  1. c= 12   
  2. c= 10
  3. c= 2
  4. c= 0

Correct answer: 3
c= 2

Bitwise AND (&) operator copies bit(s), if they are exist both of the operands. Here, binary of a is “1010” and binary of b is “0010”. Thus, result of expression (a & b) is “0010” which is equivalent to 2 in Decimal.


3) Predict the output of following program.

  1. I have purchased …:
  2. I have purchased …:Mobile, Lappy
  3. I have purchased …:Mobile,
  4. I have purchased …:Lappy

Correct answer: 2
I have purchased …:Mobile, Lappy

Bitwise OR (|) operator copies bit(s), if they are exist either side of the operands (that means if any bit is exist in any operand). Here, binary of Macro MOBILE (0x01) is “0001” and binary of Macro LAPPY (0x02) is “0010”, then result of the expression item |=MOBILE; will be “0001” and second expression item |=LAPPY; will return “0011”. Thus, both conditions (item & MOBILE) and (item & LAPPY) will be true.


4) Predict the output of following program.

  1. 8,9
  2. 4,5   
  3. 8,8
  4. 4,4

Correct answer: 2
4,5

Value of var is 0x04 (0100), Consider the expression var = var | 0x04 The OR (|) of 0100, 0100 is 0100, hence value will remain 0100. After the expression var |=0x01, value will be 0101 that is 0x05.


5) Predict the output of following program.

  1. 13
  2. d
  3. 22
  4. 10

Correct answer: 1
13

Consider the expression flag &= ~0x02 => flag = flag & (~0x02) => flag = 0x0f & (~0x02) => flag = D => flag =13.


6) Consider the given statement:

int x = 10 ^ 2What will be the value of x?

  1. 5
  2. 6
  3. 7
  4. 8

Correct answer: 4
8

XOR operator (^) copies bit(s), if one operand has 1 and other has 0, consider the given truth table:

Here, binary of 10 is “1010” and binary of 2 is “0010”, then the result of statement (10 ^ 2) will be “1000”, which is equivalent to 8 in decimal.


7) Predict the output of following program.

  1. x= 10
  2. x= 8
  3. x= 12
  4. x= 0

Correct answer: 2
x= 8

The statement x &= ~2; will clear second bit from the value of 10, binary of x is “1010” and the binary of 2 is “0010”, thus this statement will clear second bit and returns “1000” that is equivalent to 8 in Decimal.


8) Which Bitwise Operator can be used to check whether a number is EVEN or ODD quickly?

  1. Bitwise AND (&)   
  2. Bitwise OR (|)
  3. Bitwise XOR (^)
  4. Bitwise NOT (~)

Correct answer: 1
Bitwise AND (&)

Bitwise AND (&) Operator can be used to check whether a number if EVEN or ODD, consider the statement (num & 1), this statement will return 1 if first bit of the number is High (1) else it will return 0. All ODD numbers have their firs bit 1 and ODD numbers have 0.

Consider the following program:

Output


9) Which statement is suitable to check 3rd (count from 0) bit is high (set) or not?

  1. (num & (1<<3))
  2. (num & 0x08)
  3. (num & 0x03)
  4. Both (1) and (2)

Correct answer: 4
Both (1) and (2)

The value of (1<<3) is 8 in Decimal and value of 0x08 is 8 in Decimal, both statements are suitable to check whether 3rd bit of num is High (set) or not.

Consider this program:

Output

Binary of 15 is: 1111 & Binary of 7 is: 0111, thus in first case 3rd bit is high and in second case 3rd bit is low. [Count from 0]


10) Left shift (<<) and Right shift (>>) operators are equivalent to _____________ by 2.

Choose the correct words…

  1. Multiplication and Division
  2. Division and Multiplication
  3. Multiplication and Remainder
  4. Remainder and Multiplication

Correct answer: 1
Multiplication and Division

Left shift by 1 return the multiplication by 2 and Right shift by 1 return the division by 2.

Consider this program:

Output



 

if you have any queries call us +918300026060 or WhatsApp Dismiss

0

Your Cart

%d bloggers like this: