# C language 解題
「你所不知道的 C 語言」系列講座
https://hackmd.io/@sysprog/c-prog/%2F%40sysprog%2Fc-programming
> Test Case
```C =
#include <stdio.h>
int main()
{
char str[]="Hello";
char *p=str;
int *x;
double *y;
short *z;
long *a;
int n=10;
char b;
short c;
int d;
long e;
float f;
double g;
//printf("%ld\n", sizeof(str));
printf("%ld\n", sizeof(p));
printf("%ld\n", sizeof(n));
printf("%ld\n", sizeof(x));
printf("%ld\n", sizeof(y));
printf("%ld\n", sizeof(z));
printf("%ld\n", sizeof(a));
printf("==============\n");
printf("%ld\n", sizeof(b));
printf("%ld\n", sizeof(c));
printf("%ld\n", sizeof(d));
printf("%ld\n", sizeof(e));
printf("%ld\n", sizeof(f));
printf("%ld\n", sizeof(g));
return 0;
}
```
###
> Swap two variables without using a temporary variable
> XOR
```C =
#include <stdio.h>
int main()
{
int a = 123;
int b = 456;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("%d,%d\n", a , b);
return 0;
}
```
> Sum the Integers from 1 to N
> Pointer and formula
```C =
#include <stdio.h>
int main()
{
int n = 123;
sum(&n);
printf("%d", n);
return 0;
}
void sum(int *n){
*n = (*n * (*n+1))/2;
}
```
> How to find the missing integer in an array
> XOR
```C=
#include <stdio.h>
int find(int *array, int size){
int missingint=0;
for (int n=0; n < size; n++){
missingint ^= array[n];
}
return missingint;
}
int main()
{
int num[] = {1,1,2,2,3,3,4};
int size = (sizeof(num) / sizeof(num[0]));
int missingint = find(num, size);
printf("%d", missingint);
return 0;
}
```
> Test Case.
```C=
#include <stdio.h>
int main()
{
int a[]={6,7,8,9,10};
int *p=a; // p == &a[0]
*(p++)+=123; // 先拿出P[0] 之後p[0]++ 指標所指位置++ 變成p[1]
*(++p)+=123; // 先p[1]++ 變成p[2]位置 之後拿出 p[2]
printf("%d", a[2]);
return 0;
}
```
Get bit
```c=
#include <stdio.h>
char get_bit7(char in)
{
if (in & (1<<7)) return 1;
else return 0;
//return (in & (1<<7) ? 1 : 0);
}
char set_bit7(char in)
{
return (in | (1<<7));
}
char clear_bit(char in, int bit)
{
return (in & (~(1<<bit)));
}
int main()
{
int a = 255;
int b = get_bit7(a);
printf("%d\n", b);
return 0;
}
```
> 1. Please write a subroutine to print out one character’s ASCII code in 十進位 and 十六進位. For example, a subroutine printA(‘a’) will print out 97, 0x61.
```C =
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char string[1024];
int i;
printf("ASCII Input: ");
gets(string);
printf(string);
printf("\n");
printf("Decimal Ouput:");
for(i = 0; i < strlen(string); i++)
{
printf("%d ", string[i]); //%d TX Decimal
}
printf("\n");
printf("Hexadecimal Output: ");
for(i = 0; i < strlen(string); i++)
{
printf("0x%x ", string[i]); //%x TX Hexadecimal
}
}
```
```
%c:以字元方式輸出
%d:10 進位整數輸出
%o:以 8 進位整數方式輸出
%u:無號整數輸出
%x、%X:將整數以 16 進位方式輸出
%f:浮點數輸出
%e、%E:使用科學記號顯示浮點數
%g、%G:浮點數輸出,取 %f 或 %e(%f 或 %E),看哪個表示精簡
%%:顯示 %
%s:字串輸出
%lu:long unsigned 型態的整數
%p:指標型態
```
> 2. Please write a subroutine for sorting 3 input values to (max, middle, min). For example, a subroutine my_sort(12, 10, 20) then print out (20, 12, 10).
``` C =
#include<stdio.h>
void bubble_sort(int num[], int size)
{
for(int i=size-1 ; i > 0 ; i--)
{
int sp = 1;
for(int j = 0 ; j < i ; j++)
if (num[j] > num[j+1]) //if True and then transfer values.
{
int temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
sp = 0;
}
if(sp==1) {break;} //if swap == 1 and then break.
}
}
void print_array(int num[], int size)
{
for (int i = size - 1 ; i >= 0 ; i--)
{
printf("%d ", num[i]);
}
}
int main(void)
{
int num[] = {12, 10, 20, 8, 7, 100};
printf("Before:\n");
print_array(num, (int)(sizeof(num) / sizeof(num[0]))); //print values of array.
printf("\n");
bubble_sort(num, (int)(sizeof(num) / sizeof(num[0]))); // call bubble sorting.
printf("After:\n");
print_array(num, (int)(sizeof(num) / sizeof(num[0])));
return 0 ;
}
```
> 3. Please write a subroutine to concatenate two strings (i.e. write a function that implements strcat() ) to one string . And also write how to call this function. For example my_strcat(“abc”, “def”) will print out abcdef .
``` C =
void my_strcat(char str1[], char str2[], int size)
{
char concated[size];
memset(concated,"\0", size); //memset set memory to specific value.
strcat(concated, str1);
strcat(concated, str2);
printf("%s\n", concated);
}
int main(void) {
char str1[] = "abc";
char str2[] = "def";
int size = strlen(str1) + strlen(str2) + 1; //get string size and then plus 1 without overflow.
my_strcat(str1, str2, size); //call my_strcat.
return 0;
}
```
> 4. You could refer the following function to answer the question of 4-1 and 4-2:
> int rand(void);
> the Return value will be randomly generated between 0 to 65535.
> 4-1 Please write a program to randomly generate 6 values within 1 ~ 10000.
> 4-2 Please write a program to randomly generate 6 different values within 1 ~ 100.
> Note: Any of the 6 values must be different to other 5 values.
``` c =
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int myrand(int values)
{
srand(time(NULL));
int randvalues = (rand()%(values-0+1))+0;
printf("The 0 ~ 65535 Random Number is %d .\n", randvalues);
return 0;
}
int rand_num(int values, int num)
{
srand(time(NULL));
int randvalues;
for (int i = 0 ; i < num ; i++)
{
randvalues = (rand()%(values-1+1))+1;
printf("The 1 ~ 100000 Random Number is %d .\n", randvalues);
}
return 0;
}
int rand_diffnum_card(int values, int num)
{
srand(time(NULL)); // The add getpid() can raise the random.
int randvalues;
int i ;
int arraynum[values + 1];
for (i = 1 ; i < values ; i++) { arraynum[i]=i; }
for (i = 0 ; i < values; i++)
{
randvalues = (rand()%((values-1)+1))+1;
int temp = arraynum[i];
arraynum[i] = randvalues;
arraynum[randvalues] = temp;
//printf("The 1 ~ 100 Different Random Number is %d .\n", arraynum[i]);
}
for (i = 0 ; i < num ; i++) {
printf("The 1 ~ 100 Different Random Number is %d .\n", arraynum[i]);
}
return 0;
}
int main(void)
{
#define diffnumrang 100
#define gernum 6
#define setrand 65535 //0~65535
#define setrang 10000 // 1~100000
myrand(setrand);
printf("-------------- \n");
rand_num(setrang, gernum);
printf("-------------- \n");
rand_diffnum_card(diffnumrang, gernum);
return 0;
}
```
> 5. Write a function that reverses a string. The input string is given as an array of characters char[].
> Example 1: Input: ["h","e","l","l","o"] Output: ["o","l","l","e","h"]
> Example 2: Input: ["H","a","n","n","a","h"] Output: ["h","a","n","n","a","H"]
>
> void reverseString(char* s){
> // Please write your answer here Note : char* s => input string
>
> return reverses_string;
> }
```C =
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
reverseString(str);
printf("Reverse a String : %s", str);
return 0;
}
int reverseString(char *str){
int i,j;
char temp;
j = strlen(str)-1;
printf("Entered a string and size: %s , %d \n", str , strlen(str));
for(i = 0 ; i<j ; i++)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
j--;
}
return str;
}
```