29 Apr 2017

CS50. Mario Pyramid

How much time did you spend on it?

/**
 * CS50: Mario
 * Aleksey M.
 * 27.04.17
 *
 */

// Compile: clang -o mario mario.c -lcs50
// Run: ./mario

#include <stdio.h>
#include <cs50.h>

int height;
int hashes;
int spaces;

int main(void)
{
    // Ask the user about input from 1 to 23
    do
    {
        printf("Height: ");
        height = GetInt();

    } while (height < 0 || height > 23);

    for (int row = 0; row < height; row++)
    {
        // Print spaces
        for (spaces = height - (row + 1); spaces > 0; spaces--)
        {
            printf(" ");
        }

        // Print hashes
        for (hashes = 0; hashes <= row + 1; hashes++)
        {
            printf("#");
        }

        printf("\n");

    }

    return 0;

}

26 Apr 2017

How do compile and run C program using Terminal?

Installing:

Install Xcode Command Line Tools
xcode-select --install

Click “Install” to download and install Xcode Command Line Tools.

Compiling:


Using Make utility:

Make is a utility tool that builds program from source code and libraries by reading a Makefile which is specification of a target. Makefile include information about how to compile and link a program.

To buld:
make HelloWorld

To run:
./HelloWorld

Using compiler:

clang HelloWorld.c or gcc HelloWorld.c for GCC compiler


CS50:


How do instal cs50.h library?

Test code:

#include <stdio.h>
#include <cs50.h>

int main() {
    printf("Your Name, please: \n");
    string s = GetString();
    printf("Your Name is: %s! \n", s);
    return 0;
}

Try to compile:

clang YourName.c -lcs50

-l means to link an objecting code into compiling phase. Hey, link the cs50 precompiled objecting code into target project. Put all -l parameters at the end of line.

This line will create an a.out file. Initially Clang compiler build the target program into a.out file. To rename that file into name of source file project we can use -o <fileName>

Compile one more time:

clang -o YourName YourName.c -lcs50 

for gcc compiler use:

gcc -o YourName YourName.c -lcs50

Run the program:

./YourName

Vita!


Miscellaneous:


Where are C include files stored on my Mac?
/usr/include
/usr/local/include
/usr/local/lib

Help me!
clang -help

Only run the preprocessor.
clang -E <fileName>

Only run preprocess and compilation steps. Compile to assembler.
clang -S <fileName>

Only run preprocess, compile, and assemble steps.
clang -c <fileName>

-lm link math
-Wall or -w enable all warnings
-Werror stop when get warning
-ggdb include debugger data

What is .a file?
.a files are static libraries


20 Apr 2017

Terminal Commands for Everyone!

ls - Get list of the files in the directory.

cd <directoryName> - Change directory.
cd .. - Up to one directory level.
cd - Returns to home directory.
cd /<directoryName> - Jump into directory in the root.

pwd - Show path to the current directory.

mkdir <directoryName> - Make directory.
mkdir /<directoryName> - Make directory in root directory.

rm <fileName> - Remove file.
rmdir <directoryName> - Remove directory.

mv <fileName> <newFileName> - Move or Rename File


17 Apr 2017

Build system for C programming in Sublime Text Mac OS X

1. Install Xcode or Xcode Command Line Tools(GCC).
2. Create a new build system:

{
"cmd" : ["gcc $file_name -o ${file_base_name} && ./${file_base_name}"],
"selector" : "source.c",
"shell": true,
"working_dir" : "$file_path",
}

3. Save it.
4. Press cmd+B for Buil and Run!

Tested on:
Mac OS X El Capitan
Sublime Text (Build 3126)

--------------------------

Where are compiler files?

Type in Terminal:

xcodebuild -find make
xcodebuild -find gcc
xcodebuild -find g++
xcodebuild -find clang
xcodebuild -find clang++

You can also install Xcode Command Line Tools which will place tools and libs under standard Unix locations (/usr/bin, /usr/include etc.) if you want to be able to just type make or gccwithout any paths.
 

Popular Posts