Review

C Language

Introduction

In this lesson, we will review some of the most commonly used data types, operations, structures, and functions in C.

Data Types

Type Value
int -32768 to 32767
unsigned int 0 to 65535
long int -2147483648 to 2147483647
unsigned long int 0 to 4294967295
char Characters (0 to 255 ASCII)
float 6 decimal places
double 15 decimal places
long double 19 decimal places
size_t Integral values to represent units of memory (defined in stddef.h)
FILE * File pointer

Operations

Opeartor Effect
= Assignment
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
++ Increment (suffix and prefix)
-- Decrement (suffix and prefix)
+=,-=,*=,/=,%= Operation and assignment
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
! Not
&& And
|| Or
?: Ternary
[] Subscript
* Indirect access
& Address
. Member of structure
-> Member of pointer to structure
sizeof() Get size

Control Flow

Structure Control Flow
if(condition){ } else if(condition){ }...else{} if-structure
switch(value){case value:{ }...default:{ }} Switch-Case
while(condition){ } while loop
do{ }while(condition); do-while loop
for(expr 1;condition;expr 2){ } for loop

Input/Output (stdio.h)

Format Function
int printf(constant char *format,...); Print the string to stdout, substituting values with following arguments
int scanf(constant char *format,...); Get inputs in form of the format string from stdin, storing in the addresses provided in the arguments
int putchar(int ch); Print the character to stdout
int getchar(); Get character from stdin, returning the int character
FILE *fopen(const char *name,const char *mode); Open the file named name in mode and returns the file pointer (see File I/O Lesson for details)
int fclose(FILE *stream); Close the file stream and writes any output to the file
FILE *freopen(const char *name,const char *mode,FILE *stream); Closes the file stream and opens the file named name in mode (mostly used to redirect input to a file instead of stdin/stdout)
int fseek(FILE *stream,long int position,int start_position); Sets the file's position to position bytes relative to the start_position (see File I/O Lesson for details)
long int ftell(FILE *stream); Returns the current file position, this value can only be used as argument to the fseek() function
void rewind(FILE *p) Points the pointer to the beginning of the file
int ferror(FILE *p); Checks if any error occurred with operations with the file. Returns 0 if no error, else a non-zero value
int feof(FILE *p); Checks if the current position of the pointer. Non-zero value is returned when EOF (the end of the file) is reached, else, zero is returned
int fprintf(FILE *stream,const char *format,...); Same as printf(), but prints into stream
int fscanf(FILE *stream,const char *format,...); Same as scanf(), gets input from the file
size_t fread(char *str,size_t size,size_t count,FILE *stream); Reads size bytes from stream for count times, storing into the string, returning how many reads are successfully written (count)
size_t fwrite(char *str,size_t size,size_t count,FILE *stream); Writes size bytes from str into stream for count times, returning times of successful writes (count)
char *fgets(char *str,int max,FILE *stream); Reads a line or max-1 characters from the file
int fputs(char *str,FILE *stream); Prints the string into the file
int fgetc(FILE *stream); Returns the character read from stream
int fputc(int char,FILE *stream); Prints the character to stream

Memory (stdlib.h)

Format Function
void *malloc(int num); Allocates an array of num bytes returning the address of the first element
void *calloc(int num,int size); Allocates an array of num elements each with a size of size, initializing them to 0, and returns the address of the first element
void *realloc(void *address,int size); Reallocates memory extending it up to the size
void free(void *address); Frees the memory block

Strings (string.h)

Format Function
size_t strlen(char *str); Returns the length of the string (not including '\0')
char *strcpy(char *dest,char *src); Copy string src to dest
char *strncpy(char *dest,char *src,size_t n); Copies at most n characters of src to dest
char *strcat(char *dest,char *src); Append src to dest
char *strncat(char *dest,char *src,size_t n); Append up to n characters of src to dest
int strcmp(char *str1,char *str2); Compares the two strings (0 is returned when same)
char *strstr(char *haystack,char *needle); Look for the first occurrence of string needle in haystack, if found, returns the pointer to the beginning, else, returns NULL

Thank you for reading our C Programming Tutorial!



<< Previous TOC