






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive guide for java developers who want to extend their knowledge and start programming in c. It covers topics such as declarations, pass by value/reference, arrays & structures, pointers, library functions, compiling and debugging, and shell commands. Each topic includes explanations, examples, and code snippets.
Typology: Slides
1 / 11
This page cannot be seen from the preview
Don't miss anything!







C By Example 1
C By Example 2
1. “Hello World” – The
Simplest Program
This is an include file. It contains definitions of constants, structures, and functions
Printf is a library subroutine that you call to output data to a screen or file.
Every C program must have a main. It’s where the program starts execution.
This is the form used to declare and pass parameters into a program or subroutine. This says argc is an int and argv is a char string. If I run this program by saying “prog1 - C is fun” Then argc is 5 and argv[0] is “-”, argv[1] is “C”, etc.
C By Example 4
Pass by Value and Pass by Reference
#include <stdio.h>
void subroutineA( int , int * ); short functionB( int , int );
int main( int argc, char *argv[] ) { int var_A; int var_B;
var_A = 17; var_B = 33; subroutineA( var_A, &var_B ); printf( "Return from SubA: %d %d\n", var_A, var_B ); printf( "Return from FunB: %d\n", functionB( var_A, var_B ) ); } // End of main void subroutineA( int A, int *B ) { *B = A; // B is a pointer to an int } // End of subroutineA short functionB( int A, int B ) { return( A + B ); } // End of functionB
These are called “prototypes”.
Var_A is passed by value. Note the & on var_B which means that its address is passed in.
Note how int and ptr to int are declared.
C By Example 5
Arrays and Structures (1)
#define ARRAY_LEN 32
#include <stdio.h>
typedef struct { int arr1[ARRAY_LEN]; char string[ARRAY_LEN]; } MY_STRUCT;
void subroutineA( MY_STRUCT * );
This is called a “define”. Used to parameterize constants.
Here we define the structure MY_STRUCT. It’s laid out in memory as 32 ints (4 bytes each) plus 32 bytes arranged as a string.
We’ll be passing the address of this structure to the subroutine.
C By Example 7
Pointers
#include <stdio.h>
int main( int argc, char *argv[] ) { int a, b; int *p;
a = b = 7; p = &a; // p points to a printf( "p = %d\n", *p ); // 7 is printed *p = 3; printf( "a = %d\n", a ); // 3 is printed p = &b; *p = 2 * *p - a; printf( "b = %d\n", b ); // 11 is printed p = &a; printf( "Input an integer "); scanf( "%d", p ); } // End of Main
a and b are ints. p is a pointer to an int.
C By Example 8
Library Functions (1)
#include <ctype.h> int isalnum(int c); // returns TRUE if char is alpha or numeric int isspace(int c); // returns TRUE if char is white space int tolower(int c); // returns the conversion of c to lower case
#include <math.h> double cos(double x); double exp(double x);
#include <stddef.h> #typedef unsigned size_t; #define NULL ((void *) 0) #define offsetof(s_type, m)
((size_t) &(((s_type *) 0) ->m ))
#include <stdio.h> #define EOF (-1) // End of File #define NULL 0
These pages show include files and the library function prototypes that are contained in them.
C By Example 10
Compilation And Debugging
Babbage gcc – g prog4.c – o prog babbage% gdb prog (gdb) l (gdb) b 24 (gdb) r (gdb) p index (gdb) s (gdb) c (gdb) p my_struct $4 = {arr1 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, string = "\000\001\002\003\004\005\006\a\b\t\n \013\f\r\016\017\020\021\022\023
\025\026\027\030\031\032\e
\035\036\037"} (gdb) q babbage %
-g says prepare for debugging. – o is the name of the executable.
l says list lines of the program
b says set a breakpoint (at line 24 in this case)
r means run the program.
p says to print a variable.
s == single step c == continue to next breakpoint.
q == quit
C By Example 11
Shell Commands
babbage% vi make_all babbage% chmod 700 make_all babbage% ls -l make_all -rwx------ 1 jbreeche users 144 Jul 21 04:19 make_all babbage% more make_all gcc -g prog1.c -o prog gcc -g prog2.c -o prog gcc -g prog3.c -o prog gcc -g prog4.c -o prog gcc -g prog5.c -o prog gcc -g prog6.c -o prog
babbage% mkdir foo babbage% cd foo babbage% echo "This is a line" > bar babbage% cat bar This is a line babbage% rm bar
You need an editor – either emacs or vi.
Change privileges
ls – l == list directory contents
more == print out file, 1 screen at a time. mkdir == create a directory
cd == change directory echo == print a line. In this case “>” puts that line in a file. rm == delete a file (rmdir == delete a directory.)
“man” and “apropos” are my favorites.