C gloassary - t -z
typedef:
typedef gives a name to data type. For example, you may find it convenient to define a variable of type INTPOINTER than of type int *.
Syntax :
typedef existing-type NEWDATATYPE;
Normally this new data types defined are written in CAPS.
e.g
typedef int INTEGER;
INTEGER a,b,c;//more legible
typedef unsigned int POSITIVE_INT;
POSITIVE_INT m,n;
struct node {int a;int b; struct node * ptr;};
typedef struct node * NODEPTR: //very helpful
NODEPTR n1,n2;//n1 and n2 are struct pointers
Always typedef s are preferred over #define s. Let us look at an example
#define INTPTR int *
INTPTR p1,p2;//p1 is int ptr and p2 is int
typedef int * INTPTR2;
INTPTR2 p1,p2;//p1 and p2 are both int ptr
NODEPTR n1,n2;//n1 and n2 are struct pointers
Always typedef s are preferred over #define s. Let us look at an example
#define INTPTR int *
INTPTR p1,p2;//p1 is int ptr and p2 is int
typedef int * INTPTR2;
INTPTR2 p1,p2;//p1 and p2 are both int ptr
unsigned char : The 8 bit positive char. If you define a char in program, the compiler may treat it as signed char or unsigned char. (Depends on the compiler. gcc treats it as signed char). To have only positive values, you have to use unsigned char.
Range : 0 to 255
Example :
#include<stdio.h>
int main()
{
unsigned char ch;
for(ch=0;ch<255;ch++)
{
printf("Code =%d value=%c\n",ch,ch);
}
return 0;
}
| The program prints the character codes and corresponding characters for values from 0 to 254 |
unsigned int : In an int variable, most significant bit(MSB) is used to store the sign. The bit will be 1 for negative numbers. If you want to use only positive integers, you can use unsigned int datatype. The range of unsigned int will be double that of ordinary int.
Different types:
unsigned int, unsigned short int, unsigned long int, unsigned long long int
Unsigned int constants must have a suffix of U/u
Example:
#include<stdio.h> int main() { unsigned short int n; int i; for(i=0;i<=16;i++) { n = 1U< printf("n=%u\n",n); } return 0; } | n=1 n=2 n=4 n=8 n=16 n=32 n=64 n=128 n=256 n=512 n=1024 n=2048 n=4096 n=8192 n=16384 n=32768 n=0 |
volatile : volatile is a qualifier for data type. It tells the compiler that the variable may get changed any time by external programs/hardware. Hence the compiler will not perform any optimization on a volatile variable.
e.g.
volatile int a=0;
while(a==0)
;
printf("delay completed");
|
In the above example, the while loop is waiting for another event to modify our varaible a to non-zero. Once it is changed, program will proceed. If you do not specify the variable to volatile, compiler will see the while loop as redundant and remove it for optimization.
Another classic example of volatile variable is kernel variable jiffies which indicates the number of clock ticks since the system is switched on. The Linux kernel has defined it as volatile unisigned int as system clock interrupt will update this variable.
Volatile variables are typically used in embedded applications.
See wikipedia article about volatile here
while : while is a loop construct in c. There are two types of while loops while and do---while
while loop
syntax :
while(condition)
statement;
The statement will be repeatedly executed as long as the condition is true.
If there is a break inside the loop, the loop is terminated. If a continue is used, then the current iteration is skipped.
I
int a=1;while(a<10) printf("%d\n",a); | 1 1 1 --- Infinite loop |
II
int a=1;while(a<10) printf("%d\n",a++); | 1 2 3 -- Prints from 1 to 9 |
III
int a=1;while(1) { printf("%d\n",a++); if(a=10) break; } | Prints only 1 instead of 1 to 10 |
IV
int a=1;while(1) { printf("%d\n",a++); if(a==10) break; } | prints numbers from 1 to 10 |
V
int a;while(a<10) { printf("%d\n",a++); } | No output because a is not initialized |
VI
int a;while(a<10) printf("%d\n",a); a++; | Infinite loop again. a++ is not inside the loop. |
In program piece I, we get an infinite loop because, a is 1 and is never incremented inside the loop. The statement in loop prints 1 and then a<10 is tested which is true and again a is printed and this keeps on repeating.
In program piece II, this error is corrected by incrementing a, in the printf function. You can also write it as two statements, printf and then a++ separately inside a block.
In program piece III, printf is repeated only once because, the condition is written wrongly. if (a=10) assigns a to 10 and then checks this value of a. Since a is now 10 which is non-zero value, it is true. This causes the break to be executed and loop terminates. The sad part is compiler does not complain about wrong if statement.
gcc with option -Wall, gives a warning
whileloop.c:10: warning: suggest parentheses around assignment used as truth value
Also in program III, we have not used a condition, in while, but just (1). This is fine because, it is treated as while(true)
This error is corrected in program IV.
In program V, because a is not initialized, it takes some garbage value which most of the times will be greater that 10. So the loop is not repeated at all and there is not output.
In program segment VI, even though we increment a, this statement is not inside the loop. Hence while repeats printf statement infinitely printing 1 again and again. The error can be corrected by surrounding printing with braces as { printf(......);}
do while loop is exit controlled loop with the advantage that body of loop is executed atleast once.
Syntax :
do
statement;
while (condition);
There are no other differences between while and do while loops.
#include<stdio.h> int main() { int a; do { printf("Enter a number or 0 to stop"); scanf("%d",&a); if(a){ printf("square of %d is %d\n",a,a*a); } } while(a!=0); return 0; } | The program reads numbers and prints their squares until user enters 0 |
The program here repeats the loop at least once.
Comments
Post a Comment