Getting Started With C
Getting Started With C
1. Numeric Types
Type: int
Format specifier: %d
Example:
2. Character Types
Type: char
Format specifier: %c
Example:
Example:
char name[7] = "Justin"; // Interpreted as {'J', 'u', 's', 't', 'i', 'n', '\0'}
printf("Name: %s\n", name);
Arrays in C
An array is a collection of data elements of the same type stored at contiguous memory locations.
Syntax
Example
Example:
Strings are arrays of characters, but with a null character ( '\0' ) at the end to indicate the termination of the string.
char name[7] = "Justin"; // Equivalent to {'J', 'u', 's', 't', 'i', 'n', '\0'}
printf("%s\n", name); // Output: Justin
Syntax:
Example:
Specifier Type
%d or %i Integer
%f Float
%lf Double
%c Character
%s String
Syntax:
Example:
int numberOfCars;
printf("Enter number of cars: ");
scanf("%d", &numberOfCars); // Read integer from the user
int main() {
int studentAge = 21;
float studentHeight = 67.3;
double studentWeight = 55.82;
// Reading input
char carName[15];
printf("\nEnter car name: ");
scanf("%s", carName); // Read string input
printf("Car name: %s\n", carName);
int numberOfCars;
printf("Enter number of cars: ");
scanf("%d", &numberOfCars); // Read integer input
printf("Number of cars: %d\n", numberOfCars);
return 0;
}