Thursday, August 30, 2012

Data types and names in c

The char data type: An object of char data type represents a single character e.g a, b, d, A, F, D, 5,9 &,%,( etc.
computer can only store numeric code, therefore characters such as A, a, B, b and so on all have a unique numeric code that is used by computers to represent the characters. For many computers, the ASCII codes are the standard code that is used by the computer to represent a character set.

Character Variable: A variable that can represent different characters is called the character variable.
e.g char sann;
or char sann, s1, s2, stdnt; (you can define more than one characters in single line as shown here).

To print a character we use character format specifier %c, which indicates to printf() function that the argument to be printed is a character.

void main()
char c1='a';
char c2='5';
printf("output: %c",c1);
printf("output: %c",c2 );

The int data type: The int keyword is used to specify the type of a variable as an integer. Integer numbers are also called the whole numbers.

Declaring integer variables:
int variablename;(basic declaration format).
or int variablename1, variablename2, variablename3;

int type:

type                    size(byte)               minimum value               maximum value           format specifer

short int                 2                            -2^15                             2^15-1                       %hd
int                         2                            -2^15                             2^15-1                        %d
long int                  4                            -2^31                             2^31-1                        %ld
long long int           8                            -2^63                             2^63-1                        %lld

Real numbers: The real type holds the values that consists of an integral part and fractional part such as 56.908. there are three different size of real types.
(i) float (4 byes, %f, precision 6 digits) (ii) double (10 bytes, %f, precision 10 digits) (iii) long double (10 bytes, %Lf, precision 10 digits).

variable declaration:
float price;
long double radius;
double a;

Friday, August 24, 2012

the basics of c program

As a building is made of bricks, a c program is made of basic elements such as expressions, statements, statement blocks, constants, and variables.

constants and variables: A constant is a value that never changes while a variable can be used to present different values.
ex. i=1, symbol '1' represents the constants and symbol 'i' represents the variable.

Expressions: An expression is combination of constants, variables and operators that are used to denote the computations.

for ex: (4+8)*6-i+7.

C-Arithmetic operator: 
symbol                    meaning
+                           addition
-                            subtraction
*                           mmultiplication
/                            division
%                         remainder 

Statements: A statement is a complete instruction, ending with a semicolon.
e.g i=8;

Statement Blocks: A group of statements can form a statement block that starts with an opening brace ({) and ends with a closing brace (}).
e.g {

Tuesday, August 7, 2012

what is c programming language

C is a programming language. The C language was first developed in 1972 by Dennis Ritchie at AT & T (American Telephone and Telegraph company) Bell labs.
C is a high level programming language because it is closer to human language. While the lowest languages are machine language that computer understand directly.
Example: The human language: "if line is not busy connect to the internet".
High level language: if(line != busy) connect( internet).
The machine language: 1001010100101111000011111010101010

Friday, July 27, 2012

Precedence and associativity in c

Precedence: Precedence is used to determine the order in which different operators in complex expression are evaluated.

Assiciativity: Associativity is used to determine the order in which  operators with the same precedence are evaluated in a complex expression. It can be a left to right or right to left depends on the expression.

Left to right associativity: 3*8/4%4*5
Right to left associativity: Several operators have right to left associativity. for example when more than one assignment operator occur in an expression e.g a+=b*=c-=5 evaluated as (a+=(b*=(c-=5))).

difference between signed int and unsigned int (signed int vs unsigned int)

Signed integer can store positive value as well as negative value while unsigned integer can store only positive value.
For signed integer the range is -32768 to 32767 while for unsigned integer the range is: 0 to 65535.

Expressions in c

An expression is a sequence of operands and operators that reduces to a single value. An expression can be simple or complex. A simple expression contains only one operator e.g 2+5; A complex expression contains more than one operator e.g 2+5*3-9.

Expression can be categorized into 6 parts.

Primary expressions: A primary expression consists of only one operand with no operator. In c, the operand in the primary expression can be a name, a constant, or a parenthesized expression.

names: a, b12, price, calc, etc....
constants: 6,123.98, 'a'.

Parenthetical expression: (2+4*6-4).

Postfix expression: The postfix expression consists of one operand followed by one operator.
example postfix increment/ decrement e.g a++, a--;

Prefix expression: In prefix expressions, the operator comes before the operand. example: prefix increment/decrement e.g ++a, --a;

Unary Expressions: A unary expression, like a prefix expression consists of one operator and one operand.. Also like the prefix expression, the operator comes before operand. but the difference between unary and prefix expression is that prefix expression needs  a variable as an operand, while unary expression can have an expression or a variable as the operand. ex- sizeof(int). +a,-a, float(x).

Binary Expressions: Binary expressions are formed by an operand operator operand combination.
example: 10*5, (2+38*i)*(1+3+i).

Assignement expression: ex- a=5; b=90;


Saturday, July 21, 2012

difference between short int and int (int vs short int) ?

Actually the size of int is fully dependent on machine and compiler. But c requires to maintains the following relationship:  sizeof(short int)<=sizeof(int)<=sizeof(long int)<sizeof(long long int).
For e.g. in 32 bits operating system sizeof(short int)=2 bytes, sizeof(int)=2bytes, sizeof(long int)=4bytes, sizeof(long long int)=4bytes.
but in case of 64 bits operating system sizeof(long long int)=8bytes.
So the bottom line is that size of integer is totally dependent on the machines and compilers.