Posts

C - Bits Manipulation

Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a byte. C language is very efficient in manipulating bits. Here are following operators to perform bits manipulation: Bitwise Operators: Bitwise operator works on bits and perform bit by bit operation. Assume if B = 60; and B = 13; Now in binary format they will be as follows: A = 0011 1100 B = 0000 1101 ----------------- A&B = 0000 1000 A|B = 0011 1101 A^B = 0011 0001 ~A  = 1100 0011 Show Examples There are following Bitwise operators supported by C language Operator Description Example & Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100 | Binary OR Operator copies a bit if it exists in eather operand. (A | B) will give 61 which is 0011 1101 ^ Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001 ~ Binary Ones Compleme...

C - Working with Files

When accessing files through C, the first necessity is to have a way to access the files. For C File I/O you need to use a FILE pointer, which will let the program keep track of the file being accessed. For Example: FILE *fp; To open a file you need to use the  fopen  function, which returns a FILE pointer. Once you've opened a file, you can use the FILE pointer to let the compiler perform input and output functions on the file. FILE *fopen(const char *filename, const char *mode); Here filename is string literal which you will use to name your file and mode can have one of the following values w - open for writing (file need not exist) a - open for appending (file need not exist) r+ - open for reading and writing, start at beginning w+ - open for reading and writing (overwrite file) a+ - open for reading and writing (append if file exists) Note that it's possible for fopen to fail even if your program is perfectly correct: you might try to open...

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("%...