Archive for November 2014

Operators in C language

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. C language is rich in built-in operators and provides the following types of operators:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bit-wise Operators
  • Assignment Operators
  • Misc Operators
Arithmetic Operators:
Following table shows all the arithmetic operators supported by C language. Assume variable A holds 10 and variable B holds 20 then:

OperatorDescriptionExample
+Adds two operandsA + B will give 30
-Subtracts second operand from the firstA - B will give -10
*Multiplies both operandsA * B will give 200
/Divides numerator by de-numeratorB / A will give 2
%Modulus Operator and remainder of after an integer divisionB % A will give 0
++Increments operator increases integer value by oneA++ will give 11
--Decrements operator decreases integer value by oneA-- will give 9

Relational Operators:
Following table shows all the relational operators supported by C language. Assume variable A holds 10 and variable B holds 20, then:


OperatorDescriptionExample
==Checks if the values of two operands are equal or not, if yes then condition becomes true.(A == B) is not true.
!=Checks if the values of two operands are equal or not, if values are not equal then condition becomes true.(A != B) is true.
>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(A > B) is not true.
<Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.(A < B) is true.
>=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.(A >= B) is not true.
<=Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.(A <= B) is true

Logical Operators:
Following table shows all the logical operators supported by C language. Assume variable A holds 1 and variable B holds 0, then:

OperatorDescriptionExample
&&Called Logical AND operator. If both the operands are non-zero, then condition becomes true.(A && B) is false.
||Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true.(A || B) is true.
!Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.!(A && B) is true.

Bit-wise Operators:
Bit-wise operator works on bits and perform bit-by-bit operation. The truth tables for &, |, and ^ are as follows:

pqp & qp | qp ^ q
00000
01011
11110
10011

Assume if A = 60; and B = 13; now in binary format they will be as follows:

A = 0011 1100

B = 0000 1101

-----------------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A  = 1100 0011

The Bit-wise operators supported by C language are listed in the following table. Assume variable A holds 60 and variable B holds 13, then:
OperatorDescriptionExample
&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 either 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 Complement Operator is unary and has the effect of 'flipping' bits.(~A ) will give -61, which is 1100 0011 in 2's complement form.
<<Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.A << 2 will give 240 which is 1111 0000
>>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.A >> 2 will give 15 which is 0000 1111

Assignment Operators:
There are following assignment operators supported by C language:

OperatorDescriptionExample
=Simple assignment operator, Assigns values from right side operands to left side operandC = A + B will assign value of A + B into C
+=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operandC += A is equivalent to C = C + A
-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operandC -= A is equivalent to C = C - A
*=Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operandC *= A is equivalent to C = C * A
/=Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operandC /= A is equivalent to C = C / A
%=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operandC %= A is equivalent to C = C % A
<<=Left shift AND assignment operatorC <<= 2 is same as C = C << 2
>>=Right shift AND assignment operatorC >>= 2 is same as C = C >> 2
&=Bitwise AND assignment operatorC &= 2 is same as C = C & 2
^=bitwise exclusive OR and assignment operatorC ^= 2 is same as C = C ^ 2
|=bitwise inclusive OR and assignment operatorC |= 2 is same as C = C | 2
Misc Operators ↦ sizeof & ternary:

There are few other important operators including sizeof and ? : supported by C Language.

OperatorDescriptionExample
sizeof()Returns the size of an variable.sizeof(a), where a is integer, will return 4.
&Returns the address of an variable.&a; will give actual address of the variable.
*Pointer to a variable.*a; will pointer to a variable.
? :Conditional ExpressionIf Condition is true ? Then value X : Otherwise value Y

Operators Precedence in C:

Operator precedence determines the grouping of terms in an expression. This affects how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has higher precedence than the addition operator.

For example x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Category Operator Associativity 
Postfix () [] -> . ++ - -  Left to right 
Unary + - ! ~ ++ - - (type)* & sizeof Right to left 
Multiplicative  * / % Left to right 
Additive  + - Left to right 
Shift  << >> Left to right 
Relational  < <= > >= Left to right 
Equality  == != Left to right 
Bitwise AND Left to right 
Bitwise XOR Left to right 
Bitwise OR Left to right 
Logical AND && Left to right 
Logical OR || Left to right 
Conditional ?: Right to left 
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left 
Comma Left to right 
Monday, November 3, 2014

C Hello World Example

C Hello World Example:

A C program basically consists of the following parts:


  • Preprocessor Commands
  • Functions
  • Variables
  • Statements & Expressions
  • Comments


Let us look at a simple code that would print the words "Hello World":

#include <stdio.h>

int main()
{
   /* my first program in C */
   printf("Hello, World! \n");
   
   return 0;
}

Let us look various parts of the above program:

The first line of the program #include <stdio.h> is a preprocessor command, which tells a C compiler to include stdio.h file before going to actual compilation.

The next line int main() is the main function where program execution begins.

The next line /*...*/ will be ignored by the compiler and it has been put to add additional comments in the program. So such lines are called comments in the program.

The next line printf(...) is another function available in C which causes the message "Hello, World!" to be displayed on the screen.

The next line return 0; terminates main()function and returns the value 0.

Compile & Execute C Program:

Lets look at how to save the source code in a file, and how to compile and run it. Following are the simple steps:

  • Open a text editor and add the above-mentioned code.
  • Save the file as hello.c
  • Open a command prompt and go to the directory where you saved the file.
  • Type gcc hello.c and press enter to compile your code.
  • If there are no errors in your code the command prompt will take you to the next line and would generate a.out executable file.
  • Now, type a.out to execute your program.
  • You will be able to see "Hello World" printed on the screen

