List of Some C CommandsThis page is under construction.
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 source 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 indexing 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 particular 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. Standard 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 -lm 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 precede 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 slightly 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 characters of the string will be printed except for special combinations that indicate 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 any number of variables including no variables. Corresponding to each variable, there is a format specifier of the corresponding type and in the corresponding order 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 used for double precision. %e or %E corresponds to printing a floating point number or double precision variable in scientific notation. In addition, the number of digits in the number and the number of digits in the fractional part may be specified, 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 either 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,fvar,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. Following the format string is the list of &variables, which may contain any number of variables, each preceded by "&". Corresponding to each variable, there is a format specifier of the corresponding type and in the corresponding order in the format string. For example, %d corresponds to an integer variable, %f to a floating point variable and %lf (long float) corresponds to a double precision variable. 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 one 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 variables named in the list, and variable list and initial values list all the variables 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 for floating point variables. Both of these types carry a fractional part and an exponent. The integer variables are signed whole numbers. Character variables hold individual characters (actually character variables are integers that hold the ASCII code for the character and may be manipulated like integers). All of these types allow arrays which are indexed lists of variables, such as vectors, matrices and strings. C does not automatically initialize the variables. Before initialization they contain whatever may have been in the memory from before. The variables can be given an initial value right in the type declaration statement, or may be assigned later.
example
double x,y,z=1.0;
float whoseit;
char a='A',b, c='C';
b = 'B';