Skip to Main Content or Page Contents
Command |
Use |
Description/comment |
Example |
|
-- | a -- | |||
! | Operator | NOT | if !(a == 0); | |
!= | Operator | NOT Equal to | ||
#define | Define a CONSTANT | #define PI 3.1415927 | ||
#include | Add the contents of an include file <> denotes file in a known library folder. "" denotes file |
#include <stdio.h> #include "tms.h" |
||
^ | bitwise exclusive or. result 1 if either bit is 1 only | |||
& | bitwise and. result 1 if both bits 1 | |||
&& | Operator | && operator (and) | ||
| | bitwise or. result 1 if either bit is 1 or both bits 1 | |||
|| | Operator | || operator (or) | ||
% | Operator | Modulo Division remainder = b % c | remainder=7%2 ans 1 | |
%+4d | Format | Forces the sign + or to be displayed | Use below in printf & scanf | |
% d | Format | Prints a space in place of + sign | ||
%*d | Format | As above using a variable | printf("N=% d", Fortnum, age); | |
%*.*f | Format | As above using 2 variables | ||
%+4d | Format | Forces the sign + or to be displayed | ||
%04d | Format | Pad with leading zeros | ||
%4d | Format | Format to 4 chars. | ||
%-4d | Format | Left justify | ||
%5.3f | Format | Format to 5 chars for full num, 3 decimal places | cprintf("The average is %5.4f",average); | |
%-5.3f | Format | As above but LEFT justified | ||
%c | Format | Character | ||
%d | Format | Integer | ||
%f | Format | Floating point / Double | ||
%G | Format | Force a Decimal point | ||
%ld | Format | Long Integer | ||
%p | Format | Hexadecimal | ||
%s | Format | String | ||
%ud | Format | Unsigned Integer | ||
&& | Operator | AND make left most likely false | if (gender == 1 && age >= 65) | |
* | Operator | |||
/ | Divide, no remainder when used on integers | 7/3 answers 2 | ||
/* */ | Comments that can be spread of several lines if required. | /* Comments */ | ||
// | Comment. Older compilers may not support this | // Comments to end of line. | ||
; | ||||
{ } | Marks the start and end of a section of code for a function. | T2 { } | ||
( ) | Follows functions, & holds parameters | cprintf ("Hello" ); | T2 ( ) | |
[ ] | ||||
\307C | Displays ° C | |||
\' | format | single quote | ||
\" | format | double quote | ||
\\ | format | backslash | ||
\0 | format | Null Char. | ||
\a | format | alert(bell) | ||
\b | format | backspace | ||
\f | format | formfeed Cursor to next logical Page | ||
\n | format | newline cursor down 1 line | Often used with \r in a string \n\r |
T2 \n |
\r | format | return Beginning of current line | Often used with \nin a string \n\r |
T2 \r |
\t | format | horizontal tab | ||
\v | format | vertical tab | ||
|| | Operator | OR make left most likely to be true | ||
+ | Operator | |||
++ | Operator | i++ ++i | Tut 3 Shortcut | |
-- | Operator | i-- --i | ||
< | Operator | |||
<= | Operator | |||
= | Assign | N.B. Logic error if confused with == | num = 7; 7 is assigned into variable num | Tut 3 |
== | Operator | Equal to | if(a == b); | |
> | Operator | |||
>= | Operator | |||
+= |
Operator | num += 3 | Tut 3 Shortcut | |
---|---|---|---|---|
-= |
Operator | num -= 3 | ||
*= |
Operator | num *= 3 | ||
/= |
Operator | num /= 3 | ||
%= |
Operator | num %= 3 | ||
| Top of Page |
ARRAYS | Declare array with 8 elements, 0-7 Assign 2 into sixth element 6 rows 9 cols Initialises the 1st element to 0, & the remaining elements by default to 0 Use symbolic constants to define the number of elements in an array See char for strings | int num[ 8] ; num[ 5]=2 ; int TwoDimentionArray [ 6] [ 9] ; int n[ ] = { 32, 10, 5,14 }; int n[ 10] = {0} ; # define SIZE 10 int n[SIZE] ; | |
atof | <math.h> <stdlib.h> | Converts a string to floating point, or 0 if cant convert | f = atof(str) ; |
atoi | <math.h> <stdlib.h> | Converts a string to integer, or 0 if cant convert | i = atoi(str) ; |
break | Keyword | use within while do for switch | if ( n == 5) break ; |
| Top of Page |
case | See switch | |||
ceil, ceill | <math.h> | Round up See floor | up=ceil(num); up=ceill(num); | |
cgets | ||||
casts | floatVariable=(float) intVariable * 2.3 | |||
char | Data type Keyword | can be displaced as a letter or number pointer to a variable Array declaration str_s [ c], pointer to a variable printf("ASCII value of %c is %d", letter, letter) ; | char letter='A' ; char *String="Some Name" char name[ ]="John" char *n [ ]={{ "item1"}, { "item2"}, { "etc. "}} ; char menu[]="a. first\r\n" ="b. second\r\n" ="9. ninth\r\n"; cprintf (menu); | |
clearerr | <conio.h> | |||
clrscr() ; | <conio.h> | T2 clrscr() r30 | ||
const | Keyword | Define a Constant | const yr_now = 1996 ; | |
continue | Misses the rest of the code in a loop cycle | |||
cprintf() | <conio.h> | function that displays a formatted string on to the screen. Not in ANSI, used by T223 students. | cprintf( "Hello World" ) ; cprintf( "Number=%i ", num1) ; |
Tut 2 cprintf() |
| Top of Page |
declaration | Declaration See int float char | ||
default | |||
define | define CONSTANTS | #define PI 3.1415927 | |
delay(); | <dos.h> | Delay in millisecs. | delay(1000) ; // 1 second delay |
do while | Keyword | Loop while (++ counter <=10) ; | do { statement(s) ; } while (condition); |
double float | Keyword | Data type | double float lge_num1, lge_num2; |
continue | |||
else | Keyword | see: if | see Tut 5 |
eof | is a negative num, usually -1 usually Ctrl + z on a PC ( system dependant) | ||
<stdlib.h> | exit(0) ok exit(1) error |
| Top of Page |
xx| Top of Page |
getch | <conio.h> | No echo, | c = getch() ; switch(getch() ) { .. } |
getchar | <studio.h> | Gets a Char, echo to screen, wait for Carriage Return. On end of File or error returns EOF | while((c = getchar()) != '\n') |
getche | <conio.h> | Gets Char, screen echo | c = getche() ; |
gets | <studio.h> | Gets a string. Declare: char string[ 80] | gets(string) ; |
gotoxy | <conio.h> | Not Win32s or Win32 GUI | gotoxy( col, row ) ; |
if( ) |
Keyword | if ((x < y) && (x > 0)) | if (count < 50) count++; if (( x < y ) && x > 0 )) { doSomething ; }Tut 6 |
if( ) else | Note in the example where the semi colon is NOT used | if ( x < y ) { printf ("y is larger") ; } else { printf ("x is larger") ; } |
|
int | Keyword | Data type Range (varies) 32,768 to +32,767 unsigned o to 65,535 Array declaration num [ x ] | int num, age, num2; unsigned int num; |
isalnum( c ) | <ctype.h> | Returns True if c is a letter or digit | if(isalnum( c )) |
isalpha( c ) | <ctype.h> | letter | |
isascii( c ) | <ctype.h> | ASCII between 0 127 | |
iscntrl( c ) | <ctype.h> | delete char, or ordinary control char | |
isdigit( c ) | <ctype.h> | Digit. Read chars until nondigit is input | for (I=0; isdigit( c ) ; c=getchar() ) |
islower( c ) | <ctype.h> | lowercase letter | |
isprint( c ) | <ctype.h> | printing char including space | |
ispunct( c ) | <ctype.h> | punctuation char | |
isspace( c ) | <ctype.h> | space, tab, vert tab, line or form feed, CR // skipover leading whitespace | while (isspace(c=getchar()) ) ; |
isupper( c ) | <ctype.h> | uppercase letter |
| Top of Page |
kbhit | |||
long | Keyword | Data type unsigned 0 to +4,294,967,295 | long num, age, num2; unsigned long num; |
main( ) | Requires { } | T2 main() | |
modf | Returns remainder, stores integer part in *ipart see fmod | ||
pow | <math.h> | Raise to the power of | n = pow(2.4 , 3) ; // power of 3 |
printf() | <stdio.h> | Displays info on the screen, may include text &/or
variables . T223 students use cprintf() in place of printf() |
printf("Hello World in %d", thisYear); |
putchar | <stdio.h> | Display the value in variable | putchar(letter); |
rand | <stdlib.h> | Produces a number in range: 0-32767(min of) 0-5 1-6 Produces same set of numbers. See: srand(seed) | i = rand( ) ; i = rand( ) % 6 ; i = (rand( ) % 6) + 1 ; |
RAND_MAX | Symbolic constant to holding max. rand range | e.g. 32767 or more, system dependent | |
return | return(avalue) |
| Top of Page |
scanf() | <stdio.h> | Reads data entered at the keyboard, and assigns it to a variable Must start with literal string. Note & address operator | scanf("%d", &number); scanf("%d %d", num1,num2); |
short | Keyword | Data type | short num, age, num2; |
sprintf | |||
srand | <stdlib.h> <time.h> |
Proceed rand to seed the rand srand(time(NULL)) ; // super seeding | srand(seed) ; // seed is unsigned integer |
sscanf | |||
str xxx | Check | with your compiler upper / lower case differences | |
strcat | <string.h> | string concatenation Page T421 | strcat (str1, str2) ; |
strcmp | <string.h> | compare returns 0 if=, 1 if s1 is > s2, 2 if s1 is < s2 | i = strcmp(s1, s2) ; |
strcpy | <string.h> | Copy Page T419 | strcpy (strDest, strSource) ; |
strlen | <string.h> | String Length | iLength = strlen(str) ; |
strncat | <string.h> | string concatenation Page T423 | strncat (str1, str2, num) ; |
strncmp | <string.h> | i = strncmp(s1, s2, num) ; | |
struct | |||
switch | <stdio.h> | without the break all statements, including later case statements, after the first match are processed case 'd' : case 'D' : case '\n' : case ' ' : break //ignores newline & spaces | Tut 6switch ( switch variable ) {case constantExpression : statement; [break;] . default : statement; } |
| Top of Page |
tolower(c) | <ctype.h> | See also lowercase() | string[ i ] = tolower(string[ i ]) ; |
toupper(c) | <ctype.h> | ||
typedef | Keyword | Used to create new names for existing variables and function types | |
unsigned | Keyword | Data type will contain only positive values See int long | unsigned short num, age, num2; |
void | |||
wherew() | goto(2,wherey()+3) | ||
while | Loop | See also do ... while count = 0; while( ++ count <= 5) ; // 5 repetitions |
while (condition) { statements ; }
|
| Top of Page |
C Tutorial 4. Branch & Loop Introduction
C Tutorial 5. Branch & Loop Code
This site is hosted on Hostgator