Welcome to C

#C
The exercises are in VScode/Learning/
The code that human writes, is called source code.
A computer that takes source code and translates it in machine code, is called a compiler.

#include <stdio.h>
int main(void)
{
printf("Hello \n World!");
}

printf

Print format statement in C is printf('') , in Python is print('')

Escape sequences:

Header file .h

C header files end with the extension.h , which are C libraries.
So any time we want to use specific pieces of code, even printf we need to import the header files.

#include <stdio.h>

The printf function is contained in the Standard I/O header file.

Documentation for C libraries from CS50

At the beginning I'm using the <cs50.h> library which probably includes all the libraries. (Only cloud based vscode version)


User input

Whenever printing something, we need to define the type of data we are taking as input.

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

int main()
{
printf("Hello!\n");
string answer = get_string("What is your name?\n"); 
#input function = get_string
printf("hello, %s\n" , answer); 
# placeholder  %s to define string type
# /n prints a new line, moving cursor to a new line
}

For the moment I'm using the CS50 library that includes all the input functions such as:

Whenever we want to print a specific data type, we need to define the format specifier in C, which is used to tell the compiler about the type of data to be printed or scanned in input and output operations.


Variables:

Create a variable:

counter = 1;
# or
counter = counter = 1;
# or
counter += 1;
# or
counter++;
Comparisons
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int x = get_int("what is x? ");
int y = get_int("what is y? ");

if (x > y)
{
printf("x is bigger than y\n");
}

}
Comparing singe characters
#include <stdio.h>
#include <cs50.h>
int main(void)
{
	char c = get_char("Do you agree? ");
	# declare char type variable
	if (c == 'y')
	{
	printf("agreed.\n");
	}
	else
	{
	printf("Not agreed\n")
	}
}

The function get_char enforce only single character, because we declared the variable char.

Same code but with logical operator OR

#include <stdio.h>
#include <cs50.h>
int main(void)
{
	char c = get_char("Do you agree? ");
	# declare char type variable
	if (c == 'y' || c == 'Y')
	{
	printf("agreed.\n");
	}
	else
	{
	printf("Not agreed\n")
	}
}

Conditionals : While
#include <stdio.h>
#include <cs50.h>
int main(void)
{
	int i = 0;
	while (i < 5)
	{
	printf("I'm increasing..%i\n", i);
	i++;
	}
}

Attachments/Pasted image 20260512180750.png
NOTICE

For
#include <stdio.h>
#include <cs50.h>
int main(void)
{
	for (int i = 0; i < 3; i++)
	{
	printf("I'm a cat\n");
	}
}
Combination of while-for loop
#include <stdio.h>
#include <cs50.h>
int main(void)
{
	int n; 
	while (true)
	{
		n = get_int("How many times should the cat meow? \n");
		if (n < 0)
		{
			continue;
		}
		else
		{
			break;
		}
		
		for (int i = 0; i < n; i++)
		{
		printf("meow\n");
		}
	}
}

In line 5 i'm declaring the variable outside the scope of the loops so it can be used in both loops


One of the best practice is that in C, the main function contains the entire code so it should be at the top, always.

Functions:

When a function has void, it takes no input.

#include <stdio.h>
#include <cs50.h>
int main(void)
{
	meow();
}

void meow(void)
	{
		printf('meow');
	}

Note that because the function is below and outside the main function, it won't be called or it will return error, like this:

CS50/C/ $ make cowFuncWithInput 
cowFuncWithInput.c:9:5: error: call to undeclared function 'meow'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
    5 |     meow();
      |     ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
2 errors generated.
make: *** [<builtin>: cowFuncWithInput] Error 1
CS50/C/ $ ^C
CS50/C/ $ 
It's mandatory that the code is inside the main function
Prototype for functions

A prototype is a declaration to the compiler that at some point, there will be a function.

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

//Signature
void bark(void)

int main(void)
{
	for (int i = 0; i < 5; i++)
	{
		bark();
	}
}

void bark(void)
	{
		printf('woof');
	}

So I declare the function before the main func. , and can define it after the main func.

functions taking input:
#include <stdio.h>
#include <cs50.h>

// Signature moo function
void moo(int n);

int main()
{
	int n = get_int("How many times?\n")
	// Pass the user input as function argument
	moo(n);
}

void moo(int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("moooo!\n");
	}
}

Notice that in line 9 there is an input variable :

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

// Signature moo function
void moo(int mooTimes);

int main()
{
	int n = get_int("How many times?\n")
	// Pass the user input as function argument
	moo(n);
}

void moo(int mooTimes)
{
	for (int i = 0; i < mooTimes; i++)
	{
		printf("moooo!\n");
	}
}
Printing out a row of bricks : 2D matrix
#include <stdio.h>
#include <cs50.h>

//Create a 3x3 block of hashes
//Creating two loops
int main(void)
{
	//Outer loop - for each row
	for (int i = 0; i < 3; i++)
	{
	//inner loop - for each column
		for (int j = 0; j < 3; j++)
		{
		printf("#");
		}
	printf("\n");
	}
}
How to improve better code design?
#include <stdio.h>
#include <cs50.h>

//Create a 3x3 block of hashes
//Creating two loops
int main(void)
{
	//Define a constant value
	const int n = 3
	//Outer loop - for each row
	for (int i = 0; i < n; i++)
	{
	//inner loop - for each column
		for (int j = 0; j < n; j++)
		{
		printf("#");
		}
	printf("\n");
	}
}

Integer overflow:

It's a situation where a variable type has a specific memory size (bit allocation) and it exceeds its allocation ,which is called overflow.

Exercise : Make a calculator in C

Connected Pages
Welcome to C
  • printf
  • Escape sequences:
  • Header file .h
  • #include
    1. User input
    2. Variables:
    3. Comparisons
    4. Comparing singe characters
    5. Conditionals : While
    6. For
    7. Combination of while-for loop
  • One of the best practice is that in C, the main function contains the entire code so it should be at the top, always.
    1. Functions:
    2. It's mandatory that the code is inside the main function
      1. Prototype for functions
      2. functions taking input:
      3. Printing out a row of bricks : 2D matrix
      4. How to improve better code design?
      5. Integer overflow:
  • Powered by Forestry.md