C Programming Structure

Leave a Comment
  • C is Case sensitive. That is the compiler differentiates between lower and upper case characters.
  • Comments can be placed in C two ways
    Comments begins with /* and ends with */.
  • The standard C comments begin with a double slash.
  • All C programs begin with main (). It tells the compiler where the program begins. One can type main () where on the Screen.
  • In C, the semicolon; is used as statement terminator. The statements that begins with # (for example #include are not ended with semicolon because they are not treated as statements. They are preprocessor directives.
  • In C a variable must be declared before its use. In C all variables must be declared before any executable statement.
  • By declaring a variable compiler allocates memory for that variable. The number of bytes allocated for a variable depends upon its type.
  • C has the following 9 integer types. They are differentiated by the way the memory allocated to them. char, signed char, unsigned char, short int, int, long int, unsigned / short int, unsigned int, unsigned long int
  • In C characters are treated as integer type. Because characters are internally represented by their ASCII (AMERICAN STANDARD CODE FOR INFORMATION INTERCHANGE) values.
  • The size of (variable name or variable type) operator gives the size of data types in bytes.
  • The ranges of all the integer types of your machine arc preserved by compiler under the header <limits.h> Range can be known by constants variables declared in limits.h header. The variable constants are CHAR _ MIN, CHAR_MAX, INT_MIN, INT_MAX, LONG_MIN, LONG_MAX, ULONG_MIN, ULONG_MAX,etc.
  • Arithmetic operators of C are: (negate), *(multiply), / (divide), % (Remainder or Modulo), +(Add), -(Minus).
  • C allows chained assignments. Chained assignments cannot be used as an initialization in a declaration. A=b=c=d;
  • The relational operators of C are < , <=,  == (equal to not an assignment), > , >=, != (not equal to)
  • The relational operator expression returns an integer value. This integer value is one if the expression is true and zero if the expression results a false value.
  • Conditional operator or ternary operator: Simple conditions can be carried out by ternary operator.
    The format of the conditional operator is
    Var = expression 1? expression 2 : expression 3;
  • When evaluating a conditional expression first expression 1 is evaluated. If expression one is true then expression 2 is assigned to var. If expression one is false then expression 3 is assigned to var.
  • Logical operators of C are && (and) , \\ (OR) , ! (NOT)
  • C allows the following types of assignment statements. =, +=, -=,  *=,  /= , %= (arithmetic operator =) .
  • A symbolic constant is a name that substitutes for a sequence of characters or text. The text may represent a numeric constant, which can be either integer or real, a character constant or a string constant When a program is compiled, each occurrence of a symbolic constant is replaced by its corresponding character sequence. Symbolic constants are usually defined at the beginning of the program. The format of the symbolic constant is
    #define name text
    1. Symbolic constant statement does not end with a semicolon, since a symbolic constant definition is not a true c statement.
    2. #define feature of symbolic constant is one of the several features included in the C Preprocessor.
     
  • C has the following real types. They are differentiated by the way the memory allocated to them. float, double, long double.
  • The cast operator of C is used to convert the value of variables from one type to another, The format of the cast operator is
    A-(type of the converted)variable or expression;
  • Compound statement. A compound statement is a sequence of statements that is enclosed in curly braces. {st1; st2; }. A compound statement may contain any number of compound statements.
  • The if-else statement is used to carry out a logical test on the given conditional expression and then take one of the two possible actions, depending upon the outcome of the condition. The else portion of the if-else statement is optional. The format of the if-else statement is
    if (conditional expression)
    {
      statement
    }

    If (conditional expression)
    {
    st1; st2;
    }
    else
    {
    st3; st4;
    }

    The conditional expression must be placed in parentheses.
  • The switch statement causes a particular group of statements to be chosen from several groups. This
    selection is based on the value of the expression placed within the switch statement.
    switch (expression)
    {
    case 1: st1;
    break;
    case 2: st1;
    break;
    default: stn;
    }
    1. The expression must be either integer or character type.
    2. If the expression value is not in the case list then default value is assigned.
    3. The break and default statements are optional. Break statement causes control to be transferred to the outside of the switch statement.
    4. If the break statement is not used then all the following statements, which appear after the current case statement will be executed.
    5. The default statement can appear anywhere in the case list.
     
  • C has the following types of loop statements.
    While (condition)
    {
    stl; st2; stn;
    }

    for (initializations; continuation condition; update statements)
    {
    st1; st2; stn;
    }

    do
    {
    st1; st2; stn;
    }
    while (condition);

    The minimums number of times of execution of a while or for loop will be zero. (If initial condition is false).

Properties of a good program

  • Correctness
  • Interaction
  • Easily modifiable
  • Readability
  • Efficiency . Use standard library functions ( For the reason of readability & efficiency)
  • Generality.

Format of a `C` program

# include ( Preprocessor Directive main C)
 {
 Declarations;
 Statements;
 }

Declarations

  • Creating a variable
  • Allocating memory for a variable

int x;
x =10

Initialization

Process of a storing initial value in a variable
X=10: (X assigned 10)

Properties of a Variable

  • Name, data (value), and address
  • Assignment Operator (Example: x=10)
  • Input statement
  • Variable can be declared in a one statement like int x,y,z;

Constant declaration

#Define pi 3.146

#include
#define pi 3.146
{
float area, radius;
printf(“Enter value of radius:”);
scanf(“%f”, & radius);
area = pi * radius * radius;
printf(“Area of circle: %f\n”, area);
}
Readability can be achieved by selecting meaningful variables.

C Programming Structure

0 comments:

Post a Comment