#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("Hello, world\n");
return 0;
}
run
objdump -d learn
after compiling the code usinggcc learnc.c -o learn
#include <stdio.h>
int main(int argc, char const *argv[])
{
int counter = 0;
counter += 1;
printf("%d\n", counter);
}
#include <stdio.h>
#define LOWER 0
#define UPPER 300
#define STEP 20
int main(int argc, char const *argv[])
{
float fahr = LOWER;
while(fahr <= UPPER) {
float cel = ( 5 * (fahr - 32) ) / 9;
printf("%3f\t%6.1f\n", fahr, cel);
fahr += STEP;
}
}
int main(int argc, char const *argv[])
{
char c;
while (c != '$') {
c = getchar();
printf("Entered char is:");
putchar(c);
}
return 0;
}
- for EOF value, use Ctrl + D at the end of the input string.
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("Enter a string\n");
int n = 0;
while(getchar() != EOF) {
n++;
}
printf("\nTotal characters: %d\n", n);
return 0;
}
- Remember to always initialise variables in c. Otherwise it can lead to junk results.
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("Enter a few lines\n");
char c;
long int count = 0;
while ((c = getchar()) != EOF) {
if (c == '\n') {
count++;
}
}
printf("Total lines in the input is %ld\n", count);
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
if(argc < 2) {
printf("Please pass atleast 1 argument");
exit(0);
}
int counter = 0;
for(int counter=0; counter < argc; counter++) {
printf("%s\n", argv[counter]);
}
}
#include <stdio.h>
#include <stdlib.h>
#define IN 0
#define OUT 1
int main(int argc, char const *argv[])
{
int nc = 0, nw = 0;
int state = OUT;
char c;
printf("Enter a paragraph\n");
while ((c = getchar()) != EOF) {
nc++;
if(c == ' ' || c == '\t' || c == '\n') {
state = OUT;
} else if (state == OUT) {
state = IN;
nw++;
}
}
printf("\nTotal words is %d\n", nw);
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
int ndigit[10], nwhite, nother, nc;
nwhite = nother = nc = 0;
for(int i=0; i<10; i++) {
ndigit[i] = 0;
}
int c;
while ((c = getchar()) != EOF) {
nc++;
if(c >= '0' && c <= '9') {
ndigit[c - '0'] += 1;
} else if(c == ' ' || c == '\n' || c =='\t') {
nwhite++;
} else {
nother++;
}
}
printf("\nTotal characters %d\n", nc);
printf("\nTotal numbers\n");
for(int i=0; i<10; i++) {
printf("%d ", ndigit[i]);
}
printf("\n");
printf("White spaces count %d\n", nwhite);
printf("Other characters %d\n", nother);
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_CHAR_PER_LINE 100
#define MAXLINES 10
int mygetline(char line[], int max_char_per_line);
void copy(char from[], char to[]);
void clearline(char line[], int len);
int main(int argc, char const *argv[])
{
char line[MAX_CHAR_PER_LINE];
char lines[MAXLINES][MAX_CHAR_PER_LINE];
int len, nline;
len = nline = 0;
while (( len = mygetline(line, MAX_CHAR_PER_LINE)) > 0) {
copy(line, lines[nline]);
clearline(line, len);
nline++;
}
for(int i=0; i<nline; i++) {
printf("\nLine %d: %s",i , lines[i]);
}
printf("\n");
return 0;
}
int mygetline(char line[], int max_char_per_line) {
char c;
int i = 0;
while ((i < max_char_per_line - 1) && ((c = getchar()) != '\n') && c != EOF) {
line[i] = c;
i++;
}
if(c == '\n') {
line[i] = '\0';
}
return i;
}
void copy(char from[], char to[]) {
for(int i =0; from[i] != '\0'; i++) {
to[i] = from[i];
}
}
void clearline(char line[], int len) {
for (int i=0; i<len; i++) {
line[i] = '\0';
}
}
#include <stdio.h>
int main(int argc, char const *argv[])
{
int arr[] = {1,-4,2,6,5};
int max = arr[0];
for(int i=1; arr[i] != '\0' ; i++) {
if(arr[i] > max) {
max = arr[i];
}
}
printf("Max value is %d\n", max);
return 0;
}
#include <stdio.h>
int test = 25;
int main(int argc, char const *argv[])
{
extern int test;
printf("Test value before %d\n",test);
test = 10;
printf("Test value after %d\n",test);
return 0;
}
#include <stdio.h>
void hello() {
printf("Hello is a function pointer");
}
void hello_wrapper(void (*f)()) {
f();
}
int main(int argc, char const *argv[])
{
hello_wrapper(hello);
}