C言語基礎学習——1日目(タイプ+オペレータ)

44030 ワード

00main.c
int main(void)
{
    return 0;
}


      main         ,   C        main  ,            
   int           ,    void           
main                              

    : Ubuntu 12.04 gcc version 4.6.3
gcc -c main.c       -->      C   ,          : main.o
gcc main.o -o main  -->          ,              main(          a.out)

gcc main.c          -->                  a.out

C       :
hello.c   
    1. gcc -E hello.c -o hello.i == gcc -E hello.c > hello.i  (           hello.i)
       ,       cpp
    2. gcc -S hello.c -o hello.s == gcc -S hello.i -o hello.s (       hello.s)
      ,      cc1
    3. gcc -c hello.c -o hello.o == gcc -c hello.s -o hello.o (       hello.o)
      ,      as
    4. gcc hello.c -o hello      == gcc hello.o -o hello      (        hello)
      ,      ld

01hello_world.c
#include 

int main(void)
{
    printf("hello world!
"
); return 0; } stdio.h , stdio.h , printf . printf C , printf , printf , . : hello world!

02basic_type.c
#include 

int main(void)
{
    char ch = 0;
    short sh = 0;
    int i = 0;
    long l = 0;
    long long ll = 0;
    float f = 0.0;
    double d = 0.0;
    long double ld = 0.0;

    printf("sizeof char:       %u
"
, sizeof(ch)); printf("sizeof short: %u
"
, sizeof(sh)); printf("sizeof int: %u
"
, sizeof(i)); printf("sizeof long: %u
"
, sizeof(l)); printf("sizeof long long: %u
"
, sizeof(ll)); printf("sizeof float: %u
"
, sizeof(f)); printf("sizeof double: %u
"
, sizeof(d)); printf("sizeof long double:%u
"
, sizeof(ld)); return 0; } ( signed unsigned) char, short, int, long, long long float, double, long double / sizeof sizeof(type) --> sizeof var --> , : sizeof char: 1 sizeof short: 2 sizeof int: 4 sizeof long: 4 sizeof long long: 8 sizeof float: 4 sizeof double: 8 sizeof long double:12

03literal.c
#include 

int main(void)
{
    int a = 97;
    char ch = 'a';
    double d = 3.14e2;
    double d2 = -3.14e-2;

    printf("ch = %c
"
, ch); printf("ch = %d
"
, ch); printf("a = %d
"
, a); printf("a = %#o
"
, a); // , 0 printf("a = %#x
"
, a); // , 0x printf("d = %lf
"
, d); printf("d2 = %lf
"
, d2); printf("d2 = %8.3lf
"
, d2); printf("d2 = %-8.3lf
"
, d2); printf("d2 = %e
"
, d2); return 0; } ( : U, L): decimal: 3, -4, 123 hex: 0x1a, -0x1f octal: 076, -025 ( double ): 3.14 3.14e2 3.14e-2 -3.14e-2 : 'a', 'b', 'c' : "asdfg" : ch = a ch = 97 a = 97 a = 0141 a = 0x61 d = 314.000000 d2 = -0.031400 d2 = -0.031 d2 = -0.031 d2 = -3.140000e-02

04scanf.c
#include 

int main(void)
{
    int a = 0, b = 3;

    printf("input two intergers: ");

    // &       ,      a   ,    scanf
    scanf("%d %d", &a, &b);

    printf("your inputed: a = %d, b = %d
"
, a, b); return 0; } : input two intergers: 22 44 your inputed: a = 22, b = 44

05arithmetic_operator.c
#include 

int main(void)
{
    double x, y, z;
    double ret;

    printf("input x, y, z: ");
    scanf("%lf %lf %lf", &x, &y, &z);

    ret = 3*x + 4*y - 5*z;

    printf("3x + 4y - 5z = %lf
"
, ret); int r = 10 % 3; printf("remainder = %d
"
, r); r = 10 / 3; printf("quotient = %d
"
, r); printf("r++ = %d
"
, r++); printf("++r = %d
"
, ++r); printf("--r = %d
"
, --r); printf("r-- = %d
"
, r--); return 0; } : + - * / % ++ -- : input x, y, z: 2.3 5.6 -3.9 3x + 4y - 5z = 48.800000 remainder = 1 quotient = 3 r++ = 3 ++r = 5 --r = 4 r-- = 4

