Posts

C - Flow Control Statements

C provides two sytles of flow control: Branching Looping Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching: Branching is so called because the program chooses to follow one branch or another. if statement This is the most simple form of the branching statements. It takes an expression in parenthesis and an statement or block of statements. if the expression is true then the statement or block of statements gets executed otherwise these statements are skipped. NOTE:  Expression will be assumed to be true if its evaulated values is non-zero. if  statements take the following form: Show Example if (expression) statement; or if (expression) { Block of statements; } or if (expression) { Block of statements; } else { Block of statements; } or if (expression) { Block of statements; } else if(expression) { Block of statements; } else { Block of...

C - Operator Types

What is Operator?  Simple answer can be given using expression  4 + 5 is equal to 9 . Here 4 and 5 are called operands and + is called operator. C language supports following type of operators. Arithmetic Operators Logical (or Relational) Operators Bitwise Operators Assignment Operators Misc Operators Lets have a look on all operators one by one. Arithmetic Operators: There are following arithmetic operators supported by C language: Assume variable A holds 10 and variable B holds 20 then: Show Examples Operator Description Example + Adds two operands A + B will give 30 - Subtracts second operand from the first A - B will give -10 * Multiply both operands A * B will give 200 / Divide numerator by denumerator B / A will give 2 % Modulus Operator and remainder of after an integer division B % A will give 0 ++ Increment operator, increases integer value by one A++ will give 11 -- Decrement operator, decreases integer value by one A-- will give 9 ...

C - Using Constants

A C constant is usually just the written version of a number. For example 1, 0, 5.73, 12.5e9. We can specify our constants in octal or hexadecimal, or force them to be treated as long integers. Octal constants are written with a leading zero - 015. Hexadecimal constants are written with a leading 0x - 0x1ae. Long constants are written with a trailing L - 890L. Character constants are usually just the character enclosed in single quotes; 'a', 'b', 'c'. Some characters can't be represented in this way, so we use a 2 character sequence as follows. '\n' newline '\t' tab '\\' backslash '\'' single quote '\0' null ( Usedautomatically to terminate character string ) In addition, a required bit pattern can be specified using its octal equivalent. '\044' produces bit pattern 00100100. Character constants are rarely used, since string constants are more convenient. A string constant is surrounded by d...

C - Storage Classes

A storage class defines the scope (visibility) and life time of variables and/or functions within a C Program. There are following storage classes which can be used in a C Program auto register static extern auto - Storage Class auto  is the default storage class for all local variables. { int Count; auto int Month; } The example above defines two variables with the same storage class. auto can only be used within functions, i.e. local variables. register - Storage Class register  is used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and cant have the unary '&' operator applied to it (as it does not have a memory location). { register int Miles; } Register should only be used for variables that require quick access - such as counters. It should also be noted that defining 'registe...

C - Variable Types .

Image
                       C - Variable Types . A variable is just a named area of storage that can hold a single value (numeric or character). The C language demands that you declare the name of each variable that you are going to use and its type, or class, before you actually try to do anything with it. The Programming language C has two main variable types Local Variables Global Variables Local Variables Local variables scope is confined within the block or function where it is defined. Local variables must always be defined at the top of a block. When a local variable is defined - it is not initalised by the system, you must initalise it yourself. When execution of the block starts the variable is available, and when the block ends the variable 'dies'. Check following example's output main() { int i=4; int j=10; i++; if (j > 0) { /* i defined in 'm...

C - Reserved Keywordss

Image
C - Reserved Keywordss The following names are reserved by the C language. Their meaning is already defined, and they cannot be re-defined to mean anything else. auto else long switch break enum register typedef case extern return union char float short unsigned const for signed void continue goto sizeof volatile default if static while do int struct _Packed While naming your functions and variables, other than these names, you can choose any names of reasonable length for variables, functions etc. C - Basic Datatypes Data Types Objectives Having read this section you should be able to: declare (name) a local variable as being one of C's five data types initialise local variables perform simple arithmetic using local variables Now we have to start looking into the details of the C language. How easy you find the rest of this section will depend on whether you have ever programmed before - no matter what the language was. There are a great many i...

C - Program Structure.........{ }

Image
                                C - Program Structure.........{ } A C program basically has the following form: Preprocessor Commands Functions Variables Statements & Expressions Comments The following program is written in the C programming language. Open a text file  hello.c  using vi editor and put the following lines inside that file. #include <stdio.h> void main() { /* My first program */ printf("Hello, World! \n"); getch(); } Preprocessor Commands:  These commands tells the compiler to do preprocessing before doing actual compilation. Like  #include <stdio.h>  is a preprocessor command which tells a C compiler to include stdio.h file before going to actual compilation. You will learn more about C Preprocessors in  C Preprocessors  session. Functions:  are main building blocks of any C Program. Every C Progra...