CS19002 Programming and Data Structures | Autumn/Spring semester |
Indentation does not only mean that a few lines of your code leave extra blank spaces on their left. Certain conventions regarding these spaces are necessary for this indentation to be a proper indentation. This also includes proper placement of the curly braces ({ and }). A sequence of C statements enclosed within a matching pair of braces is called a block.
Fix an indentation amount for your codes. In industry, people usually use a tab as the indentetation amount. Since a tab (normally) means eight characters, programs with large levels of nesting of blocks may face difficulty with such a large indentation amount. You may instead use 2 or 3 spaces as the indentation amount.
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #include "mydefs.h" float myRoutine ( int n , float f ) { int k; Leave the indentation amount in every line inside this block. ... ... ... return(3.1415926535); } mySecondRoutine ( ) { Declare variables Declare variables Declare variables Indented instruction Indented instruction ... Indented instruction } int main ( int argc , char *argv[] ) { Declare variables Declare variables Declare variables Declare variables Indented instruction Indented instruction ... Indented instruction }
#include <stdio.h> int fibonacci1 ( int n ) { if (n < 0) { printf("Error: Non-negative argument expected.\n"); return(-1); } if (n <= 1) return (n); else return (fibonacci1(n-1) + fibonacci1(n-2)); } int fibonacci2 ( int n ) { int this, prev, next, i; if (n < 0) { printf("Error: Non-negative argument expected.\n"); return(-1); } if (n == 0) return (0); prev = 0; this = 1; for (i=2; i<=n; i++) { next = this + prev; prev = this; this = next; } return (this); } int main () { int response, n, fibn; while (1) { printf("\n\n"); printf("Enter 1 for calling the recursive function,\n"); printf(" 2 for calling the iterative function,\n"); printf(" 0 to terminate.\n"); printf("Your choice : "); scanf("%d",&response); /* Echo response for printout */ if (response == 0) exit(0); if ( (response == 1) || (response == 2) ) { printf("n = "); scanf("%d",&n); /* Echo n for printout */ if (response == 1) { printf("Calling the recursive function...\n"); fibn = fibonacci1(n); } else { printf("Calling the iterative function...\n"); fibn = fibonacci2(n); } printf("Fib(n) = %d\n", fibn); } } }
int x,y,i,n; x = 123; y = x * x * x; x = 2; n = 30; y = 1; for (i=1; i<=n; i++) y *= x;Use the function pow in the math library, when and only when one of the operands is not an integer, like pow(3.1416,2) or pow(2,3.1416) or pow(2.7183,3.1416). Calling pow to compute integral powers of integers may even lead to erroneous results.
Data type | Minimum value | Maximum value | ||
---|---|---|---|---|
char | -128 | 127 | ||
unsigned char | 0 | 255 | ||
short | -32768 | 32767 | ||
unsigned short | 0 | 65535 | ||
int | -2147483648 | 2147483647 | ||
unsigned int | 0 | 4294967295 | ||
long | -2147483648 | 2147483647 | ||
unsigned long | 0 | 4294967295 | ||
long long | -9223372036854775808 | 9223372036854775807 | ||
unsigned long long | 0 | 18446744073709551615 | ||