Difference between revisions of "C programming quick start"
|  (→case statements) | |||
| Line 149: | Line 149: | ||
| == case statements == | == case statements == | ||
| − | + | switch ( <variable> ) { | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| + | :case this-value: | ||
| + | ::Code to execute if <variable> == this-value | ||
| + | ::break; | ||
| + | :case that-value: | ||
| + | ::Code to execute if <variable> == that-value | ||
| + | ::break; | ||
| ... | ... | ||
| − | + | :default: | |
| − | default: | + | ::Code to execute if <variable> does not equal the value following any of the cases | 
| − | + | ::break; | |
| − | Code to execute if <variable> does not equal the value following any of the cases | + | |
| − | + | ||
| − | break; | + | |
| − | + | ||
| } | } | ||
Revision as of 22:12, 10 July 2014
Content take from cprogramming.com
| Contents | 
Intro
#include <stdio.h>
The #include is a "preprocessor" directive that tells the compiler to put code from the header called stdio.h into our program before actually creating the executable. stdio.h provides input output functions like printf & scanf.
int main() { }
Main body of program. Returns integer exit status to O.S. after execution. 0 = success, non zero is an error message.
hello world example
- include <stdio.h>
int main() {
printf( "Hello world!\n" ); printf( "(hit enter to end)\n" ); getchar(); return 0;
}
compiling with gcc
gcc hello.c -o hello
run hello world and check exit status of last command
$ ./hello Hello world! (hit enter to end)
$ echo $? 0 $ ./hello Hello world! (hit enter to end) ^C $ echo $? 130 <<<--- Script terminated by Control-C exit code
variables
types Char: a single character
Integer: number without decimal place
float: number with decimal place
placement
variable declarations must come before other types of statements in the given "code block"
comment syntax
/* comment text /*
conditional statements
and: & or: || not: !
examples
A. !( 1 || 0 ) ANSWER: 0 B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR) C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)
if, else if , else statement
if ( TRUE ) {
   /* Execute these statements if TRUE */
}
else if ( TRUE ) {
   /* Execute these statements if TRUE */
}
else {
   /* Execute these statements if FALSE */
}
loops
for ( variable initialization; condition; variable update ) {
Code to execute while the condition is true
}
example
   for ( x = 0; x < 10; x++ ) {
       printf( "%d\n", x );
   }
while ( condition ) { Code to execute while the condition is true }
example
 while ( x < 10 ) { 
     printf( "%d\n", x );
     x++;            
 }
do {
} while ( condition );
keywords
The break command will exit the most immediately surrounding loop regardless of what the conditions of the loop
break;
The continue statement will stop the loop's current iteration, update itself, and begin to execute again from the top.
continue;
functions
prototype
return-type function_name ( arg_type arg1, ..., arg_type argN );
arg_type: int, float, char (examples)
the may be no arguments passed at all (where the parentheses are empty)
programmer defined function
int mult ( int x, int y );           /* function prototype goes in program "header" area above main /*
...
int main()
{
...
}
int mult (int x, int y)   /* function definition /*
{
  return x * y;
}
- note - no semicolon after argument section of function definition
case statements
switch ( <variable> ) {
- case this-value:
- Code to execute if <variable> == this-value
- break;
 
- case that-value:
- Code to execute if <variable> == that-value
- break;
 
...
- default:
- Code to execute if <variable> does not equal the value following any of the cases
- break;
 
}
 
					