Introduction of C programming language

Programming Rules :-

A programmer while writing a program should follow following rules.
  1. Every program should have main() function.
  2. C statements should be terminated by a semi-colon (;). At some place, a comma operator is permitted. If only a semi-colon is placed it is treated as a statement. For example:-
                             while(condition)
                             ;
    The above statement generates infinite loop. Without semi-colon the loop will not execute.
  3. An unessential semicolon if placed by the programmer is treated as an empty statement.
  4. All statements should be written in lowercase letters. Generally, uppercase letter are used only for symbolic constants.
  5. Blank space my be inserted between the words. This leads to improvement in the read-ability of the statements. However this is not applicable while declaring a variable, keyword, constant and function.
  6. It is not necessary to fin the position of statement in the program; i.e. a programmer can write the statement anywhere between the two braces following the declaration part. The user can also write one or more statements in one line separating them with a semi-colon. Hence it is often called a free-from language. The following statements are valid.
    a=b+c;
    d=b*c;
    or
    a=b+c; d=b*c;
  7. The opening and closing braces should be balanced, i.e. if opening braces are four; closing braces should also be four.

#include<stdio.h>   :-

                                                      is directive to to the C preprocessor. Line beginning with # are processed by the preprocessor before compilation. #include<stdio.h> tells the preprocessor to include the contents of the standard input/output header(<stdio.h>) in the programs. 

Note:- White-Space charactors are normally ignored by the compiler.

