C5 C 5 Odds and Ends

Document Sample
scope of work template
							                                 C 5: Odds and Ends
    Review

    malloc - may create arrays or self-referential structures var = (int*)
     malloc(sizeof(int),30);)

       to change the array that an array points to, its address must be passed

    preprocessor (macros, include, conditional compilation)



Synopsis

 Advanced Printing

 Bit Operators

 Separate Compilation

 Rearview
    References
C5 – Odds and Ends                                                                1
Pratt & Zelkowitz: 1.5, A.2




C5 – Odds and Ends            2
                     Advanced Printing
%d                   integer(digit)

%3d                  integer with width 3

%u                   unsigned integer (mainly
                     with pointers)

%x                   print in hexadecimal

%o                   print in octal

%3.5f                float with at least 3
                     characters before the
                     decimal, and 5 after.

%e                   float in scientific notation

%g                   whichever of %f and %e is
                     shorter

%c                   character

C5 – Odds and Ends                                  3
%s                            string

When scanning a double, %lf must be used, i.e.
main()
{
    double store,
    scanf("%lf",&store);
}




C5 – Odds and Ends                               4
Finally, when a string or character is read, often the `\n` remains, it must sometimes be
removed for following output to be successfully
void scann()
{
    char c='w';
    for(;c!='\n';scanf("%c",&c));
}


main()
{
    char c, str[100];
    scanf("%s",str);


C5 – Odds and Ends                                                                          5
     scann();
     scanf("%c",&c);
}

or
main()
{
     char c, str[100];
     scanf("%s",str);
     while(getchar()!='\n');
     /* getchar() reads one character from the
            keyboard and returns its value */
     scanf("%c",&c);
}

or

C5 – Odds and Ends                               6
#define CLEAR while(getchar()!='\n')
main()
{
    char c, str[100];
    scanf("%s",str);
    CLEAR;
    scanf("%c",&c);
}




C5 – Odds and Ends                     7
                                       Bit Operators
C supports a number of bitwise operators, these are



&                    bit and               1100 & 1010 =
                                           1000

|                    bit or                1100 |1010 = 1110

^                    bit exclusive or      1100 ^ 1010 = 0110

~                    bit not               ~ 1100 = 0011

<<                   bit left shift        1100 << 1 = 1000

>>                   bit right shift       1100 >> 1 = 0110




C5 – Odds and Ends                                              8
main()
{
    int a,b;
    unsigned c=1; /* binary 0000…00001 */
    a= 9;            /* binary 0000…0001001 */
    printf("%d ", a & 3);           /* 3=0…0011, prints 1 */
    c=a | 0x18;           /* hex18=0…00011000 */
    printf("%d ",c); /* prints 25=0…011001 */
    a=1;
    printf("%d ",a<<2); /* prints 4 */




C5 – Odds and Ends                                             9
    printf("%d ",a<<sizeof(int)*8-1);
                        /* prints   -2147483648 */
    printf("%d %u ",c<<sizeof(int)*8-1,
                              c<<sizeof(int)*8-1);
                 /* prints   -2147483648, 2147483648 */
    printf("%u ",a<<sizeof(int)*8-1);
                 /* prints 2147483648 */
    printf("%u ",077>>3); /*077 is octal 77=111111
                 prints 7=111 */
}




C5 – Odds and Ends                                        10
                             Separate Compilation
Libraries can be created. To do so, a header file consisting of both constants and/or the
signatures of all subprograms in a particular file must be created. Header files usually
end in '.h'
-------- complex.h --------
struct complex{
    float real;
    float imag;
};
typedef struct complex complex;


complex add(complex, complex );
complex minus(complex, complex);
--------------------------

C5 – Odds and Ends                                                                      11
The actual code for the library must include the header file and also the bodies of the
subprograms
-------- complex.c --------
#include "complex.h"            /* so complex.c knows what a
                                        struct complex is */
complex add(complex a, complex b){
    complex final;
    final.real=a.real+b.real;
    final.imag=a.imag+b.imag;
    return final;}
complex minus(complex a, complex b){
    complex final;
    final.real=a.real-b.real;
    final.imag=a.imag-b.imag;
    return final;}
C5 – Odds and Ends                                                                        12
--------------------------

The main program may now use the

new library.
-------calc.c----------------
#include <stdio.h>       /* in the standard place */
#include "complex.h" /* in the current directory */


main(){
    complex one,two,three,four;
    one.real=2.6; one.imag=1.7;
    two.real=4.3; two.imag=1.2;
    three=add(one,two);
    four=minus(one,two);}
-------------------------------

C5 – Odds and Ends                                     13
To create the final object file,

the following commands are issued
gcc -c complex.c

creates `complex.o’ with the object code of complex.c
gcc -c calc.c

creates `calc.o’ with the object code of calc
gcc -o calc calc.o complex.o

combines the two object files into one




C5 – Odds and Ends                                      14
                                         Rearview
C's primary goal is run-time efficiency and writeability. It does this via

 run-time efficiency

            not checking correctness of program while executing (array bounds,
           pointers)

            using pointers to manipulate arrays

            not passing information out of a subprogram

            preprocessor

 writeability

            many shortcuts (*=, int, for)

            strong type coercion




C5 – Odds and Ends                                                                15
C5 – Odds and Ends   16
                                        Review
 printing (extra options for printf and scanf)

 bitwise operators

 separate compilation

 rearview (+efficiency, +writeability, -error checking, -readability)




C5 – Odds and Ends                                                       17

						
Related docs
Other docs by wanghonghx
025-2008+bipolar+disorder+rh
Views: 4  |  Downloads: 0
pag03latina
Views: 3  |  Downloads: 0
Semicolon_HANDOUT[1]
Views: 108  |  Downloads: 0