Basic grammar for c

1 minute read

c is one of my favorite languages but it’s still not easy to use. Somtimes I forget some grammars and I’m even confused the loop statement. Because I’d like to use various kinds of languages such as java, python, javascript, c#, etc. I’m trying to leave code snippets for using various languages ​​efficiently.

case

    switch (piSigNo)
    {
        case  SIGINT  :
            UserLog_(EMG, _F_L_, "Event Handler call [%d:SIGINT]", piSigNo);
            ExitProc_();
            break;
        case  SIGPIPE :
            UserLog_(EMG, _F_L_, "Event Handler call [%d:SIGPIPE]", piSigNo);
            ExitProc_();
            break;
        case  SIGILL  :
            UserLog_(EMG, _F_L_, "Event Handler call [%d:SIGILL]", piSigNo);
            ExitProc_();
            break;
        default:
            UserLog_(EMG, _F_L_, "Event Handler call [%d:XXXXXXX]", piSigNo);
            ExitProc_();
            break;
    }

String format

#include <stdio.h>
#include <string.h>

void main()
{
    printf("[%3s]\n", "abcd"); 
    printf("[%.3s]\n", "abcd");
    printf("[%.10s]\n", "abcd");
    printf("[%10.10s]\n", "abcd");
    printf("[%10s]\n", "abcd"); 
    printf("[%10.*s]\n", 3, "abcd"); 
    printf("[%-*.*s]\n", 10, 3, "abcd"); 
}
<Output>

[abcd]
[abc]
[abcd]
[      abcd]
[      abcd]
[       abc]
[abc       ]

sizeof

#include <stdio.h>

void main ()
{
    printf("short (int)    : %d, unsigned short : %d \n", sizeof(short), sizeof(unsigned short));
    printf("int            : %d, unsigned int   : %d \n", sizeof(int), sizeof(unsigned int));
    printf("long (int)     : %d, unsigned long  : %d \n", sizeof(long), sizeof(unsigned long));
    printf("long long      : %d \n", sizeof(long long));
    printf("float          : %d \n", sizeof(float));
    printf("long float     : %d \n", sizeof(long float));
    printf("double         : %d \n", sizeof(double));
    printf("long double    : %d \n", sizeof(long double));
}

<Result : IBM AIX 5.3>

short (int)    : 2, unsigned short : 2 
int            : 4, unsigned int   : 4 
long (int)     : 8, unsigned long  : 8 
long long      : 8 
float          : 4 
long float     : 8 
double         : 8 
long double    : 8 

Tags:

Categories:

Updated:

Leave a comment