You are on page 1of 3

comments

Allows inclusion of any remarks that are ignored by the compiler


syntax
/* anything */
where anything are any characters including line feeds (except star slash). Used
to add name, date, identifying information and explanatory comments to your sou
rce file to increase readability.
example
/**********************************************************
A. Treibergs
1-9-6
Week 1 First Example: Program to say hello
hello.c
**********************************************************/
for
Used to construct loops.
syntax
for( initialization ; test; increment)
{
statements;
}
where initialization is a statement that assigns the initial value to the indexi
ng variable, test is a logical statement , increment is a statment to update the
indexing variable, and statements are C code that is executed for that particul
ar value of the indexing variable provided the test is true.
example
for( i = 1 ; i <= 10; i = i+1 )
{
printf(" %d \t Hello! \n", i);
}
include
Preprocessor directive to include a header file
syntax
# include <file.h>
where file.h is a header file containing some functions needed by your code. Sta
ndard includes are for the standard function library, the standard input/output
routines and for the math library.
example
# include <stdlib.h>
# include <stdio.h>
# include <math.h>
Remember, to correctly link the math headers, invoke the compiler by cc foo.c -l
m in the xterm window.
main
Program recognized by the system that calls all subprograms
syntax
int
main(void)
{
statements;
return EXIT_SUCCESS;
}
where statements are all instructions run by your program. Only comments, global
variables, function prototypes, functions and preprocessing directives may prec
ede main. Technically, main is an integer valued function without arguments that
is recognized by the system, hence the int and void. return returns control to
the system. EXIT_SUCCESS tells the system that the program terminated normally.
stdlib.h must be included for this to work. main can also be called in a slightl

y abbreviated form.
example
int
main(void)
{
printf("Hello, world!\n");
return EXIT_SUCCESS;
}
printf
Function to print to the standard terminal window.
syntax
printf( format string, variable list);
where format string is a string to be printed to the console window. All the cha
racters of the string will be printed except for special combinations that indic
ate special characters or variable formats. For example, \n means line-feed, \t
means tab. Following the format string is the variable list, which may contain a
ny number of variables including no variables. Corresponding to each variable, t
here is a format specifier of the corresponding type and in the corresponding or
der in the format string. For example, %d corresponds to an integer variable, %f
to a floating point variable or a double precision variable. %lf may also be us
ed for double precision. %e or %E corresponds to printing a floating point numbe
r or double precision variable in scientific notation. In addition, the number o
f digits in the number and the number of digits in the fractional part may be sp
ecified, as in %10d or %20.15lf. %-12.6f meaans left-justify in the field and %+
20.8f means always print the sign. Finally %g will print a float or a double eit
her as a decimal fraction or in scientific notation as appropriate.
example
printf("Hello, world!\n");
printf("%6d. The area of a circle whose radius is \t %10.3f is %lf \n", ivar,f
var,dvar);
scanf
Function to take user input from the standard terminal window.
syntax
scanf( format string, list of &variables);
where format string is a string that describes the variable types to be read. Fo
llowing the format string is the list of &variables, which may contain any numbe
r of variables, each preceded by "&". Corresponding to each variable, there is a
format specifier of the corresponding type and in the corresponding order in th
e format string. For example, %d corresponds to an integer variable, %f to a flo
ating point variable and %lf (long float) corresponds to a double precision vari
able. This time, %f must be used only for a floating point variable and %lf must
be used only for a double precision variables. If you are scanf-ing more than o
ne variable, be sure to separate your input variables by white space characters
(space, tab, linefeed) and not by commas.
example
int n;
double x, epsilon;
printf(" Enter the number :");
scanf("%d", &n);
printf(" Please type x and epsilon :\n");
scanf("%lf %lf", &x, &epsilon);
style
It is good practice to print a prompt to remind the user what variable is being
requested.
int, float, double, char &c.
Assigns memory of various types to variable names
syntax

char variable list and initial values;


double variable list and initial values;
float variable list and initial values;
int variable list and initial values;
where the type declarer assigns the kind of memory to be reserved for the variab
les named in the list, and variable list and initial values list all the variabl
es used in the program of the given type. All variables must be assigned before
they can be used. More memory is assigned for double precision variables than fo
r floating point variables. Both of these types carry a fractional part and an e
xponent. The integer variables are signed whole numbers. Character variables hol
d individual characters (actually character variables are integers that hold the
ASCII code for the character and may be manipulated like integers). All of thes
e types allow arrays which are indexed lists of variables, such as vectors, matr
ices and strings. C does not automatically initialize the variables. Before init
ialization they contain whatever may have been in the memory from before. The va
riables can be given an initial value right in the type declaration statement, o
r may be assigned later.
example
double x,y,z=1.0;
float whoseit;
char a='A',b, c='C';
b = 'B';

You might also like