A Quick Reference To C Programming Language
A Quick Reference To C Programming Language
Structure of a C Program
#include(stdio.h) /* include IO library */
#include... /* include other files */
#define.. /* define constants */
Comments
Format: /*(body of comment) */
Example: /*This is a comment in C*/
Constant Declarations
Format: #define(constant name)(constant value)
Example: #define MAXIMUM 1000
Type Definitions
Variables
Declarations:
Format: (variable type)(name 1)(name 2),...;
Example: int firstnum, secondnum;
char alpha;
int firstarray[10];
int doublearray[2][5];
char firststring[1O];
Initializing:
Format: (variable type)(name)=(value);
Example: int firstnum=5;
Assignments:
Format: (name)=(value);
Example: firstnum=5;
Alpha='a';
Unions
Declarations:
Format: union(tag)
{(type)(member name);
(type)(member name);
...
}(variable name);
Example: union demotagname
{int a;
float b;
}demovarname;
Assignment:
Format: (tag).(member name)=(value);
demovarname.a=1;
demovarname.b=4.6;
Structures
Declarations:
Format: struct(tag)
{(type)(variable);
(type)(variable);
...
}(variable list);
Example: struct student
{int idnum;
int finalgrade;
char lettergrade;
} first,second,third;
Assignment:
Format: (variable name).(member)=(value);
Example: first.idnum=333;
second.finalgrade=92;
Operators
Symbol Operation Example
+,-,*,/ arithmetic 1 = b + c;
% mod a = b % c;
> greater than if (a > b)
>= greater than or equal if (a >= b)
< less than if (a <b)
<= less than or equal if (a <= b)
== equality if ( == b)
= assignment a=25;
!= not equal if (a != b)
! not if (!a)
&& logical and if (a) && (b)
logical or if (a) (b)
++ increment ++ a;
-- decrement -- a;
& bitwise and a = b & c;
bitwise or a = b - c;
∧ a =b∧c
bitwise xor
>> shift-right a = b >> 2;
<< shift-left a = b << 2;
~ one's complement a = ~b
Print Examples:
print("firstvar+secondvar=%d\n",thirdvar);
Input:
Scanf Format:
scanf("(conversion specs)",&(varl),&(var2),...);
Scanf Example:
scanf("%d %d %d",&first,&second,&third);
Control Structures
FOR LOOP Format:
for ((first expr);(second expr);(third expr))
(simple statement);
for ((first expr);(second expr);(third expr))
{
(compound statement);
}
IF CONDITIONAL Format:
if ((condition))
(simple statement);
if ((condition))
{
(compound statement);
}
Function Definitions
Format:
(type returned)(function name)((parameter list))
(declaration of parameter list variables)
{
(declaration of local variables);
(body of function code);
}
Example:
Int. adder(a,b)
int a,b;
{int c;
c = a + b;
return (c);
}
Pointers
Declaration of pointer variable:
Format: (type)*(variable name);
Examples: int *p;
struct student *classmember;