Home of RF and Wireless Vendors and Resources

One Stop For Your RF and Wireless Need

C programming language Tutorial-Page1

Introduction:

This C programming language tutorial covers basics of C variable data types, control flow statements,arithmetic,logical,relational operators, C function declaration and definition,array,pointer,structure,union,typedef,enum and more.

Refer following pages to navigate complete C tutorial.
C Language page1
C Language page2
C Language page3
C Language page4
C Language page5

C variable data types

Following table summarizes C variable data types,C keyword, size(in bytes) and range.

Data Type C keyword Size(bytes) Range
Character char 1 -128 to 127
Integer int 2 -32768 to 32767
Short integer short 2 -32768 to 32767
Long integer long 4 -2,147,483,648 to 2,147,438,647
Unsigned character unsigned char 1 0 to 255
Unsigned integer unsigned int 2 0 to 65535
Unsigned short integer unsigned short 2 0 to 65535
Unsigned long integer unsigned long 4 0 to 4,294,967,295
Single-precision floating-point float 4 1.2E-38 to 3.4E381
Double-precision floating-point double 8 2.2E-308 to 1.8E3082

Control flow statements in C

Following are some of the control flow loops,selection statements similar to other programming languages.

For Loop:
for ( initial; condition; increment )
{
Statements (one or multiple);
}

For example,
for (i=0;i<10;i++)
{

}

While and Do while:
while (condition check)
{
Statements; //one or many
}

do
{
Statements; //one or many
}
While(condition check)

If else statement:
If (conditioncheck1 )
{
//Statements (one or many)
}
else if(conditioncheck2 )
{
//Statements(one or many)
}
else if(conditioncheck3 )
{
//Statements(one or many)
}

else
{
//Statements(one or many)
}

Switch statement:
Switch (condition)
{
Case condition1: //statements
Case condition2: //statements
Case condition3: //statements
Default: //statements
}

One can insert break statement in each case to bring program control out of that particular case.

Break statement
Bring program control out of the loop or function is has been called.

Continue
Inside switch statement, and when called in the loop (while, for) it causes next iteration of the loop to execute.

Operators-Arithmetic,Logical,Relational

Arithmetic operators:
Addition +
Subtraction -
Multiplication *
Division /
Modulus %
Increment ++
Decrement --


Relational operators:
Equal ==
Greater than >
Less than <
Greater than or equal to >=
Less than or equal to <=
Not equal !=

Logical operators:
AND &&
OR ||
NOT !

Bitwise logical operators:
& Bitwise AND
| Bitwise inclusive OR
^ Bitwise exclusive OR
<< left shift
>> right shift
~ one's complement

Compound Assignment operators:
a+=b can be writtern for a=a+b. This is(+=) called compound assignment operator for addition. Few operators are outlined below and so on:
Addition +=
Substration -=
Bitwise AND assignment &=
Bitwise left shift <<=

Member and Pointer Operator:
Array subscript : a[0]
Indirection(Object pointed to by a) : *a
Reference(address of a) : &a
Structure dereference (member b of object pointed to by a) : a->b
Structure reference (member 'b'of object'a'):a.b

Other operators:
Function call: a(a1,a2)
Comma: a,b
Ternary Condition: a?b:c
Sizeof: sizeof(a)
Type cast: (type)a

C Function declaration/definition

Functions:
  •Modules in C
  •Programs combine user-defined functions with library functions
  •C standard library has a wide variety of functions
Function calls:
 •Invoking functions:
  •Provide function name and arguments (data)
  •Function performs operations or manipulations
  •Function returns results
 •Function call analogy:
  •Boss asks worker to complete task
  •Worker gets information, does task, returns result
  •Information hiding: boss does not know details

Any file with .c extension is called as c file. Any c file contains functions and variables as per c program structure.

Function declaration:
return_valuefunction_name( arg_1,...,arg_n);

Function Definition
return_value function_name( arg_1,...,arg_n);

{
/*C statements and local variables if any, should end with semicolon(;) */
}

Function prototype declaration for example,
doublecube_number( int number );
number is the argument which is of int type. There may be more than one arguments.
Function name is cube_number which calculates cube of a number and returns a value of type double.

