Skip to content

Latest commit

 

History

History

C - Hello World

Resources

Read or watch

man or help

  • gcc
  • printf (3)
  • puts
  • putchar

Learning Objectives

At the end of this project, you are expected to be able to explain to anyone, without the help of Google:

General

  • Why C programming is awesome
  • Who invented C
  • Who are Dennis Ritchie, Brian Kernighan and Linus Torvalds
  • What happens when you type gcc main.c
  • What is an entry point
  • What is main
  • How to print text using printf, puts and putchar
  • How to get the size of a specific type using the unary operator sizeof
  • How to compile using gcc
  • What is the default program name when compiling with gcc
  • What is the official C coding style and how to check your code with betty-style
  • How to find the right header to include in your source code when using a standard library function
  • How does the main function influence the return value of the program

Requirements

C Scripts

  • Allowed editors: vi, vim, emacs
  • All your files will be compiled on Ubuntu 20.04 LTS using gcc, using the options -Wall -Werror -Wextra -pedantic -std=gnu89
  • All your files should end with a new line
  • A README.md file at the root of the repo, containing a description of the repository
  • A README.md file, at the root of the folder of this project, containing a description of the project
  • There should be no errors and no warnings during compilation
  • You are not allowed to use system
  • Your code should use the Betty style. It will be checked using betty-style.pl and betty-doc.pl

Shell Scripts

  • Allowed editors: vi, vim, emacs
  • All your scripts will be tested on Ubuntu 20.04 LTS
  • All your scripts should be exactly two lines long ($ wc -l file should print 2)
  • All your files should end with a new line
  • The first line of all your files should be exactly #!/bin/bash

More Info

Betty linter

To run the Betty linter just with command betty <filename>:

  • Go to the Betty repository
  • Clone the repo to your local machine
  • cd into the Betty directory
  • Install the linter with sudo ./install.sh
  • emacs or vi a new file called betty, and copy the script below:
#!/bin/bash
# Simply a wrapper script to keep you from having to use betty-style
# and betty-doc separately on every item.
# Originally by Tim Britton (@wintermanc3r), multiargument added by
# Larry Madeo (@hillmonkey)

BIN_PATH="/usr/local/bin"
BETTY_STYLE="betty-style"
BETTY_DOC="betty-doc"

if [ "$#" = "0" ]; then
    echo "No arguments passed."
    exit 1
fi

for argument in "$@" ; do
    echo -e "\n========== $argument =========="
    ${BIN_PATH}/${BETTY_STYLE} "$argument"
    ${BIN_PATH}/${BETTY_DOC} "$argument"
done
  • Once saved, exit file and change permissions to apply to all users with chmod a+x betty
  • Move the betty file into /bin/ directory or somewhere else in your $PATH with sudo mv betty /bin/

You can now type betty <filename> to run the Betty linter!

Tasks

Write a script that runs a C file through the preprocessor and save the result into another file.

  • The C file name will be saved in the variable $CFILE
  • The output should be saved in the file c
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ cat main.c 
#include <stdio.h>

/**
 * main - Entry point
 *
 * Return: Always 0 (Success)
 */
int main(void)
{
    return (0);
}
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ export CFILE=main.c
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ ./0-preprocessor 
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ tail c
# 942 "/usr/include/stdio.h" 3 4

# 2 "main.c" 2


# 3 "main.c"
int main(void)
{
 return (0);
}
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ 

Repo :

Write a script that compiles a C file but does not link.

  • The C file name will be saved in the variable $CFILE
  • The output file should be named the same as the C file, but with the extension .o instead of .c.
    • Example: if the C file is main.c, the output file should be main.o
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ export CFILE=main.c
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ cat main.c
#include <stdio.h>

/**
 * main - Entry point
 *
 * Return: Always 0 (Success)
 */
int main(void)
{
    return (0);
}
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ ./1-compiler 
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ ls
0-preprocessor  1-compiler   c            main.o
Makefile               100-intel      main.c  main.s
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ cat -v main.o | head
^?ELF^B^A^A^@^@^@^@^@^@^@^@^@^A^@>^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^P^B^@^@^@^@^@^@^@^@^@^@@^@^@^@^@^@@^@^K^@^H^@UHM-^IM-eM-8^@^@^@^@]M-C^@GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.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^@^@^@^@^@^@^A^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ 

Repo :

Write a script that generates the assembly code of a C code and save it in an output file.

  • The C file name will be saved in the variable $CFILE
  • The output file should be named the same as the C file, but with the extension .s instead of .c.
    • Example: if the C file is main.c, the output file should be main.s
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ export CFILE=main.c
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ cat main.c
#include <stdio.h>

