C language smart notes for MCQ


C-----notes
  • ·        Integer constant range -32768 to 32767
  • ·        Real constant range -3.4e38 to 3.4e38(must be decimal)
  • ·        Character constant is single digit single alphabet with single inverted commas ‘A’
  • ·          It is the combination of  1 to 31 digits, alphabet,underscores
  • ·        Keywords are the words that already defined in c compiler
  • ·        Keyword must not same as like variables
  • ·        There are 45 operators in c and there is no operator for exponentiation
  • ·        the variables and constants together are called ‘operands’
  • ·        b = 3 ^ 2 ; this is not a valid statement

If we want to do the exponentiation we can get it done this

way:

#include <math.h>

main( )

{

int a ;

a = pow ( 3, 2 ) ;

printf ( “%d”, a ) ;

}

  • ·        left to right associativity used for the equal priority operator(* /)
  • ·        real constants are exponential and fractional
  • ·        Note that in C a non-zero value is considered to be true, whereas a is                   considered to be false
·        Eg. if ( 3 + 2 % 5 )

printf ( "This works" ) ;

  • ·        C allows usage of three logical operators, namely, &&, || and !.These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively

  • ·        Using nested if else statement requires lots of accuracy in pgm so it is easy to use logical operators instead of nested statements

·        Priorities
·       
! Logical NOT (highest Priority)
* / % Arithmetic and modulus
+ - Arithmetic
< > <= >= Relational
== != Relational
&& Logical AND
|| Logical OR
= Assignment

  • ·        Conditional operator:
  • ·        If?then:else that if the 1st exp. Is true then print 2nd exp. Else print 3rd exp.
  • ·        Don’t use ‘;’ after the if statement.
  • ·        && and || are binary operators, whereas, ! is a unary operator
  • ·        In C every test expression is evaluated in terms of zero and on-zero values. A zero value is considered to be false and a on-zero value is considered to be true
  • ·        Assignment statements used with conditional operators must be enclosed within pair of parenthesis.

·        PGM      
main( )

{

int k = 35 ;

printf ( "\n%d %d %d", k == 35, k = 50, k > 40 ) ;

}

  • ·        In the above like pgms always take right to left string for printf() that is 1)35>40 2)k=50 3) 50 == 35 so output become  0 50 0
  • ·        += is a compound assignment operator. It increments the value of i by 1. Similarly, j = j + 10 can also be written as j += 10. Other compound assignment operators -=,*=,/ = and %=
  • ·        While (i++ < 10) it means that first compare and then increment(POST INCREMENT)
  • ·        While (++i<10) it menas that first increment and then compare(PRE INCREMENT)
  • ·        for ( initialise counter ; test counter ; increment counter )
  • ·        Instead of i = i + 1, the statements i++ or i += 1 can also be used
  • ·        for(   ;    ;   )  semicolons must be present in the for loop, independent of expression present or not (but exp. Must presents in the body loop of the for )
  • ·        a for loop can occur within a while loop, or a while within a for
  • ·        Multiple Initialisations in the for Loop is possible but not ate testing & incrementing side Ex. for ( i = 1, j = 2 ; j <= 10 ; j++ )
  • ·        do{
  •                        }while(     )
  • ·       if break; statement is occur then program is directly terminated
  • ·       if continue; statement is occur then program is go back to beginning of the loop
  • ·       how the post decrement will work???
  • ·       Here I pgm
  • ·       main( )
  • ·       {
  • ·        int x = 4, y = 3, z ;
  • ·        z = x-- -y ;(here 1st z=4-3=1 ,2nd y=3,& 3rd is [after operation is done now we decrement x by 1 therefor ans. Is x=3])
  • ·        printf ( "\n%d %d %d", x, y, z ) ;
  • ·       }
  • ·       Break statement must be used within the loop
  • ·       Whenever there while(i=any digit) is occur then loop is goes at infinity times in while brackets there must be condition
  • if the program contains more than one function that is other than main function then at this program return() statement always return to the calling function 
  • If we want that a called function should not return any value, in that case, we must mention so by using the keyword voidas shown below.
    void display( )
    {
    printf ( "\nHeads I win..." ) ;
    printf ( "\nTails you lose" ) ;
    }
  • A function can return only one value at a time. Thus, the following statements are invalid.
    return ( a, b ) ;
    return ( x, 12 );
  • Output of contains of printf() might be right to left refer following program
  • int a = 1 ;
    printf ( "%d %d %d", a, ++a, a++ ) ;// 
  • execution of output is right to left 3 3 1
  • This is because C’s calling convention is from right to left
  • #include <stdio.h>
    int i = 10, j = 20 ;
    printf ( "%d %d %d ", i, j ) ;
    printf ( "%d", i, j ) ;
  • this program will compile independent upon how many variables are used in the printf by seeing above pgm there no variable for last "%d" then also pgm will get compile but last %d get a garbage value 
  • Any C function by default returns an int value. 
  • Return function only returns the int value not any float like other 
  • In some programming situations we want that a called function should not return any value. This is made possible by using the keyword void
  • whenever we called a function and passed something to it we have always passed the ‘values’ of variables to the called function. Such function calls are called ‘calls by value’.
  • alternative of scanf is fgets(var.,sizeof(var.),stdin);
  • buffer overflow problem is occur in scanf so that we have specify the limit and should be entered within the limit otherwise segmentation fault may occur
  • disadvantage of fgets it allows you to enter one string into buffer.

  • POINTERS
  • "&" represent the address of the constant.
  • arr[i] and *(arr+i) are same
  • array is started with zero because of pointer arithmatic
  • char *cards = "JQK";===>this variable cannot be modified the string
  • char cards[ ] = "JQK"==>here array means new string is created so that we can modify the contents of string
  • whenever program loads into memory then constants as like literals or strings are stored into read only memory(ROM) 
  • as soon as array is declared contents of read only memory are copied into stack and there it will we can modify it.

  • specifying the location of the that constant is solely dependent on the computer
  • "*" represent the value of the address also called indirection
  • main( )
    {
    int i = 3 ;
    printf ( "\nAddress of i = %u", &i ) ;
    printf ( "\nValue of i = %d", i ) ;
    printf ( "\nValue of i = %d", *( &i ) ) ;
    }

    The output of the above program would be:
    Address of i = 65524
    Value of i = 3
  • int *alpha ;
    char *ch ;
    float *s ;
    Here, alpha, ch and s are declared as pointer variables i.e variables capable of holding addresses
  • *(var.) "value of address"
  • "%p" in printf is a format specifier and is used to define address of variable in hexadecimal format  
  • A self contained block of statements that perform a coherent task of some kind is called a Function
  • Following are the data types we are using in the C lang
  • For static storage class only integer values are allowed
  • Pass by value cannot change the actual parameter value
  • fwrite write the data into binary format 
  • fgetc and fputc is in readable format
  • fread will convert binary data from fwrite into readable format
  • stdout,stdin,stdarr are streams
  • memory is shared among all 
  • If fopen() fails to open file some reason like wrong path, disk full then it return NULL pointer
  • fclose()---->first flush any changes into buffer associated with FILE structure and then delete the FILE structure which is created by fopen() for that file. it return EOF to indicate error,otherwise returns 0
  • fgetc()----> as soon as last file is read then it return NULL pointer
  • fputc()----> as soon as file write is complete then it will return the ASCII value of the last character
  • fopen contains file path and mode of that file
  • typecasting just converts from one data type to another.
  • ASCII value of '\n'(new line)=10
  • ASCII value of '\r'(carriage return)=13
  • strstr() it is a standard c library function in string.h this function signature contain character pointer as first index and another index represent that needle is in haystack or NULL is not present or just we can say that
  • char *strstr (const char *s1, const char *s2);
    
    Parameters:
    s1: This is the main string to be examined.
    s2: This is the sub-string to be searched in s1 string.


  • char s1[] = "Just read it"
  • char s2[] ="read"
  • char* p = strstr(s1, s2);
  • printf("%s",p);
  • output will be 'read it'
  • Return Value: This function returns a pointer points to the first character of the found s2 in s1otherwise a null pointer if s2 is not present in s1. If s2 points to an empty string, s1 is returned.