More than one value can be passed to a function as arguments or more than one value can be returned from a function by using concept called "pass by reference". What we have seen above is pass by value. In pass by reference pointer is used as argument to a function or pointer is used as return variable. Pointer points to the starting address of either array or structure or union and hence all the elements can be accessed easily.

Array

Array stores similar data type elements in consecutive memory locations.
Single dimensional array
#define arr_size 10
int array[arr_size] = { 1, 2, 3,4,5,6,7,8,9,10 };
Multi dimensional array
#define no_rows 4
#define no_cols 3
int array[no_rows][no_cols] = { { 1, 2, 3 } , { 4, 5, 6 } ,
{ 7, 8, 9 } , { 10, 11, 12 } };

Pointer

Pointer stores address of some data type element viz. int, float, char, char string, structure, union, array or any C function. Hence using pointer directly respective data type can be accessed once pointer is initialized for the same.

typename *ptr;
typename can be any c variable type that pointer will point

For Example,
int *p1; //p1 is the pointer and points to int variable data type

Pointer Initializing:
int employee_number;
int arr[10];
int *ptr1, *ptr2;

ptr1=&employee_number;
ptr2=arr; //array name itself points to the memory address and no & required

Structure

Structure contains one or more variable of different types. It helps for easy management as different variable types are assigned by one common structure name. All the structure variables are stored consecutively in memory. Structure can hold another structure also as member.

struct  emp_record
{ char name[10];
int number[12];
float Salary;
};

Here structure name is emp_record and name,number,salary are structure member variables/elements. In order to access structure members, variable to structure is to be declared as mentioned below. It can be simple structure variable or pointer variable to the structure.

struct emp_record employee; // employee is the instance for the structure emp_record
struct emp_record *ptr; // ptr is the pointer to the structure emp_record

Structure member elements can be accessed using (.) operator if instance is non- pointer. Can be accessed using (->) operator if instance is pointer to the structure.

For example,
employee.name OR employee.number OR employee.Salary
ptr->name OR ptr->number OR ptr->Salary
ptr = &employee; //pointer need to be initialized with structure's memory location address

Union

Union is similar to the structure, the difference in allocating size in the memory and the keyword used here in union in place of struct.

union emp_record
{
char name[10];
int number[12];
float Salary;
};

If above declaration and instance for the same is of structure, then it allocates about 7 bytes ( 1 byte for char,2 for int and 4 for float) in memory.

If the same declaration and instance is for the union, it allocates size as of maximum size of the member variable(here it is Salary and hence size allocated will be 4bytes for this union). Here size allocated in memory is used by one member variable at a time.

Typedef

Typedef is the keyword which creates synonym of some variable type. It is mainly used to create synonym of structure/union. Hence it makes easy to call structure by one simple easy to remember name rather than calling entire structure/union all the time. Hence it minimizes lines of code in a C program.

For example,
typedef struct {
char name[10];
int number[12];
float Salary;
}emp_record;

Hence here emp_record is the synonym identifier of the following lines of code.
struct {
char name[10];
int number[12];
float Salary;
}

Enum type

enum employee_num {Rahul,Ramesh,Roopesh};
As per this enumspecifier C assigns automatically, Rahul=0, Ramesh=1 and Roopesh=2

Starting value can be pre-configured as below
enum employee_num {Rahul=100,Ramesh,Roopesh};

conditional Expression

C provides short way of representing a single if-else statement as follows.
if(a>b)
z=a;
else
z=b;

Above can be represented by single conditional expression, z=(a>b)?a:b
Here if a>b is TRUE then z=a and if a>b is FALSE then z=b.

Preprocessor directive

Preprocessor statements executes prior to actual compilation of the C program. All the preprocessor directives in C language,begin with # (which is a pound sign).
One must have seen #include preprocessor directive, which includes C header file in all the C programs.

Another common preprocessor directive is #define directive, which replaces any character string with some value or string.
For Example,
#define pi 3.1428
#define CITY_NAME "BANGALORE"

Another preprocessor directive is #ifdef and #endif

#ifdef MACRO_N
stat1
stat2
-----
statN
#endif

As shown MACRO_N is defined using #define directive, here based on MACRO_N set of statements are compiled prior to the other ones.


Share this page

Translate this page