What is padding in C?

In C what is meant by padding of bits? Consider the following structure,

struct number {
int a;
char b;
int c;
};


What is the size of this structure? An integer takes 4 bytes, char takes 1 and the third integer 4 bytes. Total 9 bytes. This is wrong. We can see the size of the structure is 12 bytes. What happens here? the compiler pads 3 bytes to char element. This is for proper alignment. In a 32 bit processor it checks the next data only after 32 bits. Else the compiler will have related problems. Now the compiler knows each data is 4 bytes wider. So the benefits.

Advertisements

Is the study of pointers in C is easy?

Here explains a little about pointers.  Yes the pointers are easy, of course. But take care in your building path. If you go through pointers deeply, you will see the complexity.

In C address is only for l-values. It can be a variable or something. If we assign p = &a, the type of p is int * – pointer to an integer. type of ‘a’ is int and type of ‘&a’ is int *. C language is a weakly typed language. ie when we use assignment,  C allows to assign the right most thing to the left most without checking the types of these two.

int *p;
p = 1;
*p = 10;
Consider the above statements in C. We declared p as a pointer variable. Then 1 is assigned to p. In the third statement 10 is assigning to the memory location 1. What about the memory location 1, is there exists a memory address 1. Usually not. If yes, of course it isn’t accessible. OS allows the program to access only within a particular memory location. This is Memory protection. If OS takes no care about memory protection what happens? In that system so many programs are running and two programs want to store data on say 2000 th location then what happens? The programs access wrong data.

int *p;
p = 0;
*p = 10;

By executing above statements there is no error at compile time. Why? C takes zero as a pointer.

Its easy to go further.

Programming in C: Remember these points on constants.

A ‘long’ constant is written like 1234567697L or 567874338l. Big integer type constant is also taken as long. ‘unsigned’ constants written with a terminal u or U. ‘unsigned’ long type constants written with the terminal ul or UL. Floating point constants contain decimal point or an exponent (1e-1) or both. They are declared as ‘double’ type. The value of an integer can be specified as octal or hexadecimal. A leading zero on an integer constant means octal and a leading 0x or 0X means hexadecimal.

decimal : 31 octal : 031 hex : 0x1F

Character constants :

It is an integer. eg.’x’. A constant expression is an expression that involves only constants. They are used in any place where a constant occurs. A string constant or a string literal is a sequence of zero or more characters surrounded by double quotes.

In switch each case is labeled by integer valued constants or constant expressions. In the while and do loops ‘continue’ means that the test part is executed immediately. In for loop the control passes to the increment step. The continue statement applies only to loops not to ‘switch’.

Programming in C : Remember these points on operators.

The cast operator has same high degree of precedence as other unary operators. The increment and decrement operators can only be applied to variables. An expression like ‘(exp) ++’ is illegal. Bitwise shift operators can be used with constants or variables.

Var >> n

Here ‘Var’ is shifted n positions to the right. ‘n’ must be a positive value. Bitwise operators are using only on integral operands.

Programming in C: Remember these points on Datatypes.

‘int’ is 32 or 16 bit long and it depend upon machines. ‘short’ is often 16 bit long. ‘signed’ and ‘unsigned’ may be applied to char. But is plain chars are signed or unsigned ? It is machine dependent. If ‘x’ is float and ‘i’ is int, x = i and i = x both cause conversions.

Specify type correctly:

Please take a nice look to these points,
In C when we write an expression we must ensure that we specified the types correctly.
In an expression even if the left hand side is declared as floating point and the right hand side contain integer arithmetic & constant division, these constant division results the number truncated to integer. So we must represent these constants, as floating point number.
Consider the following expression

(float) a = (b – 18) * 7 / 9 ;

false result will goes to ‘ a ‘
we must write as,

(float) a = (b – 18) * 7 .0 / 9.0 ;

or

(float) a = (float) (b – 18) * 7 / 9 ;
Now,
when talking about the range of int , float it depends on the machines,
16 – bit int that is 2^16 = 65536 , dividing by 2 , 65536 / 2 it equals 32768
that is range – 32768 to 32767 for signed integers.

Consider the following expression,

(n > 0) ? f : n ;

If ‘f’ is float and ‘n’ is int the expression has type ‘float’ according to the conversion rule, not by the result of the condition.

Programming in C: Remember these points on Variables.

Don’t begin variable names with underscore, since library routines often use such names. Upper and lower case letters are distinct. Atleast first 31 characters of an internal name are significant. External names may be used by assemblers and loaders over which the language has no control. The definition of C gurantees that any character in the machine’s standard printing character set will never be negative.

Programming in C: A Small note on Functions.

printf, getchar(),putchar() all are C functions. No fuction can be split between files. ‘main’ function returns a value ,zero if normal termination or nonzero erroneous termination. The function prototype agree with the definition and uses of the function. If the actual arguments are more than the formal arguments, the extra arguments are discarded. And if the actual arguments are smaller than the formal arguments extra formal arguments are initialized to some garbage values. Automatic variables do not retain their values from one call to the next and must be set upon each entry. Any expression may follow a return statement. To assure a high degree of portability between programs a function should generally be coded without involving any I/O operations.Function returns int as default.To assure a high degree of portability between programs, a function should generally be coded without involving any I/O operations.

If a function fails to return a value its value is certain to be garbage. We can also compile three source files using ‘cc’ command which contain the program functions.

$ gcc main.c fun1.c fun2.c

Consider the statement, x = function1() + function2();

Here the order in which the calculation, function1() may be evaluated first or function2() may. C cannot specify such an order.