Presentation is loading. Please wait.

Presentation is loading. Please wait.

KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY

Similar presentations


Presentation on theme: "KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY"— Presentation transcript:

1 KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY
Programming in C (CS-1001) KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY School of Computer Engineering Strictly for internal circulation (within KIIT) and reference only. Not for outside circulation without permission 3 Credit Mr. Rajat Kumar Behera - Associate Professor

2 Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Hrs 12 Additional Features 3 Command line arguments Bitwise operators Macros KIIT UNIVERSITY

3 Command Line Arguments
It is possible to pass some values from the command line to C programs when they are executed. These values are called command line arguments and many times they are important for the program especially when there is a need to control the program from outside instead of hard coding those values inside the code. The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program. Following is a simple example which checks if there is any argument supplied from the command line and take action accordingly – #include <stdio.h> int main( int argc, char *argv[] ) { if( argc == 2 ) printf("The argument supplied is %s\n", argv[1]); } else if( argc > 2 ) { printf("Too many arguments supplied.\n"); } else printf("One argument expected.\n"); return 0; KIIT UNIVERSITY

4 Command Line Arguments cont…
#include <stdio.h> int main ( int argc, char *argv[] ) { if ( argc != 2 ) /* argc should be 2 for correct execution */ printf( "usage: %s filename", argv[0] ); /* We print argv[0] assuming it is the program name */ } else // We assume argv[1] is a filename to open FILE *file = fopen( argv[1], "r" ); /* fopen returns 0, the NULL pointer, on failure */ if ( file == 0 ) printf( "Could not open file\n" ); int x; while ( ( x = fgetc( file ) ) != EOF ) printf( "%c", x ); fclose( file ); return 0; /* read one character at a time from file, stopping at EOF, which indicates the end of the file. Note that the idiom of "assign to a variable, check the value" used below works because the assignment statement evaluates to the value assigned. */ KIIT UNIVERSITY

5 Bitwise Operators KIIT UNIVERSITY
C provides six bitwise operators for manipulating the individual bits in an integer quantity. Bitwise operators expect their operands to be integer quantities and treat them as bit sequences. Assume that variable A holds 60 and variable B holds 13, then - Operator Description Example & Bitwise AND compares the corresponding bits of its operands and produces a 1 when both bits are 1, and 0 otherwise. (A & B) = 12 i.e., Bitwise OR compares the corresponding bits of its operands and produces a 0 when both bits are 0, and 1 otherwise. (A ∣ B) = 61 i.e., ^ Bitwise exclusive or compares the corresponding bits of its operands and produces a 0 when both bits are 1 or both bits are 0, and 1 otherwise. (A ^ B) = 49 i.e., ~ Bitwise negation is a unary operator that complements the bits in its operands. (~A ) = 61 i.e., << Bitwise Left Shift moves the left operand (towards left) values by the number of bits specified by the right operand A << 2 = 240 i.e., >> Bitwise Right Shift moves the left operand (towards right) by values by the number of bits specified by the right operand A >> 2 = 15 i.e., KIIT UNIVERSITY

6 Macros KIIT UNIVERSITY Macros Definition What is does?
A Macro is typically an abbreviated name given to a piece of code or a value. Have a look at the following program. #define UPPER 25 int main( ) { int i ; for ( i = 1 ; i <= UPPER ; i++ ) printf ( "\n%d", i ) ; return 0; } Macros Definition What is does? During preprocessing, the preprocessor replaces every occurrence of UPPER in the program with 25. UPPER is called macro templates and 25 is called their macro expansion When we compile the program, before the source code passes to the compiler it is examined by the C preprocessor for any macro definitions. When it sees the #define directive, it goes through the entire program in search of the macro templates; wherever it finds one, it replaces the macro template with the appropriate macro expansion. Only after this procedure has been completed is the program handed over to the compiler. In C programming it is customary to use capital letters for macro template. This makes it easy for programmers to pick out all the macro templates when reading through the program. Note that a macro template and its macro expansion are separated by blanks or tabs. A space between # and define is optional. Remember that a macro definition is never to be terminated by a semicolon. KIIT UNIVERSITY

7 Macros cont… KIIT UNIVERSITY
A #define directive is many a times used to define operators as shown below. #define AND && #define OR || int main( ) { int f = 1, x = 4, y = 90 ; if ( ( f < 5 ) AND ( x <= 20 OR y <= 45 ) ) printf ( "\nYour PC will always work fine..." ) ; else printf ( "\nIn front of the maintenance man" ) ; return 0; } KIIT UNIVERSITY

8 Macros cont… KIIT UNIVERSITY
A #define directive could be used even to replace a condition, as shown below. #define AND && #define ARANGE ( a > 25 AND a < 50 ) int main( ) { int a = 30 ; if ( ARANGE ) printf ( "within range" ) ; else printf ( "out of range" ) ; return 0; } KIIT UNIVERSITY

9 Macros cont… KIIT UNIVERSITY
A #define directive could be used to replace even an entire C statement. This is shown below. #define FOUND printf ( "The Yankee Doodle Virus" ) ; int main( ) { char signature ; if ( signature == 'Y' ) FOUND else printf ( "Safe... as yet !" ) ; return 0; } KIIT UNIVERSITY

10 Macros with arguments KIIT UNIVERSITY
The macros that we have used so far are called simple macros. Macros can have arguments, just as functions can. Here is an example that illustrates this fact. #define AREA(x) ( 3.14 * x * x ) int main( ) { float r1 = 6.25, r2 = 2.5, a ; a = AREA ( r1 ) ; printf ( "\nArea of circle = %f", a ) ; a = AREA ( r2 ) ; return 0; } Output Area of circle = Area of circle = KIIT UNIVERSITY

11 Thank You KIIT UNIVERSITY


Download ppt "KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY"

Similar presentations


Ads by Google