Category Archives: C Tutorials

Here you will learn basics of C programming.

C Conditional Statement

C programming supports following conditional statement and operators.

Statement Description
if () { } An if statement consist of a condition statement followed by one or more statement. Statements can be exexuted when condition is true.
If() { } else { } An if statement can be followed by an else statement which can be executed when condion is false.
If (){ } else if () { } …  else  { } An if else ladder . this can will execute only that statement whose condition is true  otherwise else condition will be executed default.
 Switch () { case .. :   … .. } A switch statement is used to check for equality of variable again set of case values.
? : Conditional operator

Example 1:

 

Output:

a has value as 10

Example 2:

output :

a  and b are not equal

Example 3:

output :

Monday

Example 4:

Output:

Friday

Default statement will be executed then only when no case is matched.

C operators and expressions

An operator is a symbol that tells computer to perform certain mathematical and logical operations on identifier. Operators are used in program to manipulate data and variables. C language provide following type of operators:

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operator
  • Increment and Decrement Operators
  • Conditional Operators
  • Misc Operators

We will go through all operators one by one.

All below examples are performed on integer variable, output may differ on float variable.

Arithmetic Operators :

Consider A=5 ,B=2 .

S.No. Operator Operation Example
1. + Addition A+B will give 7
2. Subtraction A-B will give 3
3 * Multiplication A*B will give 10
4 / Divide A/B will give 2
5 % Modulo division A%B will give 1

Relational Operators :

S.No. Operator Example Output
1. > A>B True
2. < A<B False
3. >= A>=B True
4. <= A<=B False
5. == A==B False
6. != A!=B True

Logical Operators :

Let A=1,B=0

S.No. Operator Description Example
1. && AND operator A && B will give False (0)
2. || OR Operator A||B will give True (1)
3. ! Not Operator !A will give False (0)

Bitwise Operators:

 x

y

 x|y

x & y

x ^ y

 Operator  Explanation

0

0

0

0

0

&  Bitwise AND

0

1

1

0

1

|  Bitwise OR

1

0

1

0

1

~  Bitwise NOT

1

1

1

1

0

^  XOR
<<  Left Shift
>>  Right Shift

Assignment Operators :

Operator
Example
 Explanation 
 
Simple assignment operator
 
 =
sum = 10
Value 10 is assigned to variable sum
Compound assignment operator 
+=
sum+=10
This is same as  sum = sum+10
 -=
sum – = 10
This is same as sum = sum – 10
 *=
sum*=10
This is same as sum = sum*10
 /=
sum/=10
This is same as sum = sum/10
%=
sum%=10
This is same as sum = sum%10
&=
sum&=10
This is same as sum = sum&10
^=
sum^=10
This is same as sum = sum^10

 

Increment and Decrement Operator :

Increment and Decrement operator are used to increment and decrement variable value by one .

Operators are :

Post increment : varname ++

Pre increment : ++varname

Post decrement: varname–

Pre decrement: –varname

example:

i=1;

i++; // This means i=i+1 ;

i=1;

i–; //This means i=i-1;

 

Conditional Operator :

_?_:_;

Conditional operator returns value depending on truth of condition

eg.

i=9;

a=(i>10?10:11);

in this example i=9 which is less than 10 so condition is false so value assigned to a will be 11;if condition is true than the assigned value will be 10;

Misc Operators:

 S.No
 Operators 
Description
1
&
This is used to get the address of the variable.
Example:        &a will give address of a.
2
*
This is used as pointer to a variable.
Example: * a  where, * is pointer to the variable a.
3
Size of ()
This gives the size of the variable.
Example: size of (char) will give us 1.

C storage class

Every variable declaration has two property associated with it. storage class and data type. storage class defines that where to store a variable.

 

Syntax : storage_class datatype variable_name;

 

There are following storage classes in C.

auto,  register,  static,   extern

S.No. Storage Specifier Storage Place
1 auto CPU Memory
2 extern CPU Memory
3 static CPU Memory
4 Register Register Memory

 

auto storage class is a default storage class for all local variable. auto storage class variable has local scope.

Both declaration of variable a and b are same. Default storage is taken as auto storage.

extern storage class is used to give reference to the global variable of current file or other files. extern storage variable cannot be initialized because it refers to the global variable of current or other file.

If we deal with multiple files and we want to use global variable or function of other file in current file then we can access the gloabl variable or function of other file using storage keyword and define the variable name same as in other files.

Example:

file: 1.c

file: 2.c

 

Compile: gcc 1.c 2.c

output: ./a.out

10

 

Static storage class is default storage class for global variable and static variables.Static variables are initialized only once.

static variable can be defined as below :

The below example will explains more about static variable.

Output :

1 2 3

 

register storage class is similar to local variable. The only difference is the value stored in microprocessor register instead of RAM. This class is faster than other classes. We should not define many register variables other wise the processing will go slower.

Register variable declaration.

 

C overview

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 the ALGOL tradition, C has facilities for structured programming and allows lexical variable scope and recursion, while a static type system prevents many unintended operations. Its design provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, most notably system software like the Unix computer operating system

C is one of the most widely used programming languages of all time, and C compilers are available for the majority of available computer architectures and operating systems.

 

for more detail refere to http://en.wikipedia.org/wiki/C_programming

C Data types

In the C programming language, data types refers to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage.

C compiler supports 5 fundamental data types int (integer),float (floating point) , double (double precision floating point)  , char (character) , void 

 

C Constants

Constant in C refers to fixed values that do not change during the life of a program.

Several type of Constants are in C.

Integer Constant

Real Constant

Character Constant

String Constant

 

Backslash character constants used in formating output.

Constant Meaning
‘a’ Audible Alert (Bell)
‘b’ Back Space
‘f’ Form Feed
‘n’ New Line
‘r’ Carriage Return
‘t’ Horizontal Tab
‘v’ Vertical Tab
''’ Single Quote
‘”‘ Double Quote
‘?’ Question Mark
‘\’ Backslash
‘′ Null
 

 

 

C identifier

Identifiers are name of variables, functions and arrays. These are user-defined names.

Identifier naming rules in C :

A.) First Character must be an alphabet (a-z or A-Z)

B.) Must consist of letters, digits or underscore.

C.) Cannot use reserved keywords

D.) Must not contain white spaces

e.q.

int a123; Correct 

int a 123; wrong