You are on page 1of 14

1

C PROGRAMMING TUTORIALS

Session 1

BASICS 1. FROM CODING TO EXECUTION 2. DATA TYPES AND STORAGE CLASSES 3. OPERATORS AND OPERATOR PRECEDENCE

Session 1

FROM CODING TO EXECUTION


Preprocessor

#include <stdio.h> int main(void) { unsigned long unsigned long


Compiler/assembler generates OBJ file

tmp0 = 0; tmp1;

Librarian (optional)

tmp1 = 2; printf(Hello ff%dh, tmp1); return tmp0; }

Linker

Final Executable/binary

Session 1

PROGRAM LAYOUT
Stack Stack Base Heap Limit Stack Limit Heap Base Heap Data (RW/BSS) (ZI)

All code resides in Test (RO region)

Text (RO) Code + RO Data

All constant data ( const variables) are also in the RO region All malloc and calloc data go into HEAP. Global variables, structures, arrays and all else go into the data region. All static variables are zero initialized Global arrays are also zero initialized i.e. int my_array [ 100]; Stack typically grows downwards. Stack Overflow Condition where stack usage exceeds stack limit and corrupts memory for other things like heap etc.

Session 1

DATA TYPES
Data Type Integer Short Signed Unsigned Long Signed Unsigned Char Signed Unsigned Float Double 4bytes big [-3.4e38 to +3.4e38] 8btes big [-1.7e308 to +1.7e308] Description 2bytes big Bits[14:0] data, bit[15] for sign [-32768 to +32767] Bits[15:0] data [0 to 65535] 4bytes big Bits[30:0] data, bit[31] for sign [-2147483648 to +2147483647] Bits[31:0] data [0 to 4294967295] 1byte big Bits[6:0] data, bit[7] for sign [-128 to +127] Bits[7:0] data [0 to 255]

Implementation defined. NOTE: Could be 2 or 4 bytes. DONT Assume

Session 1

DATA TYPES

Type Casting/Conversion Mechanism of converting variable of one type into another short a; int i = (int) a; -- Converts a into an equivalent integer and stores in i. Overflow causes rolling over i.e. unsigned char a = 255; a = a+1; // Value of a is now 0 not 256. Representation of floating point numbers Learn the IEEE representation of floating point numbers Essential for DSP, Control Systems specializations

Session 1

STORAGE CLASSES
Storage Class Automatic Description Storage Memory. Default initial value An unpredictable value, which is often called a garbage value. Scope Local to the block in which the variable is defined. Life Till the control remains within the block in which the variable is defined

Register

Storage- CPU registers. Default initial value - Garbage value. Scope - Local to the block in which the variable is defined. Life - Till the control remains within the block in which the variable is defined. NOTE: It is compilers decision whether to allocate register or not. If no register is available, variable is treated like auto. Enum, structure or union ( typically of 2 or 4 bytes) can also use register storage class.

Session 1

STORAGE CLASSES
Storage Class Static Description Storage Memory. Default initial value Zero. Scope Local to the block in which the variable is defined. Life Value of the variable persists between different function calls. NOTES: Its ALL about SCOPE Static scope limits a variable to Control Block if declared inside { } Rest of the file (ONLY) if declared outside the {} Sometimes referred as static global variable.

extern

Storage Memory Default initial value - 0. Scope Global i.e. everywhere in program/process space. Life - Till the program execution doesnt come to an end. NOTE: Declare variable in .h file (say globals.h ) as extern. Define it in one c file (say globals.c). Wherever the variables are needed just include globals.h

Session 1

KEYWORDS W.R.T DATA TYPES


Keyword Const Description

const short a = 5; Value of a can and should NEVER be changed anytime during the execution of the program. Will result in compiler error if attempt is made to change const variable Lets play with words (and pointers) int const * ptr; const int * ptr; // IS THERE A DIFFERENCE?? Constant variables ( oxy-moron !!!) are good candidates for storage in registers. Specifies that any external process (hardware/software) can change the value of this variable anytime. Indicates to the compiler that it can never OPTIMIZE this variable. i.e store and use this variable directly from register. Read from memory EVERY TIME it is used.

volatile

Session 1

OPERATORS AND PRECEDENCE


er t r T e er t r Ass ci ti it left-to-right right-to-left Primary Expression Operators () [] . -> expr++ expr-Unary Operators * & + - ! ~ ++expr --expr (typecast) sizeof() */% +>> << < > <= >= == != & ^ | && ||

10

Arithmetic Operators

left-to-right

Bitwise Operators

Logical Operators

Session 1

OPERATORS AND PRECEDENCE


er t r T e ?: = += -= *= /= %= >>= <<= &= ^= |= , er t r Ass ci ti it right-to-left right-to-left left-to-right Ternary Operator Assignment Operators Comma

11

Session 1

OPERATORS AND PRECEDENCE


No Short cuts here!!! Refer the table at least once before every test and interview Its easy to sport an operation precedence question: Big expression with all operations possible. ++ or -- either in print statement, return statement or just before them. Remember i = a++ assigns a to i and then increments a i = ++a increments a and then assigns a to I Using various precedence operations in a program only makes it difficult to follow. BREAK IT DOWN!!! Whenever writing a program using a formula make sure to safe guard against precedence mistakes. Use () liberally. They are Free!!!!!

12

Session 1

OPERATORS AND PRECEDENCE

13

Logical operations stop the moment the value is determined. if ( (a) && (++b ==5)) // b is incremented ONLY if a is non zero NEVER COMPARE floating point numbers directly. 24.0 / 6.0 MAY or MAY NOT be equal to 12.0 / 3.0 ! Of ANY non zero number is zero and ! of zero is 1 (ALWAYS). ~ is also a bitwise operator. It is the inversion operator ~( 0xff) is 0x0. Note the difference between ~ and !. One is bitwise inversion and other is logical inversion. ^ is exclusive OR. Note the difference between arithmetic and unary -. a = 0-b; a = -( b);

Session 1

COMMON PROGRAMS

14

Fibonacci Series, palindrome number Check for a prime number, determine factorial of a number Extract the digits in a number i.e. if input 987, print 9 first, then 8 and then 7. Count the number of ones (bits) in a given number n. Determine if a given processor is little endian or big endian given a 32 bit input (long a), extract each byte of the 32 bit value. Swap two numbers without using a temporary variable.

Session 1

You might also like