Difference between revisions of "C programming quick start"

From thelinuxwiki
Jump to: navigation, search
Line 1: Line 1:
 +
 +
== Intro ==
  
 
'''#include <stdio.h>'''  
 
'''#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
+
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.  [ http://en.wikipedia.org/wiki/C_file_input/output stdio.h] provides input output functions like printf & scanf.
  
 
'''int main()'''
 
'''int main()'''
Line 38: Line 40:
 
  $ '''echo $?'''
 
  $ '''echo $?'''
 
  130              <<<--- Script terminated by Control-C exit code
 
  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"

Revision as of 20:51, 10 July 2014

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. [ http://en.wikipedia.org/wiki/C_file_input/output 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

  1. 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"