sudo apt install libc6-dev gcc make
hello world
file hello.c
#include<stdio.h>
int main(void){
printf("hello world");
}
make hello
./hello
hello world
steps:
preprocessing>compiling>linking
preprocessing
directives like #include pulls files from /usr/include/*.h , #define
preprocessing using gnu preprocessor(cpp)
cpp hello.c > hello.i
cpp hello.c -o hello.i
or
gcc -E hello.c -o hello.i
The resultant intermediate file "hello.i" contains the expanded source code.
compiling :
compiler generate assembly code. and use it to generate binary file(ELF, COFF, a.out, ...)
This object file contains the compiled code (in binary form) of the
symbols defined in the input. Symbols in object files are referred to by
name.
Object files can refer to symbols that are not defined. This is the case
when you use a declaration, and don't provide a definition for it. The
compiler doesn't mind this, and will happily produce the object file as
long as the source code is well-formed.Compiling only looks for one file at a time.
to manually generate assembly code
gcc -S hello.c generates hello.s
The assembler (as
) converts the assembly code into machine code in the object file "hello.o".
as -o hello.o hello.s
to manually generate object file
gcc -c hello.c generates hello.o
linking
The linker is what produces the final compilation output from the object files the compiler produced. This output can be either a shared (or dynamic) library or an executable.
It links all the object files by replacing the references to undefined symbols with the correct addresses. Each of these symbols can be defined in other object files or in libraries. If they are defined in libraries other than the standard library, you need to tell the linker about them.the linker (
ld
) links the object code with the library code to produce an executable file "hello".ld -o hello hello.o ...libraries...
ld /usr/lib/x86_64-linux-gnu/crti.o /usr/lib/x86_64-linux-gnu/crtn.o /usr/lib/x86_64-linux-gnu/crt1.o -lc hello.o -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o hello
https://linux.die.net/man/1/ld
#include <stdio.h>
is a
header file library that lets us work with input and output
functions
main()
. This is called a
main function. Any code inside its curly brackets {}
will be executed.
printf()
is a
function used to output/print text to the screen.
it does not insert a new line at the end of the output.
To insert a new line, you can use the \n
character. These are called escape sequence. Others are
\t tab
\\ backslash \
\" quote
comments
single line = //
// this is a comment
multi line /* comment here */
variable
data type(minimum size)
int(16bit/4 bytes)
char(8bit/1 byte)= -127 to 127 , A=65,0=48,a=97
float(16bit/4 byte)
double(64 bit float)
long double(80,96,128)
https://en.wikipedia.org/wiki/C_data_types
long(32bit int)
long long (64 bit)
c99 added bool type
<stdbool.h> defines macros true and false and bool type
actual size depends on compiler implementation/cpu architecture these are minimum sizes.
to check limits #include<limits.h>
check the value of CHAR_MAX
qualifiers
unsigned : double positive range
example
unsigned int
short
long
const
defining variable
int num=15;
printf(num);
size of operator [sizeof()] give size in bytes.
Format Specifiers in printf
%d or %i = integer
%c
= char
%f
= float
%lf = double
%li= long integer
%s = string
https://cplusplus.com/reference/cstdio/printf/
example
int num=65;
printf("the number is %d",num)
o/p: the number is65
printf("the number is %c",num)
o/p: the number is A
to print % in printf
prinft("%%")
return of printf() function
int
. On success, the total number of characters written is returned.
On failure, a negative number is returned.
https://en.cppreference.com/w/c/io/fprintf
Set Decimal Precision
if you print a floating point number, the output will show many digits after the decimal point.
to set decimal precision, you can use a
dot (.
) followed by a number that specifies how many digits that should be shown
after the decimal point:
printf("%.1f", num);
Declare Multiple Variables
int x =1 , y=2 , z=3;
printf("%d",x+y+z) ;
rules for naming variables are:
- Names can contain letters, digits and underscores
- Names must begin with a letter or an underscore (_) [ no number or other symbol]
- Names are case sensitive (
myVar
andmyvar
are different variables) - Names cannot contain whitespaces or special characters like !, #, %, etc.
- Reserved words (such as
int
) cannot be used as names
Type Conversion
int x = 5;
int y = 2;
int sum = 5 / 2;
printf("%d",
sum); // Outputs 2
There are two types of conversion in C:
- Implicit Conversion (automatically)
Implicit conversion is done automatically by the compiler when you assign a value of one type to another.
- Explicit Conversion (manually)
Explicit conversion is done manually by placing the type in parentheses ()
in front of the value.
Constants
const
keyword marks variable as read only.
When you declare a constant variable, it must be assigned with a value:
const int minutesPerHour = 60;
it is considered good practice to declare them with uppercase. It is not required though.
Operators
C divides the operators into the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
Arithmetic ops are add(+),subtract(-),multiply(*),divide(/),modulus/remainder(%),increment&decrement(++ and --)
Assignment operators are equals(=),arithmetico/p and equals(+,-,*,/,%)= ,logic equals (&=,|=,^=) ,>>= and <<=.
Comparision are equal to (==),not equal to (!=),<,> ,<= and >=
logical operators logic and(&&), logic or(||) and logic not(!)
Booleans
#include <stdbool.h>
1
(or any other number that is not 0) representstrue
0
representsfalse
conditional statement
// block of code to be executed if the condition is true
}
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Ternary Operator(shorthand if)
Switch Statement
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
While Loop
while (condition) {
// code block to be executed
}
The Do/While Loop
do {
// code block to be executed
}
while (condition);
For Loop
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Statement 1 is executed (one time) before the execution of the code block.
Statement 2 defines the condition for executing the code block.
Statement 3 is executed (every time) after the code block has been executed.
int i;
for (i = 0; i < 5; i++) {
printf("%d\n",
i);
}
break Keyword
When C reaches abreak
keyword, it breaks out of the switch block / loops.
Continue Keyword
The continue
statement breaks one iteration (in the loop) i.e skip once.
Arrays
To create an array, define the data type (like int
) and specify the name
of the array followed by square brackets [].
To insert values to it, use a comma-separated list, inside curly braces:
int myNumbers[] = {25, 50, 75, 100};
To access an array element, refer to its index number.
Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
This statement accesses the value of the first element [0] in
myNumbers
:
printf("%d", myNumbers[0]);
To change the value of a specific element, refer to the index number:
myNumbers[0] = 33;
int myNumbers[] = {25, 50, 75, 100};
int i;
for (i = 0; i < 4; i++)
{
printf("%d\n", myNumbers[i]);
}
// Declare an array of four integers:
int myNumbers[4];
You are not able to change the size of the array after creation.
Multidimensional Arrays
A multidimensional array is basically an array of arrays.
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("%d\n", matrix[i][j]);
}
}
Strings
use thechar
type and create
an array of characters to make a string in C: Note that you have to use double quotes (""
).
[]
.int i;
for (i = 0; i < 5; ++i) {
printf("%c\n", carName[i]);
}
String Functions
#include <string.h>
strlen()
function:printf("%d", strlen(alphabet)); // 26
printf("%d", sizeof(alphabet)); // 27
To concatenate (combine) two strings, you can use the strcat()
function:
char str1[20] = "Hello ";
char str2[] = "World!";
//
Concatenate
str2 to str1 (result is stored in str1)
strcat(str1, str2);
//
Print str1
printf("%s", str1);
To copy the value of one string to another, you can use the strcpy()
function:
char str1[20] = "Hello World!";
char str2[20];
// Copy str1 to str2
strcpy(str2, str1);
// Print str2
printf("%s", str2);
To compare two strings, you can use the strcmp()
function.
It returns 0
if the two strings are equal, otherwise a value that is not 0:
char str1[] = "Hello";
char str2[] = "Hello";
char str3[] = "Hi";
// Compare str1 and str2, and print the result
printf("%d\n",
strcmp(str1, str2)); // Returns 0 (the strings are equal)
//
Compare str1 and str3, and print the result
printf("%d\n", strcmp(str1, str3)); // Returns -4 (the strings are not
equal)
User Input
use the scanf()
function:
// Create an integer variable that will store the number we get from the user
int myNum;
// Ask the user to type a number
printf("Type a number:
\n");
// Get and save the number the user types
scanf("%d", &myNum);
// Output the number the user typed
printf("Your number is: %d", myNum);
The scanf()
function takes two arguments: the format specifier of the variable (%d
in the example above) and the
reference operator (&myNum
), which stores the memory address of the variable.
// Get and save the number AND
character the user types
scanf("%d %c", &myNum, &myChar);
take string input
char firstName[30];
// Ask the user to input some
text
printf("Enter your first name: \n");
// Get and save the text
scanf("%s", firstName);
// Output the text
printf("Hello %s",
firstName);
scanf()
, you must specify the size of
the string/array (we used a very high number, 30 in our example, but atleast then we are
certain it will store enough characters for the first name), and you don't have
to use the reference operator (&
).scanf()
function has some limitations: it considers space (whitespace,
tabs, etc) as a terminating character, which means that it can only display a
single word (even if you type many words). For example:printf("Type your full name: \n");
scanf("%s", &fullName);
printf("Hello %s", fullName);
// Type your full name: John Doe
// Hello John
That's why, when working with strings, we often use the fgets()
function to
read a line of text. Note that you must include the following
arguments: the name of the string variable, sizeof
(string_name), and stdin
:
char fullName[30];
printf("Type your full name: \n");
fgets(fullName, sizeof(fullName), stdin);
printf("Hello %s",
fullName);
// Type your full name: John Doe
// Hello John Doe
Memory Address
When a variable is created in C, a memory address is assigned to the variable.
To access it, use the reference
operator (&
), and the result represents where the variable is stored:
int myAge = 43;
printf("%p", &myAge); // Outputs 0x7fffffad36fc
You should also note that &myAge
is
often called a "pointer". A pointer basically stores the memory address
of a variable as its value. To print pointer values,
we use the %p
format specifier.
Pointers
A pointer is a variable that stores the memory address of another variable as its value.
A pointer variable points to a data type (like int
) of the same
type, and is created with the *
operator.
Dereference
You can also get the value of the variable the pointer points to, by using the *
operator (the dereference operator):
int myAge = 43; // Variable declaration
int* ptr = &myAge; // Pointer
declaration
// Reference: Output the memory address of myAge with the
pointer (0x7ffe5367e044)
printf("%p\n", ptr);
// Dereference:
Output the value of myAge with the pointer (43)
printf("%d\n", *ptr);
Note that the *
sign can be confusing here, as it does two different things
in our code:
- When used in declaration (
int* ptr
), it creates a pointer variable. - When not used in declaration, it act as a dereference operator.
ways to create pointer variable
int* myNum;
int *myNum;
Pointers are one of the things that make C stand out from other programming languages, like Python and Java.
They are important in C, because they allow us to manipulate the data in the computer's memory. This can reduce the code and improve the performance. If you are familiar with data structures like lists, trees and graphs, you should know that pointers are especially useful for implementing those. And sometimes you even have to use pointers, for example when working with files.
warning ; pointers must be handled with care, since it is possible to damage data stored in other memory addresses.
How Are Pointers Related to Arrays in c
in C, the name of an array, is actually a pointer to the first element of the array.
The memory address of the first element is the same as the name of the array:
int myNumbers[4] = {25, 50, 75, 100};
// Get the memory address of the
myNumbers array
printf("%p\n", myNumbers);
// Get the memory
address of the first array element
printf("%p\n", &myNumbers[0]);
access array using pointer
int myNumbers[4] = {25, 50, 75, 100};
// Get the value of the first
element in myNumbers
printf("%d", *myNumbers);
// Get the value of the second
element in myNumbers
printf("%d\n", *(myNumbers + 1));
// Get the value of the
third
element in myNumbers
printf("%d", *(myNumbers + 2));
int *ptr = myNumbers;
int i;
for (i = 0; i < 4; i++) {
printf("%d\n", *(ptr + i));
}
It is also possible to change the value of array elements with pointers:
int myNumbers[4] = {25, 50, 75, 100};
// Change the
value of the first element to 13
*myNumbers = 13;
// Change the
value of the second element to 17
*(myNumbers +1) = 17;
// Get
the value of the first element
printf("%d\n", *myNumbers);
// Get
the value of the second element
printf("%d\n", *(myNumbers + 1));
Functions
delcare function
void myFunction() {
// code to be executed
}
Call a Function
myFunction();printf("I just got executed!");
}
int main() {
myFunction();
myFunction();
myFunction();
return 0;
}
Parameters and Arguments
returnType functionName(parameter1, parameter2, parameter3) {// code to be executed
}
printf("Hello %s\n", name);
}
int main() {
myFunction("Liam");
myFunction("Jenny");
myFunction("Anja");
return 0;
}
// Hello Liam
// Hello Jenny
// Hello Anja
printf("Hello %s. You are %d years old.\n", name, age);
}
int main() {
myFunction("Liam", 3);
myFunction("Jenny", 14);
myFunction("Anja", 30);
return 0;
}
// Hello Liam. You are 3 years old.
// Hello Jenny. You are 14 years old.
// Hello Anja. You are 30 years old.
You can also pass arrays to a function:
void myFunction(int myNumbers[5]) {
for (int i = 0; i < 5; i++) {
printf("%d\n", myNumbers[i]);
}
}
int main() {
int
myNumbers[5] = {10, 20, 30, 40, 50};
myFunction(myNumbers);
return 0;
}
Return Values
The void
keyword, used in the previous examples, indicates that the
function should not return a value. If you
want the function to return a value, you can use a data type (such as int
or
float
, etc.) instead of void
, and use the return
keyword inside the function:
int myFunction(int x) {
return 5 + x;
}
int main() {
printf("Result is: %d", myFunction(3));
return 0;
}
// Outputs
8 (5 + 3)
int myFunction(int x, int y) {
return x + y;
}
int main()
{
printf("Result is: %d", myFunction(5, 3));
return 0;
}
// Outputs 8 (5 + 3)
Function Declaration and Definition
A function consist of two parts:
- Declaration: the function's name, return type, and parameters (if any)
- Definition: the body of the function (code to be executed)
For code optimization and organization, it is recommended to separate the declaration and the definition of the function.
You will often see C programs that have function declaration above main()
,
and function definition below main()
. This will make the code
better organized and easier to read:
// Function declaration
void myFunction();
// The main method
int main() {
myFunction(); // call the function
return 0;
}
// Function definition
void myFunction() {
printf("I just got executed!");
}
Recursion
Recursion is the technique of making a function call itself.int main() {
int result = sum(10);
printf("%d", result);
return 0;
}
int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
}
}
Math Functions
#include <math.h>
printf("%f", sqrt(16));
printf("%f", ceil(1.4));
printf("%f",
floor(1.4));
printf("%f", pow(4, 3));
others:abs(x),asin/cos/tan(x)=arc trigfn,cbrt(x),sin,cos,tan(x)=trigfn,exp(x)=e^x
Command line arguments
File Handling
fptr = fopen(filename, mode);
FILE
is basically a data type, and we need
to create a pointer variable to work with it (fptr
).fptr = fopen("filename.txt", "w");
fclose(fptr);
Write To a File
fprintf(fptr, "Some text");C Read Files
char myString[100];
fgets(myString, 100,
fptr);
Note: The fgets
function only reads the first line of the
file.
FILE *fptr;
// Read the whole content and print it
while(fgets(myString, 100, fptr)) {
printf("%s", myString);
}
good practice
if(fptr == NULL) {
printf("Not able to open the
file.");
}
Structures
structs are a way to group several related variables into one place
struct MyStructure { // Structure declaration
int myNum; // Member (int
variable)
char myLetter; // Member (char variable)
};
// End the structure with a semicolon
To access the structure, you must create a variable of it.
struct myStructure {
int myNum;
char myLetter;
};
int main()
{
struct myStructure s1;
return 0;
}
To access members of a structure, use the dot syntax (.
):
// Create a structure called myStructure
struct myStructure {
int myNum;
char myLetter;
};
int main() {
// Create a structure variable
of myStructure called s1
struct myStructure s1;
// Assign values to members of s1
s1.myNum = 13;
s1.myLetter = 'B';
// Print values
printf("My
number: %d\n", s1.myNum);
printf("My letter: %c\n", s1.myLetter);
return 0;
}
for string cant assign like this
// Trying to assign a value to the string
s1.myString = "Some text";
use
// Assign a value to the string using
the strcpy function
strcpy(s1.myString, "Some text");
You can also assign values to members of a structure variable at declaration time, in a single line.
// Create a structure variable and assign values to it
struct myStructure s1 = {13,
'B', "Some text"};
Enumeration
An enum is a special type that represents a group of constants (unchangeable values).
enum Level {
LOW,
MEDIUM,
HIGH
};
Note that the last item does not need a comma.
It is not required to use uppercase, but often considered as good practice.
Enum is short for "enumerations", which means "specifically listed".
To access the enum, you must create a variable of it.
enum Level myVar;
enum Level myVar = MEDIUM;
LOW = 25,
MEDIUM = 50,
HIGH = 75
};
Note that if you assign a value to one specific item, the next items will update their numbers accordingly:
Enums are used to give names to constants, which makes the code easier to read and maintain.
Use enums when you have values that you know aren't going to change, like month days, days, colors, deck of cards, etc.
Comments
Post a Comment