how to declare and use pointer to 2D array?
int array [i][j];
                                                                int *ptr;
ptr = &array;
WHAT IS MEMORY LEAKAGE AND DANGLING POINTER
  ----> memory leak is occur when we forgot to deallocate the allocated memory
compiler of c deallocate memory automatically
------->dangling pointer is pointer which is pointing to such memory location whose object is either deleted or de-allocated without modifying the value pointer 

Bit fields:
        e.g.  p:2 it means 2 bits must be on for constant 5 ===>0101
      result will be ====>1
bit filed could not declared as float


  • \b it backspaces one character
  • \r it take cursor on current character whatever which of already present they goes at last 
  • printf() returns the total number of characters written to stdout. 
  • int main(void) 
  • if (printf("Hello World")) ; 
  • Why should we use pointers in C?
  • to get address of variable
  • to perform pass by reference in C:pointers allow different functions to share and to modify the local variable.
  • to implement linked data structures like linked list and binary tree.

  • What is NULL pointer?
  • it is used to indicate pointer does not point to valid memory location.
  • we should initialize the NULL if we don't know value at declaration time 
  •   
Extern Keyword in C:
  • extern keyword is used for only declaration.
  • when we use extern keyword with variable data type then it is onlv declared not defined
  • it means memory is not allocated to that variable 
  • as memory is not allocated it can't be used inside the any function 
  • but if you initialiaze the extern keyword with variable then memeory gets allocated according to C standard. so that it will not give any compile time error if it is used inside any function.
  • also if defination of variable is another file which is somefile.h then that variable gets memory allocated.(expicitly extern keyword just declare the variable)
  • By default, the declaration and definition of a C function have “extern” prepended with them.
  • Same is the case with the definition of a C function (Definition of a C function means writing the body of the function). Therefore whenever we define a C function, an extern is present there in the beginning of the function definition.
  • as the extern extends the visibility to the whole program, the functions can be used (called) anywhere in any of the files of the whole program provided the declaration of the function is known. (By knowing the declaration of the function, C compiler knows that the definition of the function exists and it goes ahead to compile the program). 
  • remember that declaration can be done any number of times
  • defination is superset of declaration

  • In case of function declaration, the program knows what are the arguments to that functions, their data types, the order of arguments and the return type of the function. So that’s all about the declaration
  •  it should be obvious that a variable/function can be declared any number of times but it can be defined only once. (Remember the basic principle that you can’t have two locations of the same variable/function).

Code optimization in C
  • Avoid calculation in loops
  • Use left operations << for multiplication(a<<b-->a*2^b)
  • Use right operations >> for division(a>>b->a/2^b)
  • Use of Register variables
  • Avoid pointer dereference
  • Prefer preincrement instead of post increment

Comments

Popular posts from this blog

Basic Electronics

DATA STRUCTURES IN C.

RTOS(Real Time Operating System) notes