06relational_operator.c
#include 

int main(void)
{
    int a = 3, b = 5;
    int ret;

    ret = a < b;
    printf("ret = %d
"
, ret); ret = a <= b; printf("ret = %d
"
, ret); ret = a > b; printf("ret = %d
"
, ret); ret = a >= b; printf("ret = %d
"
, ret); ret = a == b; printf("ret = %d
"
, ret); ret = a != b; printf("ret = %d
"
, ret); return 0; } < <= > >= == != : ret = 1 ret = 1 ret = 0 ret = 0 ret = 0 ret = 1

07logical_operator.c
#include 

int main(void)
{
    int a = 3, b = 5;
    int ret;

    ret = (a != b) && (a == b);
    printf("ret = %d
"
, ret); ret = (a != b) || (a == b); printf("ret = %d
"
, ret); ret = !(a < b); printf("ret = %d
"
, ret); return 0; } ! --> logic not && --> logic and || --> logic or : ret = 0 ret = 1 ret = 0

08bit_operator.c
#include 

int main(void)
{
    int a = 3, b = 5;
    int ret = 0;

    ret = a & b;
    printf("ret = %d
"
, ret); // 1 ret = a | b; printf("ret = %d
"
, ret); // 7 // , 1, 0 ret = a ^ b; printf("ret = %d
"
, ret); // 6 ret = ~a; // sum = a + ret, ret = sum - a printf("ret = %d
"
, ret); // ret = sum - a = -1 - 3 = -4 printf("ret = %u
"
, ret); // ret = sum - a = 4294967295 - 3 = 4294967292 // , 0 a = 7; ret = a << 2; // 4 printf("ret = %d
"
, ret); // 28 unsigned int u = -1; printf("u = %u
"
, u); printf("u = %d
"
, u); // // unsigned , 0 ret = u >> 2; // 0 , %d printf("ret = %d
"
, ret); // // signed , 0 1 a = -1; ret = a >> 2; // -1 1, 1, 32 1, %d -1 printf("ret = %d
"
, ret); a = 15; ret = a >> 2; // 15 0, 0 printf("ret = %d
"
, ret); return 0; } : ~ : & : | : ^ : << : ; >> : 0 ^ 1 ^ 0 --> 1 0 ^ 1 ^ 1 --> 0 int a, b; a ^ b ^ a ==> b // 1 a ^ b ^ b ==> a // 2 int , : a = a ^ b; // a b a b = a ^ b; // b = (a ^ b) ^ b, 2, b = a, a b a = a ^ b; // b a , a = (a ^ b) ^ a, 2, a = b, b a : ret = 1 ret = 7 ret = 6 ret = -4 ret = 4294967292 ret = 28 u = 4294967295 u = -1 ret = 1073741823 ret = -1 ret = 3

09condition_operator.c
#include 

int main(void)
{
    int a = 3, b = 5;
    int min = 0;

    min = a < b ? a : b;
    printf("min of %d and %d is %d
"
, a, b, min); return 0; } : : min of 3 and 5 is 3

10comma_operator.c
#include 

int main(void)
{
    int a, b, c;
    int ret;

    ret = a = 3, b = 4, c = 5;  // ret     a   
    printf("a = %d b = %d c = %d, ret = %d
"
, a, b, c, ret); ret = (a = 3, b = 4, c = 5);// ret c printf("a = %d b = %d c = %d, ret = %d
"
, a, b, c, ret); return 0; } : a = 3 b = 4 c = 5, ret = 3 a = 3 b = 4 c = 5, ret = 5

11type_cast_1.c
#include 

int main(void)
{
    int a = 3;
    double b = 3.14;
    int reti = 0;
    double retd = 0.0;

    //       , b    
    reti = a + b;
    printf("reti = %d
"
, reti); // , a retd = a + b; printf("retd = %lf
"
, retd); // retd = a + (int)b; // b , printf("retd = %lf
"
, retd); return 0; } : reti = 6 retd = 6.140000 retd = 6.000000

