C Programming Tutorial

A variable is a named storage location in computer memory. By using variables name in program, ur refereing to the memory location.

Variable names adhere to the following rules

- The name can contain letter,digits and underscope(_)
- The first character of the variable must be alphabet.It can start with underscore(_) character
- C language variable names are case-sensitive. "name" and "NAME" are 2 different variables
- C keywords should not be used as a variable name. for ex: "break" is a keyword etc.,

Variable names can be upper case or lower case. Symbolic names(macros) can also be upper case and lower case. There is no restrictions on naming of variables in C language.

But all symbolic names uses upper case letter.

all variable names uses lower and upper case letters or combination of both

variable age is valid. Age is valid. _age is valid. __age is also valid

Phone_Number , Phone__Number Mobile_Number is Valid

$Price is *valid* because $ symbol is exceptional in 'C' language , Price$ is also valid variable name . 9Motel is not valid because digit in the beginning of the name. But Motel9 is valid

In order to use variables in 'C' Program it must be associated with Data type. see the below section

There are four basic datatypes

- char
- int
- float
- double

Integer variables holds a value without fractional part.That is also called as Whole-Numbers

Floating-point variables hold a value that have a fractional part.(i.e real numbers)

double type has greater range than float. A Type double variable also is more precise than float type.

All types can be signed or unsigned,by default all types are signed.

- unsigned char
- unsigned int
- unsigned float
- unsigned double

here *unsigned* is a qualifier. unsigned variables increase the range of values it can store

constant types and Enum Types and Preprocessing Directives

- Arithmetic Operators
- Relational and Logical Operators
- Increment and Decrement Operators
- Bitwise Operators
- Assigment Operators and Expressions

+ | Addition | Adds 2 operands or expressions |

- | Substraction | substracts operand2 from operand1 |

* | Multiplication | multiplies 2 operands |

/ | Division | divides operand1 with operand2 |

% | Modulus Operator | remainder operator, gives remainder after operand2 divides operand1 |

Arithmetic Operators are binary operators , Operands can be literal constants, variables or it can be expressions. Arithmetic operations can be performed on numbers only and even on pointers(see pointers section), following data types supported char(internally it is an integer), int, float,double etc.,

int a = 0xabc; a=10 b=11 c=13 -> 10 * pow(16,2) + 11 * pow(16,1)+ 12 * pow (16,0)= int b = 0xABc; int c = 0XABC;**Addition +**

i+20 //i=10 adding operand1 i + literal constant 20 -> 30

20+i // i+20 or 20+i both are same

**Substraction -**

**Multiplication +**

**Division /**

**Modulus %**

**Addition +**

i+20 //i=10 adding operand1 i + literal constant 20 -> 30

20+i // i+20 or 20+i both are same

**Substraction -**

**Multiplication +**

**Division /**

**Modulus %**

>(Relational Operator) | greater than | ||

>=(Relational Operator) | greater than or equal to | ||

<(Relational Operator) | less than | ||

<=(Relational Operator) | less than or equal to | ||

==(Equality Operator) | equal to | ||

!=(Equality Operator) | not equal to | ||

&&(Logical Operator) | Logical AND | ||

||(Logical Operator) | Logical OR | ||

!(Logical Operator) | Logical NOT |

> , >= , < <= operators are binary operators acting on expressions.

```
Greater than operator (>), Greater than or Equal to (>=) operator
> and >= operators checks for number is greater than or equal to given number. if greater returns 1 else 0
a>0 // a=1 returns 1
a>=0 // a=0 returns 1
a>-1 // a=100 returns 0
age>=18 // age=17 returns false(0)
//character data comparisions
c > 'Z' //c='a' returns 1
c >= 'Z'//c='A' returns 0
//floating point comparisions
```

```
Less than operator (<), Less than or Equal to (<=) operator
< and <= operators checks for number is less than other number. if it is lesser returns 1 otherwise 0
a > 0 // a=1 returns 1
a <0 // a=0 returns 0
a <-1 // a=100 returns 0
age <= 18 // age=17 returns true(0)
//character data comparisions
c < 'Z' //c='a' returns 1
c <= 'Z'//c='A' returns 0
//floating point comparisions
//solve following expression:
1 < 5 < 6 < 5+11
This expression can be evaluated like this,(programmer must know operator precedence.)
+ operator has higher precedence than relational operators so expression becomes 1 < 5< 6 < 16
< operator evaluated from left to right. i.e (1 < 5) < 6 < 16
1 < 5 true it returns 1 now expression becomes 1 < 6 < 16
again left to right (1 < 6) < 16 it becomes 1 < 16
finally (1 < 16) evaluated to 1.
```

== and != operators are binary operators acting on expressions.

checks equality for numbers, if both operands are euqal returns 1 ,otherwise 0

/*Integer Numbers comparisions */ a == b //a=10 b=10 returns 1 a == b //a=10 b=20 return 0 //Character data comparisons c=='A' // c='Z' returns 0 c=='A' // c='A' return 1 //Integer and short value comparisions a == b // int a=10 ;short b=10; returns 1 a == b // int a=10 ;short b=20; returns 0 //Integer and Character comparisions a == c // a=97 c='a' returns 1 a == c // a=97 c='z' return 0 //floating point comparisions a==b // a=10.00f b=10.00f returns 1 a==b // a=10.01f b=10.00f returns 0

