Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Language Reference The C language is a general-purpose programming language known for its efficiency, economy, and portability. While these characteristics.

Similar presentations


Presentation on theme: "C Language Reference The C language is a general-purpose programming language known for its efficiency, economy, and portability. While these characteristics."— Presentation transcript:

1 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

2 Palavras rervadas auto doubleintstruct break elselongswitch case enumregistertypedef char externreturnunion const floatshortunsigned continueforsignedvoid defaultgotosizeofvolatile doifstaticwhile C Keywords (only 32)

3 Tipos de dados unsigned 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 */ integral types floating types

4 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; };

5 Arrays int x[100]; for (i=0; i<100; i++) x[i]=i; int x[10][20]; não recomendo

6 Operadores numéricos ++ –– Unary increment and decrement operators * / % Multiplicative operators + – Additive operators (also unary + or -) 7%3 is 1 i++ is i=i+1 precedence

7 Operadores lógicos e relacionais OperatorMeaning = == != Relational operators (greater than, …) && || ! Logical operators (AND, OR, NOT) Obs.: 1 (TRUE) and 0 (FALSE) (100<200) && 10 is TRUE

8 Operadores bit a bit & | ^ Binary bitwise operators (AND, OR, XOR) > Unary shift operators (left, right) ~ Ones 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

9 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));

10 Controle de fluxo ( if ) if ( expression ) statement if ( 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

11 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] = ' '; }

12 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--; }

13 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; }

14 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 }

15 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?

16 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

17 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; }

18 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; }

19 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.c part1.c extern int counter; extern char f1(int n1); or char f1(int n1); part1.c part2.c

20 Diretivas de pré-compilação #define WIDTH 80 #define LENGTH ( WIDTH + 10 ) #ifndef _D3D_H_ #define _D3D_H_ … #endif /* _D3D_H_ */ #include

21 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


Download ppt "C Language Reference The C language is a general-purpose programming language known for its efficiency, economy, and portability. While these characteristics."

Similar presentations


Ads by Google