Resumo de C C Language Reference The C language is a general-purpose programming language known for its efficiency, economy, and portability. While these characteristics make it a good choice for almost any kind of programming, C has proven especially useful in systems programming because it facilitates writing fast, compact programs that are readily adaptable to other systems. Well-written C programs are often as fast as assembly-language programs, and they are typically easier for programmers to read and maintain. Palavras rervadas C Keywords (only 32) auto double int struct break else long switch case enum register typedef char extern return union const float short unsigned continue for signed void default goto sizeof volatile do if static while integral types Tipos de dados unsigned char var_name_1; signed char var_name_2; char alpha; /* ranges from 0 to 255 */ /* ranges from -128 to 127 */ /* plataform dependent */ short int var_name_3; unsigned short int var_name_4; long int var_name_5; unsigned long int var_name_6; /* 2 bytes */ /* 4 bytes */ floating types int var_name_7; float var_name_8; /* 4 bytes */ double var_name_9; /* 8 bytes */ Estruturas e definição de tipos typedef type_specifier new_name; typedef unsigned char uchar; struct struct_name { element 1; element 2; ... } struct_variable; struct _sphere { int x; int y; float radius; }; struct _sphere { int x; int y; float radius; } s1; typedef struct _sphere Sphere; Sphere s1; Arrays int x[100]; for (i=0; i<100; i++) x[i]=i; int x[10][20]; não recomendo precedence Operadores numéricos ++ –– Unary increment and decrement operators * / % Multiplicative operators + – Additive operators (also unary + or -) 7%3 is 1 i++ is i=i+1 Operadores lógicos e relacionais Operator Meaning < > <= >= == != Relational operators (greater than, …) && || ! Logical operators (AND, OR, NOT) Obs.: 1 (TRUE) and 0 (FALSE) (100<200) && 10 is TRUE Operadores bit a bit & << | ^ >> ~ 0100 1101 & 0011 1011 _________ 0000 1001 Binary bitwise operators (AND, OR, XOR) Unary shift operators (left, right) One’s complement 0100 1101 | 0011 1011 _________ 0111 1111 0100 1101 ^ 0011 1011 _________ 0111 0110 2x unsigned short int x; x=10; /* 00000000 00001010 x=x<<1; /* 00000000 00010100 x=x>>1; /* 00000000 00001010 x=~x; /* 11111111 11110101 = = = = 8 + 2 = 10 16 + 4 = 20 8 + 2 = 10 ... = 65525 */ */ */ */ Operadores de endereço & “address of” * “at address” y = &x; *y = 100; z = *y/10; /* 0x0064fdf4 */ /* x = 100 */ /* z = 10 */ int x[100]; int *y; y=x; y[50]=37; /* x[50] contém 37 */ int *x; x = (int *) calloc(100, sizeof(int)); Controle de fluxo (if) if ( expression ) statement if ( expression ) statement else statement convenient “shorthand” var = logical_exp ? exp1 : exp2 ; if ( i > 0 ) y = x / i; else { x = i; y = f( x ); } x = (y<10) ? 20 : 40 ; x = (x<0) ? 0 : x ; M = (a<b) ? b : a ; x = x+10; x+=10; y = y/x; y/=z; Controle de fluxo (for) for ( init-expression opt ; cond-expression opt ; loop-expression opt ) statement for ( i = space = tab = 0; i < MAX; i++ ) { if ( line[i] == ' ' ) space++; if ( line[i] == '\t' ) { tab++; line[i] = ' '; } } Controle de fluxo (do-while) do statement while ( expression ) ; do { y = f( x ); x--; } while ( x > 0 ); while ( expression ) statement while ( i >= 0 ) { string1[i] = string2[i]; i--; } Controle de fluxo (switch) switch( c ) { case 'A': capa++; case 'a': lettera++; default : total++; } switch( i ) { case -1: n++; break; case 0 : z++; break; case 1 : p++; break; } Funções return_type function_name (parameter_list) { body of function double dot( double *u, double *v ) } { int i; double val=0.0; for (i=0; i<3; i++) val+=u[i]*v[i]; return val; } void main( void ) { program-statements } int main( int argc, char *argv[ ]) { program-statements } Valores e Referência void main( void ) { double a=5.0; Div2(a); printf(“ %d \n”, a); } void Div2(double a ) { a=a/2; return; } Qual o valor de a? Valores e Referência void main( void ) { double a=5.0; Div2(&a); printf(“ %d \n”, a); } &a void Div2(double *a ) { *a=(*a)/2; return; } a 5.0 2.5 Classes de variáveis Scope and Visibility - file, function, block, or function prototype int x; void main( void ) { int y; y=get_value(); x=100; printf(“%d %d”, x, x*y); } int f1( void ) { int x; x=get_value(); return x; } Modificadores de classe (static) /* Example of the static keyword */ static int i; /* Variable accessible only from this file */ static void func(); /* Function accessible only from this file */ int max_so_far( int curr ) { static int biggest; /* Variable whose value is retained */ /* between each function call if ( curr > biggest ) biggest = curr; return biggest; } */ Modificadores de classe (extern) main.c int counter=0, up, down; double x, y, z; char f1(int n1) { … } double f2(int n2) { … } void main (void) { … } part1.c extern int counter; extern char f1(int n1); or char f1(int n1); part2.c extern int counter, up, down; extern double f2(int n2); Diretivas de pré-compilação #define WIDTH #define LENGTH #include <stdio.h> #ifndef _D3D_H_ #define _D3D_H_ … #endif /* _D3D_H_ */ 80 ( WIDTH + 10 ) Referências em C The C Programming Language Brian W. Kerningham, Dennis M. Ritchie Prentice-Hall, 1978 (ver tambem traducao de 1990 da CAMPUS) C How to Program (second edition) H. M. Deitel, P. J. Deitel Prentice Hall, 1994