&& and || operators are binary operators acting on expressions.

! is a unary operator, when applied on expressions yields 0 or 1

(Operator &&) | Op1 | Op2 | Result |

0 | 0 | 0 | |

1 | 1 | 1 | |

0 | 1 | 0 | |

1 | 0 | 0 |

```
Logical Binary operator &&
Logical AND && Both values should be non-zero
a && b //a=1 b= 1 a is checked first if it is greater than 0, result will be 1, it will check both values.
```

(Operator ||) | Op1 | Op2 | Result |

0 | 0 | 0 | |

1 | 1 | 1 | |

0 | 1 | 1 | |

1 | 0 | 1 |

```
Logical Binary operator ||
Logical OR || either value should be non-zero
a || b //a=1 b= 0 a is checked first if it is greater than 0, result will be, it won't check/evaluate b value.
```

```
Logical Unary operator !
!b is equavalent to (b==0)
!b // b=0 then return 1
!b // b=1 return 0
!(x-y) // x=10,y=20 return 0
!(x-y) //x=10 y=20 return 1
!!a // a=0 , !!a = a a is 0 returns 1
```

Increment and decrement operarators are unary operators.It increases or decreases value by 1. these operators are simple to use, i++, i--, these statements can be re-written using binary operators i=i+1 or i=i-1

++ | Increment operator | ||

-- | decrement operator |

**++ increment operator**

it increments the value by 1

it is of 2 types

- post increment
- pre increment

++i is a pre increment operator i++ is a post increment operator

int i=26; int j=++i; // it increments i value by 1 and the assigns i value to j. now i=27 and j=27; int k=26; int l = k++; // it first assigns the value of k to j, then k value will be incremented by 1. l=26, k=27;

**-- decrement operator**

it decrements the value by 1

it is of 2 types

- post decrement
- pre decrement

--i is a pre decrement operator i-- is a post decrement operator

int i=26; int j=--i; // it decrement i value by 1 and the assigns i value to j. now i=25 and j=25; int k=26; int l = k--; // it first assigns the value of k to j, then k value will be decrement by 1. l=26, k=25;

int i = 1; printf("%d,%d,%d",i,++i,i++); //1,2,2 post-increment ++ operator applied after i evaluated.

+= ,Assignment Operators are used when left hand side variable repeated in right hand side.

for ex: expression i = i+10; here variable i repeated in right hand side.

The same expression can be rewritten using Assignment Operator. i+=10;

C language supports Assigment Operators for other binary operators also -= /= *= %=

sizeof operator gives numbers of bytes required to store an object

sizeof int gives 4 bytes spaces required for integer type

sizeof array gives total occupied by all elements,for ex: int a[10] sizeof(a) gives 40, because there are 10 elements each element requires 4 bytes space , so 10 * 4 = 40, i.e n times the size of array element

similarly you can find out size of structure or union

sizeof operator is system dependent, check processor details whether it is 16-bit, 32-bit or 64-bit. below values are 64-bit processor, and OS is fedora 64-bitData Type | SizeOf | Explanation |

char | sizeof(char) | 1 byte |

short | sizeof(short) | 2 bytes |

short int | sizeof(short int) | 2 bytes |

int | sizeof(int) | 4 bytes |

long | sizeof(long) | 8 bytes |

long int | sizeof(long int) | 8 bytes |

float | sizeof(float) | 4 bytes |

double | sizeof(double) | 8 bytes |

long double | sizeof(long double) | 16 bytes |

long double * (size of long double *) | 8 bytes | for any pointer variables, size will be 8 bytes only |

Sizeof | value | explanation |

struct tag { int i; float j; double k; }; |
struct tag { int i; float j; double k; char ca[3]; }; |
struct tag { int i; float j; double k; char *p; }; |

int 4 bytes float 4 bytes double 8 bytes so 4 +4 + 8=16 bytes | int 4,float 4 double 8,for character arrays or any type of arrays structure treats arrays as pointers, so pointer variables size witll be 8 bytes so, 4 + 4+ 8+ 8=24 bytes | same as previous one, answer is: 24 bytes |

& | Address Operator | ||

* | Indirection Operator | ||

+ | Unary plus operator | ||

- | Unary minus operator | ||

~ | bitwise NOT operator | ||

! | Logical Negation Operator |

Used in

- scanf function- for reading primitive data types
- assigning variable to a pointer

Used in

- declaring pointer variable
- Changing/accessing value of a variable using pointers

by default all numbers are positive numbers only. even then user can specify

i=+10+10+i+ +10;

i=-i+10-(-i);

see bitwise- operators section

see Logical Negation section

