C Flashcards
Tips
Global variable
Scope: can be shared by every and any function/block of code within the program.
It should not be put within curley braces.
eg. a constant
care should be taken against another variable
memory allocation
int: 4 bytes
float: 4 bytes
char: 1 byte
double: 8 bytes
long: 4 or 8 bytes (depends on the system, 4 bytes on 32-bit systems, and 8 bytes on 64-bit systems)
C functions syntax template
function( ){
return 0;
}
SCANF( )
scanf is a function in C that is used to read input from the standard input stream (usually the keyboard) and store it into variables. It works with different data types based on the format specifier used in the function call. Here’s a basic overview of how scanf works with different data types:
Integer (%d, %i): If you want to read an integer, you use %d or %i as the format specifier in scanf. For example:
int num;
scanf(“%d”, &num);
Floating-point (%f, %lf): For floating-point numbers, you use %f for float and %lf for double. For example:
float num;
scanf(“%f”, &num);
Character (%c): To read a single character, you use %c. For example:
char ch;
scanf(“%c”, &ch);
String (%s): To read a string (sequence of characters), you use %s. For example:
char str[100];
scanf(“%s”, str);
Other Data Types: There are other format specifiers for different data types (%u for unsigned int, %ld for long, etc.) that you can use with scanf depending on the data you want to read.
Command Line Arguement
Command line arguments are values provided to a program when it is executed. In C, you can access these arguments through the argc and argv parameters of the main function.
argc (argument count) is an integer that specifies the number of arguments passed to the program, including the name of the program itself.
argv (argument vector) is an array of strings where each element is one of the arguments passed to the program. argv[0] is the name of the program, and argv[1] through argv[argc-1] are the actual arguments.
Enables C to detect user input from Command Line, which is usable within c.
Argc - Takes in Command Line Arguement count
Argv[] - Takes in Command line Array
Functions
A function in programming is a blackbox, a reusable piece of code that performs a specific task. It typically takes inputs (arguments), processes them, and returns a result. Functions help organize code, improve readability, and avoid repetition by allowing you to call the same code multiple times with different inputs.
Process of writing a function
- Declaration: telling the compiler ahead of time. this is typically written before the main function.
It is cosed with a semi-colon ;
return Type name (Arguemnts-list);
eg,
int add_num (int x, int y);
float mult_decimals(float a, float b);
double mult_decimals(float a, float b);
***note that double can store floating point numbers
- Function definition: the process of writing the code into the blackbox. No semi-colon.
//a function that multiply two floating point numbers//
**float mult_decimals(float a, float b)
{
//the operation//
product = ab;
//return value of our calculations/operation or output of the blackbox; //
return product;
}
While Loop vs If statement
IF = check condition once
while = continuous
Array
A reserved /segmentchunk of memory.
First index: 0
Last Index: n-1
Array Syntax
type name[size]
Array example
int num[10] ; –> make room for 10 integers
bool menu[5]; –> make room for 5 booleans
char name[24]; –> reserve room for 24characters
Array declaration
declare the array as above:
syntax type name[size];***
1- char names[12];
2- names[2];
// line 1 is the declaration
// line 2 is the index specification (the third element)
Array segmentation error
Specifying an index that is out of the range of a declared array
Array Elements declaration
// method 1:
string cities[3];
cities[0] = kaduna;
cities[1] = lagos;
cities[2] = sokoto;
// method 2
string cities[3] = {kaduna, lagos, sokoto};
Array Tip
Always consider using a Loop to iterate the elements in an array
unsized array/ unbounded array
In C, you can declare an array without specifying the index size if you initialize it with values. This is known as an “unsized array” or “unbounded array.” The compiler will automatically determine the size based on the number of elements you provide. However, you cannot leave the array declaration completely empty without initializing it.
int numbers[] = {1, 2, 3, 4, 5};
// Compiler will determine the size (5) based on the number of elements
Array notes
Array grid
// eg for a 10x10 grid array
int numbers[10] [10];
in memory this is simply 100 elements.
//Array as variables
Each individual element in an array can be treated as a variable
//re-assigning arrays
this is directly impossibles unlike in variables.
eg.
int numbers[] = {1, 2, 3, 4, 5};
int age = int numbers //this is not possible
//inC, the only way is to copy the content of numbers into age using a loop
int numbers[] = {1, 2, 3, 4, 5};
for(i = 0; i<5;i++)
{
age = numbers[i];
}
Command Line inputs
This is used to get user inputs before runtime to minimize start and stop.
//Syntax
int main(argc, argv[])
Argc - Argument count. this counts the number of arguments on the command line, starting with the program name as the first element 0, then proceeds to count the other sets of elements found on the command line.
Argv[] - stores the actual text into the index captured by th argc.
the first element of argv is always found at index[0]
while the last element of argv is found at index Argv[Argc - 1]
//everything stored in argv is in form of a string
Array size change
once deeclared, an Array size cannot be changed
If you would not use a number or set of numbers for any math,atical operation, store as array
add a DOT (.) to a file to make it hidden
Compare strings vs Int
strcmp function
this is used to compare the phone number strings since you cannot directly compare strings using the == operator in C.
#include<string.h></string.h>
strcmp
compares the ASCII values of the characthers provided.
while == operator returns a boolean value.
typedef struct
{
name;
phone_number;
}
NameYourDataType;