Clever Use of the C language # And # (example), clever use of the C Language

Source: Internet
Author: User

Clever Use of the C language # And # (example), clever use of the C Language

For example:

The normal process of defining a struct is

typdef struct __str__Student  {    char* name;    int id;}Student;

However, when a large number of struct definitions are used, the definition is too troublesome. The clever use of the # connector can solve the problem as follows:

# Include <stdio. h> # define STRUCT (type) typedef struct _ tag _ # type; \ struct _ tag _ # typeSTRUCT (Student) // when a project contains a large number of struct, this can save a lot of time {char * name; int id ;}; int main () {Student s1; Student s2; s1.name = "s1"; s1.id = 0; s2.name = "s2"; s2.id = 1; printf ("% s \ n", s1.name ); printf ("% d \ n", s1.id); printf ("% s \ n", s2.name); printf ("% d \ n", s2.id); return 0 ;}

On linux, compile gcc-E test. c-0 test. I in one step. The result is as follows:

Test. I output:

All files in stdio. h are in front of test. I. You do not need to check whether the following is the process of handling the above connector:

#1 "aa. c "#1" <built-in> "#1" <command-line> "#1"/usr/include/stdc-predef.h "1 3 4 #1" <command-line> "2 #1" aa. c "#1"/usr/include/stdio. h "1 3 4 #27"/usr/include/stdio. h "3 4 #1"/usr/include/features. h "1 3 4 #367"/usr/include/features. h "3 4 #1"/usr/include/x86_64-linux-gnu/sys/cdefs. h "1 3 4 #410"/usr/include/x86_64-linux-gnu/sys/cdefs. h "3 4 #1"/usr/include/x86_64-linux-gnu/bits/wordsize. h "1 3 4 #411"/usr/include/x86_64-linux-gnu/sys/cdefs. h "2 3 4 #368"/usr/include/features. h "2 3 4 #391"/usr/include/features. h "3 4 #1"/usr/include/x86_64-linux-gnu/gnu/stubs. h "1 3 4 #10"/usr/include/x86_64-linux-gnu/gnu/stubs. h "3 4 #1"/usr/include/x86_64-linux-gnu/gnu/stubs-64.h "1 3 4 #11"/usr/include/x86_64-linux-gnu/gnu/stubs. h "2 3 4 #392"/usr/include/features. h "2 3 4 #28"/usr/include/stdio. h "2 3 4 #1"/usr/lib/gcc/x86_64-linux-gnu/5/include/stddef. h "1 3 3 4 #216"/usr/lib/gcc/x86_64-linux-gnu/5/include/stddef. h & quot; 3 4 #216 & quot;/usr/lib/gcc/x86_64-linux-gnu/5/include/stddef. h "3 4 typedef long unsigned int size_t; #34"/usr/include/stdio. h "2 3 4 #1"/usr/include/x86_64-linux-gnu/bits/types. h "1 3 4 #27"/usr/include/x86_64-linux-gnu/bits/types. h "3 4 #1"/usr/include/x86_64-linux-gnu/bits/wordsize. h "1 3 4 #28"/usr/include/x86_64-linux-gnu/bits/types. h "2 3 4 typedef unsigned char _ u_char; typedef unsigned short int _ u_short; typedef unsigned int _ u_int; typedef unsigned long int _ u_long; typedef signed char _ int8_t; typedef unsigned char _ uint8_t; typedef signed short int _ int16_t; typedef unsigned short int _ uint16_t; typedef signed int _ int32_t; typedef unsigned int _ uint32_t; typedef signed long int _ int64_t; typedef unsigned long int _ uint64_t; typedef long int _ quad_t; typedef unsigned long int _ u_quad_t; #121 "/usr/include/x86_64-linux-gnu/bits/types. h "3 4 #1"/usr/include/x86_64-linux-gnu/bits/typesizes. h & quot; 1 3 4 #122 & quot;/usr/include/x86_64-linux-gnu/bits/types. h "2 3 4 typedef unsigned long int _ dev_t; typedef unsigned int _ uid_t; typedef unsigned int _ gid_t; typedef unsigned long int _ ino_t; typedef unsigned long int _ ino64_t; typedef unsigned int _ mode_t; typedef unsigned long int _ nlink_t; typedef long int _ off_t; typedef long int _ off64_t; typedef int _ pid_t; typedef struct {int _ val [2];} _ fsid_t; typedef long int _ clock_t; typedef unsigned long int _ rlim_t; typedef unsigned long int _ rlim64_t; typedef unsigned int _ id_t; typedef long int _ time_t; typedef unsigned int _ useconds_t; typedef long int _ suseconds_t; typedef int _ daddr_t; typedef int _ key_t; typedef int _ clockid_t; typedef void * _ timer_t; typedef long int _ blksize_t; typedef long int _ blkcnt_t; typedef long int _ forward; typedef unsigned long int _ fsblkcnt_t; typedef unsigned long int _ forward; typedef unsigned long int _ fsfilcnt_t; typedef unsigned long int _ fsfilcnt64_t; typedef long int _ fsword_t; typedef long int _ ssize_t; typedef long int _ struct; typedef unsigned long int _ syscall_ulong_t; typedef _ off64_t _ loff_t; typedef _ quad_t * _ qaddr_t; typedef char * _ caddr_t; typedef long int _ intptr_t; typedef unsigned int _ socklen_t; #36 "/usr/include/stdio. h "2 3 4 #44"/usr/include/stdio. h "3 4 struct _ IO_FILE; typedef struct _ IO_FILE FILE; #64"/usr/include/stdio. h "3 4 typedef struct _ IO_FILE _ FILE; #74"/usr/include/stdio. h "3 4 #1"/usr/include/libio. h "1 3 4 #31"/usr/include/libio. h "3 4 #1"/usr/include/_ G_config.h "1 3 4 #15"/usr/include/_ G_config.h "3 4 #1"/usr/lib/gcc /x86_64-linux-gnu/5/include/stddef. h "1 3 4 #16"/usr/include/_ G_config.h "2 3 4 #1"/usr/include/wchar. h "1 3 4 #82"/usr/include/wchar. h "3 4 typedef struct {int _ count; union {unsigned int _ wch; char _ wchb [4] ;}_ _ value ;}_ _ mbstate_t; #21 "/usr/include/_ G_config.h" 2 3 4 typedef struct {_ off_t _ pos; _ mbstate_t _ state;} _ G_fpos_t; typedef struct {_ off64_t _ pos; _ mbstate_t _ state;} _ G_fpos64_t; #32 "/usr/include/libio. h "2 3 4 #49"/usr/include/libio. h "3 4 #1"/usr/lib/gcc/x86_64-linux-gnu/5/include/stdarg. h "1 3 4 #40"/usr/lib/gcc/x86_64-linux-gnu/5/include/stdarg. h "3 4 typedef _ builtin_va_list _ gnuc_va_list; #50"/usr/include/libio. h "2 3 4 #144"/usr/include/libio. h "3 4 struct _ IO_jump_t; struct _ IO_FILE; typedef void _ IO_lock_t; struct _ IO_marker {struct _ IO_marker * _ next; struct _ IO_FILE * _ sbuf; int _ pos; #173 "/usr/include/libio. h "3 4}; enum _ codecvt_result {_ codecvt_ OK, _ codecvt_partial, _ codecvt_error, _ codecvt_noconv}; #241"/usr/include/libio. h "3 4 struct _ IO_FILE {int _ flags; char * _ IO_read_ptr; char * _ IO_read_end; char * _ IO_read_base; char * _ IO_write_base; char * _ IO_write_ptr; char * _ IO_write_end; char * _ IO_buf_base; char * _ IO_buf_end; char * _ IO_save_base; char * _ IO_backup_base; char * _ IO_save_end; struct _ IO_marker * _ markers; struct _ IO_FILE * _ chain; int _ fileno; int _ flags2; _ off_t _ old_offset; unsigned short _ cur_column; signed char _ vtable_offset; char _ character Buf [1]; _ IO_lock_t * _ lock; #289 "/usr/include/libio. h "3 4 _ off64_t _ offset; void * _ pad1; void * _ pad2; void * _ pad3; void * _ pad4; size_t _ pad5; int _ mode; char _ unused2 [15 * sizeof (int)-4 * sizeof (void *)-sizeof (size_t)];}; typedef struct _ IO_FILE; struct _ IO_FILE_plus; extern struct _ IO_FILE_plus _ IO_2_1_stdin _; extern struct _ IO_FILE_plus _ Empty _; #333 "/usr/include/libio. h "3 4 typedef _ ssize_t _ io_read_fn (void * _ cookie, char * _ buf, size_t _ nbytes ); typedef _ ssize_t _ io_write_fn (void * _ cookie, const char * _ buf, size_t _ n); typedef int _ io_seek_fn (void * _ cookie, _ off64_t * _ pos, int _ w); typedef int _ io_close_fn (void * _ cookie); #385 "/usr/include/libio. h "3 4 extern int _ underflow (_ IO_FILE *); extern int _ uflow (_ IO_FILE *); extern int _ overflow (_ IO_FILE *, int ); #429 "/usr/include/libio. h "3 4 extern int _ IO_getc (_ IO_FILE * _ fp); extern int _ IO_putc (int _ c, _ IO_FILE * _ fp ); extern int _ IO_feof (_ IO_FILE * _ fp) _ attribute _ (_ nothrow _, _ leaf __)); extern int _ IO_ferror (_ IO_FILE * _ fp) _ attribute _ (_ nothrow _, _ leaf __)); extern int _ IO_peekc_locked (_ IO_FILE * _ fp); extern void _ IO_flockfile (_ IO_FILE *) _ attribute _ (_ nothrow __, _ leaf _); extern void _ IO_funlockfile (_ IO_FILE *) _ attribute _ (_ nothrow _, _ leaf __)); extern int _ IO_ftrylockfile (_ IO_FILE *) _ attribute _ (_ nothrow _, _ leaf _); #459 "/usr/include/libio. h "3 4 extern int _ IO_vfscanf (_ IO_FILE * _ restrict, const char * _ restrict, _ gnuc_va_list, int * _ restrict ); extern int _ IO_vfprintf (_ IO_FILE * _ restrict, const char * _ restrict, _ gnuc_va_list); extern _ ssize_t _ IO_padn (_ IO_FILE *, int, _ ssize_t); extern size_t _ IO_sgetn (_ IO_FILE *, void *, size_t); extern _ off64_t _ partition (_ IO_FILE *, _ off64_t, int, int ); extern _ off64_t _ IO_seekpos (_ IO_FILE *, _ off64_t, int); extern void _ IO_free_backup_area (_ IO_FILE *) _ attribute _ (_ nothrow __, _ leaf _); #75 "/usr/include/stdio. h "2 3 4 typedef _ gnuc_va_list va_list; #90"/usr/include/stdio. h "3 4 typedef _ off_t; #102"/usr/include/stdio. h "3 4 typedef _ ssize_t; typedef _ G_fpos_t fpos_t; #164"/usr/include/stdio. h "3 4 #1"/usr/include/x86_64-linux-gnu/bits/stdio_lim.h "1 3 4 #165"/usr/include/stdio. h "2 3 4 extern struct _ IO_FILE * stdin; extern struct _ IO_FILE * stdout; extern struct _ IO_FILE * stderr; extern int remove (const char * _ filename) _ attribute _ (_ nothrow _, _ leaf _); extern int rename (const char * _ old, const char * _ new) _ attribute _ (_ nothrow _, _ leaf _); extern int renameat (int _ oldfd, const char * _ old, int _ newfd, const char * _ new) _ attribute _ (_ nothrow _, _ leaf __)); extern FILE * tmpfile (void); #209 "/usr/include/stdio. h "3 4 extern char * tmpnam (char * _ s) _ attribute _ (_ nothrow _, _ leaf __)); extern char * tmpnam_r (char * _ s) _ attribute _ (_ nothrow _, _ leaf __)); #227 "/usr/include/stdio. h "3 4 extern char * tempnam (const char * _ dir, const char * _ pfx) _ attribute _ (_ nothrow __, _ leaf _) _ attribute _ (_ malloc _); extern int fclose (FILE * _ stream ); extern int fflush (FILE * _ stream); #252 "/usr/include/stdio. h "3 4 extern int fflush_unlocked (FILE * _ stream); #266"/usr/include/stdio. h "3 4 extern FILE * fopen (const char * _ restrict _ filename, const char * _ restrict _ modes ); extern FILE * freopen (const char * _ restrict _ filename, const char * _ restrict _ modes, FILE * _ restrict _ stream ); #295 "/usr/include/stdio. h "3 4 #306"/usr/include/stdio. h "3 4 extern FILE * fdopen (int _ fd, const char * _ modes) _ attribute _ (_ nothrow __, _ leaf _); #319 "/usr/include/stdio. h "3 4 extern FILE * fmemopen (void * _ s, size_t _ len, const char * _ modes) _ attribute _ (_ nothrow __, _ leaf _); extern FILE * open_memstream (char ** _ bufloc, size_t * _ sizeloc) _ attribute _ (_ nothrow __, _ leaf _); extern void setbuf (FILE * _ restrict _ stream, char * _ restrict _ buf) _ attribute _ (_ nothrow _, _ leaf _); extern int setvbuf (FILE * _ restrict _ stream, char * _ restrict _ buf, int _ modes, size_t _ n) _ attribute _ (_ nothrow _, _ leaf __)); extern void setbuffer (FILE * _ restrict _ stream, char * _ restrict _ buf, size_t _ size) _ attribute _ (_ nothrow __, _ leaf _); extern void setlinebuf (FILE * _ stream) _ attribute _ (_ nothrow _, _ leaf __)); extern int fprintf (FILE * _ restrict _ stream, const char * _ restrict _ format ,...); extern int printf (const char * _ restrict _ format ,...); extern int sprintf (char * _ restrict _ s, const char * _ restrict _ format ,...) _ attribute _ (_ nothrow _); extern int vfprintf (FILE * _ restrict _ s, const char * _ restrict _ format, _ gnuc_va_list _ arg); extern int vprintf (const char * _ restrict _ format, _ gnuc_va_list _ arg ); extern int vsprintf (char * _ restrict _ s, const char * _ restrict _ format, _ gnuc_va_list _ arg) _ attribute _ (_ nothrow _); extern int snprintf (char * _ restrict _ s, size_t _ maxlen, const char * _ restrict _ format ,...) _ attribute _ (_ nothrow _) _ attribute _ (_ format _ (_ printf __, 3, 4 ))); extern int vsnprintf (char * _ restrict _ s, size_t _ maxlen, const char * _ restrict _ format, _ gnuc_va_list _ arg) _ attribute _ (_ nothrow _) _ attribute _ (_ format _ (_ printf __, 3, 0 ))); #412 "/usr/include/stdio. h "3 4 extern int vdprintf (int _ fd, const char * _ restrict _ fmt, _ gnuc_va_list _ arg) _ attribute _ (_ format _ (_ printf __, 2, 0); extern int dprintf (int _ fd, const char * _ restrict _ fmt ,...) _ attribute _ (_ format _ (_ printf __, 2, 3); extern int fscanf (FILE * _ restrict _ stream, const char * _ restrict _ format ,...); extern int scanf (const char * _ restrict _ format ,...); extern int sscanf (const char * _ restrict _ s, const char * _ restrict _ format ,...) _ attribute _ (_ nothrow _, _ leaf _); #443 "/usr/include/stdio. h "3 4 extern int fscanf (FILE * _ restrict _ stream, const char * _ restrict _ format ,...) _ asm _ ("" _ isoc99_fscanf "); extern int scanf (const char * _ restrict _ format ,...) _ asm _ ("" _ isoc99_scanf "); extern int sscanf (const char * _ restrict _ s, const char * _ restrict _ format ,...) _ asm _ ("" _ isoc99_sscanf ") _ attribute _ (_ nothrow _, _ leaf __)); #463 "/usr/include/stdio. h "3 4 extern int vfscanf (FILE * _ restrict _ s, const char * _ restrict _ format, _ gnuc_va_list _ arg) _ attribute _ (_ format _ (_ scanf __, 2, 0); extern int vscanf (const char * _ restrict _ format, _ gnuc_va_list _ arg) _ attribute _ (_ format _ (_ scanf __, 1, 0 ))); extern int vsscanf (const char * _ restrict _ s, const char * _ restrict _ format, _ gnuc_va_list _ arg) _ attribute _ (_ nothrow _, _ leaf _) _ attribute _ (_ format _ (_ scanf __, 2, 0); #494 "/usr/include/stdio. h "3 4 extern int vfscanf (FILE * _ restrict _ s, const char * _ restrict _ format, _ gnuc_va_list _ arg) _ asm _ ("" _ isoc99_vfscanf ") _ attribute _ (_ format _ (_ scanf __, 2, 0 ))); extern int vscanf (const char * _ restrict _ format, _ gnuc_va_list _ arg) _ asm _ ("" _ isoc99_vscanf ") _ attribute _ (_ format _ (_ scanf __, 1, 0); extern int vsscanf (const char * _ restrict _ s, const char * _ restrict _ format, _ gnuc_va_list _ arg) _ asm _ ("" _ isoc99_vsscanf ") _ attribute _ (_ nothrow _, _ leaf _) _ attribute _ (_ format _ (_ scanf __, 2, 0); #522 "/usr/include/stdio. h "3 4 extern int fgetc (FILE * _ stream); extern int getc (FILE * _ stream); extern int getchar (void ); #550 "/usr/include/stdio. h "3 4 extern int getc_unlocked (FILE * _ stream); extern int getchar_unlocked (void); #561"/usr/include/stdio. h "3 4 extern int fgetc_unlocked (FILE * _ stream); extern int fputc (int _ c, FILE * _ stream); extern int putc (int _ c, FILE * _ stream); extern int putchar (int _ c); #594 "/usr/include/stdio. h "3 4 extern int fputc_unlocked (int _ c, FILE * _ stream); extern int putc_unlocked (int _ c, FILE * _ stream ); extern int putchar_unlocked (int _ c); extern int getw (FILE * _ stream); extern int putw (int _ w, FILE * _ stream ); extern char * fgets (char * _ restrict _ s, int _ n, FILE * _ restrict _ stream); #640 "/usr/include/stdio. h "3 4 #665"/usr/include/stdio. h "3 4 extern _ ssize_t _ getdelim (char ** _ restrict _ lineptr, size_t * _ restrict _ n, int _ delimiter, FILE * _ restrict _ stream); extern _ ssize_t getdelim (char ** _ restrict _ lineptr, size_t * _ restrict _ n, int _ delimiter, FILE * _ restrict _ stream); extern _ ssize_t getline (char ** _ restrict _ lineptr, size_t * _ restrict _ n, FILE * _ restrict _ stream); extern int fputs (const char * _ restrict _ s, FILE * _ restrict _ stream ); extern int puts (const char * _ s); extern int ungetc (int _ c, FILE * _ stream ); extern size_t fread (void * _ restrict _ ptr, size_t _ size, size_t _ n, FILE * _ restrict _ stream ); extern size_t fwrite (const void * _ restrict _ ptr, size_t _ size, size_t _ n, FILE * _ restrict _ s ); #737 "/usr/include/stdio. h "3 4 extern size_t fread_unlocked (void * _ restrict _ ptr, size_t _ size, size_t _ n, FILE * _ restrict _ stream ); extern size_t fwrite_unlocked (const void * _ restrict _ ptr, size_t _ size, size_t _ n, FILE * _ restrict _ stream ); extern int fseek (FILE * _ stream, long int _ off, int _ whence); extern long int ftell (FILE * _ stream ); extern void rewind (FILE * _ stream); #773 "/usr/include/stdio. h "3 4 extern int fseeko (FILE * _ stream, _ off_t _ off, int _ whence); extern _ off_t ftello (FILE * _ stream ); #792 "/usr/include/stdio. h "3 4 extern int fgetpos (FILE * _ restrict _ stream, fpos_t * _ restrict _ pos); extern int fsetpos (FILE * _ stream, const fpos_t * _ pos); #815 "/usr/include/stdio. h "3 4 #824"/usr/include/stdio. h "3 4 extern void clearerr (FILE * _ stream) _ attribute _ (_ nothrow _, _ leaf __)); extern int feof (FILE * _ stream) _ attribute _ (_ nothrow _, _ leaf __)); extern int ferror (FILE * _ stream) _ attribute _ (_ nothrow _, _ leaf __)); extern void clearerr_unlocked (FILE * _ stream) _ attribute _ (_ nothrow _, _ leaf __)); extern int feof_unlocked (FILE * _ stream) _ attribute _ (_ nothrow _, _ leaf __)); extern int ferror_unlocked (FILE * _ stream) _ attribute _ (_ nothrow _, _ leaf __)); extern void perror (const char * _ s ); #1 "/usr/include/x86_64-linux-gnu/bits/sys_errlist.h" 1 3 4 #26 "/usr/include/x86_64-linux-gnu/bits/sys_errlist.h" 3 4 extern int sys_nerr; extern const char * const sys_errlist []; #854 "/usr/include/stdio. h "2 3 4 extern int fileno (FILE * _ stream) _ attribute _ (_ nothrow _, _ leaf __)); extern int fileno_unlocked (FILE * _ stream) _ attribute _ (_ nothrow _, _ leaf __)); #872 "/usr/include/stdio. h "3 4 extern FILE * popen (const char * _ command, const char * _ modes); extern int pclose (FILE * _ stream ); extern char * ctermid (char * _ s) _ attribute _ (_ nothrow _, _ leaf __)); #912 "/usr/include/stdio. h "3 4 extern void flockfile (FILE * _ stream) _ attribute _ (_ nothrow _, _ leaf __)); extern int ftrylockfile (FILE * _ stream) _ attribute _ (_ nothrow _, _ leaf __)); extern void funlockfile (FILE * _ stream) _ attribute _ (_ nothrow _, _ leaf __)); #942 "/usr/include/stdio. h "3 4 #2" aa. c "2 -------------------- here is # Is the handling process of the connector found clever use # the connector can greatly reduce the workload and improve work efficiency #6" aa. c "typedef struct _ tag_Student Student; struct _ tag_Student {char * name; int id ;}; int main () {Student s1; Student s2; s1.name =" s1 "; s1.id = 0; s2.name = "s2"; s2.id = 1; printf ("% s \ n", s1.name); printf ("% d \ n", s1.id ); printf ("% s \ n", s2.name); printf ("% d \ n", s2.id); return 0 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.