Conditional Operator is equivalent to if elese statement.Another way of writting if else conditions. compared to if else statement less keywords involved in Conditional statements and also better readability and easy to maintain ,debug and test the code.

Conditional Operator Syntax

returnvalue=(condition)?truevalue:falsevalue;

If condition is true, true value will will be returned t that is next to Question mark

If condition is false, false value will be returned that is next to colon

**Note:**Conditional Operator can be nested

**Note:** Conditional Operator(?:) also known as *ternery operator*

Conditional Operator Example

int a=20,b=30; int max = (a>b)?a:b; //here (a>b) is a condition //True value is a //False value is b //(a>b) <==> (20>30) condition is false. //it will return b value that is 30. //max value is 30

**finding maximum of 3 numbers using conditional operator**

We already discussed conditional operators can be nested

int x=10,y=20,z=30; int max = (x > y && x >z)?x:(y>z)?y:z; //x is greater than other numbers then x is max value //if not is y>z then y is max value //otherwise z will be max value. //max=z max=30

**convert lower case letter to upper case letter using conditional operator**

char c='a'; char u=(c>=97 && c<=122)?c+'A'-'a':c; printf("Upper case letter =%c",u);

Operator | Associativity | |

() [] . -> ++(postfix) --(postfix) | Left to Right | |

+(unary) -(unary) ++(prefix) --(prefix) ! ~ sizeof &(Address) *(dereference) | Right to Left | |

* / % | Left to Right | |

+ - | Left to Right | |

>> << | Left to Right | |

< <= > >= | Left to Right | |

== != | Left to Right | |

& ^ | | Left to Right | |

&& || | Left to Right | |

?: | Right to Left | |

= += -= *= /= etc | Right to Left | |

,(comma) | Left to Right | |

The comma operator is a binary operator. It has 2 forms

Form1:* expression1,expression2 *

Form2: * (expression1,expression2) *

Comma Operator is evaluates expression1 and then expression2 sequentially, and returns a result which has type and left hand expression value. expression2 value is discarded.

see the following example.x = 10+2, 10/2 expression1 is evaluated and value is returned to x, expression2(10/2), evaluated and value is discarded. x has 10+2 i.e 12.

Comma Operator is evaluates expression1 and then expression2 sequentially, and returns a result which has type and right hand expression value. expression1 value is discarded.

see the following example.x = (10+2, 10/2) expression1(10+2), evaluated and value is discarded. expression2(10/2 is evaluated and value is returned to x, x has 10/2 i.e 5.

multiple comma operators, each expression is evaluated

Right hand side expression/value will be returned. int x = (1,2,3); printf("%d\n",x) 3

int a,b,c,d; // a is assigned to 10, b to 20, c is 30 //What about d? //d gets expression1 i.e a=10; //i.e d value is 10 d=a=10,b=20,c=a+b; printf("%d, %d,%d,%d\n",a,b,c,d); Answer: a=10,b=20,c=30,d=10 d=(a=10,b=20,c=a+b); printf("%d, %d,%d,%d\n",a,b,c,d); // a is assigned to 10, b to 20, c is 30 //What about d? //d gets right hand expressionc i.e c=a+b; //i.e d value is 30 Answer: 10,20,30,30

Comma has lowest operator precedence compared to any other operator and associativity is left to right

An expression is anything that evaluates to a Numeric value.

Simple Expressions and Complex Expressions

The simplest C expression consists of a single item: a simple variable, literal constant, or symbolic constant. Here are four expressions:A literal constant evaluates to its own value. A symbolic constant evaluates to the value it was given when you created it using the #define directive. A variable evaluates to the current value assigned to it by the program.

Complex expressions consist of simpler expressions connected by operators. For example:

Variables declared with in the function or block of code are called as Local Variables. These Variables cannot be accessed out-side of the function or block. And also Same Variable can be declared outer block and inner block, these are called as Local variables of that block. inner block variable declaration hides outside block variable declarartion. So both blocks has same variable declarion, only local block declartion is considered, out block variable cannot be accessed.

Local Variables Ex:x is a variable of type int is a Local Variable for function main/or temporary variable for function main(). Life time of the variable x is end of the function. int main(){ int x=0; }

x is a variable of type int is a Local Variable for function main/or temporary variable for function main(). Life time of the variable x is end of the function. Innner Block declared using { statements .... } Inner Block variable x has different type or it can be of same type. scope of inner block variable x is end of the Block i.e '}' int main(){ int x=0; { char x='z'; } }

In the inner Block value of x is 'z', outer block value of x is 0.

int main(){ int x=0; printf("x=%d\n",x); { char x='z'; printf("x=%c\n",x); } printf("x=%d\n",x); }OUTPUT:

x=0 x=z x=0Note: Blocks can be nested to any Level, Check your compiler for details

Identifiers are Functions,structure tags,unions,and enumerations,member of structures or unions,enumeration constants,typedef names

The storage class determines the lifetime of the storage associated with the identified object,

Identifiers in C language has *external Linkage*, *Internal Linkage* or *No Linkage*

ADS