kenics.net

Technical notes on perl, python, php, sql, cgi, c/c++, q/kdb+, unix/shell, revision control tools, data structures & algorithms, and their applications into web services and other various forms of software engineering.

C programming

 
very incomplete. 
 
typedef 
preprocessing: the statement with "#" 
 
### 
###  .h (header file) and .c (source file) 
### 
 
.h has declarations of structs and function prototypes, and macros to be shared across (potentially multiple) source files. 
$ gcc -std=c99 foo.h bar.c kenics.c hoge.h -o test.exe 
 
obviously, if you are writing something very simple, then a single .c file can have everything. 
but if you are writing huge software, then multiple header/source files are common. 
 
### 
###  sample code 
### 
 
#include <stdio.h> 
#define FIVE 5         # notice no semi colon 
 
 
void main(void){ 
   int foo[5];     # allocates mem space for 5 ints 
   int i; 
 
   for(i=0; i<5; i++){ 
      foo[i] = i*2; 
   } 
 

 
### 
###  array 
### 
 
#include <stdio.h> 
 
int main(void) { 
  int  x,  nums[5] = {10, 20, 30}; 
  for (x = 0;  x < 5;  x++) 
    printf ("%i  ",   nums[x] ); 
  return 0; 

 
prints 10 20 30 A B  where A and B are whatever in the addr 
 
 
### 
###  pointers 
### 
 
int n; 
int *ptr; 
ptr = &n; 
 
 
### 
###  IO 
### 
 
char name[20]; 
scanf("%s", name);    # suppose "john smith jr" is entered 
printf("%s",name);    # this actually only prints "john" 
 
https://stackoverflow.com/questions/1247989/how-do-you-allow-spaces-to-be-entered-using-scanf 
 
### 
###  strcmp(const char* str1, const char* str2) 
### 
 
Here is the declaration of strcmp. It takes and compares two strings, and returns an integer 
int strcmp(const char *str1, const char *str2) 
 
Specifically, strcmp() compares the two string’s each character one by one. 
For simple illustration, let’s say we have str1 “USA” and str2 “USa”. 
 
strcmp() first compares “U” and “U”. Then “S” and “S”. So far both str1 and str2 are equal. Then the third character “A” and “a” are not matched. At this point it compares the decimal value of both ascii characters. “A” is 65, and “a” is 97.  So we will get a return value of 65-97 = -32 
 
Ref : http://www.asciitable.com/ 
 
Overall, we can make the following observation. 
 
If return value is 0, then it means str1 and str2 are equal. 
If return value > 0, then it means str1 is more than str2 
If return value < 0, then it means str1 is less than str2 
 
 
### 
###  struct 
### 
 
struct YMD 

 int month; 
 int day; 
 int year; 
}; 
 
void printYMD(struct YMD);     # note the syntax, it's not void printYMD(struct YMD ymd); 
 
main() 

 struct YMD today = {8, 9, 2018};      # one way to init 
 
 struct YMD yesterday;     # another way to init 
 yesterday.month = 8; 
 yesterday.day = 8; 
 yesterday.year = 2018; 
 
 printf ("enter a date in the format mm/dd/yyyy : ");          # 8/9/2018 
 scanf ("%d/%d/%d", &today.month, &today.day, &today.year);    # another way to init 
 
 printf (enter a date in the format mm dd yyyy : "); 
 scanf ("%i%i%i", &today.month, &today.day, &today.year);    # another way to init 
 
 printf ("today = %.2i/%.2i/%i\n", today.month, today.day, today.year);   #  08/09/2018 
 printYMD(today); 
 
 return 0; 

 
# note:  %.2i  is 2 int space with zero pad 
#        %2i   is 2 int space with whitespace pad 
 
void printYMD(struct YMD ymd){    # pass by value 
 printf ("today = %.2i/%.2i/%i\n", ymd.month, ymd.day, ymd.year); 

 
# an alternative way is to pass by reference 
 
 
struct YMD week[7] = {4,19,2018, 11,11,2018, 12,31,2018}; 
struct YMD week[7] = {{4,19,2018}, {11,11,2018}, {12,31,2018}}; 
 
week[0].day = 19; 
week[0].month = 4; 
week[0].year = 2018;      # just supply  &week[0].year  in scanf() 
 
 
void printYMDs(struct YMD [], int);            # prototype 
void printYMDs(struct YMD ymd[], int size){    # definition 
 int i; 
 for(i = 0; i < size; i++) 
 printf("%.2i/%.2i/%i\n", ymd[i].month, ymd[i].day, ymd[i].year); 

 
 
### 
###  struct mdade of struct 
### 
 
struct YMD 

 int month; 
 int day; 
 int year; 
};                   # note this emi colon is required. a typical newbie error to forget. 
 
struct HMS 

 int hour; 
 int minute; 
 int second; 
}; 
 
struct YMDHMS 

 struct YMD ymd; 
 struct HMS hms; 
}; 
 
main(){ 
 struct YMDHMS timestamp; 
 
 timestamp.ymd.day = 19; 
 timestamp.ymd.month = 4; 
 timestamp.ymd.year = 2018; 
 timestamp.hms.hour = 23; 
 timestamp.hms.minute = 59; 
 timestamp.hms.second = 45; 
 
 return 0; 

 
 
## 
##  array var inside struct 
## 
 
e.g. 
 
struct human 

  int weight; 
  int height; 
  int bday[3];     # let's say bday[0] = yyyy, bday[1] = mm, bday[2] = dd 
}; 
 
main(){ 
 
 struct human students[10];     # suppose ten students in the class 
 
 students[0].weight = 147; 
 students[0].height = 182; 
 students[0].bday[0] = 1993;      # fairly straight forward 
 students[0].bday[1] = 5; 
 students[0].bday[2] = 31;        # you can simply do  scanf("%i", &students[0].bday[0]) 
 
 return 0; 

 
 
NOTE: a little note about the scanf() syntax, if you delimiter is whitespace, you dont have to put a whitespace in scanf() 
      see the below "%i%i" 
 
inTnt foo,bar; 
printf("enter two integers seperated by a whitespace: "); 
scanf("%i%i", &foo, &bar); 
 
 
 
### 
###   linked list - data structure 
### 
 
NOTE: you cannot have an instance of itself within a struct definition 
 
struct YMD 

 int month; 
 int day; 
 int year; 
 struct YMD tomorrow;      # illegal syntax 
}; 
 
struct YMD 

 int month; 
 int day; 
 int year; 
 struct YMD *tomorrow;      # valid syntax (very common in linked list) 
}; 
 
 
# note:  as you can see above, typing "struct" every time is annoyance. 
         hence, the use of "typedef" 
         (ref) http://kenics.net/unix/typedef_struct/ 
 
 