The Main function   :-

                                                      int main() is part of every program. The parentheses after main indicate that main is a program building block called a function.  C programs contain one or more functions, one of which must be main. Every program in C begins executing at the function main. Function can return information. The keyword int to left of main indicates that main "returns" an integer(whole number) value.
                                    A left brace, {, begins the body of every function. A corresponding right brace "}" ends each function. This pair of braces and the portion of the program between the braces is called a block. The block is an important program unit in C.

An Output Statement  :-

                                                            printf("Welcome to Smartprogrammeron.blogspot.in"); 
instructs the computer to perform an action, namely to print on the screen the string of characters marked by the quotation marks. A string is sometimes called a Character string, a message or a literal. The entire line, including the printf function (the 'f' stands for "formatted"), its argument within the parentheses and the semicolon (;), is called statement. Every statement must end with a semicolon ( also known as the statement terminator ),. When the preceding printf statement is executed, it prints the message whatever you entered in between " "(double quotes). The characters normally prints exactly as they appear between the double quotes in the printf statement.

Escape Sequences  :-

                                                  Notice that the characters \n were not printed on the screen. The backslash ( \ ) is called an escape character. In indicates that printf is supposed to do something out of the ordinary. When en-counting a backslash in staring, the compiler looks ahead at the next character and combines it with the backslash to form an escape sequence. The escape sequence \n means newline. When a newline appears in the string output by a printf, the newline causes the cursor to position to the beginning of the next line on the screen. Some common escape sequences are listed below:-


   Because the backslash has special meaning in a string , i.e., the compiler recognizes it as an escape character, we use a double backslash (\\) to place a single backslash in a string. Printing a double quote also presents a problem because double quotes mark the boundaries of a string -- Such quotes are not printed. By using the escape sequence \" in a string to output by printf, we indicate that printf should display a double quote.

The scanf function and Formatted Input  :-

                                                                                                              The statement scanf("%d",&a); uses scanf(the "f" stands for formatted") to obtain a value from the user. The function reads from the standard input, which usually the keyboard. This scanf has two arguments, "%d" and &a. The first, the format control string, indicates the type of data that should be entered by the user. The %d conversion specifier indicates that the data should be an integer type(the letter d stands for "decimal integer"). The % in this context is treated by scanf (and printf as we'll see) as a special character that begins a conversion specifier. The second argument of scanf begins with an ampersand (&) -- called the address operator -- followed by the variable name. The &, when combined with  the variable name, tells scanf the location (or address) in memory at which the variable "a" is stored. The computer then stores the value that the user enters for "a" at that location. The use of ampersand (&) is often confusing to novice to  programmers or to people who have programmed in other languages that do not require this notation. For now, just remember to precede each variable in every call to sacnf with an ampersand.
                                                                           When the computer executes the preceding scanf, it wait for the user to enter a value for variable "a", The user responds by typing an integer, then pressing the Enter Key to sent the number to the computer. The computer then assigns this number, or value, or variable, to the variable "a". Any subsequent references to "a" in this program will use this same value. Functions printf and scanf facilitate interaction between the user and the computer. Because this interaction resembles a dialogue,. it's often called interactive computing.

Assignment Statement  :-

                                                                The assignment sum=a+b; calculates the total of variables a and b and assigns the result to variables sum using the assignment operator =. The statement is read as, "sum gets the value of a + b". Most calculations are performed in assignments. The = operator and the + operator are called binary operator because each has two operands. The + operator's two operands are a and b. The = operator's two operands are sum and the value of the expression a + b.

Printing with control String  :-

                                                                            print("Sum is %d",sum) calls function printf to print the literal sum fillewed by the numerical values of variable sum on the screen. This printf has two arguments, "Sum is %d\n" and sum. The first argument is the format control string. It contains some literal characters to be displayed, and it contains the conversion specifier %d indicating that an integer will be printed. The second argument specifies the value to be printed. 
Note:- The conversion specifier for an integer is the same in both printf and scanf -- this in the case for most C data types.

Calculation in printf Statments  :-

                                                                                  Calculation can also be performed inside printf statements. We could have combined the previous two statements into the statement
printf("Sum is %d" a+b);

Memory Concepts  :-

                                                         Variable name such as a, b and sum actually correspond to location in the computer's memory. Every variable has a name, a type  and  a value.
is executed, the value entered by the user is placed into a memory location to which name "a"  has been assigned. Suppose the user enter the number 34 as the value for integer "a". The computer will place 34 into location "a".
Note:- Whenever a value is placed in a memory location the value replaces the previous value in that location; thus this process is said to be destructive.

Arithmetic in C

Most C programs perform calculation using the C arithmetic operators. Note the use of various speical symbols not used in algebra. the asterisk (*) indicates multiplication and the percent sign (%) denotes the remainder operator, which is introduced below, In algebra, to multiply a times b, we simply place these single - letter variable names side by side , as in ab. In C, however, if we were to do this, ab would be interpreted as a single two letter name (or identifier). Therefore C(and many other programming languages) require that multiplication be explicitly denoted by using the * operator, as in a*b. The arithmetic operators are all binary operators. For example, the expression 3 + 6 contains the binary operator + and the operands 3 and 7.

Arithmetic Expressions in  Straight - Line Form  :-

                                                                                             Arithmetic expression in C must  be written in straight line form to facilitate entering programs into the computer. This expression such as " a divide by b "
must be written as a/b so that all operators and operands appear in a straight line. The algebraic notation 
a
-----
b
is generally not acceptable to compiler although some special - purpose software packages do  support more natural notation for complex mathematical expression.

parentheses for Grouping Sub-expressions  :-

                                                                                       Parentheses are used in C expression int the same manner as in algebraic expressions. For example, to multiply a times the quantity b + c we write a*(b+c)

Rules of Operator Precedence  :-

                                                              C applies the operators in arithmetic expression in a precise sequence determined by the following rule of operator precedence. Which are generally the same as those in algebra :
  1. Operators in expressions contained with pairs of parentheses are evaluated first. Parentheses are said to be at the "highest level of precedence."  In cases of nested or embedded, parentheses such as ((a+b)+c)
    the operator in the innermost pair of parentheses are applied first.
  2.  Multiplication, division and remainder operations are applied next. If an expression contains several multiplication, division and remainder operations, evaluation proceeds from left to right. Multiplication, division and remainder are said to be on the same level of precedence.
  3. Addition and subtraction operation are evaluated next. If an expression contains several addition and subtraction operation, evaluation proceeds from left to right. Addition and subtraction also have the same level of precedence which is lower than the precedence of the multiplication, division and remainder operations.
  4. The assignment operator (=) is evaluated last.
The rule of operator precedence specify the order C uses to evaluate expressions. When  we say evaluation proceeds from left to right, we're referring to the associativity of the operators. We'll see that some operators associate from right to left. 


Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons