C glossary I - a - c
auto :
auto in c is a storage class specifier. Auto indicates the variable is a automatic. Which means it is created automatically and destroyed automatically. This variable has a block scope.
This keyword is redundant.
All non-static local variables are auto by default.
Here both a and b are auto variables.
break:
This is used to break from a loop or a switch case. If used outside of loops and switches, you get a syntax error.
Note:
In the first example break causes the loop to print 1 to 3 instead of 1 to 9. When a is 1, break stops the loop.
In the second example, as break is not used, if the number is even, the code segment prints both even and odd .e.g. 8 is even 8 is odd. To correct the error, one should use break; at the end of line number 4.
boolean
Unfortunately C does not have boolean data type. Instead it treats all non-zero values as true and zero as false.
Here if (a) checks the condition if a is zero or non-zero. Since a is non-zero, it is true and printf is executed and print a is true.
Be careful about the conditions such as
if(a=2)
This condition is always true for any value of a. Because expression inside brackets is assignment and its value is non-zero.
case :
case is used for individual values inside a switch statement. If the switch expression matches any of the case values, the subsequent statements till a break are executed. If none of the case values are matching, then default statements are executed.
Note:
auto in c is a storage class specifier. Auto indicates the variable is a automatic. Which means it is created automatically and destroyed automatically. This variable has a block scope.
This keyword is redundant.
All non-static local variables are auto by default.
#include <stdio.h> int main() { auto int a; int b; ---- } |
break:
This is used to break from a loop or a switch case. If used outside of loops and switches, you get a syntax error.
Note:
- If used in a nested loop, break breaks from inner most loop
- break can not be used to break out of a function. For that one needs
to use return statement instead. - If break is not used in switch statement at the end of each case, execution will continue to next case block also.
1 a=1; 2 while(a<10) 3 { 4 printf("%d ",a); 5 if(a==3) 6 break; 7 a++; 8 } | 1 scanf("%d",&a); 2 switch(a%2) 3{ 4 case 1: printf("%d is even ",a); 5 case 0: printf("%d is odd ",a); 6 } |
In the first example break causes the loop to print 1 to 3 instead of 1 to 9. When a is 1, break stops the loop.
In the second example, as break is not used, if the number is even, the code segment prints both even and odd .e.g. 8 is even 8 is odd. To correct the error, one should use break; at the end of line number 4.
boolean
Unfortunately C does not have boolean data type. Instead it treats all non-zero values as true and zero as false.
int a=1; if(a) printf("a is true"); a=0; if(!a) printf("a is false"); |
Here if (a) checks the condition if a is zero or non-zero. Since a is non-zero, it is true and printf is executed and print a is true.
Be careful about the conditions such as
if(a=2)
This condition is always true for any value of a. Because expression inside brackets is assignment and its value is non-zero.
case :
case is used for individual values inside a switch statement. If the switch expression matches any of the case values, the subsequent statements till a break are executed. If none of the case values are matching, then default statements are executed.
Note:
- case values must be constants or constant expressions
- case values must be integer
- Since character constants are treated as int, case values can also be
character literals
------ switch(m+n) { case 3: printf("three");//valid break; case 3+2: printf("five");//valid. constant expression break; case m: printf("n is zero");//invalid. //non-constant expression not allowed break; case <0:printf("Negative");//invalid - relational op.s not allowed } -------- |
char:
Is a datatype in C. char is stored using a single byte and stored internally as integer ASCII (or other code which machine uses to represent characters) code. For all practical purposes C treats char as a byte sized integer. In fact you can use char in place of int to save memory. char can also be signed char or unsigned char. Default depends on the compiler
#include<stdio.h> int main() { char c = 'A'; printf("%c %d \n",c,c); int m=68; c = m; printf("%c",c); c = '0'; m=c; printf("c=%c m=%d\n",c,m); return 0; } | #include<stdio.h> int main() { unsigned char c; for (c = 0;c<128;c++) { printf("c=%c ASCII code = %d\n",c,c); } return 0; } |
This program prints A 65 D c=0 m=48 In line 7 c is assigned to an integer 68. Hence c will be 'D'. In line 10 m is assigned to c and m will be 48 which is ASCII code for 0. Second program prints the ASCII codes and corresponding characters for 0 to 127. continue: continue when used inside a loop, skips the current iteration of the loop and goes to next iteration. continue can only be used inside for, while or do-while statements.
#include<stdio.h> int main() { int i; for(i=1;i<100;i++) { if(i%3==0) continue; printf("%d\t",i); } return 0; } |
Program given above prints all integers from 1 to 100 excluding numbers divisible by 3. When i%3 is 0, next statements are skipped and loop will continue for next value of i
Comments
Post a Comment