/**
 * main - Entry point
 *
 * Return: Always 0 (Success)
 */
int main(void)
{
    return (0);
}
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ ./2-assembler
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ ls
0-preprocessor  1-compiler  2-assembler c  main.c  main.s  Makefile
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ cat main.s
    .file   "main.c"
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    pushq   %rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    movq    %rsp, %rbp
    .cfi_def_cfa_register 6
    movl    $0, %eax
    popq    %rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609"
    .section    .note.GNU-stack,"",@progbits
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ 

Repo :

Write a script that compiles a C file and creates an executable named cisfun.

  • The C file name will be saved in the variable $CFILE
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ export CFILE=main.c
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ cat main.c
#include <stdio.h>

/**
 * main - Entry point
 *
 * Return: Always 0 (Success)
 */
int main(void)
{
    return (0);
}
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ ./3-name 
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ ls
0-preprocessor  1-compiler   3-name  cisfun  main.o  Makefile
100-intel       2-assembler  c       main.c  main.s
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ 

Repo :

Write a C program that prints exactly "Programming is like building a multilingual puzzle, followed by a new line.

  • Use the function puts
  • You are not allowed to use printf
  • Your program should end with the value 0
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ gcc -Wall -Werror -Wextra -pedantic -std=gnu89 4-puts.c && ./a.out
"Programming is like building a multilingual puzzle
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ echo $?
0
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ 

Repo :

Write a C program that prints exactly with proper grammar, but the outcome is a piece of art,, followed by a new line.

  • Use the function printf
  • You are not allowed to use the function puts
  • Your program should return 0
  • Your program should compile without warning when using the -Wall gcc option
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ gcc -Wall -Werror -Wextra -pedantic -std=gnu89 5-printf.c
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ ./a.out 
with proper grammar, but the outcome is a piece of art,
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ echo $?
0
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ 

Repo :

Write a C program that prints the size of various types on the computer it is compiled and run on.

You should produce the exact same output as in the example Warnings are allowed Your program should return 0 You might have to install the package libc6-dev-i386 on your Linux (Vagrant) to test the -m32 gcc option

root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ gcc 6-size.c -m32 -o size32 2> /tmp/32
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ gcc 6-size.c -m64 -o size64 2> /tmp/64
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ ./size32
Size of a char: 1 byte(s)
Size of an int: 4 byte(s)
Size of a long int: 4 byte(s)
Size of a long long int: 8 byte(s)
Size of a float: 4 byte(s)
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ ./size64
Size of a char: 1 byte(s)
Size of an int: 4 byte(s)
Size of a long int: 8 byte(s)
Size of a long long int: 8 byte(s)
Size of a float: 4 byte(s)
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ echo $?
0
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ 

Repo :

Write a script that generates the assembly code (Intel syntax) of a C code and save it in an output file.

  • The C file name will be saved in the variable $CFILE.
  • The output file should be named the same as the C file, but with the extension .s instead of .c.
    • Example: if the C file is main.c, the output file should be main.s
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ export CFILE=main.c
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ cat main.c
#include <stdio.h>

/**
 * main - Entry point
 *
 * Return: Always 0 (Success)
 */
int main(void)
{
    return (0);
}
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ ./100-intel 
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ cat main.s
    .file   "main.c"
    .intel_syntax noprefix
    .text
    .globl  main
    .type   main, @function
main:
.LFB0:
    .cfi_startproc
    push    rbp
    .cfi_def_cfa_offset 16
    .cfi_offset 6, -16
    mov rbp, rsp
    .cfi_def_cfa_register 6
    mov eax, 0
    pop rbp
    .cfi_def_cfa 7, 8
    ret
    .cfi_endproc
.LFE0:
    .size   main, .-main
    .ident  "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609"
    .section    .note.GNU-stack,"",@progbits
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ 

Repo :

Write a C program that prints exactly and that piece of art is useful" - Dora Korpar, 2015-10-19, followed by a new line, to the standard error.

You are not allowed to use any functions listed in the NAME section of the man (3) printf or man (3) puts Your program should return 1 Your program should compile without any warnings when using the -Wall gcc option

root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ gcc -Wall -Werror -Wextra -pedantic -std=gnu89 -o quote 101-quote.c
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ ./quote
and that piece of art is useful" - Dora Korpar, 2015-10-19
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ echo $?
1
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ ./quote 2> q
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ cat q
and that piece of art is useful" - Dora Korpar, 2015-10-19
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ grep printf < 101-quote.c
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ grep put < 101-quote.c
root@ubuntu:~/holbertonschool-low_level_programming/hello_world$ 

Repo :