### 
###  "pass by value" VS "pass by reference" 
### 
 
technically speaking, everything in C is "pass by value" including pointers. 
but with pointers you can dereference it to the mem addr where the data is, and update that data. 
 
more in general, pass by value is meant as passing the variable name so the local copy is created and processed, whereas passing a pointer is like passing by reference in C++ where you can dereference it to modify the underlying data. (but then again, semantically, even the pointer itself is really passed by value) 
 
int foo(struct bar val); // pass by value, struct bar 
int foo(char* str); // pass by array 
 
### 
###  when is function pointer useful ? 
### 
 
A function pointer means a pointer to a function variable. It is just like pointers to other data types (e.g. integer variables). 
A function pointer proves useful because it provides a layer of abstraction and flexibility. 
 
For example, suppose we have a function Foo that takes two integer variables x & y, and returns some arithmetic computation of x & y.  Maybe that arithmetic operation is addition, or multiplication of some sort. Rather than writing two versions of Foo (as in Foo1 & Foo2) which are mostly the same code, we can instead simply add “a function pointer” as another input argument to Foo, then we can flexibly control what arithmetic operation. 
 
 
### 
###   <assert.h> 
### 
 
it is useful. 
 
---------------------- test.c 
#include <stdio.h> 
#include <stdlib.h> 
#include <assert.h> 
 
typedef struct { 
 int age; 
 char firstName[20]; 
 char familyName[20]; 
} personData, *personDataPtr; 
 
int main() 

 personDataPtr kenPtr = (personDataPtr) malloc (sizeof(personData)); 
 
 assert (kenPtr != NULL);   // or assert(kenPtr != 0) 
 printf ("malloc success\n"); 
 
 free (kenPtr); 
 return 0; 

---------------------- 
 

  1. 2018-10-13 22:37:30 |
  2. Category : uml
  3. Page View:

Google Ads