c language reference the c language is a general-purpose programming language known for its...
TRANSCRIPT
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.
Resumo de C
Palavras rervadas
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
C Keywords (only 32)
Tipos de dadosunsigned char var_name_1; /* ranges from 0 to 255 */signed char var_name_2; /* ranges from -128 to 127 */char alpha; /* plataform dependent */
short int var_name_3; /* 2 bytes */unsigned short int var_name_4;
long int var_name_5; /* 4 bytes */unsigned long int var_name_6;
int var_name_7;
float var_name_8; /* 4 bytes */
double var_name_9; /* 8 bytes */
inte
gral
type
sfl
oati
ng ty
pes
Estruturas e definição de tipos
struct struct_name {element 1;element 2;...
} struct_variable;
struct _sphere { int x; int y; float radius; } s1;
typedef type_specifier new_name;
typedef unsigned char uchar;
typedef struct _sphere Sphere;
Sphere s1;
struct _sphere { int x; int y; float radius; };
Arrays
int x[100];
for (i=0; i<100; i++) x[i]=i;
int x[10][20]; não recomendo
Operadores numéricos
++ –– Unary increment and decrement operators
* / % Multiplicative operators
+ – Additive operators (also unary + or -)
7%3 is 1
i++ is i=i+1
prec
eden
ce
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
& | ^ Binary bitwise operators (AND, OR, XOR)
<< >> Unary shift operators (left, right)
~ One’s complement
0100 1101& 0011 1011 _________ 0000 1001
0100 1101| 0011 1011 _________ 0111 1111
0100 1101^ 0011 1011 _________ 0111 0110
unsigned short int x;x=10; /* 00000000 00001010 = 8 + 2 = 10 */x=x<<1; /* 00000000 00010100 = 16 + 4 = 20 */x=x>>1; /* 00000000 00001010 = 8 + 2 = 10 */x=~x; /* 11111111 11110101 = ... = 65525 */
2x
Operadores de endereço
y = &x; /* 0x0064fdf4 */*y = 100; /* x = 100 */ z = *y/10; /* z = 10 */
& “address of”
* “at address”
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 ) statementif ( expression ) statement else statement
if ( i > 0 ) y = x / i;else { x = i; y = f( x );}
var = logical_exp ? exp1 : exp2 ;
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;
convenient “shorthand”
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 Div2(double a ) { a=a/2; return;}
void main( void ) { double a=5.0; Div2(a); printf(“ %d \n”, a);}
Qual o valor de a?
Valores e Referência
void Div2(double *a ) { *a=(*a)/2; return;}
void main( void ) { double a=5.0; Div2(&a); printf(“ %d \n”, a);}
&a
5.0
a
2.5
Classes de variáveisScope 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)
int counter=0, up, down;double x, y, z;
char f1(int n1){ …}
double f2(int n2){…}
void main (void){…}
extern int counter;
extern char f1(int n1);
or
char f1(int n1);
extern int counter, up, down;
extern double f2(int n2);
main.cpart1.c
extern int counter;
extern char f1(int n1);
or
char f1(int n1);
part1.c
part2.c
Diretivas de pré-compilação
#define WIDTH 80
#define LENGTH ( WIDTH + 10 )
#ifndef _D3D_H_#define _D3D_H_…#endif /* _D3D_H_ */
#include <stdio.h>
Referências em C
The C Programming LanguageBrian W. Kerningham, Dennis M. RitchiePrentice-Hall, 1978(ver tambem traducao de 1990 da CAMPUS)
C How to Program (second edition)H. M. Deitel, P. J. DeitelPrentice Hall, 1994