Semicolons and Comments in C language

Semicolons and Comments in C language:

Semicolons are very important in C programming language. They are used as terminators.
They are used to terminate a statement. They are used at the end of every statement. You will get an error if you forget to add semicolon at the end of every statement.

In C program, the semicolon is a statement terminator. That is, each individual statement must be ended with a semicolon. It indicates the end of one logical entity.
For example, following are two different statements:
printf("Hello, World! \n");
return 0;

Comments are used in C language for better understanding of the program. Comments are like helping text in your C program and they are ignored by the compiler. They start with /* and terminates with the characters */ as shown below:

/* my first program in C */

Keywords in C programming Language

Keywords in C programming Language:

There are 31 Keywords/Reserved words in C programming language. 
The following list shows the reserved words in C. These reserved words may not be used as constant or variable or any other identifier names.

autoelselongswitch
breakenumregistertypedef
caseexternreturnunion
charfloatshortunsigned
constforsignedvoid
continuegotosizeofvolatile
defaultifstaticwhile
dointstruct_Packed
double   

Keywords in C programming language must not be used as a variable name or as a constant.
If you do so, then you will get an error in your program.

Variables in C Programming Language

Variables in C Programming Language:

A variable is a memory location whose value can change (vary) during run-time, which is when a program is running. Most of the memory locations declared in a program are variables. 

In a program that inputs the radius of any circle and then calculates and outputs the circle’s area, a programmer would declare variables to store the values of the radius and area; doing this allows those values to vary while the program is running.

Variable Naming:

  • The name must begin with a letter.
  • The name can contain only letters, numbers, and the underscore character. No punctuation marks, spaces, or other special characters are allowed in the name.
  • The name cannot be a keyword. 
  • Names in C are case sensitive.

Variable Declaration:

Following syntax is used in declaration of the variables:
"Data_type Variable_name;"
Data Type defined the type of data that is to be stored in the variable while variable name can be anything according to the roles of naming variables.

Data Types: 

Following are different types of data type:
Basic Types:
They are arithmetic types and consists of the two types: (a) integer types and (b) floating-point types.
Enumerated types:
They are again arithmetic types and they are used to define variables that can only be assigned certain discrete integer values throughout the program.
The type void:
The type specifier void indicates that no value is available.
Derived types:
They include (a) Pointer types, (b) Array types, (c) Structure types, (d) Union types and (e) Function types.

Integer Data Type:

Following table gives you details about standard integer types with its storage sizes and value ranges:


To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in bytes.

Floating Point Data Type:

Following table gives you details about standard floating-point types with storage sizes and value ranges and their precision:

To get the exact size of a type or a variable on a particular platform, you can use the sizeof operator. The expressions sizeof(type) yields the storage size of the object or type in bytes.

The Void Type:

The void type specifies that no value is available. It is used in three kinds of situations:

Function returns as void:
There are various functions in C which do not return value or you can say they return void. A function with no return value has the return type as void. For example void exit (int status);

Function arguments as void:
There are various functions in C which do not accept any parameter. A function with no parameter can accept as a void. For example, int rand(void);

Pointers to void :
A pointer of type void * represents the address of an object, but not its type. For example a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be casted to any data type.


C Programming Language

C Programming Language:



In computing, C is a general-purpose programming language initially developed by Dennis Ritchie between 1969 and 1973 at AT&T Bell Labs.
Like most imperative languages(In computer science terminologies, imperative programming is a programming paradigm(A programming paradigm is a fundamental style of computer programming, a way of building the structure and elements of computer programs.) that describes computation in terms of statements that change a program state.) in the ALGOL(ALGOrithmic Language) tradition, C has facilities for structured programming(aimed at improving the clarity, quality, and development time of a program) and allows lexical variable scope and recursion(a method where the solution to a problem depends on solutions to smaller instances of the same problem), while a static type system prevents many unintended operations.
Its design provides constructs that map efficiently to typical machine instructions(a set of instructions executed directly by a computer), and therefore it has found lasting use in applications that had formerly been coded in assembly language(An assembly language is a low-level programming language for a computer, or other programmable device, in which there is a very strong (generally one-to-one) correspondence between the language and the architecture's machine code instructions), most notably system software(computer software designed to operate and control the computer hardware) like the Unix(a multitasking, multi-user computer operating system that exists in many variants) computer operating system(An operating system (OS) is software that manages computer hardware and software resources and provides common services for computer programs).

Many later languages have borrowed directly or indirectly from C, including D, Go, Rust, Java, JavaScript, Limbo, LPC, C#, Objective-C, Perl, PHP, Python etc.

Design of C Language:

C is an imperative (procedural) language. It was designed to be compiled using a relatively straightforward compiler, to provide low-level access to memory, to provide language constructs that map efficiently to machine instructions, and to require minimal run-time support. C was therefore useful for many applications that had formerly been coded in assembly language, such as in system programming.

Given below is the simplest code written in C which displays Hello Wolrd! to the user.

/* Hello World program */

#include<stdio.h>

main()
{
    printf("Hello World");

}

This is just some introduction to the  C Programming language.
However, it is a vast and powerful language which can be used for different purposes.

Popular Post

Geek Fixes. Powered by Blogger.

Translate

- Copyright © Geek Fixes -Amazing Wallpapers- Powered by Blogger - Designed by Adeel Sarwar