Posts

Showing posts from December, 2017

C - Structured Datatypes

A structure in C is a collection of items of different types. You can think of a structure as a "record" is in Pascal or a class in Java without methods. Structures, or structs, are very useful in creating data structures larger and more complex than the ones we have discussed so far. Simply you can group various built-in data types into a structure. Object conepts was derived from Structure concept. You can achieve few object oriented goals using C structure but it is very complex. Following is the example how to define a structure. struct student { char firstName[20]; char lastName[20]; char SSN[9]; float gpa; }; Now you have a new datatype called student and you can use this datatype define your variables of  student  type: struct student student_a, student_b; or an array of students as struct student students[50]; Another way to declare the same thing is: struct { char firstName[20]; char lastName[20]; char SSN[10]; ...

C - Play with Strings

In C language Strings are defined as an array of characters or a pointer to a portion of memory containing ASCII characters. A string in C is a sequence of zero or more characters followed by a NULL '\0' character: It is important to preserve the NULL terminating character as it is how C defines and manages variable length strings. All the C standard library functions require this for successful operation. All the string handling functions are prototyped in: string.h or stdio.h standard header file. So while using any string related function, don't forget to include either stdio.h or string.h. May be your compiler differes so please check before going ahead. If you were to have an array of characters WITHOUT the null character as the last element, you'd have an ordinary character array, rather than a string constant. String constants have double quote marks around them, and can be assigned to char pointers as shown below. Alternatively, you can assign a string co...

C - Using Functions

A function is a module or block of program code which deals with a particular task. Making functions is a way of isolating one block of code from other independent blocks of code. Functions serve two purposes. They allow a programmer to say: `this piece of code does a specific job which stands by itself and should not be mixed up with anyting else', Second they make a block of code reusable since a function can be reused in many different contexts without repeating parts of the program text. A function can take a number of parameters, do required processing and then return a value. There may be a function which does not return any value. You already have seen couple of built-in functions like printf(); Similar way you can define your own functions in C language. Consider the following chunk of code int total = 10; printf("Hello World"); total = total + l; To turn it into a function you simply wrap the code in a pair of curly brackets to convert i...

C - Pointing to Data

A pointer is a special kind of variable. Pointers are designed for storing memory address i.e. the address of another variable. Declaring a pointer is the same as declaring a normal variable except you stick an asterisk '*' in front of the variables identifier. There are two new operators you will need to know to work with pointers. The "address of" operator '&' and the "dereferencing" operator '*'. Both are prefix unary operators. When you place an ampersand in front of a variable you will get it's address, this can be stored in a pointer vairable. When you place an asterisk in front of a pointer you will get the value at the memory address pointed to. Here is an example to understand what I have stated above. #include <stdio.h> int main() { int my_variable = 6, other_variable = 10; int *my_pointer; printf("the address of my_variable is : %p\n", &my_variable); printf("the address of o...

C - Input and Output

Input :  In any programming language input means to feed some data into program. This can be given in the form of file or from command line. C programming language provides a set of built-in functions to read given input and feed it to the program as per requirement. Output :  In any programming language output means to display some data on screen, printer or in any file. C programming language provides a set of built-in functions to output required data. Here we will discuss only one input function and one putput function just to understand the meaning of input and output. Rest of the functions are given into  C - Built-in Functions printf() function This is one of the most frequently used functions in C for output. ( we will discuss what is function in subsequent chapter. ). Try following program to understand  printf()  function. #include <stdio.h> main() { int dec = 5; char str[] = "abc"; char ch = 's'; float pi = 3.14; printf("%...

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...

Facts about C

Image
                                                                                    C is a general-purpose high level language that was originally developed by Dennis Ritchie for the Unix operating system. It was first implemented on the Digital Eqquipment Corporation PDP-11 computer in 1972. The Unix operating system and virtually all Unix applications are written in the C language. C has now become a widely used professional language for various reasons. Easy to learn Structured language It produces efficient programs. It can handle low-level activities. It can be compiled on a variety of computers. Facts about C C was invented to write an operating system called UNIX. C is a successor of B language which was introduced around 1970 The language was formalized...