Home of RF and Wireless Vendors and Resources

One Stop For Your RF and Wireless Need

C programming language Tutorial-Page2

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

Figure mentions function structure in C. Each C function will have some arguments,return statement, function name and input(scanf) or output(printf) statement to provide input and print the output/result.

Compilation of code

Compile code : gcc helloworld.c
Output created : a.out
Run Program : ./a.out

helloworld.out : helloworld.o
gcc helloworld.o -o helloworld.out
helloworld.o : helloworld.c
gcc -c helloworld.c

Compilation Models

• Preprocessor: Resolves all preprocessor directives.
#include, #define macros, #ifdef, etc.

• Compiler: Converts text into object files
May have unresolved interobject references

• Linker: Resolves all interobject references (or gives you a linker error)
Creates the binary executable

• Loader: Loads the program into RAM and runs the main() function

c function

Data types

We will see here Data Types,Operators and Expressions,Control Flow and Functions in the detail with some examples to practice.

Instructions can operate and process data of finite size All data are sequence of Bits, 8 bits make a Byte Hence all Programs need to know what kind of data the instruction operates on (how it is organized, how long it is etc). This is defined using the data types supported by the program Word is used for general notation of default size of data that is handled by the processor, current processors support 64 bit word operations. We have already seen basic data types supported by C on page1.

Characters (char):
Simple 1 byte used to store a number Usually use ASCII encoding in machines to map these 1 byte numbers to character symbols
Integer (int):
4 bytes of memory storage Data range in binary available from 0 to b11111111111111111111111111111111
Floating point:
4 bytes of memory storage Interpretation defined by IEEE 754 standards for single precision
As per standard has to be at least 32 bit (4 bytes). Same as int on 32 bit systems. Enumeration Data types:
Sub set of integer values E.g., a Color = red, blue, black, or yellow. Check page 1 for detail.

Overflow of Data types

Overflows causes the status register bits to be set.
unsigned int x = 2123456789;
unsigned int y = 3123456789;
unsigned int z;
z = x + y;

Here z is 951,946,282 and not 5,246,913,578 as expected,hence compiler generates warnings though, because they are constants.

Type Conversion and Casting

Following are the rules followed by C compiler for automatic type conversions:
•  char and short operands are converted to int •  Lower data types are converted to the higher data types and result is of higher type. •  The conversions between unsigned and signed types may not yield intuitive results. •  Example:
float f; double d; long l;
int i; short s;
d + f f will be converted to double
i / s s will be converted to int
l / i i is converted to long; long result

Explicit Conversion (Type Casting)

General form of a type casting operator is
(The type-name) expression
It is generally a good practice to use explicit casts than to rely on automatic type conversions.
C = (float)9 / 5 * ( f-32 )
float to int conversion causes truncation of fractional part
double to float conversion causes rounding of digits
long int to int causes dropping of the higher order bits.


Arithmetic operators,Unary operators,Binary operators,Comparison operators/relational operators Logical operators,Compound assignment operators,Member and pointer operators and Other operators are used in C language. The same is mentioned on page-1.

Arithmetic Operators:
There are 2 types of arithmetic operators in C:
unary operators:operators that require only one operand.
binary operators: operators that require two operands.

Pre and Post Increment

It is also possible to use ++i and --i instead of i++ and i--
However, the two forms have a slightly yet important difference.
Consider example below:
int a=9;

The output would be: 9, 10
But if we have:
int a = 9;
The output would be:10,10
a++ would return the current value of a and then increment the value of a
++a on the other hand increment the value of a before returning the value

Control Statements

There are two main types one for selection and the other for repetitions as mentioned below and the syntax for the same is mentioned on page1.

Selection Statements:
• If, if-else
• Switch
• Condition operator (ternary operator)
• While, do-while
• For
• Nested loops
• Break and continue

break and continue with Examples

int a = 10;
{ printf("\nValue of a = %d",a);
if(a==5) break;

Value of a = 10
Value of a = 9
Value of a = 8
Value of a = 7
Value of a = 6

int a = 6;
while(a>= 0 )
if(a==3) continue;
printf("\nValue of a = %d",a);

Value of a = 5
Value of a = 4
Value of a = 2
Value of a = 1
Value of a = 0

break and continue always works on inner most loop or switch case statement. Other condition statements are ignored.

C Function Example

#include <stdio.h>
int maximum(int a, int b);

int main()
int a,b;
int result;
printf("Input 2 integers \n");
scanf("%d %d",&a, &b);

result = maximum(a,b);
printf("max is %d\n",result);

int maximum(int a, int b)
return (a>b?a:b);

Storage Classes

•Storage class specifiers
  •Storage duration -how long an object exists in memory
  •Scope -where object can be referenced in program
•Automatic storage
  •Object created and destroyed within its block
  •auto: default for local variables
  •auto double x, y;
  •register: tries to put variable into high-speed registers
  •Can only be used for automatic variables
   register int counter = 1;

•Static storage
  •Variables exist for entire program execution
  •Default value of zero
  •static: local variables defined in functions.
    •Keep value after function ends
    •Only known in their own function
  •extern: default for global variables and functions
    •Known in any function


•File scope
  •Identifier defined outside function, known in all functions
  •Used for global variables, function definitions, function prototypes
•Function scope
  •Can only be referenced inside a function body
•Block scope
  •Identifier declared inside a block
  •Block scope begins at declaration, ends at right brace
  •Used for variables, function parameters (local variables of function)
  •Outer blocks "hidden" from inner blocks if there is a variable with the same name in the inner block
•Function prototype scope
  •Used for identifiers in parameter list

#include <stdio.h>

int x = 1; //global scope

void a(void);
void b(void);

int main()
{ int x = 5;
printf("outer loop:x is %d\n",x);

int x = 7;
printf("inner loop:x is %d\n",x);

return 0;

void a()
int x = 9;
printf("auto x is %d\n");
printf("auto x is %d\n";

void b()
static int x = 11;
printf("static x is %d\n");
printf("static x is %d\n");

void c()
printf("global x is %d\n");
printf("global x is %d\n");

Output of example above

outer loop x is 5
Inner loop x is 7

auto x is 9
auto x is 10

static x is 11
static x is 12

global x is 1
global x is 2

auto x is 9
auto x is 10

static x is 12
static x is 13

global x is 2
global x is 3


•Recursive functions
  •Functions that call themselves
  •Can only solve a base case
  •Divide a problem up into
    •What it can do
    •What it cannot do
      •What it cannot do resembles original problem
      •The function launches a new copy of itself (recursion step) to solve what it cannot do   •Eventually base case gets solved
Gets plugged in, works its way up and solves whole problem


int fact(int in) { return in>0? in * fact(in-1): 1; }


Share this page

Translate this page