0% found this document useful (0 votes)
56 views4 pages

Capitulo 1

The document contains C code examples demonstrating various language features including defining functions, using inline, attributes like packed and noreturn, and makefiles. It includes code snippets showing a "Hello World" program, using inline to increment a variable, defining a function in a header file, using noreturn to exit a program, using packed to control struct layout, and compiling with flags like -ansi. It also has examples of printing user IDs, a library implementation, and rules for compiling multiple programs into object files and libraries.

Uploaded by

jose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views4 pages

Capitulo 1

The document contains C code examples demonstrating various language features including defining functions, using inline, attributes like packed and noreturn, and makefiles. It includes code snippets showing a "Hello World" program, using inline to increment a variable, defining a function in a header file, using noreturn to exit a program, using packed to control struct layout, and compiling with flags like -ansi. It also has examples of printing user IDs, a library implementation, and rules for compiling multiple programs into object files and libraries.

Uploaded by

jose
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Capitulo 1

/*
* hello.c Canonical "Hello, World!" program
*/
#include <stdio.h>

int main(void)
{
puts("Hello there, Linux programmer!");
return 0;
}

/*
* inline.c - Using the inline keyword
*/
#include <stdio.h>

inline void inc(int *a)


{
(*a)++;
}

int main(void)
{
int i = 0;
while(i < 100) {
inc(&i);
printf("%d ", i);
}
printf("\n");

return 0;
}

/*
* msg.c - Define function from msg.h
*/
#include <stdio.h>
#include "msg.h"

void prmsg(char *msg)


{
printf("%s\n", msg);
}

/*
* noret.c - Using the noreturn attribute
*/
#include <stdio.h>

void die_now(void) __attribute__ ((noreturn));

int main(void)
{
int i = 0;
while(1) {
switch(i) {
case 0 ... 5:
printf("i = %d\n", i);
break;
default:
die_now();
}
++i;
}
return 0;
}

void die_now(void)
{
puts("dying now");
exit(1);
}

/*
* packme.c - Using gcc's packed attributed
*/
#include <stdio.h>

struct P {
short s[3];
long l;
char str[5];
} __attribute__ ((packed));

struct UP {
short s[3];
long l;
char str[5];
};

int main(void)
{
struct P packed;
struct UP unpacked;

printf("sizeof char str[5] = %d bytes\n", sizeof(char) * 5);


printf(" sizeof long = %d bytes\n", sizeof(long));
printf(" sizeof short s[3] = %d bytes\n", sizeof(short) * 3);
printf(" sizeof packed = %d bytes\n", sizeof packed);
printf(" sizeof unpacked = %d bytes\n", sizeof unpacked);

return 0;
}

/*
* pedant.c - Compile with -ansi, -pedantic or -pedantic-errors
*/
#include <stdio.h>

void main(void)
{
long long int i = 0l;
puts("This is a non-conforming C program");
}

/*
* showit.c Display driver
*/
#include <stdio.h>
#include "msg.h"

int main(void)
{
char msg_hi[] = {"Hi there, programmer!"};
char msg_bye[] = {"Goodbye, programmer!"};

printf("%s\n", msg_hi);
prmsg(msg_bye);
return 0;
}

LDFLAGS :=

# Switch the comment characters on the next two lines if


# you want to optimize
CFLAGS := -g $(CPPFLAGS)
#CFLAGS := -O2 $(CPPFLAGS)

PROGS = \
hello \
pedant \
newhello \
newhello-lib \
noret \
inline \
packme

all: $(PROGS)

.c.o:
$(CC) $(CFLAGS) -c $*.c

.SUFFIXES : .c .o

hello: hello.c

pedant: pedant.c

newhello: showit.c msg.c


$(CC) $(CFLAGS) $^ -o $@

newhello-lib: showit.c libmsg.a


$(CC) $(CFLAGS) $< -o $@ -I. -L. -lmsg

libmsg.a: msg.c
$(CC) $(CFLAGS) -c $< -o libmsg.o
$(AR) rcs libmsg.a libmsg.o

noret: noret.c

inline: inline.c
$(CC) -O2 $< -o $@

packme: packme.c

.PHONY : clean zip

clean:
$(RM) $(PROGS) *.o *.a *~ *.out

zip: clean
zip 215101cd.zip *.c *.h Makefile

/*
* msg.h - Header for msg.c
*/

#ifndef MSG_H_
#define MSG_H_

void prmsg(char *msg);

#endif /* MSG_H_ */

Capitulo 2
/*
* ids.c - Print UIDs and GIDs
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main(void)
{
printf("Real user ID: %d\n", getuid());
printf("Effective user ID: %d\n", geteuid());
printf("Real group ID: %d\n", getgid());
printf("Effective group ID: %d\n", getegid());
exit(EXIT_SUCCESS);
}

You might also like