12type_cast_2.c
#include 

int main(void)
{
    unsigned char uch = -1;
    char ch = -1;
    int a = 385;    // 1_10000001
    int ret;

    // unsigned   ,  0  
    ret = uch;
    printf("ret = %d
"
, ret); // 255 // signed , 0 1 ret = ch; printf("ret = %d
"
, ret); // -1 printf("ret = %u
"
, ret); // 4294967295 ch = 127; ret = ch; printf("ret = %d
"
, ret); // 127 printf("ret = %u
"
, ret); // 127 printf("
"
); // // , . ch = a; // ch 10000001( , ) --> 1 --> 1 --> 01111111 --> 127 printf("ch = %d
"
, ch); printf("ch = %u
"
, ch); // ch 1, 1 , ch = 11111111_11111111_11111111_10000001 printf("
"
); // unsigned char uch1 = 253, uch2 = 255; ret = uch1 + uch2; printf("ret = %d
"
, ret); uch1 = 258, uch2 = 255; ret = uch1 + uch2; printf("ret = %d
"
, ret); return 0; } : ret = 255 ret = -1 ret = 4294967295 ret = 127 ret = 127 ch = -127 ch = 4294967169 ret = 508 ret = 257

exercise1.c
#include <stdio.h>

int main(void)
{
    int i = 0, j = 0;

    for(i = 1; i < 10; i++)
    {
        for(j = 1; j <= i; j++)
        {
            printf("%dx%d=%02d ", j, i, i*j);
        }
        printf("\n");
    }

    printf("\n");

    for(i = 9; i > 0; i--)
    {
        for(j = 1; j <= i; j++)
        {
            printf("%dx%d=%02d ", j, i, i*j);
        }
        printf("\n");
    }

    return 0;
}


  1:    9 x 9    

    :
1x1=01
1x2=02 2x2=04
1x3=03 2x3=06 3x3=09
1x4=04 2x4=08 3x4=12 4x4=16
1x5=05 2x5=10 3x5=15 4x5=20 5x5=25
1x6=06 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=07 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=08 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=09 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81

1x9=09 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
1x8=08 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x7=07 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x6=06 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x5=05 2x5=10 3x5=15 4x5=20 5x5=25
1x4=04 2x4=08 3x4=12 4x4=16
1x3=03 2x3=06 3x3=09
1x2=02 2x2=04
1x1=01

exercise2.c
#include 

int main(void)
{
    int a = 58, b = 87;
    int temp = 0;

    printf("before: a = %d, b = %d
"
, a, b); temp = a; a = b; b = temp; printf("after: a = %d, b = %d
"
, a, b); printf("
"
); a = 58, b = 87; // printf("before: a = %d, b = %d
"
, a, b); a = a + b; b = a - b; a = a - b; printf("after: a = %d, b = %d
"
, a, b); printf("
"
); a = 58, b = 87; printf("before: a = %d, b = %d
"
, a, b); a = a ^ b; b = a ^ b; a = a ^ b; printf("after: a = %d, b = %d
"
, a, b); return 0; } 2: , : before: a = 58, b = 87 after: a = 87, b = 58 before: a = 58, b = 87 after: a = 87, b = 58 before: a = 58, b = 87 after: a = 87, b = 58

exercise3.c
#include 

int main(void)
{
    unsigned int a = 0;
    int i = 0;

    printf("input the number: ");
    scanf("%u", &a);

    for(i = 31; i >= 0; i--)
    {
        (a & (1<putchar('1') : putchar('0');

        if((i != 0) && (i%8 == 0))
            putchar('_');
    }

    putchar('
'
); return 0; } 3: : input the number: 259 00000000_00000000_00000001_00000011

be careful:
unsigned int i = 0;
for(i = 31; i >= 0; i--)
{
    printf("infinite loop
"); // } unsigned char j = 0; for(j = 0; j <= 255; j++) { printf("infinite loop
"); // , }