//Headers and libraries #include #include #include #include #include //Comments // or /* */ //Types of variables Bytes Range(256^bytes) pointer 4 FILE 148 void 1 char 1 //-127 a 127 unsigned char 1 //0 a 255 int 4 //-2.147.483.648 a 2.147.483.647 unsigned int 4 //0 a 4.294.967.295 short int 4 //-32.768 a 32.767 unsigned short int 2 //0 a 65.535 long int 4 //same int unsigned long int 4 //same int long long int 8 //same int unsigned long long int 8 //same int bool 1 //0 a 1 float 4 //Six digits of precision double 8 //Ten digits of precision long double 12 //Ten digits of precision //Arithmetic operators Basic assignment = Addition + Subtraction - Multiplication * Division / Modulo % var++; //Suffix, the value of the variable is used and after incremented. --var; //Prefix, the value is incremented and used after the command. //Compound assignment operators var = var + var2; || var += var2; var = var - var2; || var -= var2; var = var * var2; || var *= var2; var = var / var2; || var /= var2; //Comparison operators/relational operators Greater than > Greater than or equal to >= Less than < Less than or equal to <= Equal to == Not equal to != //Logical operators Logical AND && Logical OR || Logical negation (NOT) ! //Operator precedence //Left-to-right Suffix increment, Suffix decrement, Function call //Right-to-left Prefix increment, Prefix decrement,Logical NOT, Type cast, Indirection(*), Address-of(&), Size-of //Left-to-right Multiplication, Division, Modulo, Addition, Subtraction, Less than, Less than or equal to, Greater than, Greater than or equal to, Equal to, Not, equal to, Logical AND, Logical OR //Right-to-left Direct assignment(=), Assignment by sum(+=), by difference, by product, by quotient, by remainder //Left-to-right Comma(,) //Format specifiers "%c" Character. "%s" String of character. "%d" Decimal. "%i" Integer. "%u" Unsigned Decimal. "%e" Scientific notation, lowercase. "%E" Scientific notation, uppercase. "%f" Decimal floating point, lowercase. "%F" Decimal floating point, uppercase. "%g" Use the shortest representation: %e or %f. "%G" Use the shortest representation: %E or %F. "%o" Octal. "%x" Hexadecimal integer lowercase. "%X" Hexadecimal integer uppercase. "%a" Hexadecimal floating point, lowercase. "%A" Hexadecimal floating point uppercase. "%%" % followed by another % character will write a single %. "%p" Pointer address. // Formatting codes New line "\n" Tab "\t" Backspace "\b" Quotes "\"" Slash "\\" Jump form "\f" Null "\0" int printf ( const char * format, ...);//The total number of characters written is returned. /*A format specifier follows this prototype: %[flags][width][.precision][length]specifier Flags Description - Left-justify. + Plus or minus sign (+ or -) even for positive numbers. Width Description number Minimum number of characters to be printed. * The width is not specified in the format string, but as an additional integer value. precision Description .number Precision specifies the minimum number of digits to be written or number of digits to be printed after the decimal point. .* The width is not specified in the format string, but as an additional integer value. */ int scanf ( const char * format, ...);//The function returns the number of items of the argument list successfully filled. /*A format specifier follows this prototype: %[*][width][length]specifier sub-specifier Description * An optional starting asterisk indicates that the data is to be read from the stream but ignored. width Specifies the maximum number of characters to be read in the current reading operation. */ int fflush ( FILE * stream );//A zero value indicates success. //If stream is a null pointer, all such streams are flushed. //stdin -> Standard input stream //stdout -> Standard output stream //stderr -> Standard error stream int getchar ( void );//On success, the character read is returned (promoted to an int value). //Returns the next character from the standard input (stdin). #include int main(){ int var; printf("Enter any value:"); fflush(stdin); scanf("%d",&var); printf("\nvar = %d",var); printf("\nThe double %d is %d",var,2*var); fflush(stdin); getchar(); return 0; } //if - else - else if if (condition) { /* Block to be executed if the condition is true */ } else { /* Block to be executed if the condition is false */ } //switch - case switch (var) { case value_1: instructions; break; case value_n: instructions; break; default: instructions;; } // ?: condition ? true : false //while while (condition) { instructions; } //while infinite while (1) { instructions; } //do - while do{ instructions; }while (condition); //for for (initializations, condition, increments) { instructions; } //break and continue break;//Always causes program execution to continue at the first statement following the loop or block. continue; //Equates to reach the end of the block, the increments are performed and the condition is reevaluated. //Exemplo vetor #include int main(){ int vetor[5],i; float media = 0; for(i = 0;i < 5;i++){ printf("Vetor[%d]: ",i); scanf("%d",&vetor[i]); } for(i = 0;i < 5;i++){ media = media + vetor[i]; } printf("\nMédia = %.1f",media/5); return 0; } //Exemplo matriz #include int main(){ int matriz[3][3] = {00,01,02,10,11,12,20,21,22}; int i,j; for(i = 0;i < 3;i++){ printf("\n"); for(j = 0;j < 3;j++){ printf("%.2d ",matriz[i][j]); } } printf("\n"); for(i = 0;i < 3;i++){ printf("\n"); for(j = 0;j < 3;j++){ if(i == j){ printf("__ "); } else{ printf("%.2d ",matriz[i][j]); } } } printf("\n"); for(i = 0;i < 3;i++){ printf("\n"); for(j = 0;j < 3;j++){ if(i + j == 2){ printf("__ "); } else{ printf("%.2d ",matriz[i][j]); } } } printf("\n"); for(i = 0;i < 3;i++){ printf("\n"); for(j = 0;j < 3;j++){ if(i > j){ printf("__ "); } else{ printf("%.2d ",matriz[i][j]); } } } printf("\n"); for(i = 0;i < 3;i++){ printf("\n"); for(j = 0;j < 3;j++){ if(i < j){ printf("__ "); } else{ printf("%.2d ",matriz[i][j]); } } } printf("\n"); return 0; } //Exemplo string #include #include int main(){ int i,aux=0; char letra,string[201]; char letraA = 'a'; char string1[101] = "string"; char string2[101] = {'s','t','r','i','n','g','\0'}; printf("String1: "); puts(string1); printf("String2: "); puts(string2); printf("\nDigite um caractere: "); letra = getchar(); fflush(stdin); printf("Digite uma string: "); gets(string); printf("\nCaractere: "); putchar(letra); printf("\nString: "); puts(string); printf("Tamanho da string: %d\n",strlen(string)); if(strcmp(string1,string2) == 0) printf("\nString1 == String2"); else printf("\nString1 != String2"); strcpy(string1,"Um"); strcpy(string2,"Dois"); printf("\n\nString1: "); puts(string1); printf("String2: "); puts(string2); strcat(string,string1); strcat(string,string2); printf("\nString: "); puts(string); for(i = 0;i < strlen(string); i++){ if (string[i] == letraA){ aux++; } } printf("\nQuantidade de letras a na String: %d",aux); return 0; } //Exemplo switch - case #include int main(){ int num; printf("Digite 1 para solteiro ou 2 para casado:\n"); scanf("%d",&num); printf("\n"); switch(num){ case 1: printf("Solteiro"); break; case 2: printf("Casado"); break; default: printf("Valor invalido"); } return 0; } //Exemplo while #include int main(){ char tecla; while(tecla != 'a'){ tecla = getchar(); } return 0; } //Exemplo do - while #include int main(){ int i=0; do{ i++; printf("%d\t",i); }while(i < 10); return 0; } // Variáveis locais e globais #include int varGlobal;// Qualquer função pode acessá-la. main(){ int varLocal;// Apenas a função main() tem acesso a esta variável. } //Exemplo 1 Modularidade(Passagem por Valor) #include float media(float a, float b, float c); int main(){ float n1,n2,n3; printf("Digite três valores quaisquer: \n"); scanf("%f%f%f",&n1,&n2,&n3); printf("\nMédia = %.1f",media(n1,n2,n3)); return 0; } float media(float a, float b, float c){ return (a + b + c) / 3; } //Exemplo 2 Modularidade #include float n1,n2,n3; void media(); int main(){ printf("Digite três valores quaisquer: \n"); scanf("%f%f%f",&n1,&n2,&n3); media(); return 0; } void media(){ printf("\nMédia = %.1f",(n1 + n2 + n3) / 3); } //Exemplo Ponteiros #include int main(){ int a, *pa; a = 50; pa = &a; // pa recebe o endereço de a; printf("\nEndereço do ponteiro pa: %X", pa); printf("\nEndereço da variável contida no ponteiro pa: %X", &pa); printf("\nValor da variável contida no ponteiro pa: %d", *pa); return 0; } //Exemplo 1 Parâmetro(Passagem de um valor por referência) #include float media(float *a, float b, float c); int main(){ float n1,n2,n3; printf("Digite três valores quaisquer: \n"); scanf("%f%f%f",&n1,&n2,&n3); printf("\nMédia = %.1f",media(&n1,n2,n3)); return 0; } float media(float *a, float b, float c){ return (*a + b + c) / 3; } //Exemplo 2 Parâmetro(Passagem e retorno por referência) #include void media(float *a, float *b, float *c, float *retorno); int main(){ float n1,n2,n3,aux; printf("Digite três valores quaisquer: \n"); scanf("%f%f%f",&n1,&n2,&n3); media(&n1,&n2,&n3,&aux); printf("\nMédia = %.1f",aux); return 0; } void media(float *a, float *b, float *c, float *retorno){ *retorno = (*a + *b + *c) / 3; } //Exemplo 3 Parâmetro(Vetor por referência) #include void media(float *vetor, float *retorno); int main(){ float v[3],aux; int i; for(i = 0; i < 3; i++){ printf("v[%d]: ",i); scanf("%f",&v[i]); } media(v,&aux); printf("\nMédia = %.1f",aux); return 0; } void media(float *vetor, float *retorno){ int i; float aux = 0; for(i = 0;i < 3; i++) aux += vetor[i]; *retorno = aux/3; }